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

Side by Side Diff: android_webview/browser/find_helper.cc

Issue 10941015: [Android] Upstream the WebView find-in-page API implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nit fixes. Created 8 years, 3 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "android_webview/browser/find_helper.h"
6
7 #include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h"
8 #include "base/message_loop.h"
9 #include "content/public/browser/render_view_host.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/common/stop_find_action.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFindOptions.h"
13
14 using content::WebContents;
15 using WebKit::WebFindOptions;
16
17 namespace android_webview {
18
19 FindHelper::FindHelper(WebContents* web_contents)
20 : WebContentsObserver(web_contents),
21 listener_(NULL),
22 async_find_started_(false),
23 sync_find_started_(false),
24 find_request_id_counter_(0),
25 current_request_id_(0),
26 last_match_count_(-1),
27 last_active_ordinal_(-1),
28 weak_factory_(this) {
29 }
30
31 FindHelper::~FindHelper() {
32 }
33
34 void FindHelper::SetListener(Listener* listener) {
35 listener_ = listener;
36 }
37
38 int FindHelper::FindAllSync(const string16& search_string) {
39 sync_find_started_ = true;
40 async_find_started_ = false;
41
42 WebFindOptions options;
43 options.forward = true;
44 options.matchCase = false;
45 options.findNext = false;
46
47 int match_count = 0;
48 int active_ordinal = 0;
49
50 StartNewRequest(search_string);
51
52 // Any ongoing asynchronous requests will be stopped in the renderer when
53 // calling SynchronousFind. Using the asynchronous StopFinding message could
54 // lead to deadblocks as the message could arrive in the middle of the
55 // synchronous operation and cancel the reply back.
56 ScopedAllowWaitForLegacyWebViewApi wait;
57 web_contents()->GetRenderViewHost()->SynchronousFind(current_request_id_,
58 search_string,
59 options,
60 &match_count,
61 &active_ordinal);
62
63 // Post the task to ourselves to prevent trigerring the notification before
64 // we actually return from the request.
65 MessageLoop::current()->PostTask(
66 FROM_HERE,
67 base::Bind(&FindHelper::NotifyResults, weak_factory_.GetWeakPtr(),
68 active_ordinal, match_count, true));
69
70 return match_count;
71 }
72
73 void FindHelper::FindAllAsync(const string16& search_string) {
74 // Stop any ongoing asynchronous request.
75 web_contents()->GetRenderViewHost()->StopFinding(
76 content::STOP_FIND_ACTION_KEEP_SELECTION);
77
78 sync_find_started_ = false;
79 async_find_started_ = true;
80
81 WebFindOptions options;
82 options.forward = true;
83 options.matchCase = false;
84 options.findNext = false;
85
86 StartNewRequest(search_string);
87 web_contents()->GetRenderViewHost()->Find(current_request_id_,
88 search_string, options);
89 }
90
91 void FindHelper::HandleFindReply(int request_id,
92 int match_count,
93 int active_ordinal,
94 bool finished) {
95 if ((!async_find_started_ && !sync_find_started_) ||
96 request_id != current_request_id_) {
97 return;
98 }
99
100 NotifyResults(active_ordinal, match_count, finished);
101 }
102
103 void FindHelper::FindNext(bool forward) {
104 if (!sync_find_started_ && !async_find_started_)
105 return;
106
107 WebFindOptions options;
108 options.forward = forward;
109 options.matchCase = false;
110 options.findNext = true;
111
112 web_contents()->GetRenderViewHost()->Find(current_request_id_,
113 last_search_string_,
114 options);
115 }
116
117 void FindHelper::ClearMatches() {
118 web_contents()->GetRenderViewHost()->StopFinding(
119 content::STOP_FIND_ACTION_CLEAR_SELECTION);
120
121 sync_find_started_ = false;
122 async_find_started_ = false;
123 last_search_string_.clear();
124 last_match_count_ = -1;
125 last_active_ordinal_ = -1;
126 }
127
128 void FindHelper::StartNewRequest(const string16& search_string) {
129 current_request_id_ = find_request_id_counter_++;
130 last_search_string_ = search_string;
131 last_match_count_ = -1;
132 last_active_ordinal_ = -1;
133 }
134
135 void FindHelper::NotifyResults(int active_ordinal,
136 int match_count,
137 bool finished) {
138 // Match count or ordinal values set to -1 refer to the received replies.
139 if (match_count == -1)
140 match_count = last_match_count_;
141 else
142 last_match_count_ = match_count;
143
144 if (active_ordinal == -1)
145 active_ordinal = last_active_ordinal_;
146 else
147 last_active_ordinal_ = active_ordinal;
148
149 // Skip the update if we don't still have a valid ordinal.
150 // The next update, final or not, should have this information.
151 if (!finished && active_ordinal == -1)
152 return;
153
154 // Safeguard in case of errors to prevent reporting -1 to the API listeners.
155 if (match_count == -1) {
156 NOTREACHED();
157 match_count = 0;
158 }
159
160 if (active_ordinal == -1) {
161 NOTREACHED();
162 active_ordinal = 0;
163 }
164
165 // WebView.FindListener active match ordinals are 0-based while WebKit sends
166 // 1-based ordinals. Still we can receive 0 ordinal in case of no results.
167 active_ordinal = std::max(active_ordinal - 1, 0);
168
169 if (listener_)
170 listener_->OnFindResultReceived(active_ordinal, match_count, finished);
171 }
172
173 } // namespace android_webview
OLDNEW
« no previous file with comments | « android_webview/browser/find_helper.h ('k') | android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698