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