Home | History | Annotate | Download | only in android
      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.
      4 
      5 #include "chrome/browser/ui/android/chrome_http_auth_handler.h"
      6 
      7 #include <jni.h>
      8 
      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 "grit/generated_resources.h"
     15 #include "jni/ChromeHttpAuthHandler_jni.h"
     16 #include "ui/base/l10n/l10n_util.h"
     17 
     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;
     23 
     24 ChromeHttpAuthHandler::ChromeHttpAuthHandler(const string16& explanation)
     25     : observer_(NULL),
     26       explanation_(explanation) {
     27 }
     28 
     29 ChromeHttpAuthHandler::~ChromeHttpAuthHandler() {}
     30 
     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<jint>(this)));
     36 }
     37 
     38 void ChromeHttpAuthHandler::SetObserver(LoginHandler* observer) {
     39   observer_ = observer;
     40 }
     41 
     42 jobject ChromeHttpAuthHandler::GetJavaObject() {
     43   return java_chrome_http_auth_handler_.obj();
     44 }
     45 
     46 void ChromeHttpAuthHandler::OnAutofillDataAvailable(const string16& username,
     47                                                     const string16& password) {
     48   DCHECK(java_chrome_http_auth_handler_.obj() != NULL);
     49   JNIEnv* env = base::android::AttachCurrentThread();
     50   ScopedJavaLocalRef<jstring> j_username =
     51       ConvertUTF16ToJavaString(env, username);
     52   ScopedJavaLocalRef<jstring> j_password =
     53       ConvertUTF16ToJavaString(env, password);
     54   Java_ChromeHttpAuthHandler_onAutofillDataAvailable(
     55       env, java_chrome_http_auth_handler_.obj(),
     56       j_username.obj(), j_password.obj());
     57 }
     58 
     59 void ChromeHttpAuthHandler::SetAuth(JNIEnv* env,
     60                                     jobject,
     61                                     jstring username,
     62                                     jstring password) {
     63   if (observer_) {
     64     string16 username16 = ConvertJavaStringToUTF16(env, username);
     65     string16 password16 = ConvertJavaStringToUTF16(env, password);
     66     observer_->SetAuth(username16, password16);
     67   }
     68 }
     69 
     70 void ChromeHttpAuthHandler::CancelAuth(JNIEnv* env, jobject) {
     71   if (observer_)
     72     observer_->CancelAuth();
     73 }
     74 
     75 ScopedJavaLocalRef<jstring> ChromeHttpAuthHandler::GetMessageTitle(
     76     JNIEnv* env, jobject) {
     77   return ConvertUTF16ToJavaString(env,
     78       l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_TITLE));
     79 }
     80 
     81 ScopedJavaLocalRef<jstring> ChromeHttpAuthHandler::GetMessageBody(
     82     JNIEnv* env, jobject) {
     83   return ConvertUTF16ToJavaString(env, explanation_);
     84 }
     85 
     86 ScopedJavaLocalRef<jstring> ChromeHttpAuthHandler::GetUsernameLabelText(
     87     JNIEnv* env, jobject) {
     88   return ConvertUTF16ToJavaString(env,
     89       l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_USERNAME_FIELD));
     90 }
     91 
     92 ScopedJavaLocalRef<jstring> ChromeHttpAuthHandler::GetPasswordLabelText(
     93     JNIEnv* env, jobject) {
     94   return ConvertUTF16ToJavaString(env,
     95       l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_PASSWORD_FIELD));
     96 }
     97 
     98 ScopedJavaLocalRef<jstring> ChromeHttpAuthHandler::GetOkButtonText(
     99     JNIEnv* env, jobject) {
    100   return ConvertUTF16ToJavaString(env,
    101       l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_OK_BUTTON_LABEL));
    102 }
    103 
    104 ScopedJavaLocalRef<jstring> ChromeHttpAuthHandler::GetCancelButtonText(
    105     JNIEnv* env, jobject) {
    106   return ConvertUTF16ToJavaString(env, l10n_util::GetStringUTF16(IDS_CANCEL));
    107 }
    108 
    109 // static
    110 bool ChromeHttpAuthHandler::RegisterChromeHttpAuthHandler(JNIEnv* env) {
    111   return RegisterNativesImpl(env);
    112 }
    113