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" |
(...skipping 21 matching lines...) Expand all Loading... |
32 void FindHelper::SetListener(Listener* listener) { | 32 void FindHelper::SetListener(Listener* listener) { |
33 listener_ = listener; | 33 listener_ = listener; |
34 } | 34 } |
35 | 35 |
36 void FindHelper::FindAllAsync(const base::string16& search_string) { | 36 void FindHelper::FindAllAsync(const base::string16& search_string) { |
37 // Stop any ongoing asynchronous request. | 37 // Stop any ongoing asynchronous request. |
38 web_contents()->StopFinding(content::STOP_FIND_ACTION_KEEP_SELECTION); | 38 web_contents()->StopFinding(content::STOP_FIND_ACTION_KEEP_SELECTION); |
39 | 39 |
40 async_find_started_ = true; | 40 async_find_started_ = true; |
41 | 41 |
42 StartNewRequest(search_string); | 42 StartNewSession(search_string); |
43 | 43 |
44 if (MaybeHandleEmptySearch(search_string)) | 44 if (MaybeHandleEmptySearch(search_string)) |
45 return; | 45 return; |
46 | 46 |
47 WebFindOptions options; | 47 WebFindOptions options; |
48 options.forward = true; | 48 options.forward = true; |
49 options.matchCase = false; | 49 options.matchCase = false; |
50 options.findNext = false; | 50 options.findNext = false; |
51 | 51 |
52 web_contents()->Find(current_request_id_, search_string, options); | 52 web_contents()->Find(current_request_id_, search_string, options); |
53 } | 53 } |
54 | 54 |
55 void FindHelper::HandleFindReply(int request_id, | 55 void FindHelper::HandleFindReply(int request_id, |
56 int match_count, | 56 int match_count, |
57 int active_ordinal, | 57 int active_ordinal, |
58 bool finished) { | 58 bool finished) { |
59 if (!async_find_started_ || request_id != current_request_id_) | 59 if (!async_find_started_ || request_id != current_request_id_) |
60 return; | 60 return; |
61 | 61 |
62 NotifyResults(active_ordinal, match_count, finished); | 62 NotifyResults(active_ordinal, match_count, finished); |
63 } | 63 } |
64 | 64 |
65 void FindHelper::FindNext(bool forward) { | 65 void FindHelper::FindNext(bool forward) { |
66 if (!async_find_started_) | 66 if (!async_find_started_) |
67 return; | 67 return; |
68 | 68 |
| 69 current_request_id_ = find_request_id_counter_++; |
| 70 |
69 if (MaybeHandleEmptySearch(last_search_string_)) | 71 if (MaybeHandleEmptySearch(last_search_string_)) |
70 return; | 72 return; |
71 | 73 |
72 WebFindOptions options; | 74 WebFindOptions options; |
73 options.forward = forward; | 75 options.forward = forward; |
74 options.matchCase = false; | 76 options.matchCase = false; |
75 options.findNext = true; | 77 options.findNext = true; |
76 | 78 |
77 web_contents()->Find(current_request_id_, last_search_string_, options); | 79 web_contents()->Find(current_request_id_, last_search_string_, options); |
78 } | 80 } |
79 | 81 |
80 void FindHelper::ClearMatches() { | 82 void FindHelper::ClearMatches() { |
81 web_contents()->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION); | 83 web_contents()->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION); |
82 | 84 |
83 async_find_started_ = false; | 85 async_find_started_ = false; |
84 last_search_string_.clear(); | 86 last_search_string_.clear(); |
85 last_match_count_ = -1; | 87 last_match_count_ = -1; |
86 last_active_ordinal_ = -1; | 88 last_active_ordinal_ = -1; |
87 } | 89 } |
88 | 90 |
89 bool FindHelper::MaybeHandleEmptySearch(const base::string16& search_string) { | 91 bool FindHelper::MaybeHandleEmptySearch(const base::string16& search_string) { |
90 if (!search_string.empty()) | 92 if (!search_string.empty()) |
91 return false; | 93 return false; |
92 | 94 |
93 web_contents()->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION); | 95 web_contents()->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION); |
94 NotifyResults(0, 0, true); | 96 NotifyResults(0, 0, true); |
95 return true; | 97 return true; |
96 } | 98 } |
97 | 99 |
98 void FindHelper::StartNewRequest(const base::string16& search_string) { | 100 void FindHelper::StartNewSession(const base::string16& search_string) { |
99 current_request_id_ = find_request_id_counter_++; | 101 current_request_id_ = find_request_id_counter_++; |
100 last_search_string_ = search_string; | 102 last_search_string_ = search_string; |
101 last_match_count_ = -1; | 103 last_match_count_ = -1; |
102 last_active_ordinal_ = -1; | 104 last_active_ordinal_ = -1; |
103 } | 105 } |
104 | 106 |
105 void FindHelper::NotifyResults(int active_ordinal, | 107 void FindHelper::NotifyResults(int active_ordinal, |
106 int match_count, | 108 int match_count, |
107 bool finished) { | 109 bool finished) { |
108 // Match count or ordinal values set to -1 refer to received replies. | 110 // Match count or ordinal values set to -1 refer to received replies. |
(...skipping 25 matching lines...) Expand all Loading... |
134 | 136 |
135 // WebView.FindListener active match ordinals are 0-based while WebKit sends | 137 // WebView.FindListener active match ordinals are 0-based while WebKit sends |
136 // 1-based ordinals. Still we can receive 0 ordinal in case of no results. | 138 // 1-based ordinals. Still we can receive 0 ordinal in case of no results. |
137 active_ordinal = std::max(active_ordinal - 1, 0); | 139 active_ordinal = std::max(active_ordinal - 1, 0); |
138 | 140 |
139 if (listener_) | 141 if (listener_) |
140 listener_->OnFindResultReceived(active_ordinal, match_count, finished); | 142 listener_->OnFindResultReceived(active_ordinal, match_count, finished); |
141 } | 143 } |
142 | 144 |
143 } // namespace android_webview | 145 } // namespace android_webview |
OLD | NEW |