OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/spellchecker/spellchecker_session_bridge_android.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <utility> | |
9 | |
10 #include "base/android/jni_array.h" | |
11 #include "base/android/jni_string.h" | |
12 #include "components/spellcheck/common/spellcheck_messages.h" | |
13 #include "components/spellcheck/common/spellcheck_result.h" | |
14 #include "content/public/browser/render_process_host.h" | |
15 #include "jni/SpellCheckerSessionBridge_jni.h" | |
16 | |
17 using base::android::JavaParamRef; | |
18 | |
19 SpellCheckerSessionBridge::SpellCheckerSessionBridge(int render_process_id) | |
20 : render_process_id_(render_process_id) {} | |
21 | |
22 SpellCheckerSessionBridge::~SpellCheckerSessionBridge() {} | |
23 | |
24 // static | |
25 bool SpellCheckerSessionBridge::RegisterJNI(JNIEnv* env) { | |
26 return RegisterNativesImpl(env); | |
27 } | |
28 | |
29 void SpellCheckerSessionBridge::RequestTextCheck(int route_id, | |
30 int identifier, | |
31 const base::string16& text) { | |
32 // SpellCheckerSessionBridge#create() will return null if spell checker | |
33 // service is unavailable. | |
34 if (java_object_initialization_failed_) | |
35 return; | |
36 | |
37 // RequestTextCheck IPC arrives at the message filter before | |
38 // ToggleSpellCheck IPC when the user focuses an input field that already | |
39 // contains completed text. We need to initialize the spellchecker here | |
40 // rather than in response to ToggleSpellCheck so that the existing text | |
41 // will be spellchecked immediately. | |
42 if (java_object_.is_null()) { | |
43 java_object_.Reset(Java_SpellCheckerSessionBridge_create( | |
44 base::android::AttachCurrentThread(), | |
45 reinterpret_cast<intptr_t>(this))); | |
46 if (java_object_.is_null()) { | |
47 java_object_initialization_failed_ = true; | |
48 return; | |
49 } | |
50 } | |
51 | |
52 // Save incoming requests to run at the end of the currently active request. | |
53 // If multiple requests arrive during one active request, only the most | |
54 // recent request will run (the others get overwritten). | |
55 if (active_request_) { | |
56 pending_request_.reset(new SpellingRequest(route_id, identifier, text)); | |
57 return; | |
58 } | |
59 | |
60 active_request_.reset(new SpellingRequest(route_id, identifier, text)); | |
61 | |
62 JNIEnv* env = base::android::AttachCurrentThread(); | |
63 Java_SpellCheckerSessionBridge_requestTextCheck( | |
64 env, java_object_, base::android::ConvertUTF16ToJavaString(env, text)); | |
65 } | |
66 | |
67 void SpellCheckerSessionBridge::ProcessSpellCheckResults( | |
68 JNIEnv* env, | |
69 const JavaParamRef<jobject>& jobj, | |
70 const JavaParamRef<jintArray>& offset_array, | |
71 const JavaParamRef<jintArray>& length_array) { | |
72 std::vector<int> offsets; | |
73 std::vector<int> lengths; | |
74 | |
75 base::android::JavaIntArrayToIntVector(env, offset_array, &offsets); | |
76 base::android::JavaIntArrayToIntVector(env, length_array, &lengths); | |
77 | |
78 std::vector<SpellCheckResult> results; | |
79 for (size_t i = 0; i < offsets.size(); i++) { | |
80 results.push_back( | |
81 SpellCheckResult(SpellCheckResult::SPELLING, offsets[i], lengths[i])); | |
82 } | |
83 | |
84 content::RenderProcessHost* sender = | |
85 content::RenderProcessHost::FromID(render_process_id_); | |
86 | |
87 if (sender != nullptr) { | |
88 sender->Send(new SpellCheckMsg_RespondTextCheck( | |
89 active_request_->route_id, active_request_->identifier, | |
90 active_request_->text, results)); | |
91 } | |
92 | |
93 active_request_ = std::move(pending_request_); | |
94 if (active_request_) { | |
95 JNIEnv* env = base::android::AttachCurrentThread(); | |
96 Java_SpellCheckerSessionBridge_requestTextCheck( | |
97 env, java_object_, | |
98 base::android::ConvertUTF16ToJavaString(env, active_request_->text)); | |
99 } | |
100 } | |
101 | |
102 void SpellCheckerSessionBridge::DisconnectSession() { | |
103 java_object_.Reset(); | |
104 } | |
105 | |
106 SpellCheckerSessionBridge::SpellingRequest::SpellingRequest( | |
107 int route_id, | |
108 int identifier, | |
109 const base::string16& text) | |
110 : route_id(route_id), identifier(identifier), text(text) {} | |
111 | |
112 SpellCheckerSessionBridge::SpellingRequest::~SpellingRequest() {} | |
OLD | NEW |