- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / android / infobars / auto_login_infobar_delegate_android.cc
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.
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_helper.h"
9 #include "base/android/jni_string.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/infobars/simple_alert_infobar_delegate.h"
12 #include "chrome/browser/ui/auto_login_infobar_delegate.h"
13 #include "content/public/browser/web_contents.h"
14 #include "grit/generated_resources.h"
15 #include "grit/theme_resources.h"
16 #include "jni/AutoLoginDelegate_jni.h"
17 #include "ui/base/l10n/l10n_util.h"
18
19 using base::android::ConvertUTF8ToJavaString;
20 using base::android::ScopedJavaLocalRef;
21
22 AutoLoginInfoBarDelegateAndroid::AutoLoginInfoBarDelegateAndroid(
23     InfoBarService* owner,
24     const Params& params)
25     : AutoLoginInfoBarDelegate(owner, params),
26       params_(params) {
27 }
28
29 AutoLoginInfoBarDelegateAndroid::~AutoLoginInfoBarDelegateAndroid() {
30 }
31
32 bool AutoLoginInfoBarDelegateAndroid::AttachAccount(
33     JavaObjectWeakGlobalRef weak_java_auto_login_delegate) {
34   weak_java_auto_login_delegate_ = weak_java_auto_login_delegate;
35   JNIEnv* env = base::android::AttachCurrentThread();
36   ScopedJavaLocalRef<jstring> jrealm = ConvertUTF8ToJavaString(env, realm());
37   ScopedJavaLocalRef<jstring> jaccount =
38       ConvertUTF8ToJavaString(env, account());
39   ScopedJavaLocalRef<jstring> jargs = ConvertUTF8ToJavaString(env, args());
40   DCHECK(!jrealm.is_null());
41   DCHECK(!jaccount.is_null());
42   DCHECK(!jargs.is_null());
43
44   ScopedJavaLocalRef<jobject> delegate =
45       weak_java_auto_login_delegate_.get(env);
46   DCHECK(delegate.obj());
47   user_ = base::android::ConvertJavaStringToUTF8(
48       Java_AutoLoginDelegate_initializeAccount(
49           env, delegate.obj(), reinterpret_cast<jint>(this), jrealm.obj(),
50           jaccount.obj(), jargs.obj()));
51   return !user_.empty();
52 }
53
54 string16 AutoLoginInfoBarDelegateAndroid::GetMessageText() const {
55   return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE,
56                                     UTF8ToUTF16(user_));
57 }
58
59 bool AutoLoginInfoBarDelegateAndroid::Accept() {
60   JNIEnv* env = base::android::AttachCurrentThread();
61   ScopedJavaLocalRef<jobject> delegate =
62       weak_java_auto_login_delegate_.get(env);
63   DCHECK(delegate.obj());
64
65   Java_AutoLoginDelegate_logIn(env, delegate.obj(),
66                                reinterpret_cast<jint>(this));
67
68   // Do not close the infobar on accept, it will be closed as part
69   // of the log in callback.
70   return false;
71 }
72
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<jint>(this));
80   return true;
81 }
82
83 void AutoLoginInfoBarDelegateAndroid::LoginSuccess(JNIEnv* env,
84                                                    jobject obj,
85                                                    jstring result) {
86   if (owner()) {
87     content::WebContents* web_contents = owner()->web_contents();
88     if (web_contents) {
89       web_contents->Stop();
90       web_contents->OpenURL(content::OpenURLParams(
91           GURL(base::android::ConvertJavaStringToUTF8(env, result)),
92           content::Referrer(), CURRENT_TAB,
93           content::PAGE_TRANSITION_AUTO_BOOKMARK, false));
94     }
95     owner()->RemoveInfoBar(this);
96   }
97 }
98
99 void AutoLoginInfoBarDelegateAndroid::LoginFailed(JNIEnv* env, jobject obj) {
100   ScopedJavaLocalRef<jobject> delegate =
101       weak_java_auto_login_delegate_.get(env);
102   DCHECK(delegate.obj());
103   if (owner()) {
104     SimpleAlertInfoBarDelegate::Create(
105         owner(), IDR_INFOBAR_WARNING,
106         l10n_util::GetStringUTF16(IDS_AUTO_LOGIN_FAILED), false);
107     owner()->RemoveInfoBar(this);
108   }
109 }
110
111 void AutoLoginInfoBarDelegateAndroid::LoginDismiss(JNIEnv* env, jobject obj) {
112   if (owner())
113     owner()->RemoveInfoBar(this);
114 }
115
116 bool AutoLoginInfoBarDelegateAndroid::Register(JNIEnv* env) {
117   return RegisterNativesImpl(env);
118 }