| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/guest_view/web_view/web_view_find_helper.h" | 5 #include "extensions/browser/guest_view/web_view/web_view_find_helper.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "components/guest_view/browser/guest_view_event.h" | 9 #include "components/guest_view/browser/guest_view_event.h" |
| 10 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h" | 10 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h" |
| 11 #include "extensions/browser/guest_view/web_view/web_view_constants.h" | 11 #include "extensions/browser/guest_view/web_view/web_view_constants.h" |
| 12 | 12 |
| 13 using guest_view::GuestViewEvent; | 13 using guest_view::GuestViewEvent; |
| 14 | 14 |
| 15 namespace extensions { | 15 namespace extensions { |
| 16 | 16 |
| 17 WebViewFindHelper::WebViewFindHelper(WebViewGuest* webview_guest) | 17 WebViewFindHelper::WebViewFindHelper(WebViewGuest* webview_guest) |
| 18 : webview_guest_(webview_guest), current_find_request_id_(0) { | 18 : webview_guest_(webview_guest), current_find_request_id_(0) { |
| 19 } | 19 } |
| 20 | 20 |
| 21 WebViewFindHelper::~WebViewFindHelper() { | 21 WebViewFindHelper::~WebViewFindHelper() { |
| 22 } | 22 } |
| 23 | 23 |
| 24 void WebViewFindHelper::CancelAllFindSessions() { | 24 void WebViewFindHelper::CancelAllFindSessions() { |
| 25 current_find_session_ = linked_ptr<WebViewFindHelper::FindInfo>(); | |
| 26 while (!find_info_map_.empty()) { | 25 while (!find_info_map_.empty()) { |
| 27 find_info_map_.begin()->second->SendResponse(true /* canceled */); | 26 find_info_map_.begin()->second->SendResponse(true /* canceled */); |
| 28 find_info_map_.erase(find_info_map_.begin()); | 27 find_info_map_.erase(find_info_map_.begin()); |
| 29 } | 28 } |
| 30 if (find_update_event_.get()) | 29 if (find_update_event_) |
| 31 DispatchFindUpdateEvent(true /* canceled */, true /* final_update */); | 30 DispatchFindUpdateEvent(true /* canceled */, true /* final_update */); |
| 32 find_update_event_.reset(); | 31 find_update_event_.reset(); |
| 33 } | 32 } |
| 34 | 33 |
| 35 void WebViewFindHelper::DispatchFindUpdateEvent(bool canceled, | 34 void WebViewFindHelper::DispatchFindUpdateEvent(bool canceled, |
| 36 bool final_update) { | 35 bool final_update) { |
| 37 DCHECK(find_update_event_.get()); | 36 DCHECK(find_update_event_.get()); |
| 38 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 37 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
| 39 find_update_event_->PrepareResults(args.get()); | 38 find_update_event_->PrepareResults(args.get()); |
| 40 args->SetBoolean(webview::kFindCanceled, canceled); | 39 args->SetBoolean(webview::kFindCanceled, canceled); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 const blink::WebFindOptions& options, | 92 const blink::WebFindOptions& options, |
| 94 scoped_refptr<WebViewInternalFindFunction> find_function) { | 93 scoped_refptr<WebViewInternalFindFunction> find_function) { |
| 95 // Need a new request_id for each new find request. | 94 // Need a new request_id for each new find request. |
| 96 ++current_find_request_id_; | 95 ++current_find_request_id_; |
| 97 | 96 |
| 98 // Stores the find request information by request_id so that its callback | 97 // Stores the find request information by request_id so that its callback |
| 99 // function can be called when the find results are available. | 98 // function can be called when the find results are available. |
| 100 std::pair<FindInfoMap::iterator, bool> insert_result = | 99 std::pair<FindInfoMap::iterator, bool> insert_result = |
| 101 find_info_map_.insert(std::make_pair( | 100 find_info_map_.insert(std::make_pair( |
| 102 current_find_request_id_, | 101 current_find_request_id_, |
| 103 linked_ptr< | 102 make_scoped_ptr(new WebViewFindHelper::FindInfo( |
| 104 WebViewFindHelper::FindInfo>(new WebViewFindHelper::FindInfo( | |
| 105 current_find_request_id_, search_text, options, find_function)))); | 103 current_find_request_id_, search_text, options, find_function)))); |
| 106 // No duplicate insertions. | 104 // No duplicate insertions. |
| 107 DCHECK(insert_result.second); | 105 DCHECK(insert_result.second); |
| 108 | 106 |
| 109 // Find options including the implicit |findNext| field. | 107 // Find options including the implicit |findNext| field. |
| 110 blink::WebFindOptions* full_options = insert_result.first->second->options(); | 108 blink::WebFindOptions* full_options = insert_result.first->second->options(); |
| 111 | 109 |
| 112 // Set |findNext| implicitly. | 110 // Set |findNext| implicitly. |
| 113 if (current_find_session_.get()) { | 111 if (current_find_session_) { |
| 114 const base::string16& current_search_text = | 112 const base::string16& current_search_text = |
| 115 current_find_session_->search_text(); | 113 current_find_session_->search_text(); |
| 116 bool current_match_case = current_find_session_->options()->matchCase; | 114 bool current_match_case = current_find_session_->options()->matchCase; |
| 117 full_options->findNext = !current_search_text.empty() && | 115 full_options->findNext = !current_search_text.empty() && |
| 118 current_search_text == search_text && | 116 current_search_text == search_text && |
| 119 current_match_case == options.matchCase; | 117 current_match_case == options.matchCase; |
| 120 } else { | 118 } else { |
| 121 full_options->findNext = false; | 119 full_options->findNext = false; |
| 122 } | 120 } |
| 123 | 121 |
| 124 // Link find requests that are a part of the same find session. | 122 // Link find requests that are a part of the same find session. |
| 125 if (full_options->findNext && current_find_session_.get()) { | 123 if (full_options->findNext && current_find_session_) { |
| 126 DCHECK(current_find_request_id_ != current_find_session_->request_id()); | 124 DCHECK(current_find_request_id_ != current_find_session_->request_id()); |
| 127 current_find_session_->AddFindNextRequest( | 125 current_find_session_->AddFindNextRequest( |
| 128 insert_result.first->second->AsWeakPtr()); | 126 insert_result.first->second->AsWeakPtr()); |
| 129 } | 127 } |
| 130 | 128 |
| 131 // Update the current find session, if necessary. | 129 // Update the current find session, if necessary. |
| 132 if (!full_options->findNext) | 130 if (!full_options->findNext) |
| 133 current_find_session_ = insert_result.first->second; | 131 current_find_session_ = insert_result.first->second->AsWeakPtr(); |
| 134 | 132 |
| 135 // Handle the empty |search_text| case internally. | 133 // Handle the empty |search_text| case internally. |
| 136 if (search_text.empty()) { | 134 if (search_text.empty()) { |
| 137 guest_web_contents->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION); | 135 guest_web_contents->StopFinding(content::STOP_FIND_ACTION_CLEAR_SELECTION); |
| 138 FindReply(current_find_request_id_, 0, gfx::Rect(), 0, true); | 136 FindReply(current_find_request_id_, 0, gfx::Rect(), 0, true); |
| 139 return; | 137 return; |
| 140 } | 138 } |
| 141 | 139 |
| 142 guest_web_contents->Find(current_find_request_id_, | 140 guest_web_contents->Find(current_find_request_id_, |
| 143 search_text, *full_options); | 141 search_text, *full_options); |
| 144 } | 142 } |
| 145 | 143 |
| 146 void WebViewFindHelper::FindReply(int request_id, | 144 void WebViewFindHelper::FindReply(int request_id, |
| 147 int number_of_matches, | 145 int number_of_matches, |
| 148 const gfx::Rect& selection_rect, | 146 const gfx::Rect& selection_rect, |
| 149 int active_match_ordinal, | 147 int active_match_ordinal, |
| 150 bool final_update) { | 148 bool final_update) { |
| 151 FindInfoMap::iterator find_iterator = find_info_map_.find(request_id); | 149 FindInfoMap::iterator find_iterator = find_info_map_.find(request_id); |
| 152 | 150 |
| 153 // Ignore slow replies to canceled find requests. | 151 // Ignore slow replies to canceled find requests. |
| 154 if (find_iterator == find_info_map_.end()) | 152 if (find_iterator == find_info_map_.end()) |
| 155 return; | 153 return; |
| 156 | 154 |
| 157 // This find request must be a part of an existing find session. | 155 // This find request must be a part of an existing find session. |
| 158 DCHECK(current_find_session_.get()); | 156 DCHECK(current_find_session_); |
| 159 | 157 |
| 160 WebViewFindHelper::FindInfo* find_info = find_iterator->second.get(); | 158 WebViewFindHelper::FindInfo* find_info = find_iterator->second.get(); |
| 161 | 159 |
| 162 // Handle canceled find requests. | 160 // Handle canceled find requests. |
| 163 if (!find_info->options()->findNext && | 161 if (!find_info->options()->findNext && |
| 164 find_info_map_.begin()->first < request_id) { | 162 find_info_map_.begin()->first < request_id) { |
| 165 DCHECK_NE(current_find_session_->request_id(), | 163 DCHECK_NE(current_find_session_->request_id(), |
| 166 find_info_map_.begin()->first); | 164 find_info_map_.begin()->first); |
| 167 DispatchFindUpdateEvent(true /* canceled */, true /* final_update */); | 165 DispatchFindUpdateEvent(true /* canceled */, true /* final_update */); |
| 168 EndFindSession(find_info_map_.begin()->first, true /* canceled */); | 166 EndFindSession(find_info_map_.begin()->first, true /* canceled */); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 base::DictionaryValue results; | 281 base::DictionaryValue results; |
| 284 find_results_.PrepareResults(&results); | 282 find_results_.PrepareResults(&results); |
| 285 results.SetBoolean(webview::kFindCanceled, canceled); | 283 results.SetBoolean(webview::kFindCanceled, canceled); |
| 286 | 284 |
| 287 // Call the callback. | 285 // Call the callback. |
| 288 find_function_->SetResult(results.DeepCopy()); | 286 find_function_->SetResult(results.DeepCopy()); |
| 289 find_function_->SendResponse(true); | 287 find_function_->SendResponse(true); |
| 290 } | 288 } |
| 291 | 289 |
| 292 } // namespace extensions | 290 } // namespace extensions |
| OLD | NEW |