Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / android / autofill / autofill_popup_view_android.cc
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/autofill/autofill_popup_view_android.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "chrome/browser/ui/android/window_android_helper.h"
10 #include "chrome/browser/ui/autofill/autofill_popup_controller.h"
11 #include "content/public/browser/android/content_view_core.h"
12 #include "jni/AutofillPopupBridge_jni.h"
13 #include "ui/base/android/view_android.h"
14 #include "ui/base/android/window_android.h"
15 #include "ui/gfx/rect.h"
16
17 namespace autofill {
18
19 AutofillPopupViewAndroid::AutofillPopupViewAndroid(
20     AutofillPopupController* controller)
21     : controller_(controller) {}
22
23 AutofillPopupViewAndroid::~AutofillPopupViewAndroid() {}
24
25 void AutofillPopupViewAndroid::Show() {
26   JNIEnv* env = base::android::AttachCurrentThread();
27   ui::ViewAndroid* view_android = controller_->container_view();
28
29   DCHECK(view_android);
30
31   java_object_.Reset(Java_AutofillPopupBridge_create(
32       env,
33       reinterpret_cast<intptr_t>(this),
34       view_android->GetWindowAndroid()->GetJavaObject().obj(),
35       view_android->GetJavaObject().obj()));
36
37   UpdateBoundsAndRedrawPopup();
38 }
39
40 void AutofillPopupViewAndroid::Hide() {
41   controller_ = NULL;
42   JNIEnv* env = base::android::AttachCurrentThread();
43   Java_AutofillPopupBridge_hide(env, java_object_.obj());
44 }
45
46 void AutofillPopupViewAndroid::UpdateBoundsAndRedrawPopup() {
47   JNIEnv* env = base::android::AttachCurrentThread();
48   Java_AutofillPopupBridge_setAnchorRect(
49       env,
50       java_object_.obj(),
51       controller_->element_bounds().x(),
52       controller_->element_bounds().y(),
53       controller_->element_bounds().width(),
54       controller_->element_bounds().height());
55
56   // We need an array of AutofillSuggestion.
57   size_t count = controller_->names().size();
58
59   ScopedJavaLocalRef<jobjectArray> data_array =
60       Java_AutofillPopupBridge_createAutofillSuggestionArray(env, count);
61
62   for (size_t i = 0; i < count; ++i) {
63     ScopedJavaLocalRef<jstring> name =
64         base::android::ConvertUTF16ToJavaString(env, controller_->names()[i]);
65     ScopedJavaLocalRef<jstring> subtext =
66         base::android::ConvertUTF16ToJavaString(env,
67                                                 controller_->subtexts()[i]);
68     Java_AutofillPopupBridge_addToAutofillSuggestionArray(
69         env,
70         data_array.obj(),
71         i,
72         name.obj(),
73         subtext.obj(),
74         controller_->identifiers()[i]);
75   }
76
77   Java_AutofillPopupBridge_show(
78       env, java_object_.obj(), data_array.obj(), controller_->IsRTL());
79 }
80
81 void AutofillPopupViewAndroid::SuggestionSelected(JNIEnv* env,
82                                                   jobject obj,
83                                                   jint list_index) {
84   controller_->AcceptSuggestion(list_index);
85 }
86
87 void AutofillPopupViewAndroid::PopupDismissed(JNIEnv* env, jobject obj) {
88   if (controller_)
89     controller_->ViewDestroyed();
90
91   delete this;
92 }
93
94 void AutofillPopupViewAndroid::InvalidateRow(size_t) {}
95
96 // static
97 bool AutofillPopupViewAndroid::RegisterAutofillPopupViewAndroid(JNIEnv* env) {
98   return RegisterNativesImpl(env);
99 }
100
101 // static
102 AutofillPopupView* AutofillPopupView::Create(
103     AutofillPopupController* controller) {
104   return new AutofillPopupViewAndroid(controller);
105 }
106
107 }  // namespace autofill