OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "android_webview/browser/find_helper.h" | 5 #include "android_webview/browser/find_helper.h" |
6 | 6 |
7 #include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h" | 7 #include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "content/public/browser/render_view_host.h" | 9 #include "content/public/browser/render_view_host.h" |
10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
11 #include "content/public/common/stop_find_action.h" | 11 #include "content/public/common/stop_find_action.h" |
12 #include "third_party/WebKit/public/web/WebFindOptions.h" | 12 #include "third_party/WebKit/public/web/WebFindOptions.h" |
13 | 13 |
14 using content::WebContents; | 14 using content::WebContents; |
15 using blink::WebFindOptions; | 15 using blink::WebFindOptions; |
16 | 16 |
17 namespace android_webview { | 17 namespace android_webview { |
18 | 18 |
19 FindHelper::FindHelper(WebContents* web_contents) | 19 FindHelper::FindHelper(WebContents* web_contents) |
20 : WebContentsObserver(web_contents), | 20 : WebContentsObserver(web_contents), |
21 listener_(NULL), | 21 listener_(nullptr), |
22 async_find_started_(false), | 22 async_find_started_(false), |
23 sync_find_started_(false), | |
24 find_request_id_counter_(0), | 23 find_request_id_counter_(0), |
25 current_request_id_(0), | 24 current_request_id_(0), |
26 last_match_count_(-1), | 25 last_match_count_(-1), |
27 last_active_ordinal_(-1), | 26 last_active_ordinal_(-1) { |
28 weak_factory_(this) { | |
29 } | 27 } |
30 | 28 |
31 FindHelper::~FindHelper() { | 29 FindHelper::~FindHelper() { |
32 } | 30 } |
33 | 31 |
34 void FindHelper::SetListener(Listener* listener) { | 32 void FindHelper::SetListener(Listener* listener) { |
35 listener_ = listener; | 33 listener_ = listener; |
36 } | 34 } |
37 | 35 |
38 void FindHelper::FindAllAsync(const base::string16& search_string) { | 36 void FindHelper::FindAllAsync(const base::string16& search_string) { |
39 // Stop any ongoing asynchronous request. | 37 // Stop any ongoing asynchronous request. |
40 web_contents()->StopFinding(content::STOP_FIND_ACTION_KEEP_SELECTION); | 38 web_contents()->StopFinding(content::STOP_FIND_ACTION_KEEP_SELECTION); |
41 | 39 |
42 sync_find_started_ = false; | |
43 async_find_started_ = true; | 40 async_find_started_ = true; |
44 | 41 |
| 42 StartNewRequest(search_string); |
| 43 |
| 44 if (MaybeHandleEmptySearch(search_string)) |
| 45 return; |
| 46 |
45 WebFindOptions options; | 47 WebFindOptions options; |
46 options.forward = true; | 48 options.forward = true; |
47 options.matchCase = false; | 49 options.matchCase = false; |
48 options.findNext = false; | 50 options.findNext = false; |
49 | 51 |
50 StartNewRequest(search_string); | |
51 web_contents()->Find(current_request_id_, search_string, options); | 52 web_contents()->Find(current_request_id_, search_string, options); |
52 } | 53 } |
53 | 54 |
54 void FindHelper::HandleFindReply(int request_id, | 55 void FindHelper::HandleFindReply(int request_id, |
55 int match_count, | 56 int match_count, |
56 int active_ordinal, | 57 int active_ordinal, |
57 bool finished) { | 58 bool finished) { |
58 if ((!async_find_started_ && !sync_find_started_) || | 59 if (!async_find_started_ || request_id != current_request_id_) |
59 request_id != current_request_id_) { | |
60 return; | 60 return; |
61 } | |
62 | 61 |
63 NotifyResults(active_ordinal, match_count, finished); | 62 NotifyResults(active_ordinal, match_count, finished); |
64 } | 63 } |
65 | 64 |
66 void FindHelper::FindNext(bool forward) { | 65 void FindHelper::FindNext(bool forward) { |
67 if (!sync_find_started_ && !async_find_started_) | 66 if (!async_find_started_) |
| 67 return; |
| 68 |
| 69 if (MaybeHandleEmptySearch(last_search_string_)) |
68 return; | 70 return; |
69 | 71 |
70 WebFindOptions options; | 72 WebFindOptions options; |
71 options.forward = forward; | 73 options.forward = forward; |
72 options.matchCase = false; | 74 options.matchCase = false; |
73 options.findNext = true; | 75 options.findNext = true; |
74 | 76 |
75 web_contents()->Find(current_request_id_, last_search_string_, options); | 77 web_contents()->Find(current_request_id_, last_search_string_, options); |
76 } | 78 } |
77 | 79 |
78 void FindHelper::ClearMatches() { | 80 void FindHelper::ClearMatches() { |
79 web_contents()->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION); | 81 web_contents()->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION); |
80 | 82 |
81 sync_find_started_ = false; | |
82 async_find_started_ = false; | 83 async_find_started_ = false; |
83 last_search_string_.clear(); | 84 last_search_string_.clear(); |
84 last_match_count_ = -1; | 85 last_match_count_ = -1; |
85 last_active_ordinal_ = -1; | 86 last_active_ordinal_ = -1; |
86 } | 87 } |
87 | 88 |
| 89 bool FindHelper::MaybeHandleEmptySearch(const base::string16& search_string) { |
| 90 if (!search_string.empty()) |
| 91 return false; |
| 92 |
| 93 web_contents()->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION); |
| 94 NotifyResults(0, 0, true); |
| 95 return true; |
| 96 } |
| 97 |
88 void FindHelper::StartNewRequest(const base::string16& search_string) { | 98 void FindHelper::StartNewRequest(const base::string16& search_string) { |
89 current_request_id_ = find_request_id_counter_++; | 99 current_request_id_ = find_request_id_counter_++; |
90 last_search_string_ = search_string; | 100 last_search_string_ = search_string; |
91 last_match_count_ = -1; | 101 last_match_count_ = -1; |
92 last_active_ordinal_ = -1; | 102 last_active_ordinal_ = -1; |
93 } | 103 } |
94 | 104 |
95 void FindHelper::NotifyResults(int active_ordinal, | 105 void FindHelper::NotifyResults(int active_ordinal, |
96 int match_count, | 106 int match_count, |
97 bool finished) { | 107 bool finished) { |
98 // Match count or ordinal values set to -1 refer to the received replies. | 108 // Match count or ordinal values set to -1 refer to received replies. |
99 if (match_count == -1) | 109 if (match_count == -1) |
100 match_count = last_match_count_; | 110 match_count = last_match_count_; |
101 else | 111 else |
102 last_match_count_ = match_count; | 112 last_match_count_ = match_count; |
103 | 113 |
104 if (active_ordinal == -1) | 114 if (active_ordinal == -1) |
105 active_ordinal = last_active_ordinal_; | 115 active_ordinal = last_active_ordinal_; |
106 else | 116 else |
107 last_active_ordinal_ = active_ordinal; | 117 last_active_ordinal_ = active_ordinal; |
108 | 118 |
(...skipping 15 matching lines...) Expand all Loading... |
124 | 134 |
125 // WebView.FindListener active match ordinals are 0-based while WebKit sends | 135 // 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. | 136 // 1-based ordinals. Still we can receive 0 ordinal in case of no results. |
127 active_ordinal = std::max(active_ordinal - 1, 0); | 137 active_ordinal = std::max(active_ordinal - 1, 0); |
128 | 138 |
129 if (listener_) | 139 if (listener_) |
130 listener_->OnFindResultReceived(active_ordinal, match_count, finished); | 140 listener_->OnFindResultReceived(active_ordinal, match_count, finished); |
131 } | 141 } |
132 | 142 |
133 } // namespace android_webview | 143 } // namespace android_webview |
OLD | NEW |