Chromium Code Reviews| 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) { |
| 33 // SpellCheckerSessionBridge#create() will return null if spell checker | 38 // SpellCheckerSessionBridge#create() will return null if spell checker |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 58 return; | 63 return; |
| 59 } | 64 } |
| 60 | 65 |
| 61 active_request_.reset(new SpellingRequest(route_id, identifier, text)); | 66 active_request_.reset(new SpellingRequest(route_id, identifier, text)); |
| 62 | 67 |
| 63 JNIEnv* env = base::android::AttachCurrentThread(); | 68 JNIEnv* env = base::android::AttachCurrentThread(); |
| 64 Java_SpellCheckerSessionBridge_requestTextCheck( | 69 Java_SpellCheckerSessionBridge_requestTextCheck( |
| 65 env, java_object_, base::android::ConvertUTF16ToJavaString(env, text)); | 70 env, java_object_, base::android::ConvertUTF16ToJavaString(env, text)); |
| 66 } | 71 } |
| 67 | 72 |
| 68 void SpellCheckerSessionBridge::ProcessSpellCheckResults( | 73 void SpellCheckerSessionBridge::ProcessSpellCheckResults( |
|
Tobias Sargeant
2016/09/30 14:13:21
DCHECK that this occurs on the UI thread, to docum
timvolodine
2016/09/30 14:46:00
Done.
| |
| 69 JNIEnv* env, | 74 JNIEnv* env, |
| 70 const JavaParamRef<jobject>& jobj, | 75 const JavaParamRef<jobject>& jobj, |
| 71 const JavaParamRef<jintArray>& offset_array, | 76 const JavaParamRef<jintArray>& offset_array, |
| 72 const JavaParamRef<jintArray>& length_array) { | 77 const JavaParamRef<jintArray>& length_array) { |
| 73 std::vector<int> offsets; | 78 std::vector<int> offsets; |
| 74 std::vector<int> lengths; | 79 std::vector<int> lengths; |
| 75 | 80 |
| 76 base::android::JavaIntArrayToIntVector(env, offset_array, &offsets); | 81 base::android::JavaIntArrayToIntVector(env, offset_array, &offsets); |
| 77 base::android::JavaIntArrayToIntVector(env, length_array, &lengths); | 82 base::android::JavaIntArrayToIntVector(env, length_array, &lengths); |
| 78 | 83 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 93 | 98 |
| 94 active_request_ = std::move(pending_request_); | 99 active_request_ = std::move(pending_request_); |
| 95 if (active_request_) { | 100 if (active_request_) { |
| 96 JNIEnv* env = base::android::AttachCurrentThread(); | 101 JNIEnv* env = base::android::AttachCurrentThread(); |
| 97 Java_SpellCheckerSessionBridge_requestTextCheck( | 102 Java_SpellCheckerSessionBridge_requestTextCheck( |
| 98 env, java_object_, | 103 env, java_object_, |
| 99 base::android::ConvertUTF16ToJavaString(env, active_request_->text)); | 104 base::android::ConvertUTF16ToJavaString(env, active_request_->text)); |
| 100 } | 105 } |
| 101 } | 106 } |
| 102 | 107 |
| 103 void SpellCheckerSessionBridge::DisconnectSession() { | 108 void SpellCheckerSessionBridge::DisconnectSession() { |
|
Tobias Sargeant
2016/09/30 14:13:21
If there's an active request at this point that we
timvolodine
2016/09/30 14:46:00
Don't think we need to send an (empty?) IPC in the
| |
| 104 java_object_.Reset(); | 109 // Needs to be executed on the same thread as the RequestTextCheck and |
| 110 // ProcessSpellCheckResults methods, which is the UI thread. | |
| 111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 112 | |
| 113 if (!java_object_.is_null()) { | |
| 114 Java_SpellCheckerSessionBridge_disconnect( | |
| 115 base::android::AttachCurrentThread(), java_object_); | |
| 116 java_object_.Reset(); | |
| 117 } | |
| 105 } | 118 } |
| 106 | 119 |
| 107 SpellCheckerSessionBridge::SpellingRequest::SpellingRequest( | 120 SpellCheckerSessionBridge::SpellingRequest::SpellingRequest( |
| 108 int route_id, | 121 int route_id, |
| 109 int identifier, | 122 int identifier, |
| 110 const base::string16& text) | 123 const base::string16& text) |
| 111 : route_id(route_id), identifier(identifier), text(text) {} | 124 : route_id(route_id), identifier(identifier), text(text) {} |
| 112 | 125 |
| 113 SpellCheckerSessionBridge::SpellingRequest::~SpellingRequest() {} | 126 SpellCheckerSessionBridge::SpellingRequest::~SpellingRequest() {} |
| OLD | NEW |