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/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/google/google_util.h" | 13 #include "chrome/browser/google/google_util.h" |
12 #include "chrome/browser/infobars/infobar_tab_helper.h" | 14 #include "chrome/browser/infobars/infobar_tab_helper.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/signin/token_service.h" | 17 #include "chrome/browser/signin/ubertoken_fetcher.h" |
16 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" | 18 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" |
17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
18 #include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h" | 20 #include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h" |
19 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
20 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/common/net/gaia/gaia_constants.h" | 23 #include "chrome/common/net/gaia/gaia_constants.h" |
22 #include "chrome/common/net/gaia/gaia_urls.h" | 24 #include "chrome/common/net/gaia/gaia_urls.h" |
23 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
24 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
25 #include "content/public/browser/notification_details.h" | 27 #include "content/public/browser/notification_details.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 | 68 |
67 HISTOGRAM_MAX | 69 HISTOGRAM_MAX |
68 }; | 70 }; |
69 | 71 |
70 // AutoLoginRedirector -------------------------------------------------------- | 72 // AutoLoginRedirector -------------------------------------------------------- |
71 | 73 |
72 // This class is created by the AutoLoginInfoBarDelegate when the user wishes to | 74 // This class is created by the AutoLoginInfoBarDelegate when the user wishes to |
73 // auto-login. It holds context information needed while re-issuing service | 75 // auto-login. It holds context information needed while re-issuing service |
74 // tokens using the TokenService, gets the browser cookies with the TokenAuth | 76 // tokens using the TokenService, gets the browser cookies with the TokenAuth |
75 // API, and finally redirects the user to the correct page. | 77 // API, and finally redirects the user to the correct page. |
76 class AutoLoginRedirector : public content::NotificationObserver { | 78 class AutoLoginRedirector : public UbertokenConsumer { |
77 public: | 79 public: |
78 AutoLoginRedirector(TokenService* token_service, | 80 AutoLoginRedirector(NavigationController* navigation_controller, |
79 NavigationController* navigation_controller, | |
80 const std::string& args); | 81 const std::string& args); |
81 virtual ~AutoLoginRedirector(); | 82 virtual ~AutoLoginRedirector(); |
82 | 83 |
83 private: | 84 private: |
84 // content::NotificationObserver override. | 85 // Overriden from UbertokenConsumer: |
85 virtual void Observe(int type, | 86 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE; |
86 const content::NotificationSource& source, | 87 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE; |
87 const content::NotificationDetails& details) OVERRIDE; | |
88 | 88 |
89 // Redirect tab to MergeSession URL, logging the user in and navigating | 89 // Redirect tab to MergeSession URL, logging the user in and navigating |
90 // to the desired page. | 90 // to the desired page. |
91 void RedirectToMergeSession(const std::string& token); | 91 void RedirectToMergeSession(const std::string& token); |
92 | 92 |
93 NavigationController* navigation_controller_; | 93 NavigationController* navigation_controller_; |
94 const std::string args_; | 94 const std::string args_; |
95 content::NotificationRegistrar registrar_; | 95 scoped_ptr<UbertokenFetcher> ubertoken_fetcher_; |
96 | 96 |
97 DISALLOW_COPY_AND_ASSIGN(AutoLoginRedirector); | 97 DISALLOW_COPY_AND_ASSIGN(AutoLoginRedirector); |
98 }; | 98 }; |
99 | 99 |
100 AutoLoginRedirector::AutoLoginRedirector( | 100 AutoLoginRedirector::AutoLoginRedirector( |
101 TokenService* token_service, | |
102 NavigationController* navigation_controller, | 101 NavigationController* navigation_controller, |
103 const std::string& args) | 102 const std::string& args) |
104 : navigation_controller_(navigation_controller), | 103 : navigation_controller_(navigation_controller), |
105 args_(args) { | 104 args_(args) { |
106 // Register to receive notification for new tokens and then force the tokens | 105 ubertoken_fetcher_.reset(new UbertokenFetcher( |
107 // to be re-issued. The token service guarantees to fire either | 106 Profile::FromBrowserContext(navigation_controller_->GetBrowserContext()), |
108 // TOKEN_AVAILABLE or TOKEN_REQUEST_FAILED, so we will get at least one or | 107 this)); |
109 // the other, allow AutoLoginRedirector to delete itself correctly. | 108 ubertoken_fetcher_->StartFetchingToken(); |
110 registrar_.Add(this, | |
111 chrome::NOTIFICATION_TOKEN_AVAILABLE, | |
112 content::Source<TokenService>(token_service)); | |
113 registrar_.Add(this, | |
114 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, | |
115 content::Source<TokenService>(token_service)); | |
116 token_service->StartFetchingTokens(); | |
117 } | 109 } |
118 | 110 |
119 AutoLoginRedirector::~AutoLoginRedirector() { | 111 AutoLoginRedirector::~AutoLoginRedirector() { |
120 } | 112 } |
121 | 113 |
122 void AutoLoginRedirector::Observe(int type, | 114 void AutoLoginRedirector::OnUbertokenSuccess(const std::string& token) { |
123 const content::NotificationSource& source, | 115 RedirectToMergeSession(token); |
124 const content::NotificationDetails& details) { | 116 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
125 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE || | 117 } |
126 type == chrome::NOTIFICATION_TOKEN_REQUEST_FAILED); | |
127 | 118 |
128 // We are only interested in GAIA tokens. | 119 void AutoLoginRedirector::OnUbertokenFailure( |
129 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { | 120 const GoogleServiceAuthError& error) { |
130 TokenService::TokenAvailableDetails* tok_details = | 121 LOG(WARNING) << "AutoLoginRedirector: token request failed"; |
131 content::Details<TokenService::TokenAvailableDetails>(details).ptr(); | 122 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
132 if (tok_details->service() == GaiaConstants::kGaiaService) { | |
133 RedirectToMergeSession(tok_details->token()); | |
134 delete this; | |
135 } | |
136 } else { | |
137 TokenService::TokenRequestFailedDetails* tok_details = | |
138 content::Details<TokenService::TokenRequestFailedDetails>(details). | |
139 ptr(); | |
140 if (tok_details->service() == GaiaConstants::kGaiaService) { | |
141 LOG(WARNING) << "AutoLoginRedirector: token request failed"; | |
142 delete this; | |
143 } | |
144 } | |
145 } | 123 } |
146 | 124 |
147 void AutoLoginRedirector::RedirectToMergeSession(const std::string& token) { | 125 void AutoLoginRedirector::RedirectToMergeSession(const std::string& token) { |
148 // The args are URL encoded, so we need to decode them before use. | 126 // The args are URL encoded, so we need to decode them before use. |
149 std::string unescaped_args = | 127 std::string unescaped_args = |
150 net::UnescapeURLComponent(args_, net::UnescapeRule::URL_SPECIAL_CHARS); | 128 net::UnescapeURLComponent(args_, net::UnescapeRule::URL_SPECIAL_CHARS); |
151 // TODO(rogerta): what is the correct page transition? | 129 // TODO(rogerta): what is the correct page transition? |
152 navigation_controller_->LoadURL( | 130 navigation_controller_->LoadURL( |
153 GURL(GaiaUrls::GetInstance()->merge_session_url() + | 131 GURL(GaiaUrls::GetInstance()->merge_session_url() + |
154 "?source=chrome&uberauth=" + token + "&" + unescaped_args), | 132 "?source=chrome&uberauth=" + token + "&" + unescaped_args), |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 UTF8ToUTF16(username_)); | 174 UTF8ToUTF16(username_)); |
197 } | 175 } |
198 | 176 |
199 string16 AutoLoginInfoBarDelegate::GetButtonLabel( | 177 string16 AutoLoginInfoBarDelegate::GetButtonLabel( |
200 InfoBarButton button) const { | 178 InfoBarButton button) const { |
201 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? | 179 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? |
202 IDS_AUTOLOGIN_INFOBAR_OK_BUTTON : IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON); | 180 IDS_AUTOLOGIN_INFOBAR_OK_BUTTON : IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON); |
203 } | 181 } |
204 | 182 |
205 bool AutoLoginInfoBarDelegate::Accept() { | 183 bool AutoLoginInfoBarDelegate::Accept() { |
206 TokenService* token_service = | |
207 TabContentsWrapper::GetCurrentWrapperForContents( | |
208 owner()->web_contents())->profile()->GetTokenService(); | |
209 // AutoLoginRedirector deletes itself. | 184 // AutoLoginRedirector deletes itself. |
210 new AutoLoginRedirector(token_service, | 185 new AutoLoginRedirector(&owner()->web_contents()->GetController(), args_); |
211 &owner()->web_contents()->GetController(), args_); | |
212 RecordHistogramAction(HISTOGRAM_ACCEPTED); | 186 RecordHistogramAction(HISTOGRAM_ACCEPTED); |
213 button_pressed_ = true; | 187 button_pressed_ = true; |
214 return true; | 188 return true; |
215 } | 189 } |
216 | 190 |
217 bool AutoLoginInfoBarDelegate::Cancel() { | 191 bool AutoLoginInfoBarDelegate::Cancel() { |
218 PrefService* pref_service = | 192 PrefService* pref_service = |
219 TabContentsWrapper::GetCurrentWrapperForContents( | 193 TabContentsWrapper::GetCurrentWrapperForContents( |
220 owner()->web_contents())->profile()->GetPrefs(); | 194 owner()->web_contents())->profile()->GetPrefs(); |
221 pref_service->SetBoolean(prefs::kAutologinEnabled, false); | 195 pref_service->SetBoolean(prefs::kAutologinEnabled, false); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 content::PAGE_TRANSITION_LINK, | 286 content::PAGE_TRANSITION_LINK, |
313 false); | 287 false); |
314 owner()->web_contents()->OpenURL(params); | 288 owner()->web_contents()->OpenURL(params); |
315 RecordHistogramAction(HISTOGRAM_LEARN_MORE); | 289 RecordHistogramAction(HISTOGRAM_LEARN_MORE); |
316 return false; | 290 return false; |
317 } | 291 } |
318 | 292 |
319 void ReverseAutoLoginInfoBarDelegate::RecordHistogramAction(int action) { | 293 void ReverseAutoLoginInfoBarDelegate::RecordHistogramAction(int action) { |
320 UMA_HISTOGRAM_ENUMERATION("AutoLogin.Reverse", action, HISTOGRAM_MAX); | 294 UMA_HISTOGRAM_ENUMERATION("AutoLogin.Reverse", action, HISTOGRAM_MAX); |
321 } | 295 } |
OLD | NEW |