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