1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/android/infobars/auto_login_infobar_delegate_android.h" 6 7 #include "base/android/jni_android.h" 8 #include "base/android/jni_string.h" 9 #include "base/android/jni_weak_ref.h" 10 #include "base/strings/utf_string_conversions.h" 11 #include "chrome/browser/infobars/infobar_service.h" 12 #include "chrome/browser/infobars/simple_alert_infobar_delegate.h" 13 #include "chrome/browser/ui/auto_login_infobar_delegate.h" 14 #include "components/infobars/core/infobar.h" 15 #include "content/public/browser/web_contents.h" 16 #include "grit/generated_resources.h" 17 #include "grit/theme_resources.h" 18 #include "jni/AutoLoginDelegate_jni.h" 19 #include "ui/base/l10n/l10n_util.h" 20 21 using base::android::ConvertUTF8ToJavaString; 22 using base::android::ScopedJavaLocalRef; 23 24 25 AutoLoginInfoBarDelegateAndroid::AutoLoginInfoBarDelegateAndroid( 26 const Params& params, 27 Profile* profile) 28 : AutoLoginInfoBarDelegate(params, profile), 29 params_(params) { 30 } 31 32 AutoLoginInfoBarDelegateAndroid::~AutoLoginInfoBarDelegateAndroid() { 33 } 34 35 bool AutoLoginInfoBarDelegateAndroid::AttachAccount( 36 JavaObjectWeakGlobalRef weak_java_auto_login_delegate) { 37 weak_java_auto_login_delegate_ = weak_java_auto_login_delegate; 38 JNIEnv* env = base::android::AttachCurrentThread(); 39 ScopedJavaLocalRef<jstring> jrealm = ConvertUTF8ToJavaString(env, realm()); 40 ScopedJavaLocalRef<jstring> jaccount = 41 ConvertUTF8ToJavaString(env, account()); 42 ScopedJavaLocalRef<jstring> jargs = ConvertUTF8ToJavaString(env, args()); 43 DCHECK(!jrealm.is_null()); 44 DCHECK(!jaccount.is_null()); 45 DCHECK(!jargs.is_null()); 46 47 ScopedJavaLocalRef<jobject> delegate = 48 weak_java_auto_login_delegate_.get(env); 49 DCHECK(delegate.obj()); 50 user_ = base::android::ConvertJavaStringToUTF8( 51 Java_AutoLoginDelegate_initializeAccount( 52 env, delegate.obj(), reinterpret_cast<intptr_t>(this), jrealm.obj(), 53 jaccount.obj(), jargs.obj())); 54 return !user_.empty(); 55 } 56 57 base::string16 AutoLoginInfoBarDelegateAndroid::GetMessageText() const { 58 return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE, 59 base::UTF8ToUTF16(user_)); 60 } 61 62 bool AutoLoginInfoBarDelegateAndroid::Accept() { 63 JNIEnv* env = base::android::AttachCurrentThread(); 64 ScopedJavaLocalRef<jobject> delegate = 65 weak_java_auto_login_delegate_.get(env); 66 DCHECK(delegate.obj()); 67 Java_AutoLoginDelegate_logIn(env, delegate.obj(), 68 reinterpret_cast<intptr_t>(this)); 69 // Do not close the infobar on accept, it will be closed as part 70 // of the log in callback. 71 RecordHistogramAction(ACCEPTED); 72 return false; 73 } 74 75 bool AutoLoginInfoBarDelegateAndroid::Cancel() { 76 JNIEnv* env = base::android::AttachCurrentThread(); 77 ScopedJavaLocalRef<jobject> delegate = 78 weak_java_auto_login_delegate_.get(env); 79 DCHECK(delegate.obj()); 80 Java_AutoLoginDelegate_cancelLogIn(env, delegate.obj(), 81 reinterpret_cast<intptr_t>(this)); 82 RecordHistogramAction(REJECTED); 83 return true; 84 } 85 86 void AutoLoginInfoBarDelegateAndroid::LoginSuccess(JNIEnv* env, 87 jobject obj, 88 jstring result) { 89 if (!infobar()->owner()) 90 return; // We're closing; don't call anything, it might access the owner. 91 92 // TODO(miguelg): Test whether the Stop() and RemoveInfoBar() calls here are 93 // necessary, or whether OpenURL() will do this for us. 94 content::WebContents* contents = 95 InfoBarService::WebContentsFromInfoBar(infobar()); 96 contents->Stop(); 97 infobar()->RemoveSelf(); 98 // WARNING: |this| may be deleted at this point! Do not access any members! 99 contents->OpenURL(content::OpenURLParams( 100 GURL(base::android::ConvertJavaStringToUTF8(env, result)), 101 content::Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_AUTO_BOOKMARK, 102 false)); 103 } 104 105 void AutoLoginInfoBarDelegateAndroid::LoginFailed(JNIEnv* env, jobject obj) { 106 if (!infobar()->owner()) 107 return; // We're closing; don't call anything, it might access the owner. 108 109 // TODO(miguelg): Using SimpleAlertInfoBarDelegate::Create() animates in a new 110 // infobar while we animate the current one closed. It would be better to use 111 // ReplaceInfoBar(). 112 InfoBarService* infobar_service = InfoBarService::FromWebContents( 113 InfoBarService::WebContentsFromInfoBar(infobar())); 114 DCHECK(infobar_service); 115 SimpleAlertInfoBarDelegate::Create( 116 infobar_service, IDR_INFOBAR_WARNING, 117 l10n_util::GetStringUTF16(IDS_AUTO_LOGIN_FAILED), false); 118 infobar()->RemoveSelf(); 119 } 120 121 void AutoLoginInfoBarDelegateAndroid::LoginDismiss(JNIEnv* env, jobject obj) { 122 infobar()->RemoveSelf(); 123 } 124 125 bool AutoLoginInfoBarDelegateAndroid::Register(JNIEnv* env) { 126 return RegisterNativesImpl(env); 127 } 128