1 // Copyright 2013 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_helper.h"
9 #include "base/android/jni_string.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/infobars/infobar.h"
12 #include "chrome/browser/infobars/simple_alert_infobar_delegate.h"
13 #include "chrome/browser/ui/auto_login_infobar_delegate.h"
14 #include "content/public/browser/web_contents.h"
15 #include "grit/generated_resources.h"
16 #include "grit/theme_resources.h"
17 #include "jni/AutoLoginDelegate_jni.h"
18 #include "ui/base/l10n/l10n_util.h"
20 using base::android::ConvertUTF8ToJavaString
;
21 using base::android::ScopedJavaLocalRef
;
24 AutoLoginInfoBarDelegateAndroid::AutoLoginInfoBarDelegateAndroid(
27 : AutoLoginInfoBarDelegate(params
, profile
),
31 AutoLoginInfoBarDelegateAndroid::~AutoLoginInfoBarDelegateAndroid() {
34 bool AutoLoginInfoBarDelegateAndroid::AttachAccount(
35 JavaObjectWeakGlobalRef weak_java_auto_login_delegate
) {
36 weak_java_auto_login_delegate_
= weak_java_auto_login_delegate
;
37 JNIEnv
* env
= base::android::AttachCurrentThread();
38 ScopedJavaLocalRef
<jstring
> jrealm
= ConvertUTF8ToJavaString(env
, realm());
39 ScopedJavaLocalRef
<jstring
> jaccount
=
40 ConvertUTF8ToJavaString(env
, account());
41 ScopedJavaLocalRef
<jstring
> jargs
= ConvertUTF8ToJavaString(env
, args());
42 DCHECK(!jrealm
.is_null());
43 DCHECK(!jaccount
.is_null());
44 DCHECK(!jargs
.is_null());
46 ScopedJavaLocalRef
<jobject
> delegate
=
47 weak_java_auto_login_delegate_
.get(env
);
48 DCHECK(delegate
.obj());
49 user_
= base::android::ConvertJavaStringToUTF8(
50 Java_AutoLoginDelegate_initializeAccount(
51 env
, delegate
.obj(), reinterpret_cast<intptr_t>(this), jrealm
.obj(),
52 jaccount
.obj(), jargs
.obj()));
53 return !user_
.empty();
56 base::string16
AutoLoginInfoBarDelegateAndroid::GetMessageText() const {
57 return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE
,
58 base::UTF8ToUTF16(user_
));
61 bool AutoLoginInfoBarDelegateAndroid::Accept() {
62 JNIEnv
* env
= base::android::AttachCurrentThread();
63 ScopedJavaLocalRef
<jobject
> delegate
=
64 weak_java_auto_login_delegate_
.get(env
);
65 DCHECK(delegate
.obj());
66 Java_AutoLoginDelegate_logIn(env
, delegate
.obj(),
67 reinterpret_cast<intptr_t>(this));
68 // Do not close the infobar on accept, it will be closed as part
69 // of the log in callback.
73 bool AutoLoginInfoBarDelegateAndroid::Cancel() {
74 JNIEnv
* env
= base::android::AttachCurrentThread();
75 ScopedJavaLocalRef
<jobject
> delegate
=
76 weak_java_auto_login_delegate_
.get(env
);
77 DCHECK(delegate
.obj());
78 Java_AutoLoginDelegate_cancelLogIn(env
, delegate
.obj(),
79 reinterpret_cast<intptr_t>(this));
83 void AutoLoginInfoBarDelegateAndroid::LoginSuccess(JNIEnv
* env
,
86 if (!infobar()->owner())
87 return; // We're closing; don't call anything, it might access the owner.
89 // TODO(miguelg): Test whether the Stop() and RemoveInfoBar() calls here are
90 // necessary, or whether OpenURL() will do this for us.
91 content::WebContents
* contents
= web_contents();
93 infobar()->RemoveSelf();
94 // WARNING: |this| may be deleted at this point! Do not access any members!
95 contents
->OpenURL(content::OpenURLParams(
96 GURL(base::android::ConvertJavaStringToUTF8(env
, result
)),
97 content::Referrer(), CURRENT_TAB
, content::PAGE_TRANSITION_AUTO_BOOKMARK
,
101 void AutoLoginInfoBarDelegateAndroid::LoginFailed(JNIEnv
* env
, jobject obj
) {
102 if (!infobar()->owner())
103 return; // We're closing; don't call anything, it might access the owner.
105 // TODO(miguelg): Using SimpleAlertInfoBarDelegate::Create() animates in a new
106 // infobar while we animate the current one closed. It would be better to use
108 SimpleAlertInfoBarDelegate::Create(
109 infobar()->owner(), IDR_INFOBAR_WARNING
,
110 l10n_util::GetStringUTF16(IDS_AUTO_LOGIN_FAILED
), false);
111 infobar()->RemoveSelf();
114 void AutoLoginInfoBarDelegateAndroid::LoginDismiss(JNIEnv
* env
, jobject obj
) {
115 infobar()->RemoveSelf();
118 bool AutoLoginInfoBarDelegateAndroid::Register(JNIEnv
* env
) {
119 return RegisterNativesImpl(env
);