| 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_.obj(), | |
| 65 base::android::ConvertUTF16ToJavaString(env, text).obj()); | |
| 66 } | |
| 67 | |
| 68 void SpellCheckerSessionBridge::ProcessSpellCheckResults( | |
| 69 JNIEnv* env, | |
| 70 const JavaParamRef<jobject>& jobj, | |
| 71 const JavaParamRef<jintArray>& offset_array, | |
| 72 const JavaParamRef<jintArray>& length_array) { | |
| 73 std::vector<int> offsets; | |
| 74 std::vector<int> lengths; | |
| 75 | |
| 76 base::android::JavaIntArrayToIntVector(env, offset_array, &offsets); | |
| 77 base::android::JavaIntArrayToIntVector(env, length_array, &lengths); | |
| 78 | |
| 79 std::vector<SpellCheckResult> results; | |
| 80 for (size_t i = 0; i < offsets.size(); i++) { | |
| 81 results.push_back( | |
| 82 SpellCheckResult(SpellCheckResult::SPELLING, offsets[i], lengths[i])); | |
| 83 } | |
| 84 | |
| 85 content::RenderProcessHost* sender = | |
| 86 content::RenderProcessHost::FromID(render_process_id_); | |
| 87 | |
| 88 if (sender != nullptr) { | |
| 89 sender->Send(new SpellCheckMsg_RespondTextCheck( | |
| 90 active_request_->route_id, active_request_->identifier, | |
| 91 active_request_->text, results)); | |
| 92 } | |
| 93 | |
| 94 active_request_ = std::move(pending_request_); | |
| 95 if (active_request_) { | |
| 96 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 97 Java_SpellCheckerSessionBridge_requestTextCheck( | |
| 98 env, java_object_.obj(), | |
| 99 base::android::ConvertUTF16ToJavaString(env, active_request_->text) | |
| 100 .obj()); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 void SpellCheckerSessionBridge::DisconnectSession() { | |
| 105 java_object_.Reset(); | |
| 106 } | |
| 107 | |
| 108 SpellCheckerSessionBridge::SpellingRequest::SpellingRequest( | |
| 109 int route_id, | |
| 110 int identifier, | |
| 111 const base::string16& text) | |
| 112 : route_id(route_id), identifier(identifier), text(text) {} | |
| 113 | |
| 114 SpellCheckerSessionBridge::SpellingRequest::~SpellingRequest() {} | |
| OLD | NEW |