Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / android / chrome_http_auth_handler.cc
blob881e205cccc5550d62843700b6483b8753460daf
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/android/chrome_http_auth_handler.h"
7 #include <jni.h>
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/logging.h"
13 #include "base/strings/string16.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "jni/ChromeHttpAuthHandler_jni.h"
16 #include "ui/base/l10n/l10n_util.h"
18 using base::android::AttachCurrentThread;
19 using base::android::CheckException;
20 using base::android::ConvertJavaStringToUTF16;
21 using base::android::ConvertUTF16ToJavaString;
22 using base::android::ScopedJavaLocalRef;
24 ChromeHttpAuthHandler::ChromeHttpAuthHandler(const base::string16& explanation)
25 : observer_(NULL),
26 explanation_(explanation) {
29 ChromeHttpAuthHandler::~ChromeHttpAuthHandler() {}
31 void ChromeHttpAuthHandler::Init() {
32 DCHECK(java_chrome_http_auth_handler_.is_null());
33 JNIEnv* env = AttachCurrentThread();
34 java_chrome_http_auth_handler_.Reset(
35 Java_ChromeHttpAuthHandler_create(env, reinterpret_cast<intptr_t>(this)));
38 void ChromeHttpAuthHandler::SetObserver(LoginHandler* observer) {
39 observer_ = observer;
42 void ChromeHttpAuthHandler::ShowDialog(jobject window_android) {
43 JNIEnv* env = AttachCurrentThread();
44 Java_ChromeHttpAuthHandler_showDialog(
45 env,
46 java_chrome_http_auth_handler_.obj(),
47 window_android);
50 void ChromeHttpAuthHandler::OnAutofillDataAvailable(
51 const base::string16& username,
52 const base::string16& password) {
53 DCHECK(java_chrome_http_auth_handler_.obj() != NULL);
54 JNIEnv* env = base::android::AttachCurrentThread();
55 ScopedJavaLocalRef<jstring> j_username =
56 ConvertUTF16ToJavaString(env, username);
57 ScopedJavaLocalRef<jstring> j_password =
58 ConvertUTF16ToJavaString(env, password);
59 Java_ChromeHttpAuthHandler_onAutofillDataAvailable(
60 env, java_chrome_http_auth_handler_.obj(),
61 j_username.obj(), j_password.obj());
64 void ChromeHttpAuthHandler::SetAuth(JNIEnv* env,
65 jobject,
66 jstring username,
67 jstring password) {
68 if (observer_) {
69 base::string16 username16 = ConvertJavaStringToUTF16(env, username);
70 base::string16 password16 = ConvertJavaStringToUTF16(env, password);
71 observer_->SetAuth(username16, password16);
75 void ChromeHttpAuthHandler::CancelAuth(JNIEnv* env, jobject) {
76 if (observer_)
77 observer_->CancelAuth();
80 ScopedJavaLocalRef<jstring> ChromeHttpAuthHandler::GetMessageBody(
81 JNIEnv* env, jobject) {
82 return ConvertUTF16ToJavaString(env, explanation_);
85 // static
86 bool ChromeHttpAuthHandler::RegisterChromeHttpAuthHandler(JNIEnv* env) {
87 return RegisterNativesImpl(env);