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.
5 #include "chrome/browser/ui/android/infobars/auto_login_infobar_delegate_android.h"
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"
21 using base::android::ConvertUTF8ToJavaString;
22 using base::android::ScopedJavaLocalRef;
25 AutoLoginInfoBarDelegateAndroid::AutoLoginInfoBarDelegateAndroid(
28 : AutoLoginInfoBarDelegate(params, profile),
32 AutoLoginInfoBarDelegateAndroid::~AutoLoginInfoBarDelegateAndroid() {
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());
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();
57 base::string16 AutoLoginInfoBarDelegateAndroid::GetMessageText() const {
58 return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE,
59 base::UTF8ToUTF16(user_));
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);
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);
86 void AutoLoginInfoBarDelegateAndroid::LoginSuccess(JNIEnv* env,
89 if (!infobar()->owner())
90 return; // We're closing; don't call anything, it might access the owner.
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());
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,
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.
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
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();
121 void AutoLoginInfoBarDelegateAndroid::LoginDismiss(JNIEnv* env, jobject obj) {
122 infobar()->RemoveSelf();
125 bool AutoLoginInfoBarDelegateAndroid::Register(JNIEnv* env) {
126 return RegisterNativesImpl(env);