OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/auto_login_info_bar_delegate.h" | 5 #include "chrome/browser/ui/auto_login_info_bar_delegate.h" |
6 | 6 |
7 #include "base/bind.h" | |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | |
8 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
9 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/infobars/infobar_tab_helper.h" | 12 #include "chrome/browser/infobars/infobar_tab_helper.h" |
11 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
12 #include "chrome/browser/signin/token_service.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/signin/ubertoken_fetcher.h" | |
13 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" | 16 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" |
14 #include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h" | 17 #include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h" |
15 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
16 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
17 #include "chrome/common/net/gaia/gaia_constants.h" | 20 #include "chrome/common/net/gaia/gaia_constants.h" |
18 #include "chrome/common/net/gaia/gaia_urls.h" | 21 #include "chrome/common/net/gaia/gaia_urls.h" |
19 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
20 #include "content/public/browser/notification_details.h" | 23 #include "content/public/browser/notification_details.h" |
21 #include "content/public/browser/notification_observer.h" | 24 #include "content/public/browser/notification_observer.h" |
22 #include "content/public/browser/notification_registrar.h" | 25 #include "content/public/browser/notification_registrar.h" |
(...skipping 21 matching lines...) Expand all Loading... | |
44 HISTOGRAM_IGNORED, | 47 HISTOGRAM_IGNORED, |
45 HISTOGRAM_MAX | 48 HISTOGRAM_MAX |
46 }; | 49 }; |
47 | 50 |
48 // AutoLoginRedirector -------------------------------------------------------- | 51 // AutoLoginRedirector -------------------------------------------------------- |
49 | 52 |
50 // This class is created by the AutoLoginInfoBarDelegate when the user wishes to | 53 // This class is created by the AutoLoginInfoBarDelegate when the user wishes to |
51 // auto-login. It holds context information needed while re-issuing service | 54 // auto-login. It holds context information needed while re-issuing service |
52 // tokens using the TokenService, gets the browser cookies with the TokenAuth | 55 // tokens using the TokenService, gets the browser cookies with the TokenAuth |
53 // API, and finally redirects the user to the correct page. | 56 // API, and finally redirects the user to the correct page. |
54 class AutoLoginRedirector : public content::NotificationObserver { | 57 class AutoLoginRedirector : public UbertokenConsumer { |
55 public: | 58 public: |
56 AutoLoginRedirector(TokenService* token_service, | 59 AutoLoginRedirector(NavigationController* navigation_controller, |
57 NavigationController* navigation_controller, | |
58 const std::string& args); | 60 const std::string& args); |
59 virtual ~AutoLoginRedirector(); | 61 virtual ~AutoLoginRedirector(); |
60 | 62 |
63 static void Delete(AutoLoginRedirector* redirector) { | |
64 delete redirector; | |
65 } | |
66 | |
61 private: | 67 private: |
62 // content::NotificationObserver override. | 68 // Overriden from UbertokenConsumer: |
63 virtual void Observe(int type, | 69 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE; |
64 const content::NotificationSource& source, | 70 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE; |
65 const content::NotificationDetails& details) OVERRIDE; | |
66 | 71 |
67 // Redirect tab to MergeSession URL, logging the user in and navigating | 72 // Redirect tab to MergeSession URL, logging the user in and navigating |
68 // to the desired page. | 73 // to the desired page. |
69 void RedirectToMergeSession(const std::string& token); | 74 void RedirectToMergeSession(const std::string& token); |
70 | 75 |
71 NavigationController* navigation_controller_; | 76 NavigationController* navigation_controller_; |
72 const std::string args_; | 77 const std::string args_; |
73 content::NotificationRegistrar registrar_; | 78 scoped_ptr<UbertokenFetcher> ubertoken_fetcher_; |
74 | 79 |
75 DISALLOW_COPY_AND_ASSIGN(AutoLoginRedirector); | 80 DISALLOW_COPY_AND_ASSIGN(AutoLoginRedirector); |
76 }; | 81 }; |
77 | 82 |
78 AutoLoginRedirector::AutoLoginRedirector( | 83 AutoLoginRedirector::AutoLoginRedirector( |
79 TokenService* token_service, | |
80 NavigationController* navigation_controller, | 84 NavigationController* navigation_controller, |
81 const std::string& args) | 85 const std::string& args) |
82 : navigation_controller_(navigation_controller), | 86 : navigation_controller_(navigation_controller), |
83 args_(args) { | 87 args_(args) { |
84 // Register to receive notification for new tokens and then force the tokens | 88 ubertoken_fetcher_.reset(new UbertokenFetcher( |
85 // to be re-issued. The token service guarantees to fire either | 89 static_cast<Profile*>(navigation_controller_->GetBrowserContext()), |
sky
2012/01/31 16:54:41
Profile::FromBrowserContext. That said, it's prefe
qsr
2012/02/01 14:59:18
We need the navigation_controller_ anyway. I can a
| |
86 // TOKEN_AVAILABLE or TOKEN_REQUEST_FAILED, so we will get at least one or | 90 this)); |
87 // the other, allow AutoLoginRedirector to delete itself correctly. | 91 ubertoken_fetcher_->StartFetchingToken(); |
88 registrar_.Add(this, | |
89 chrome::NOTIFICATION_TOKEN_AVAILABLE, | |
90 content::Source<TokenService>(token_service)); | |
91 registrar_.Add(this, | |
92 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, | |
93 content::Source<TokenService>(token_service)); | |
94 token_service->StartFetchingTokens(); | |
95 } | 92 } |
96 | 93 |
97 AutoLoginRedirector::~AutoLoginRedirector() { | 94 AutoLoginRedirector::~AutoLoginRedirector() { |
98 } | 95 } |
99 | 96 |
100 void AutoLoginRedirector::Observe(int type, | 97 void AutoLoginRedirector::OnUbertokenSuccess(const std::string& token) { |
101 const content::NotificationSource& source, | 98 RedirectToMergeSession(token); |
102 const content::NotificationDetails& details) { | 99 MessageLoop::current()->PostTask(FROM_HERE, |
103 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE || | 100 base::Bind(&AutoLoginRedirector::Delete, |
104 type == chrome::NOTIFICATION_TOKEN_REQUEST_FAILED); | 101 this)); |
Roger Tawa OOO till Jul 10th
2012/01/31 21:38:23
'this' should be aligned under &. same below.
qsr
2012/02/01 14:59:18
Not relevant anymore.
| |
102 } | |
105 | 103 |
106 // We are only interested in GAIA tokens. | 104 void AutoLoginRedirector::OnUbertokenFailure( |
107 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { | 105 const GoogleServiceAuthError& error) { |
108 TokenService::TokenAvailableDetails* tok_details = | 106 LOG(WARNING) << "AutoLoginRedirector: token request failed"; |
109 content::Details<TokenService::TokenAvailableDetails>(details).ptr(); | 107 MessageLoop::current()->PostTask(FROM_HERE, |
sky
2012/01/31 16:54:41
Is there a reason you're not using DeleteSoon on t
qsr
2012/02/01 14:59:18
The usual one -> ignorance. Done.
| |
110 if (tok_details->service() == GaiaConstants::kGaiaService) { | 108 base::Bind(&AutoLoginRedirector::Delete, |
111 RedirectToMergeSession(tok_details->token()); | 109 this)); |
112 delete this; | |
113 } | |
114 } else { | |
115 TokenService::TokenRequestFailedDetails* tok_details = | |
116 content::Details<TokenService::TokenRequestFailedDetails>(details). | |
117 ptr(); | |
118 if (tok_details->service() == GaiaConstants::kGaiaService) { | |
119 LOG(WARNING) << "AutoLoginRedirector: token request failed"; | |
120 delete this; | |
121 } | |
122 } | |
123 } | 110 } |
124 | 111 |
125 void AutoLoginRedirector::RedirectToMergeSession(const std::string& token) { | 112 void AutoLoginRedirector::RedirectToMergeSession(const std::string& token) { |
126 // The args are URL encoded, so we need to decode them before use. | 113 // The args are URL encoded, so we need to decode them before use. |
127 std::string unescaped_args = | 114 std::string unescaped_args = |
128 net::UnescapeURLComponent(args_, net::UnescapeRule::URL_SPECIAL_CHARS); | 115 net::UnescapeURLComponent(args_, net::UnescapeRule::URL_SPECIAL_CHARS); |
129 // TODO(rogerta): what is the correct page transition? | 116 // TODO(rogerta): what is the correct page transition? |
130 navigation_controller_->LoadURL( | 117 navigation_controller_->LoadURL( |
131 GURL(GaiaUrls::GetInstance()->merge_session_url() + | 118 GURL(GaiaUrls::GetInstance()->merge_session_url() + |
132 "?source=chrome&uberauth=" + token + "&" + unescaped_args), | 119 "?source=chrome&uberauth=" + token + "&" + unescaped_args), |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 } | 164 } |
178 | 165 |
179 string16 AutoLoginInfoBarDelegate::GetButtonLabel( | 166 string16 AutoLoginInfoBarDelegate::GetButtonLabel( |
180 InfoBarButton button) const { | 167 InfoBarButton button) const { |
181 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? | 168 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? |
182 IDS_AUTOLOGIN_INFOBAR_OK_BUTTON : IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON); | 169 IDS_AUTOLOGIN_INFOBAR_OK_BUTTON : IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON); |
183 } | 170 } |
184 | 171 |
185 bool AutoLoginInfoBarDelegate::Accept() { | 172 bool AutoLoginInfoBarDelegate::Accept() { |
186 // AutoLoginRedirector deletes itself. | 173 // AutoLoginRedirector deletes itself. |
187 new AutoLoginRedirector(token_service_, navigation_controller_, args_); | 174 new AutoLoginRedirector(navigation_controller_, args_); |
Roger Tawa OOO till Jul 10th
2012/01/31 21:38:23
the token_service_ member does not look likes its
qsr
2012/02/01 14:59:18
Not relevant anymore after your change.
| |
188 RecordHistogramAction(HISTOGRAM_ACCEPTED); | 175 RecordHistogramAction(HISTOGRAM_ACCEPTED); |
189 button_pressed_ = true; | 176 button_pressed_ = true; |
190 return true; | 177 return true; |
191 } | 178 } |
192 | 179 |
193 bool AutoLoginInfoBarDelegate::Cancel() { | 180 bool AutoLoginInfoBarDelegate::Cancel() { |
194 pref_service_->SetBoolean(prefs::kAutologinEnabled, false); | 181 pref_service_->SetBoolean(prefs::kAutologinEnabled, false); |
195 RecordHistogramAction(HISTOGRAM_REJECTED); | 182 RecordHistogramAction(HISTOGRAM_REJECTED); |
196 button_pressed_ = true; | 183 button_pressed_ = true; |
197 return true; | 184 return true; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 bool ReverseAutoLoginInfoBarDelegate::Cancel() { | 252 bool ReverseAutoLoginInfoBarDelegate::Cancel() { |
266 pref_service_->SetBoolean(prefs::kReverseAutologinEnabled, false); | 253 pref_service_->SetBoolean(prefs::kReverseAutologinEnabled, false); |
267 RecordHistogramAction(HISTOGRAM_REJECTED); | 254 RecordHistogramAction(HISTOGRAM_REJECTED); |
268 button_pressed_ = true; | 255 button_pressed_ = true; |
269 return true; | 256 return true; |
270 } | 257 } |
271 | 258 |
272 void ReverseAutoLoginInfoBarDelegate::RecordHistogramAction(int action) { | 259 void ReverseAutoLoginInfoBarDelegate::RecordHistogramAction(int action) { |
273 UMA_HISTOGRAM_ENUMERATION("AutoLogin.Reverse", action, HISTOGRAM_MAX); | 260 UMA_HISTOGRAM_ENUMERATION("AutoLogin.Reverse", action, HISTOGRAM_MAX); |
274 } | 261 } |
OLD | NEW |