Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: chrome/browser/spellchecker/spellchecker_session_bridge_android.cc

Issue 1275813002: Implemented typo recognition in Chrome for Android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@4_remove_mac_redundancies
Patch Set: Changed pending requests to be handled in native, other comment addressing Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "chrome/common/spellcheck_messages.h"
8 #include "chrome/common/spellcheck_result.h"
9 #include "content/public/browser/render_process_host.h"
10 #include "jni/SpellCheckerSessionBridge_jni.h"
11
12 SpellCheckerSessionBridge::SpellingRequest::SpellingRequest(
13 int new_route_id,
14 int new_identifier,
15 const base::string16& new_text)
16 : route_id(new_route_id), identifier(new_identifier), text(new_text) {}
17
18 SpellCheckerSessionBridge::SpellCheckerSessionBridge(int render_process_id)
19 : render_process_id_(render_process_id),
20 is_active_request_(false),
21 is_pending_request_(false),
22 java_object_(Java_SpellCheckerSessionBridge_create(
23 base::android::AttachCurrentThread(),
24 reinterpret_cast<intptr_t>(this))) {}
25
26 // static
27 bool SpellCheckerSessionBridge::RegisterJNI(JNIEnv* env) {
28 return RegisterNativesImpl(env);
29 }
30
31 void SpellCheckerSessionBridge::RequestTextCheck(int route_id,
32 int identifier,
33 const base::string16& text) {
34 // Save incoming requests to run at the end of the currently active request.
35 // If multiple requests arrive during one active request, only the most
36 // recent request will run (the others get overwritten).
37 if (is_active_request_) {
38 pending_spelling_request_ = SpellingRequest(route_id, identifier, text);
39 is_pending_request_ = true;
40 return;
41 }
42
43 active_spelling_request_ = SpellingRequest(route_id, identifier, text);
44 is_active_request_ = true;
45
46 JNIEnv* env = base::android::AttachCurrentThread();
47 Java_SpellCheckerSessionBridge_requestTextCheck(
48 env, java_object_.obj(),
49 base::android::ConvertUTF16ToJavaString(env, text).obj());
50 }
51
52 void SpellCheckerSessionBridge::ProcessSpellCheckResults(
53 JNIEnv* env,
54 jobject jobj,
55 jintArray offset_array,
56 jintArray length_array) {
57 std::vector<int> offsets;
58 std::vector<int> lengths;
59
60 base::android::JavaIntArrayToIntVector(env, offset_array, &offsets);
61 base::android::JavaIntArrayToIntVector(env, length_array, &lengths);
62
63 // Convert specific offset/length pairs into SpellCheckResults, which can be
64 // sent to the renderer to be drawn as red underlines.
65 std::vector<SpellCheckResult> results;
66 for (size_t i = 0; i < offsets.size(); i++) {
67 results.push_back(
68 SpellCheckResult(SpellCheckResult::SPELLING, offsets[i], lengths[i]));
69 }
70
71 content::RenderProcessHost* sender =
72 content::RenderProcessHost::FromID(render_process_id_);
73
74 if (sender != nullptr) {
75 sender->Send(new SpellCheckMsg_RespondTextCheck(
76 active_spelling_request_.route_id, active_spelling_request_.identifier,
77 active_spelling_request_.text, results));
78 }
79
80 // If any pending requests arrived during the execution of this active
81 // request, the most recently arrived pending request will become the active
82 // request and be run.
83 if (is_pending_request_) {
newt (away) 2015/08/13 20:40:31 slightly simpler way to write this: active_requ
dylanking 2015/08/14 02:57:03 Will definitely change to this if I end up using s
84 active_spelling_request_ = pending_spelling_request_;
85 is_pending_request_ = false;
86
87 JNIEnv* env = base::android::AttachCurrentThread();
88 Java_SpellCheckerSessionBridge_requestTextCheck(
89 env, java_object_.obj(), base::android::ConvertUTF16ToJavaString(
newt (away) 2015/08/13 20:40:31 weird indentation
dylanking 2015/08/14 02:57:03 Fixed, but I have a feeling this was git cl format
90 env, active_spelling_request_.text)
91 .obj());
92 } else {
93 is_active_request_ = false;
94 }
95 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698