| 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_(nullptr), | 21 listener_(nullptr), |
| 22 async_find_started_(false), | 22 async_find_started_(false), |
| 23 find_request_id_counter_(0), | 23 find_request_id_counter_(0), |
| 24 current_request_id_(0), | 24 current_request_id_(0), |
| 25 current_session_id_(0), |
| 25 last_match_count_(-1), | 26 last_match_count_(-1), |
| 26 last_active_ordinal_(-1) { | 27 last_active_ordinal_(-1) { |
| 27 } | 28 } |
| 28 | 29 |
| 29 FindHelper::~FindHelper() { | 30 FindHelper::~FindHelper() { |
| 30 } | 31 } |
| 31 | 32 |
| 32 void FindHelper::SetListener(Listener* listener) { | 33 void FindHelper::SetListener(Listener* listener) { |
| 33 listener_ = listener; | 34 listener_ = listener; |
| 34 } | 35 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 49 options.matchCase = false; | 50 options.matchCase = false; |
| 50 options.findNext = false; | 51 options.findNext = false; |
| 51 | 52 |
| 52 web_contents()->Find(current_request_id_, search_string, options); | 53 web_contents()->Find(current_request_id_, search_string, options); |
| 53 } | 54 } |
| 54 | 55 |
| 55 void FindHelper::HandleFindReply(int request_id, | 56 void FindHelper::HandleFindReply(int request_id, |
| 56 int match_count, | 57 int match_count, |
| 57 int active_ordinal, | 58 int active_ordinal, |
| 58 bool finished) { | 59 bool finished) { |
| 59 if (!async_find_started_ || request_id != current_request_id_) | 60 if (!async_find_started_ || request_id < current_session_id_) |
| 60 return; | 61 return; |
| 61 | 62 |
| 62 NotifyResults(active_ordinal, match_count, finished); | 63 NotifyResults(active_ordinal, match_count, finished); |
| 63 } | 64 } |
| 64 | 65 |
| 65 void FindHelper::FindNext(bool forward) { | 66 void FindHelper::FindNext(bool forward) { |
| 66 if (!async_find_started_) | 67 if (!async_find_started_) |
| 67 return; | 68 return; |
| 68 | 69 |
| 69 current_request_id_ = find_request_id_counter_++; | 70 current_request_id_ = find_request_id_counter_++; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 92 if (!search_string.empty()) | 93 if (!search_string.empty()) |
| 93 return false; | 94 return false; |
| 94 | 95 |
| 95 web_contents()->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION); | 96 web_contents()->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION); |
| 96 NotifyResults(0, 0, true); | 97 NotifyResults(0, 0, true); |
| 97 return true; | 98 return true; |
| 98 } | 99 } |
| 99 | 100 |
| 100 void FindHelper::StartNewSession(const base::string16& search_string) { | 101 void FindHelper::StartNewSession(const base::string16& search_string) { |
| 101 current_request_id_ = find_request_id_counter_++; | 102 current_request_id_ = find_request_id_counter_++; |
| 103 current_session_id_ = current_request_id_; |
| 102 last_search_string_ = search_string; | 104 last_search_string_ = search_string; |
| 103 last_match_count_ = -1; | 105 last_match_count_ = -1; |
| 104 last_active_ordinal_ = -1; | 106 last_active_ordinal_ = -1; |
| 105 } | 107 } |
| 106 | 108 |
| 107 void FindHelper::NotifyResults(int active_ordinal, | 109 void FindHelper::NotifyResults(int active_ordinal, |
| 108 int match_count, | 110 int match_count, |
| 109 bool finished) { | 111 bool finished) { |
| 110 // Match count or ordinal values set to -1 refer to received replies. | 112 // Match count or ordinal values set to -1 refer to received replies. |
| 111 if (match_count == -1) | 113 if (match_count == -1) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 136 | 138 |
| 137 // WebView.FindListener active match ordinals are 0-based while WebKit sends | 139 // WebView.FindListener active match ordinals are 0-based while WebKit sends |
| 138 // 1-based ordinals. Still we can receive 0 ordinal in case of no results. | 140 // 1-based ordinals. Still we can receive 0 ordinal in case of no results. |
| 139 active_ordinal = std::max(active_ordinal - 1, 0); | 141 active_ordinal = std::max(active_ordinal - 1, 0); |
| 140 | 142 |
| 141 if (listener_) | 143 if (listener_) |
| 142 listener_->OnFindResultReceived(active_ordinal, match_count, finished); | 144 listener_->OnFindResultReceived(active_ordinal, match_count, finished); |
| 143 } | 145 } |
| 144 | 146 |
| 145 } // namespace android_webview | 147 } // namespace android_webview |
| OLD | NEW |