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.
5 #include "android_webview/browser/find_helper.h"
7 #include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h"
8 #include "base/message_loop/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/public/web/WebFindOptions.h"
14 using content::WebContents
;
15 using blink::WebFindOptions
;
17 namespace android_webview
{
19 FindHelper::FindHelper(WebContents
* web_contents
)
20 : WebContentsObserver(web_contents
),
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),
31 FindHelper::~FindHelper() {
34 void FindHelper::SetListener(Listener
* listener
) {
38 void FindHelper::FindAllAsync(const string16
& search_string
) {
39 // Stop any ongoing asynchronous request.
40 web_contents()->GetRenderViewHost()->StopFinding(
41 content::STOP_FIND_ACTION_KEEP_SELECTION
);
43 sync_find_started_
= false;
44 async_find_started_
= true;
46 WebFindOptions options
;
47 options
.forward
= true;
48 options
.matchCase
= false;
49 options
.findNext
= false;
51 StartNewRequest(search_string
);
52 web_contents()->GetRenderViewHost()->Find(current_request_id_
,
53 search_string
, options
);
56 void FindHelper::HandleFindReply(int request_id
,
60 if ((!async_find_started_
&& !sync_find_started_
) ||
61 request_id
!= current_request_id_
) {
65 NotifyResults(active_ordinal
, match_count
, finished
);
68 void FindHelper::FindNext(bool forward
) {
69 if (!sync_find_started_
&& !async_find_started_
)
72 WebFindOptions options
;
73 options
.forward
= forward
;
74 options
.matchCase
= false;
75 options
.findNext
= true;
77 web_contents()->GetRenderViewHost()->Find(current_request_id_
,
82 void FindHelper::ClearMatches() {
83 web_contents()->GetRenderViewHost()->StopFinding(
84 content::STOP_FIND_ACTION_CLEAR_SELECTION
);
86 sync_find_started_
= false;
87 async_find_started_
= false;
88 last_search_string_
.clear();
89 last_match_count_
= -1;
90 last_active_ordinal_
= -1;
93 void FindHelper::StartNewRequest(const string16
& search_string
) {
94 current_request_id_
= find_request_id_counter_
++;
95 last_search_string_
= search_string
;
96 last_match_count_
= -1;
97 last_active_ordinal_
= -1;
100 void FindHelper::NotifyResults(int active_ordinal
,
103 // Match count or ordinal values set to -1 refer to the received replies.
104 if (match_count
== -1)
105 match_count
= last_match_count_
;
107 last_match_count_
= match_count
;
109 if (active_ordinal
== -1)
110 active_ordinal
= last_active_ordinal_
;
112 last_active_ordinal_
= active_ordinal
;
114 // Skip the update if we don't still have a valid ordinal.
115 // The next update, final or not, should have this information.
116 if (!finished
&& active_ordinal
== -1)
119 // Safeguard in case of errors to prevent reporting -1 to the API listeners.
120 if (match_count
== -1) {
125 if (active_ordinal
== -1) {
130 // WebView.FindListener active match ordinals are 0-based while WebKit sends
131 // 1-based ordinals. Still we can receive 0 ordinal in case of no results.
132 active_ordinal
= std::max(active_ordinal
- 1, 0);
135 listener_
->OnFindResultReceived(active_ordinal
, match_count
, finished
);
138 } // namespace android_webview