| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/spellcheck/browser/spellchecker_session_bridge_android.h" | 5 #include "components/spellcheck/browser/spellchecker_session_bridge_android.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "components/spellcheck/common/spellcheck_messages.h" | 12 #include "components/spellcheck/common/spellcheck_messages.h" |
| 13 #include "components/spellcheck/common/spellcheck_result.h" | 13 #include "components/spellcheck/common/spellcheck_result.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
| 15 #include "jni/SpellCheckerSessionBridge_jni.h" | 16 #include "jni/SpellCheckerSessionBridge_jni.h" |
| 16 | 17 |
| 17 using base::android::JavaParamRef; | 18 using base::android::JavaParamRef; |
| 18 | 19 |
| 19 SpellCheckerSessionBridge::SpellCheckerSessionBridge(int render_process_id) | 20 SpellCheckerSessionBridge::SpellCheckerSessionBridge(int render_process_id) |
| 20 : render_process_id_(render_process_id), | 21 : render_process_id_(render_process_id), |
| 21 java_object_initialization_failed_(false) {} | 22 java_object_initialization_failed_(false) {} |
| 22 | 23 |
| 23 SpellCheckerSessionBridge::~SpellCheckerSessionBridge() {} | 24 SpellCheckerSessionBridge::~SpellCheckerSessionBridge() { |
| 25 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 26 // Clean-up java side to avoid any stale JNI callbacks. |
| 27 DisconnectSession(); |
| 28 } |
| 24 | 29 |
| 25 // static | 30 // static |
| 26 bool SpellCheckerSessionBridge::RegisterJNI(JNIEnv* env) { | 31 bool SpellCheckerSessionBridge::RegisterJNI(JNIEnv* env) { |
| 27 return RegisterNativesImpl(env); | 32 return RegisterNativesImpl(env); |
| 28 } | 33 } |
| 29 | 34 |
| 30 void SpellCheckerSessionBridge::RequestTextCheck(int route_id, | 35 void SpellCheckerSessionBridge::RequestTextCheck(int route_id, |
| 31 int identifier, | 36 int identifier, |
| 32 const base::string16& text) { | 37 const base::string16& text) { |
| 38 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 33 // SpellCheckerSessionBridge#create() will return null if spell checker | 39 // SpellCheckerSessionBridge#create() will return null if spell checker |
| 34 // service is unavailable. | 40 // service is unavailable. |
| 35 if (java_object_initialization_failed_) | 41 if (java_object_initialization_failed_) |
| 36 return; | 42 return; |
| 37 | 43 |
| 38 // RequestTextCheck IPC arrives at the message filter before | 44 // RequestTextCheck IPC arrives at the message filter before |
| 39 // ToggleSpellCheck IPC when the user focuses an input field that already | 45 // ToggleSpellCheck IPC when the user focuses an input field that already |
| 40 // contains completed text. We need to initialize the spellchecker here | 46 // contains completed text. We need to initialize the spellchecker here |
| 41 // rather than in response to ToggleSpellCheck so that the existing text | 47 // rather than in response to ToggleSpellCheck so that the existing text |
| 42 // will be spellchecked immediately. | 48 // will be spellchecked immediately. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 63 JNIEnv* env = base::android::AttachCurrentThread(); | 69 JNIEnv* env = base::android::AttachCurrentThread(); |
| 64 Java_SpellCheckerSessionBridge_requestTextCheck( | 70 Java_SpellCheckerSessionBridge_requestTextCheck( |
| 65 env, java_object_, base::android::ConvertUTF16ToJavaString(env, text)); | 71 env, java_object_, base::android::ConvertUTF16ToJavaString(env, text)); |
| 66 } | 72 } |
| 67 | 73 |
| 68 void SpellCheckerSessionBridge::ProcessSpellCheckResults( | 74 void SpellCheckerSessionBridge::ProcessSpellCheckResults( |
| 69 JNIEnv* env, | 75 JNIEnv* env, |
| 70 const JavaParamRef<jobject>& jobj, | 76 const JavaParamRef<jobject>& jobj, |
| 71 const JavaParamRef<jintArray>& offset_array, | 77 const JavaParamRef<jintArray>& offset_array, |
| 72 const JavaParamRef<jintArray>& length_array) { | 78 const JavaParamRef<jintArray>& length_array) { |
| 79 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 73 std::vector<int> offsets; | 80 std::vector<int> offsets; |
| 74 std::vector<int> lengths; | 81 std::vector<int> lengths; |
| 75 | 82 |
| 76 base::android::JavaIntArrayToIntVector(env, offset_array, &offsets); | 83 base::android::JavaIntArrayToIntVector(env, offset_array, &offsets); |
| 77 base::android::JavaIntArrayToIntVector(env, length_array, &lengths); | 84 base::android::JavaIntArrayToIntVector(env, length_array, &lengths); |
| 78 | 85 |
| 79 std::vector<SpellCheckResult> results; | 86 std::vector<SpellCheckResult> results; |
| 80 for (size_t i = 0; i < offsets.size(); i++) { | 87 for (size_t i = 0; i < offsets.size(); i++) { |
| 81 results.push_back( | 88 results.push_back( |
| 82 SpellCheckResult(SpellCheckResult::SPELLING, offsets[i], lengths[i])); | 89 SpellCheckResult(SpellCheckResult::SPELLING, offsets[i], lengths[i])); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 94 active_request_ = std::move(pending_request_); | 101 active_request_ = std::move(pending_request_); |
| 95 if (active_request_) { | 102 if (active_request_) { |
| 96 JNIEnv* env = base::android::AttachCurrentThread(); | 103 JNIEnv* env = base::android::AttachCurrentThread(); |
| 97 Java_SpellCheckerSessionBridge_requestTextCheck( | 104 Java_SpellCheckerSessionBridge_requestTextCheck( |
| 98 env, java_object_, | 105 env, java_object_, |
| 99 base::android::ConvertUTF16ToJavaString(env, active_request_->text)); | 106 base::android::ConvertUTF16ToJavaString(env, active_request_->text)); |
| 100 } | 107 } |
| 101 } | 108 } |
| 102 | 109 |
| 103 void SpellCheckerSessionBridge::DisconnectSession() { | 110 void SpellCheckerSessionBridge::DisconnectSession() { |
| 104 java_object_.Reset(); | 111 // Needs to be executed on the same thread as the RequestTextCheck and |
| 112 // ProcessSpellCheckResults methods, which is the UI thread. |
| 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 114 |
| 115 active_request_.reset(); |
| 116 pending_request_.reset(); |
| 117 |
| 118 if (!java_object_.is_null()) { |
| 119 Java_SpellCheckerSessionBridge_disconnect( |
| 120 base::android::AttachCurrentThread(), java_object_); |
| 121 java_object_.Reset(); |
| 122 } |
| 105 } | 123 } |
| 106 | 124 |
| 107 SpellCheckerSessionBridge::SpellingRequest::SpellingRequest( | 125 SpellCheckerSessionBridge::SpellingRequest::SpellingRequest( |
| 108 int route_id, | 126 int route_id, |
| 109 int identifier, | 127 int identifier, |
| 110 const base::string16& text) | 128 const base::string16& text) |
| 111 : route_id(route_id), identifier(identifier), text(text) {} | 129 : route_id(route_id), identifier(identifier), text(text) {} |
| 112 | 130 |
| 113 SpellCheckerSessionBridge::SpellingRequest::~SpellingRequest() {} | 131 SpellCheckerSessionBridge::SpellingRequest::~SpellingRequest() {} |
| OLD | NEW |