Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / android / autofill / autofill_keyboard_accessory_view.cc
blobed42f87fd6d8ef3af4ab874dd0a12c9b13902511
1 // Copyright (c) 2015 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/autofill/autofill_keyboard_accessory_view.h"
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 "components/autofill/core/browser/popup_item_ids.h"
13 #include "components/autofill/core/browser/suggestion.h"
14 #include "grit/components_strings.h"
15 #include "jni/AutofillKeyboardAccessoryBridge_jni.h"
16 #include "ui/android/view_android.h"
17 #include "ui/android/window_android.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/gfx/geometry/rect.h"
22 namespace autofill {
24 namespace {
26 void AddToJavaArray(const Suggestion& suggestion,
27 const AutofillPopupController& controller,
28 JNIEnv* env,
29 jobjectArray data_array,
30 size_t position,
31 bool deletable) {
32 int android_icon_id = 0;
33 if (!suggestion.icon.empty()) {
34 android_icon_id = ResourceMapper::MapFromChromiumId(
35 controller.GetIconResourceID(suggestion.icon));
38 Java_AutofillKeyboardAccessoryBridge_addToAutofillSuggestionArray(
39 env, data_array, position,
40 base::android::ConvertUTF16ToJavaString(env, suggestion.value).obj(),
41 base::android::ConvertUTF16ToJavaString(env, suggestion.label).obj(),
42 android_icon_id, suggestion.frontend_id, deletable);
45 } // namespace
47 AutofillKeyboardAccessoryView::AutofillKeyboardAccessoryView(
48 AutofillPopupController* controller)
49 : controller_(controller),
50 deleting_index_(-1) {
51 JNIEnv* env = base::android::AttachCurrentThread();
52 java_object_.Reset(Java_AutofillKeyboardAccessoryBridge_create(env));
55 AutofillKeyboardAccessoryView::~AutofillKeyboardAccessoryView() {
56 JNIEnv* env = base::android::AttachCurrentThread();
57 Java_AutofillKeyboardAccessoryBridge_resetNativeViewPointer(
58 env, java_object_.obj());
61 void AutofillKeyboardAccessoryView::Show() {
62 JNIEnv* env = base::android::AttachCurrentThread();
63 ui::ViewAndroid* view_android = controller_->container_view();
64 DCHECK(view_android);
65 Java_AutofillKeyboardAccessoryBridge_init(
66 env, java_object_.obj(),
67 reinterpret_cast<intptr_t>(this),
68 view_android->GetWindowAndroid()->GetJavaObject().obj());
70 UpdateBoundsAndRedrawPopup();
73 void AutofillKeyboardAccessoryView::Hide() {
74 controller_ = nullptr;
75 JNIEnv* env = base::android::AttachCurrentThread();
76 Java_AutofillKeyboardAccessoryBridge_dismiss(env, java_object_.obj());
79 void AutofillKeyboardAccessoryView::UpdateBoundsAndRedrawPopup() {
80 JNIEnv* env = base::android::AttachCurrentThread();
81 size_t count = controller_->GetLineCount();
82 ScopedJavaLocalRef<jobjectArray> data_array =
83 Java_AutofillKeyboardAccessoryBridge_createAutofillSuggestionArray(env,
84 count);
85 positions_.resize(count);
86 size_t position = 0;
88 // Place "CLEAR FORM" item first in the list.
89 for (size_t i = 0; i < count; ++i) {
90 const Suggestion& suggestion = controller_->GetSuggestionAt(i);
91 if (suggestion.frontend_id == POPUP_ITEM_ID_CLEAR_FORM) {
92 AddToJavaArray(suggestion, *controller_, env, data_array.obj(), position,
93 false);
94 positions_[position++] = i;
98 for (size_t i = 0; i < count; ++i) {
99 const Suggestion& suggestion = controller_->GetSuggestionAt(i);
100 if (suggestion.frontend_id != POPUP_ITEM_ID_CLEAR_FORM) {
101 bool deletable =
102 controller_->GetRemovalConfirmationText(i, nullptr, nullptr);
103 AddToJavaArray(suggestion, *controller_, env, data_array.obj(), position,
104 deletable);
105 positions_[position++] = i;
109 Java_AutofillKeyboardAccessoryBridge_show(
110 env, java_object_.obj(), data_array.obj(), controller_->IsRTL());
113 void AutofillKeyboardAccessoryView::SuggestionSelected(JNIEnv* env,
114 jobject obj,
115 jint list_index) {
116 // Race: Hide() may have already run.
117 if (controller_)
118 controller_->AcceptSuggestion(positions_[list_index]);
121 void AutofillKeyboardAccessoryView::DeletionRequested(JNIEnv* env,
122 jobject obj,
123 jint list_index) {
124 if (!controller_)
125 return;
127 base::string16 confirmation_title, confirmation_body;
128 if (!controller_->GetRemovalConfirmationText(
129 positions_[list_index], &confirmation_title, &confirmation_body)) {
130 return;
133 deleting_index_ = positions_[list_index];
134 Java_AutofillKeyboardAccessoryBridge_confirmDeletion(
135 env, java_object_.obj(),
136 base::android::ConvertUTF16ToJavaString(env, confirmation_title).obj(),
137 base::android::ConvertUTF16ToJavaString(env, confirmation_body).obj());
140 void AutofillKeyboardAccessoryView::DeletionConfirmed(JNIEnv* env,
141 jobject obj) {
142 if (!controller_)
143 return;
145 CHECK_GE(deleting_index_, 0);
146 controller_->RemoveSuggestion(deleting_index_);
149 void AutofillKeyboardAccessoryView::ViewDismissed(JNIEnv* env, jobject obj) {
150 if (controller_)
151 controller_->ViewDestroyed();
153 delete this;
156 void AutofillKeyboardAccessoryView::InvalidateRow(size_t) {
159 // static
160 bool AutofillKeyboardAccessoryView::RegisterAutofillKeyboardAccessoryView(
161 JNIEnv* env) {
162 return RegisterNativesImpl(env);
165 } // namespace autofill