| 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 "base/metrics/histogram_macros.h" |
| 12 #include "components/spellcheck/common/spellcheck_messages.h" | 13 #include "components/spellcheck/common/spellcheck_messages.h" |
| 13 #include "components/spellcheck/common/spellcheck_result.h" | 14 #include "components/spellcheck/common/spellcheck_result.h" |
| 14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/render_process_host.h" | 16 #include "content/public/browser/render_process_host.h" |
| 16 #include "jni/SpellCheckerSessionBridge_jni.h" | 17 #include "jni/SpellCheckerSessionBridge_jni.h" |
| 17 | 18 |
| 18 using base::android::JavaParamRef; | 19 using base::android::JavaParamRef; |
| 19 | 20 |
| 21 namespace { |
| 22 |
| 23 void RecordAvailabilityUMA(bool spellcheck_available) { |
| 24 UMA_HISTOGRAM_BOOLEAN("Spellcheck.Android.Available", spellcheck_available); |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 20 SpellCheckerSessionBridge::SpellCheckerSessionBridge(int render_process_id) | 29 SpellCheckerSessionBridge::SpellCheckerSessionBridge(int render_process_id) |
| 21 : render_process_id_(render_process_id), | 30 : render_process_id_(render_process_id), |
| 22 java_object_initialization_failed_(false) {} | 31 java_object_initialization_failed_(false), |
| 32 active_session_(false) {} |
| 23 | 33 |
| 24 SpellCheckerSessionBridge::~SpellCheckerSessionBridge() { | 34 SpellCheckerSessionBridge::~SpellCheckerSessionBridge() { |
| 25 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 35 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 26 // Clean-up java side to avoid any stale JNI callbacks. | 36 // Clean-up java side to avoid any stale JNI callbacks. |
| 27 DisconnectSession(); | 37 DisconnectSession(); |
| 28 } | 38 } |
| 29 | 39 |
| 30 // static | 40 // static |
| 31 bool SpellCheckerSessionBridge::RegisterJNI(JNIEnv* env) { | 41 bool SpellCheckerSessionBridge::RegisterJNI(JNIEnv* env) { |
| 32 return RegisterNativesImpl(env); | 42 return RegisterNativesImpl(env); |
| 33 } | 43 } |
| 34 | 44 |
| 35 void SpellCheckerSessionBridge::RequestTextCheck(int route_id, | 45 void SpellCheckerSessionBridge::RequestTextCheck(int route_id, |
| 36 int identifier, | 46 int identifier, |
| 37 const base::string16& text) { | 47 const base::string16& text) { |
| 38 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 48 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 39 // SpellCheckerSessionBridge#create() will return null if spell checker | 49 // SpellCheckerSessionBridge#create() will return null if spell checker |
| 40 // service is unavailable. | 50 // service is unavailable. |
| 41 if (java_object_initialization_failed_) | 51 if (java_object_initialization_failed_) { |
| 52 if (!active_session_) { |
| 53 RecordAvailabilityUMA(false); |
| 54 active_session_ = true; |
| 55 } |
| 42 return; | 56 return; |
| 57 } |
| 43 | 58 |
| 44 // RequestTextCheck IPC arrives at the message filter before | 59 // RequestTextCheck IPC arrives at the message filter before |
| 45 // ToggleSpellCheck IPC when the user focuses an input field that already | 60 // ToggleSpellCheck IPC when the user focuses an input field that already |
| 46 // contains completed text. We need to initialize the spellchecker here | 61 // contains completed text. We need to initialize the spellchecker here |
| 47 // rather than in response to ToggleSpellCheck so that the existing text | 62 // rather than in response to ToggleSpellCheck so that the existing text |
| 48 // will be spellchecked immediately. | 63 // will be spellchecked immediately. |
| 49 if (java_object_.is_null()) { | 64 if (java_object_.is_null()) { |
| 50 java_object_.Reset(Java_SpellCheckerSessionBridge_create( | 65 java_object_.Reset(Java_SpellCheckerSessionBridge_create( |
| 51 base::android::AttachCurrentThread(), | 66 base::android::AttachCurrentThread(), |
| 52 reinterpret_cast<intptr_t>(this))); | 67 reinterpret_cast<intptr_t>(this))); |
| 68 if (!active_session_) { |
| 69 RecordAvailabilityUMA(!java_object_.is_null()); |
| 70 active_session_ = true; |
| 71 } |
| 53 if (java_object_.is_null()) { | 72 if (java_object_.is_null()) { |
| 54 java_object_initialization_failed_ = true; | 73 java_object_initialization_failed_ = true; |
| 55 return; | 74 return; |
| 56 } | 75 } |
| 57 } | 76 } |
| 58 | 77 |
| 59 // Save incoming requests to run at the end of the currently active request. | 78 // Save incoming requests to run at the end of the currently active request. |
| 60 // If multiple requests arrive during one active request, only the most | 79 // If multiple requests arrive during one active request, only the most |
| 61 // recent request will run (the others get overwritten). | 80 // recent request will run (the others get overwritten). |
| 62 if (active_request_) { | 81 if (active_request_) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 } | 126 } |
| 108 } | 127 } |
| 109 | 128 |
| 110 void SpellCheckerSessionBridge::DisconnectSession() { | 129 void SpellCheckerSessionBridge::DisconnectSession() { |
| 111 // Needs to be executed on the same thread as the RequestTextCheck and | 130 // Needs to be executed on the same thread as the RequestTextCheck and |
| 112 // ProcessSpellCheckResults methods, which is the UI thread. | 131 // ProcessSpellCheckResults methods, which is the UI thread. |
| 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 132 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 114 | 133 |
| 115 active_request_.reset(); | 134 active_request_.reset(); |
| 116 pending_request_.reset(); | 135 pending_request_.reset(); |
| 136 active_session_ = false; |
| 117 | 137 |
| 118 if (!java_object_.is_null()) { | 138 if (!java_object_.is_null()) { |
| 119 Java_SpellCheckerSessionBridge_disconnect( | 139 Java_SpellCheckerSessionBridge_disconnect( |
| 120 base::android::AttachCurrentThread(), java_object_); | 140 base::android::AttachCurrentThread(), java_object_); |
| 121 java_object_.Reset(); | 141 java_object_.Reset(); |
| 122 } | 142 } |
| 123 } | 143 } |
| 124 | 144 |
| 125 SpellCheckerSessionBridge::SpellingRequest::SpellingRequest( | 145 SpellCheckerSessionBridge::SpellingRequest::SpellingRequest( |
| 126 int route_id, | 146 int route_id, |
| 127 int identifier, | 147 int identifier, |
| 128 const base::string16& text) | 148 const base::string16& text) |
| 129 : route_id(route_id), identifier(identifier), text(text) {} | 149 : route_id(route_id), identifier(identifier), text(text) {} |
| 130 | 150 |
| 131 SpellCheckerSessionBridge::SpellingRequest::~SpellingRequest() {} | 151 SpellCheckerSessionBridge::SpellingRequest::~SpellingRequest() {} |
| OLD | NEW |