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_infobar_delegate.h" | 5 #include "chrome/browser/ui/android/infobars/auto_login_infobar_delegate.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/jni_helper.h" | |
9 #include "base/android/jni_string.h" | |
7 #include "base/bind.h" | 10 #include "base/bind.h" |
8 #include "base/logging.h" | 11 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
10 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
11 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
12 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
13 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/google/google_util.h" | 18 #include "chrome/browser/google/google_util.h" |
16 #include "chrome/browser/infobars/infobar.h" | 19 #include "chrome/browser/infobars/infobar.h" |
17 #include "chrome/browser/infobars/infobar_service.h" | 20 #include "chrome/browser/infobars/infobar_service.h" |
21 #include "chrome/browser/infobars/simple_alert_infobar_delegate.h" | |
18 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 23 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 24 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
21 #include "chrome/browser/signin/signin_manager.h" | 25 #include "chrome/browser/signin/signin_manager.h" |
22 #include "chrome/browser/signin/signin_manager_factory.h" | 26 #include "chrome/browser/signin/signin_manager_factory.h" |
23 #include "chrome/browser/ui/sync/sync_promo_ui.h" | 27 #include "chrome/browser/ui/sync/sync_promo_ui.h" |
24 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
25 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
26 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
27 #include "content/public/browser/navigation_controller.h" | 31 #include "content/public/browser/navigation_controller.h" |
28 #include "content/public/browser/notification_details.h" | 32 #include "content/public/browser/notification_details.h" |
29 #include "content/public/browser/notification_observer.h" | 33 #include "content/public/browser/notification_observer.h" |
30 #include "content/public/browser/notification_registrar.h" | 34 #include "content/public/browser/notification_registrar.h" |
31 #include "content/public/browser/notification_source.h" | 35 #include "content/public/browser/notification_source.h" |
32 #include "content/public/browser/notification_types.h" | 36 #include "content/public/browser/notification_types.h" |
33 #include "content/public/browser/page_navigator.h" | 37 #include "content/public/browser/page_navigator.h" |
34 #include "content/public/browser/web_contents.h" | 38 #include "content/public/browser/web_contents.h" |
35 #include "content/public/browser/web_contents_observer.h" | 39 #include "content/public/browser/web_contents_observer.h" |
36 #include "content/public/common/referrer.h" | 40 #include "content/public/common/referrer.h" |
37 #include "google_apis/gaia/gaia_constants.h" | 41 #include "google_apis/gaia/gaia_constants.h" |
38 #include "google_apis/gaia/gaia_urls.h" | 42 #include "google_apis/gaia/gaia_urls.h" |
39 #include "google_apis/gaia/ubertoken_fetcher.h" | 43 #include "google_apis/gaia/ubertoken_fetcher.h" |
40 #include "grit/chromium_strings.h" | 44 #include "grit/chromium_strings.h" |
41 #include "grit/generated_resources.h" | 45 #include "grit/generated_resources.h" |
42 #include "grit/theme_resources.h" | 46 #include "grit/theme_resources.h" |
47 #include "jni/AutoLoginDelegate_jni.h" | |
43 #include "net/base/escape.h" | 48 #include "net/base/escape.h" |
44 #include "net/url_request/url_request.h" | 49 #include "net/url_request/url_request.h" |
45 #include "ui/base/l10n/l10n_util.h" | 50 #include "ui/base/l10n/l10n_util.h" |
46 | 51 |
47 #if defined(OS_ANDROID) | 52 using base::android::ConvertUTF8ToJavaString; |
48 #include "chrome/browser/ui/android/infobars/auto_login_infobar_delegate_android .h" | 53 using base::android::ScopedJavaLocalRef; |
49 #endif | |
50 | 54 |
51 | 55 |
52 // AutoLoginRedirector -------------------------------------------------------- | 56 // AutoLoginRedirector -------------------------------------------------------- |
53 | 57 |
54 namespace { | 58 namespace { |
55 | 59 |
56 // This class is created by the AutoLoginInfoBarDelegate when the user wishes to | 60 // This class is created by the AutoLoginInfoBarDelegate when the user wishes to |
57 // auto-login. It holds context information needed while re-issuing service | 61 // auto-login. It holds context information needed while re-issuing service |
58 // tokens using the OAuth2TokenService, gets the browser cookies with the | 62 // tokens using the OAuth2TokenService, gets the browser cookies with the |
59 // TokenAuth API, and finally redirects the user to the correct page. | 63 // TokenAuth API, and finally redirects the user to the correct page. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 const Params& params) { | 146 const Params& params) { |
143 // If |web_contents| is hosted in a WebDialog, there may be no infobar | 147 // If |web_contents| is hosted in a WebDialog, there may be no infobar |
144 // service. | 148 // service. |
145 InfoBarService* infobar_service = | 149 InfoBarService* infobar_service = |
146 InfoBarService::FromWebContents(web_contents); | 150 InfoBarService::FromWebContents(web_contents); |
147 if (!infobar_service) | 151 if (!infobar_service) |
148 return false; | 152 return false; |
149 | 153 |
150 Profile* profile = | 154 Profile* profile = |
151 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 155 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
152 #if defined(OS_ANDROID) | |
153 typedef AutoLoginInfoBarDelegateAndroid Delegate; | |
154 #else | |
155 typedef AutoLoginInfoBarDelegate Delegate; | |
156 #endif | |
157 return !!infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar( | 156 return !!infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar( |
158 scoped_ptr<ConfirmInfoBarDelegate>(new Delegate(params, profile)))); | 157 scoped_ptr<ConfirmInfoBarDelegate>( |
158 new AutoLoginInfoBarDelegate(params, profile)))); | |
159 } | 159 } |
160 | 160 |
161 AutoLoginInfoBarDelegate::AutoLoginInfoBarDelegate(const Params& params, | 161 AutoLoginInfoBarDelegate::AutoLoginInfoBarDelegate(const Params& params, |
162 Profile* profile) | 162 Profile* profile) |
163 : ConfirmInfoBarDelegate(), | 163 : ConfirmInfoBarDelegate(), |
164 params_(params), | 164 params_(params), |
165 button_pressed_(false) { | 165 button_pressed_(false) { |
166 RecordHistogramAction(SHOWN); | 166 RecordHistogramAction(SHOWN); |
167 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | 167 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, |
168 content::Source<Profile>(profile)); | 168 content::Source<Profile>(profile)); |
169 } | 169 } |
170 | 170 |
171 AutoLoginInfoBarDelegate::~AutoLoginInfoBarDelegate() { | 171 AutoLoginInfoBarDelegate::~AutoLoginInfoBarDelegate() { |
172 if (!button_pressed_) | 172 if (!button_pressed_) |
173 RecordHistogramAction(IGNORED); | 173 RecordHistogramAction(IGNORED); |
174 } | 174 } |
175 | 175 |
176 bool AutoLoginInfoBarDelegate::AttachAccount( | |
177 JavaObjectWeakGlobalRef weak_java_auto_login_delegate) { | |
178 weak_java_auto_login_delegate_ = weak_java_auto_login_delegate; | |
179 JNIEnv* env = base::android::AttachCurrentThread(); | |
180 ScopedJavaLocalRef<jstring> jrealm = ConvertUTF8ToJavaString(env, realm()); | |
181 ScopedJavaLocalRef<jstring> jaccount = | |
182 ConvertUTF8ToJavaString(env, account()); | |
183 ScopedJavaLocalRef<jstring> jargs = ConvertUTF8ToJavaString(env, args()); | |
184 DCHECK(!jrealm.is_null()); | |
185 DCHECK(!jaccount.is_null()); | |
186 DCHECK(!jargs.is_null()); | |
187 | |
188 ScopedJavaLocalRef<jobject> delegate = | |
189 weak_java_auto_login_delegate_.get(env); | |
190 DCHECK(delegate.obj()); | |
191 user_ = base::android::ConvertJavaStringToUTF8( | |
192 Java_AutoLoginDelegate_initializeAccount( | |
193 env, delegate.obj(), reinterpret_cast<intptr_t>(this), jrealm.obj(), | |
194 jaccount.obj(), jargs.obj())); | |
195 return !user_.empty(); | |
196 } | |
197 | |
198 void AutoLoginInfoBarDelegate::LoginSuccess(JNIEnv* env, | |
199 jobject obj, | |
200 jstring result) { | |
201 if (!infobar()->owner()) | |
202 return; // We're closing; don't call anything, it might access the owner. | |
203 | |
204 // TODO(miguelg): Test whether the Stop() and RemoveInfoBar() calls here are | |
205 // necessary, or whether OpenURL() will do this for us. | |
206 content::WebContents* contents = web_contents(); | |
207 contents->Stop(); | |
208 infobar()->RemoveSelf(); | |
209 // WARNING: |this| may be deleted at this point! Do not access any members! | |
210 contents->OpenURL(content::OpenURLParams( | |
211 GURL(base::android::ConvertJavaStringToUTF8(env, result)), | |
212 content::Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_AUTO_BOOKMARK, | |
213 false)); | |
214 } | |
215 | |
216 void AutoLoginInfoBarDelegate::LoginFailed(JNIEnv* env, jobject obj) { | |
217 if (!infobar()->owner()) | |
218 return; // We're closing; don't call anything, it might access the owner. | |
219 | |
220 // TODO(miguelg): Using SimpleAlertInfoBarDelegate::Create() animates in a new | |
221 // infobar while we animate the current one closed. It would be better to use | |
222 // ReplaceInfoBar(). | |
223 SimpleAlertInfoBarDelegate::Create( | |
224 infobar()->owner(), IDR_INFOBAR_WARNING, | |
225 l10n_util::GetStringUTF16(IDS_AUTO_LOGIN_FAILED), false); | |
226 infobar()->RemoveSelf(); | |
227 } | |
228 | |
229 void AutoLoginInfoBarDelegate::LoginDismiss(JNIEnv* env, jobject obj) { | |
230 infobar()->RemoveSelf(); | |
231 } | |
232 | |
233 bool AutoLoginInfoBarDelegate::Register(JNIEnv* env) { | |
234 return RegisterNativesImpl(env); | |
235 } | |
236 | |
176 void AutoLoginInfoBarDelegate::InfoBarDismissed() { | 237 void AutoLoginInfoBarDelegate::InfoBarDismissed() { |
177 RecordHistogramAction(DISMISSED); | 238 RecordHistogramAction(DISMISSED); |
178 button_pressed_ = true; | 239 button_pressed_ = true; |
179 } | 240 } |
180 | 241 |
181 int AutoLoginInfoBarDelegate::GetIconID() const { | 242 int AutoLoginInfoBarDelegate::GetIconID() const { |
182 return IDR_INFOBAR_AUTOLOGIN; | 243 return IDR_INFOBAR_AUTOLOGIN; |
183 } | 244 } |
184 | 245 |
185 InfoBarDelegate::Type AutoLoginInfoBarDelegate::GetInfoBarType() const { | 246 InfoBarDelegate::Type AutoLoginInfoBarDelegate::GetInfoBarType() const { |
186 return PAGE_ACTION_TYPE; | 247 return PAGE_ACTION_TYPE; |
187 } | 248 } |
188 | 249 |
189 AutoLoginInfoBarDelegate* | 250 AutoLoginInfoBarDelegate* |
190 AutoLoginInfoBarDelegate::AsAutoLoginInfoBarDelegate() { | 251 AutoLoginInfoBarDelegate::AsAutoLoginInfoBarDelegate() { |
191 return this; | 252 return this; |
192 } | 253 } |
193 | 254 |
194 base::string16 AutoLoginInfoBarDelegate::GetMessageText() const { | 255 base::string16 AutoLoginInfoBarDelegate::GetMessageText() const { |
195 return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE, | 256 return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE, |
196 base::UTF8ToUTF16(params_.username)); | 257 base::UTF8ToUTF16(user_)); |
197 } | 258 } |
198 | 259 |
199 base::string16 AutoLoginInfoBarDelegate::GetButtonLabel( | 260 base::string16 AutoLoginInfoBarDelegate::GetButtonLabel( |
200 InfoBarButton button) const { | 261 InfoBarButton button) const { |
201 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? | 262 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? |
202 IDS_AUTOLOGIN_INFOBAR_OK_BUTTON : IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON); | 263 IDS_AUTOLOGIN_INFOBAR_OK_BUTTON : IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON); |
203 } | 264 } |
204 | 265 |
205 bool AutoLoginInfoBarDelegate::Accept() { | 266 bool AutoLoginInfoBarDelegate::Accept() { |
206 // AutoLoginRedirector deletes itself. | 267 JNIEnv* env = base::android::AttachCurrentThread(); |
207 new AutoLoginRedirector(web_contents(), params_.header.args); | 268 ScopedJavaLocalRef<jobject> delegate = |
208 RecordHistogramAction(ACCEPTED); | 269 weak_java_auto_login_delegate_.get(env); |
209 button_pressed_ = true; | 270 DCHECK(delegate.obj()); |
271 Java_AutoLoginDelegate_logIn(env, delegate.obj(), | |
272 reinterpret_cast<intptr_t>(this)); | |
273 // Do not close the infobar on accept, it will be closed as part | |
274 // of the log in callback. | |
275 return false; | |
276 } | |
277 | |
278 bool AutoLoginInfoBarDelegate::Cancel() { | |
279 JNIEnv* env = base::android::AttachCurrentThread(); | |
280 ScopedJavaLocalRef<jobject> delegate = | |
281 weak_java_auto_login_delegate_.get(env); | |
282 DCHECK(delegate.obj()); | |
283 Java_AutoLoginDelegate_cancelLogIn(env, delegate.obj(), | |
284 reinterpret_cast<intptr_t>(this)); | |
210 return true; | 285 return true; |
211 } | 286 } |
212 | 287 |
213 bool AutoLoginInfoBarDelegate::Cancel() { | |
214 PrefService* pref_service = Profile::FromBrowserContext( | |
215 web_contents()->GetBrowserContext())->GetPrefs(); | |
216 pref_service->SetBoolean(prefs::kAutologinEnabled, false); | |
217 RecordHistogramAction(REJECTED); | |
nyquist
2014/03/03 17:56:29
Does this histogram go away now?
Roger Tawa OOO till Jul 10th
2014/03/04 20:32:34
Its possible this will no longer be recorded. I'l
| |
218 button_pressed_ = true; | |
219 return true; | |
220 } | |
221 | |
222 void AutoLoginInfoBarDelegate::Observe( | 288 void AutoLoginInfoBarDelegate::Observe( |
223 int type, | 289 int type, |
224 const content::NotificationSource& source, | 290 const content::NotificationSource& source, |
225 const content::NotificationDetails& details) { | 291 const content::NotificationDetails& details) { |
226 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, type); | 292 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, type); |
227 infobar()->RemoveSelf(); | 293 infobar()->RemoveSelf(); |
228 } | 294 } |
229 | 295 |
230 void AutoLoginInfoBarDelegate::RecordHistogramAction(Actions action) { | 296 void AutoLoginInfoBarDelegate::RecordHistogramAction(Actions action) { |
231 UMA_HISTOGRAM_ENUMERATION("AutoLogin.Regular", action, | 297 UMA_HISTOGRAM_ENUMERATION("AutoLogin.Regular", action, |
232 HISTOGRAM_BOUNDING_VALUE); | 298 HISTOGRAM_BOUNDING_VALUE); |
233 } | 299 } |
OLD | NEW |