Index: chrome/browser/spellchecker/spellchecker_session_bridge_android.cc |
diff --git a/chrome/browser/spellchecker/spellchecker_session_bridge_android.cc b/chrome/browser/spellchecker/spellchecker_session_bridge_android.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9b385ba729ac94d76436c088efcff44f282e4e60 |
--- /dev/null |
+++ b/chrome/browser/spellchecker/spellchecker_session_bridge_android.cc |
@@ -0,0 +1,95 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/spellchecker/spellchecker_session_bridge_android.h" |
+ |
+#include "chrome/common/spellcheck_messages.h" |
+#include "chrome/common/spellcheck_result.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "jni/SpellCheckerSessionBridge_jni.h" |
+ |
+SpellCheckerSessionBridge::SpellingRequest::SpellingRequest( |
+ int new_route_id, |
+ int new_identifier, |
+ const base::string16& new_text) |
+ : route_id(new_route_id), identifier(new_identifier), text(new_text) {} |
+ |
+SpellCheckerSessionBridge::SpellCheckerSessionBridge(int render_process_id) |
+ : render_process_id_(render_process_id), |
+ is_active_request_(false), |
+ is_pending_request_(false), |
+ java_object_(Java_SpellCheckerSessionBridge_create( |
+ base::android::AttachCurrentThread(), |
+ reinterpret_cast<intptr_t>(this))) {} |
+ |
+// static |
+bool SpellCheckerSessionBridge::RegisterJNI(JNIEnv* env) { |
+ return RegisterNativesImpl(env); |
+} |
+ |
+void SpellCheckerSessionBridge::RequestTextCheck(int route_id, |
+ int identifier, |
+ const base::string16& text) { |
+ // Save incoming requests to run at the end of the currently active request. |
+ // If multiple requests arrive during one active request, only the most |
+ // recent request will run (the others get overwritten). |
+ if (is_active_request_) { |
+ pending_spelling_request_ = SpellingRequest(route_id, identifier, text); |
+ is_pending_request_ = true; |
+ return; |
+ } |
+ |
+ active_spelling_request_ = SpellingRequest(route_id, identifier, text); |
+ is_active_request_ = true; |
+ |
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ Java_SpellCheckerSessionBridge_requestTextCheck( |
+ env, java_object_.obj(), |
+ base::android::ConvertUTF16ToJavaString(env, text).obj()); |
+} |
+ |
+void SpellCheckerSessionBridge::ProcessSpellCheckResults( |
+ JNIEnv* env, |
+ jobject jobj, |
+ jintArray offset_array, |
+ jintArray length_array) { |
+ std::vector<int> offsets; |
+ std::vector<int> lengths; |
+ |
+ base::android::JavaIntArrayToIntVector(env, offset_array, &offsets); |
+ base::android::JavaIntArrayToIntVector(env, length_array, &lengths); |
+ |
+ // Convert specific offset/length pairs into SpellCheckResults, which can be |
+ // sent to the renderer to be drawn as red underlines. |
+ std::vector<SpellCheckResult> results; |
+ for (size_t i = 0; i < offsets.size(); i++) { |
+ results.push_back( |
+ SpellCheckResult(SpellCheckResult::SPELLING, offsets[i], lengths[i])); |
+ } |
+ |
+ content::RenderProcessHost* sender = |
+ content::RenderProcessHost::FromID(render_process_id_); |
+ |
+ if (sender != nullptr) { |
+ sender->Send(new SpellCheckMsg_RespondTextCheck( |
+ active_spelling_request_.route_id, active_spelling_request_.identifier, |
+ active_spelling_request_.text, results)); |
+ } |
+ |
+ // If any pending requests arrived during the execution of this active |
+ // request, the most recently arrived pending request will become the active |
+ // request and be run. |
+ if (is_pending_request_) { |
newt (away)
2015/08/13 20:40:31
slightly simpler way to write this:
active_requ
dylanking
2015/08/14 02:57:03
Will definitely change to this if I end up using s
|
+ active_spelling_request_ = pending_spelling_request_; |
+ is_pending_request_ = false; |
+ |
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ Java_SpellCheckerSessionBridge_requestTextCheck( |
+ env, java_object_.obj(), base::android::ConvertUTF16ToJavaString( |
newt (away)
2015/08/13 20:40:31
weird indentation
dylanking
2015/08/14 02:57:03
Fixed, but I have a feeling this was git cl format
|
+ env, active_spelling_request_.text) |
+ .obj()); |
+ } else { |
+ is_active_request_ = false; |
+ } |
+} |