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 base::string16
& search_string
) {
39 // Stop any ongoing asynchronous request.
40 web_contents()->StopFinding(content::STOP_FIND_ACTION_KEEP_SELECTION
);
42 sync_find_started_
= false;
43 async_find_started_
= true;
45 WebFindOptions options
;
46 options
.forward
= true;
47 options
.matchCase
= false;
48 options
.findNext
= false;
50 StartNewRequest(search_string
);
51 web_contents()->Find(current_request_id_
, search_string
, options
);
54 void FindHelper::HandleFindReply(int request_id
,
58 if ((!async_find_started_
&& !sync_find_started_
) ||
59 request_id
!= current_request_id_
) {
63 NotifyResults(active_ordinal
, match_count
, finished
);
66 void FindHelper::FindNext(bool forward
) {
67 if (!sync_find_started_
&& !async_find_started_
)
70 WebFindOptions options
;
71 options
.forward
= forward
;
72 options
.matchCase
= false;
73 options
.findNext
= true;
75 web_contents()->Find(current_request_id_
, last_search_string_
, options
);
78 void FindHelper::ClearMatches() {
79 web_contents()->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION
);
81 sync_find_started_
= false;
82 async_find_started_
= false;
83 last_search_string_
.clear();
84 last_match_count_
= -1;
85 last_active_ordinal_
= -1;
88 void FindHelper::StartNewRequest(const base::string16
& search_string
) {
89 current_request_id_
= find_request_id_counter_
++;
90 last_search_string_
= search_string
;
91 last_match_count_
= -1;
92 last_active_ordinal_
= -1;
95 void FindHelper::NotifyResults(int active_ordinal
,
98 // Match count or ordinal values set to -1 refer to the received replies.
99 if (match_count
== -1)
100 match_count
= last_match_count_
;
102 last_match_count_
= match_count
;
104 if (active_ordinal
== -1)
105 active_ordinal
= last_active_ordinal_
;
107 last_active_ordinal_
= active_ordinal
;
109 // Skip the update if we don't still have a valid ordinal.
110 // The next update, final or not, should have this information.
111 if (!finished
&& active_ordinal
== -1)
114 // Safeguard in case of errors to prevent reporting -1 to the API listeners.
115 if (match_count
== -1) {
120 if (active_ordinal
== -1) {
125 // WebView.FindListener active match ordinals are 0-based while WebKit sends
126 // 1-based ordinals. Still we can receive 0 ordinal in case of no results.
127 active_ordinal
= std::max(active_ordinal
- 1, 0);
130 listener_
->OnFindResultReceived(active_ordinal
, match_count
, finished
);
133 } // namespace android_webview