Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / spellchecker / spellchecker_session_bridge_android.cc
blobcf02f8c9b917f00cd5363b36f4c86bb0f88ec482
1 // Copyright 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/spellchecker/spellchecker_session_bridge_android.h"
7 #include "base/android/jni_array.h"
8 #include "base/android/jni_string.h"
9 #include "chrome/common/spellcheck_messages.h"
10 #include "chrome/common/spellcheck_result.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "jni/SpellCheckerSessionBridge_jni.h"
14 SpellCheckerSessionBridge::SpellCheckerSessionBridge(int render_process_id)
15 : render_process_id_(render_process_id) {}
17 SpellCheckerSessionBridge::~SpellCheckerSessionBridge() {}
19 // static
20 bool SpellCheckerSessionBridge::RegisterJNI(JNIEnv* env) {
21 return RegisterNativesImpl(env);
24 void SpellCheckerSessionBridge::RequestTextCheck(int route_id,
25 int identifier,
26 const base::string16& text) {
27 // SpellCheckerSessionBridge#create() will return null if spell checker
28 // service is unavailable.
29 if (java_object_initialization_failed_)
30 return;
32 // RequestTextCheck IPC arrives at the message filter before
33 // ToggleSpellCheck IPC when the user focuses an input field that already
34 // contains completed text. We need to initialize the spellchecker here
35 // rather than in response to ToggleSpellCheck so that the existing text
36 // will be spellchecked immediately.
37 if (java_object_.is_null()) {
38 java_object_.Reset(Java_SpellCheckerSessionBridge_create(
39 base::android::AttachCurrentThread(),
40 reinterpret_cast<intptr_t>(this)));
41 if (java_object_.is_null()) {
42 java_object_initialization_failed_ = true;
43 return;
47 // Save incoming requests to run at the end of the currently active request.
48 // If multiple requests arrive during one active request, only the most
49 // recent request will run (the others get overwritten).
50 if (active_request_) {
51 pending_request_.reset(new SpellingRequest(route_id, identifier, text));
52 return;
55 active_request_.reset(new SpellingRequest(route_id, identifier, text));
57 JNIEnv* env = base::android::AttachCurrentThread();
58 Java_SpellCheckerSessionBridge_requestTextCheck(
59 env, java_object_.obj(),
60 base::android::ConvertUTF16ToJavaString(env, text).obj());
63 void SpellCheckerSessionBridge::ProcessSpellCheckResults(
64 JNIEnv* env,
65 jobject jobj,
66 jintArray offset_array,
67 jintArray length_array) {
68 std::vector<int> offsets;
69 std::vector<int> lengths;
71 base::android::JavaIntArrayToIntVector(env, offset_array, &offsets);
72 base::android::JavaIntArrayToIntVector(env, length_array, &lengths);
74 std::vector<SpellCheckResult> results;
75 for (size_t i = 0; i < offsets.size(); i++) {
76 results.push_back(
77 SpellCheckResult(SpellCheckResult::SPELLING, offsets[i], lengths[i]));
80 content::RenderProcessHost* sender =
81 content::RenderProcessHost::FromID(render_process_id_);
83 if (sender != nullptr) {
84 sender->Send(new SpellCheckMsg_RespondTextCheck(
85 active_request_->route_id, active_request_->identifier,
86 active_request_->text, results));
89 active_request_ = pending_request_.Pass();
90 if (active_request_) {
91 JNIEnv* env = base::android::AttachCurrentThread();
92 Java_SpellCheckerSessionBridge_requestTextCheck(
93 env, java_object_.obj(),
94 base::android::ConvertUTF16ToJavaString(env, active_request_->text)
95 .obj());
99 void SpellCheckerSessionBridge::DisconnectSession() {
100 java_object_.Reset();
103 SpellCheckerSessionBridge::SpellingRequest::SpellingRequest(
104 int route_id,
105 int identifier,
106 const base::string16& text)
107 : route_id(route_id), identifier(identifier), text(text) {}
109 SpellCheckerSessionBridge::SpellingRequest::~SpellingRequest() {}