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