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 "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" |
| 13 |
| 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))) {} |
| 19 |
| 20 SpellCheckerSessionBridge::~SpellCheckerSessionBridge() {} |
| 21 |
| 22 // static |
| 23 bool SpellCheckerSessionBridge::RegisterJNI(JNIEnv* env) { |
| 24 return RegisterNativesImpl(env); |
| 25 } |
| 26 |
| 27 void SpellCheckerSessionBridge::RequestTextCheck(int route_id, |
| 28 int identifier, |
| 29 const base::string16& text) { |
| 30 if (java_object_.is_null()) |
| 31 return; |
| 32 |
| 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)); |
| 38 return; |
| 39 } |
| 40 |
| 41 active_request_.reset(new SpellingRequest(route_id, identifier, text)); |
| 42 |
| 43 JNIEnv* env = base::android::AttachCurrentThread(); |
| 44 Java_SpellCheckerSessionBridge_requestTextCheck( |
| 45 env, java_object_.obj(), |
| 46 base::android::ConvertUTF16ToJavaString(env, text).obj()); |
| 47 } |
| 48 |
| 49 void SpellCheckerSessionBridge::ProcessSpellCheckResults( |
| 50 JNIEnv* env, |
| 51 jobject jobj, |
| 52 jintArray offset_array, |
| 53 jintArray length_array) { |
| 54 std::vector<int> offsets; |
| 55 std::vector<int> lengths; |
| 56 |
| 57 base::android::JavaIntArrayToIntVector(env, offset_array, &offsets); |
| 58 base::android::JavaIntArrayToIntVector(env, length_array, &lengths); |
| 59 |
| 60 std::vector<SpellCheckResult> results; |
| 61 for (size_t i = 0; i < offsets.size(); i++) { |
| 62 results.push_back( |
| 63 SpellCheckResult(SpellCheckResult::SPELLING, offsets[i], lengths[i])); |
| 64 } |
| 65 |
| 66 content::RenderProcessHost* sender = |
| 67 content::RenderProcessHost::FromID(render_process_id_); |
| 68 |
| 69 if (sender != nullptr) { |
| 70 sender->Send(new SpellCheckMsg_RespondTextCheck( |
| 71 active_request_->route_id, active_request_->identifier, |
| 72 active_request_->text, results)); |
| 73 } |
| 74 |
| 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) |
| 81 .obj()); |
| 82 } |
| 83 } |
| 84 |
| 85 SpellCheckerSessionBridge::SpellingRequest::SpellingRequest( |
| 86 int route_id, |
| 87 int identifier, |
| 88 const base::string16& text) |
| 89 : route_id(route_id), identifier(identifier), text(text) {} |
| 90 |
| 91 SpellCheckerSessionBridge::SpellingRequest::~SpellingRequest() {} |
OLD | NEW |