Home | History | Annotate | Download | only in native
      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 "android_webview/native/aw_autofill_manager_delegate.h"
      6 
      7 #include "android_webview/browser/aw_browser_context.h"
      8 #include "android_webview/browser/aw_content_browser_client.h"
      9 #include "android_webview/browser/aw_pref_store.h"
     10 #include "android_webview/native/aw_contents.h"
     11 #include "base/android/jni_android.h"
     12 #include "base/android/jni_string.h"
     13 #include "base/android/scoped_java_ref.h"
     14 #include "base/logging.h"
     15 #include "base/prefs/pref_registry_simple.h"
     16 #include "base/prefs/pref_service.h"
     17 #include "base/prefs/pref_service_builder.h"
     18 #include "components/autofill/content/browser/autocheckout/whitelist_manager.h"
     19 #include "components/autofill/core/browser/autofill_popup_delegate.h"
     20 #include "components/autofill/core/common/autofill_pref_names.h"
     21 #include "components/user_prefs/user_prefs.h"
     22 #include "content/public/browser/web_contents.h"
     23 #include "content/public/browser/web_contents_view.h"
     24 #include "jni/AwAutofillManagerDelegate_jni.h"
     25 
     26 using base::android::AttachCurrentThread;
     27 using base::android::ConvertUTF16ToJavaString;
     28 using base::android::ScopedJavaLocalRef;
     29 using content::WebContents;
     30 
     31 DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwAutofillManagerDelegate);
     32 
     33 namespace android_webview {
     34 
     35 // Ownership: The native object is created (if autofill enabled) and owned by
     36 // AwContents. The native object creates the java peer which handles most
     37 // autofill functionality at the java side. The java peer is owned by Java
     38 // AwContents. The native object only maintains a weak ref to it.
     39 AwAutofillManagerDelegate::AwAutofillManagerDelegate(WebContents* contents)
     40     : web_contents_(contents),
     41       save_form_data_(false) {
     42   JNIEnv* env = AttachCurrentThread();
     43   ScopedJavaLocalRef<jobject> delegate;
     44   delegate.Reset(
     45       Java_AwAutofillManagerDelegate_create(env, reinterpret_cast<jint>(this)));
     46 
     47   AwContents* aw_contents = AwContents::FromWebContents(web_contents_);
     48   aw_contents->SetAwAutofillManagerDelegate(delegate.obj());
     49   java_ref_ = JavaObjectWeakGlobalRef(env, delegate.obj());
     50 }
     51 
     52 AwAutofillManagerDelegate::~AwAutofillManagerDelegate() {
     53   HideAutofillPopup();
     54 }
     55 
     56 void AwAutofillManagerDelegate::SetSaveFormData(bool enabled) {
     57   save_form_data_ = enabled;
     58 }
     59 
     60 bool AwAutofillManagerDelegate::GetSaveFormData() {
     61   return save_form_data_;
     62 }
     63 
     64 PrefService* AwAutofillManagerDelegate::GetPrefs() {
     65   return user_prefs::UserPrefs::Get(
     66       AwContentBrowserClient::GetAwBrowserContext());
     67 }
     68 
     69 autofill::PersonalDataManager*
     70 AwAutofillManagerDelegate::GetPersonalDataManager() {
     71   return NULL;
     72 }
     73 
     74 autofill::autocheckout::WhitelistManager*
     75 AwAutofillManagerDelegate::GetAutocheckoutWhitelistManager() const {
     76   return NULL;
     77 }
     78 
     79 void AwAutofillManagerDelegate::ShowAutofillPopup(
     80     const gfx::RectF& element_bounds,
     81     base::i18n::TextDirection text_direction,
     82     const std::vector<string16>& values,
     83     const std::vector<string16>& labels,
     84     const std::vector<string16>& icons,
     85     const std::vector<int>& identifiers,
     86     base::WeakPtr<autofill::AutofillPopupDelegate> delegate) {
     87 
     88   values_ = values;
     89   identifiers_ = identifiers;
     90   delegate_ = delegate;
     91 
     92   // Convert element_bounds to be in screen space.
     93   gfx::Rect client_area;
     94   web_contents_->GetView()->GetContainerBounds(&client_area);
     95   gfx::RectF element_bounds_in_screen_space =
     96       element_bounds + client_area.OffsetFromOrigin();
     97 
     98   ShowAutofillPopupImpl(element_bounds_in_screen_space,
     99                         values,
    100                         labels,
    101                         identifiers);
    102 }
    103 
    104 void AwAutofillManagerDelegate::ShowAutofillPopupImpl(
    105     const gfx::RectF& element_bounds,
    106     const std::vector<string16>& values,
    107     const std::vector<string16>& labels,
    108     const std::vector<int>& identifiers) {
    109   JNIEnv* env = AttachCurrentThread();
    110   ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
    111   if (obj.is_null())
    112     return;
    113 
    114   // We need an array of AutofillSuggestion.
    115   size_t count = values.size();
    116 
    117   ScopedJavaLocalRef<jobjectArray> data_array =
    118       Java_AwAutofillManagerDelegate_createAutofillSuggestionArray(env, count);
    119 
    120   for (size_t i = 0; i < count; ++i) {
    121     ScopedJavaLocalRef<jstring> name = ConvertUTF16ToJavaString(env, values[i]);
    122     ScopedJavaLocalRef<jstring> label =
    123         ConvertUTF16ToJavaString(env, labels[i]);
    124     Java_AwAutofillManagerDelegate_addToAutofillSuggestionArray(
    125         env,
    126         data_array.obj(),
    127         i,
    128         name.obj(),
    129         label.obj(),
    130         identifiers[i]);
    131   }
    132 
    133   Java_AwAutofillManagerDelegate_showAutofillPopup(
    134       env,
    135       obj.obj(),
    136       element_bounds.x(),
    137       element_bounds.y(), element_bounds.width(),
    138       element_bounds.height(), data_array.obj());
    139 }
    140 
    141 void AwAutofillManagerDelegate::UpdateAutofillPopupDataListValues(
    142     const std::vector<base::string16>& values,
    143     const std::vector<base::string16>& labels) {
    144   // Leaving as an empty method since updating autofill popup window
    145   // dynamically does not seem to be a useful feature for android webview.
    146   // See crrev.com/18102002 if need to implement.
    147 }
    148 
    149 void AwAutofillManagerDelegate::HideAutofillPopup() {
    150   JNIEnv* env = AttachCurrentThread();
    151   ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
    152   if (obj.is_null())
    153     return;
    154   delegate_.reset();
    155   Java_AwAutofillManagerDelegate_hideAutofillPopup(env, obj.obj());
    156 }
    157 
    158 bool AwAutofillManagerDelegate::IsAutocompleteEnabled() {
    159   return GetSaveFormData();
    160 }
    161 
    162 void AwAutofillManagerDelegate::SuggestionSelected(JNIEnv* env,
    163                                                    jobject object,
    164                                                    jint position) {
    165   if (delegate_)
    166     delegate_->DidAcceptSuggestion(values_[position], identifiers_[position]);
    167 }
    168 
    169 void AwAutofillManagerDelegate::HideRequestAutocompleteDialog() {
    170   NOTIMPLEMENTED();
    171 }
    172 
    173 void AwAutofillManagerDelegate::OnAutocheckoutError() {
    174   NOTIMPLEMENTED();
    175 }
    176 
    177 void AwAutofillManagerDelegate::OnAutocheckoutSuccess() {
    178   NOTIMPLEMENTED();
    179 }
    180 
    181 void AwAutofillManagerDelegate::ShowAutofillSettings() {
    182   NOTIMPLEMENTED();
    183 }
    184 
    185 void AwAutofillManagerDelegate::ConfirmSaveCreditCard(
    186     const autofill::AutofillMetrics& metric_logger,
    187     const autofill::CreditCard& credit_card,
    188     const base::Closure& save_card_callback) {
    189   NOTIMPLEMENTED();
    190 }
    191 
    192 bool AwAutofillManagerDelegate::ShowAutocheckoutBubble(
    193     const gfx::RectF& bounding_box,
    194     bool is_google_user,
    195     const base::Callback<void(autofill::AutocheckoutBubbleState)>& callback) {
    196   NOTIMPLEMENTED();
    197   return false;
    198 }
    199 
    200 void AwAutofillManagerDelegate::HideAutocheckoutBubble() {
    201   NOTIMPLEMENTED();
    202 }
    203 
    204 void AwAutofillManagerDelegate::ShowRequestAutocompleteDialog(
    205     const autofill::FormData& form,
    206     const GURL& source_url,
    207     autofill::DialogType dialog_type,
    208     const base::Callback<void(const autofill::FormStructure*,
    209                               const std::string&)>& callback) {
    210   NOTIMPLEMENTED();
    211 }
    212 
    213 void AwAutofillManagerDelegate::AddAutocheckoutStep(
    214     autofill::AutocheckoutStepType step_type) {
    215   NOTIMPLEMENTED();
    216 }
    217 
    218 void AwAutofillManagerDelegate::UpdateAutocheckoutStep(
    219     autofill::AutocheckoutStepType step_type,
    220     autofill::AutocheckoutStepStatus step_status) {
    221   NOTIMPLEMENTED();
    222 }
    223 
    224 bool RegisterAwAutofillManagerDelegate(JNIEnv* env) {
    225   return RegisterNativesImpl(env) >= 0;
    226 }
    227 
    228 } // namespace android_webview
    229