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
),
16 java_object_(Java_SpellCheckerSessionBridge_create(
17 base::android::AttachCurrentThread(),
18 reinterpret_cast<intptr_t>(this))) {}
20 SpellCheckerSessionBridge::~SpellCheckerSessionBridge() {}
23 bool SpellCheckerSessionBridge::RegisterJNI(JNIEnv
* env
) {
24 return RegisterNativesImpl(env
);
27 void SpellCheckerSessionBridge::RequestTextCheck(int route_id
,
29 const base::string16
& text
) {
30 if (java_object_
.is_null())
33 // Save incoming requests to run at the end of the currently active request.
34 // If multiple requests arrive during one active request, only the most
35 // recent request will run (the others get overwritten).
36 if (active_request_
) {
37 pending_request_
.reset(new SpellingRequest(route_id
, identifier
, text
));
41 active_request_
.reset(new SpellingRequest(route_id
, identifier
, text
));
43 JNIEnv
* env
= base::android::AttachCurrentThread();
44 Java_SpellCheckerSessionBridge_requestTextCheck(
45 env
, java_object_
.obj(),
46 base::android::ConvertUTF16ToJavaString(env
, text
).obj());
49 void SpellCheckerSessionBridge::ProcessSpellCheckResults(
52 jintArray offset_array
,
53 jintArray length_array
) {
54 std::vector
<int> offsets
;
55 std::vector
<int> lengths
;
57 base::android::JavaIntArrayToIntVector(env
, offset_array
, &offsets
);
58 base::android::JavaIntArrayToIntVector(env
, length_array
, &lengths
);
60 std::vector
<SpellCheckResult
> results
;
61 for (size_t i
= 0; i
< offsets
.size(); i
++) {
63 SpellCheckResult(SpellCheckResult::SPELLING
, offsets
[i
], lengths
[i
]));
66 content::RenderProcessHost
* sender
=
67 content::RenderProcessHost::FromID(render_process_id_
);
69 if (sender
!= nullptr) {
70 sender
->Send(new SpellCheckMsg_RespondTextCheck(
71 active_request_
->route_id
, active_request_
->identifier
,
72 active_request_
->text
, results
));
75 active_request_
= pending_request_
.Pass();
76 if (active_request_
) {
77 JNIEnv
* env
= base::android::AttachCurrentThread();
78 Java_SpellCheckerSessionBridge_requestTextCheck(
79 env
, java_object_
.obj(),
80 base::android::ConvertUTF16ToJavaString(env
, active_request_
->text
)
85 SpellCheckerSessionBridge::SpellingRequest::SpellingRequest(
88 const base::string16
& text
)
89 : route_id(route_id
), identifier(identifier
), text(text
) {}
91 SpellCheckerSessionBridge::SpellingRequest::~SpellingRequest() {}