| 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>(); | 25   current_find_session_ = nullptr; | 
| 26   while (!find_info_map_.empty()) { | 26   while (!find_info_map_.empty()) { | 
| 27     find_info_map_.begin()->second->SendResponse(true /* canceled */); | 27     find_info_map_.begin()->second->SendResponse(true /* canceled */); | 
| 28     find_info_map_.erase(find_info_map_.begin()); | 28     find_info_map_.erase(find_info_map_.begin()); | 
| 29   } | 29   } | 
| 30   if (find_update_event_.get()) | 30   if (find_update_event_) | 
| 31     DispatchFindUpdateEvent(true /* canceled */, true /* final_update */); | 31     DispatchFindUpdateEvent(true /* canceled */, true /* final_update */); | 
| 32   find_update_event_.reset(); | 32   find_update_event_.reset(); | 
| 33 } | 33 } | 
| 34 | 34 | 
| 35 void WebViewFindHelper::DispatchFindUpdateEvent(bool canceled, | 35 void WebViewFindHelper::DispatchFindUpdateEvent(bool canceled, | 
| 36                                                 bool final_update) { | 36                                                 bool final_update) { | 
| 37   DCHECK(find_update_event_.get()); | 37   DCHECK(find_update_event_.get()); | 
| 38   scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 38   scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 
| 39   find_update_event_->PrepareResults(args.get()); | 39   find_update_event_->PrepareResults(args.get()); | 
| 40   args->SetBoolean(webview::kFindCanceled, canceled); | 40   args->SetBoolean(webview::kFindCanceled, canceled); | 
| 41   args->SetBoolean(webview::kFindFinalUpdate, final_update); | 41   args->SetBoolean(webview::kFindFinalUpdate, final_update); | 
| 42   DCHECK(webview_guest_); | 42   DCHECK(webview_guest_); | 
| 43   webview_guest_->DispatchEventToView( | 43   webview_guest_->DispatchEventToView(make_scoped_ptr( | 
| 44       new GuestViewEvent(webview::kEventFindReply, std::move(args))); | 44       new GuestViewEvent(webview::kEventFindReply, std::move(args)))); | 
| 45 } | 45 } | 
| 46 | 46 | 
| 47 void WebViewFindHelper::EndFindSession(int session_request_id, bool canceled) { | 47 void WebViewFindHelper::EndFindSession(int session_request_id, bool canceled) { | 
| 48   FindInfoMap::iterator session_iterator = | 48   FindInfoMap::iterator session_iterator = | 
| 49       find_info_map_.find(session_request_id); | 49       find_info_map_.find(session_request_id); | 
| 50   DCHECK(session_iterator != find_info_map_.end()); | 50   DCHECK(session_iterator != find_info_map_.end()); | 
| 51   FindInfo* find_info = session_iterator->second.get(); | 51   FindInfo* find_info = session_iterator->second.get(); | 
| 52 | 52 | 
| 53   // Call the callback function of the first request of the find session. | 53   // Call the callback function of the first request of the find session. | 
| 54   find_info->SendResponse(canceled); | 54   find_info->SendResponse(canceled); | 
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 93     const blink::WebFindOptions& options, | 93     const blink::WebFindOptions& options, | 
| 94     scoped_refptr<WebViewInternalFindFunction> find_function) { | 94     scoped_refptr<WebViewInternalFindFunction> find_function) { | 
| 95   // Need a new request_id for each new find request. | 95   // Need a new request_id for each new find request. | 
| 96   ++current_find_request_id_; | 96   ++current_find_request_id_; | 
| 97 | 97 | 
| 98   // Stores the find request information by request_id so that its callback | 98   // Stores the find request information by request_id so that its callback | 
| 99   // function can be called when the find results are available. | 99   // function can be called when the find results are available. | 
| 100   std::pair<FindInfoMap::iterator, bool> insert_result = | 100   std::pair<FindInfoMap::iterator, bool> insert_result = | 
| 101       find_info_map_.insert(std::make_pair( | 101       find_info_map_.insert(std::make_pair( | 
| 102           current_find_request_id_, | 102           current_find_request_id_, | 
| 103           linked_ptr< | 103           make_scoped_refptr(new FindInfo(current_find_request_id_, search_text, | 
| 104               WebViewFindHelper::FindInfo>(new WebViewFindHelper::FindInfo( | 104                                           options, find_function)))); | 
| 105               current_find_request_id_, search_text, options, find_function)))); |  | 
| 106   // No duplicate insertions. | 105   // No duplicate insertions. | 
| 107   DCHECK(insert_result.second); | 106   DCHECK(insert_result.second); | 
| 108 | 107 | 
| 109   // Find options including the implicit |findNext| field. | 108   // Find options including the implicit |findNext| field. | 
| 110   blink::WebFindOptions* full_options = insert_result.first->second->options(); | 109   blink::WebFindOptions* full_options = insert_result.first->second->options(); | 
| 111 | 110 | 
| 112   // Set |findNext| implicitly. | 111   // Set |findNext| implicitly. | 
| 113   if (current_find_session_.get()) { | 112   if (current_find_session_) { | 
| 114     const base::string16& current_search_text = | 113     const base::string16& current_search_text = | 
| 115         current_find_session_->search_text(); | 114         current_find_session_->search_text(); | 
| 116     bool current_match_case = current_find_session_->options()->matchCase; | 115     bool current_match_case = current_find_session_->options()->matchCase; | 
| 117     full_options->findNext = !current_search_text.empty() && | 116     full_options->findNext = !current_search_text.empty() && | 
| 118         current_search_text == search_text && | 117         current_search_text == search_text && | 
| 119         current_match_case == options.matchCase; | 118         current_match_case == options.matchCase; | 
| 120   } else { | 119   } else { | 
| 121     full_options->findNext = false; | 120     full_options->findNext = false; | 
| 122   } | 121   } | 
| 123 | 122 | 
| 124   // Link find requests that are a part of the same find session. | 123   // Link find requests that are a part of the same find session. | 
| 125   if (full_options->findNext && current_find_session_.get()) { | 124   if (full_options->findNext && current_find_session_) { | 
| 126     DCHECK(current_find_request_id_ != current_find_session_->request_id()); | 125     DCHECK(current_find_request_id_ != current_find_session_->request_id()); | 
| 127     current_find_session_->AddFindNextRequest( | 126     current_find_session_->AddFindNextRequest( | 
| 128         insert_result.first->second->AsWeakPtr()); | 127         insert_result.first->second->AsWeakPtr()); | 
| 129   } | 128   } | 
| 130 | 129 | 
| 131   // Update the current find session, if necessary. | 130   // Update the current find session, if necessary. | 
| 132   if (!full_options->findNext) | 131   if (!full_options->findNext) | 
| 133     current_find_session_ = insert_result.first->second; | 132     current_find_session_ = insert_result.first->second; | 
| 134 | 133 | 
| 135   // Handle the empty |search_text| case internally. | 134   // Handle the empty |search_text| case internally. | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
| 148                                   const gfx::Rect& selection_rect, | 147                                   const gfx::Rect& selection_rect, | 
| 149                                   int active_match_ordinal, | 148                                   int active_match_ordinal, | 
| 150                                   bool final_update) { | 149                                   bool final_update) { | 
| 151   FindInfoMap::iterator find_iterator = find_info_map_.find(request_id); | 150   FindInfoMap::iterator find_iterator = find_info_map_.find(request_id); | 
| 152 | 151 | 
| 153   // Ignore slow replies to canceled find requests. | 152   // Ignore slow replies to canceled find requests. | 
| 154   if (find_iterator == find_info_map_.end()) | 153   if (find_iterator == find_info_map_.end()) | 
| 155     return; | 154     return; | 
| 156 | 155 | 
| 157   // This find request must be a part of an existing find session. | 156   // This find request must be a part of an existing find session. | 
| 158   DCHECK(current_find_session_.get()); | 157   DCHECK(current_find_session_); | 
| 159 | 158 | 
| 160   WebViewFindHelper::FindInfo* find_info = find_iterator->second.get(); | 159   WebViewFindHelper::FindInfo* find_info = find_iterator->second.get(); | 
| 161 | 160 | 
| 162   // Handle canceled find requests. | 161   // Handle canceled find requests. | 
| 163   if (!find_info->options()->findNext && | 162   if (!find_info->options()->findNext && | 
| 164       find_info_map_.begin()->first < request_id) { | 163       find_info_map_.begin()->first < request_id) { | 
| 165     DCHECK_NE(current_find_session_->request_id(), | 164     DCHECK_NE(current_find_session_->request_id(), | 
| 166               find_info_map_.begin()->first); | 165               find_info_map_.begin()->first); | 
| 167     DispatchFindUpdateEvent(true /* canceled */, true /* final_update */); | 166     DispatchFindUpdateEvent(true /* canceled */, true /* final_update */); | 
| 168     EndFindSession(find_info_map_.begin()->first, true /* canceled */); | 167     EndFindSession(find_info_map_.begin()->first, true /* canceled */); | 
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 253     const blink::WebFindOptions& options, | 252     const blink::WebFindOptions& options, | 
| 254     scoped_refptr<WebViewInternalFindFunction> find_function) | 253     scoped_refptr<WebViewInternalFindFunction> find_function) | 
| 255     : request_id_(request_id), | 254     : request_id_(request_id), | 
| 256       search_text_(search_text), | 255       search_text_(search_text), | 
| 257       options_(options), | 256       options_(options), | 
| 258       find_function_(find_function), | 257       find_function_(find_function), | 
| 259       replied_(false), | 258       replied_(false), | 
| 260       weak_ptr_factory_(this) { | 259       weak_ptr_factory_(this) { | 
| 261 } | 260 } | 
| 262 | 261 | 
| 263 WebViewFindHelper::FindInfo::~FindInfo() { |  | 
| 264 } |  | 
| 265 |  | 
| 266 void WebViewFindHelper::FindInfo::AggregateResults( | 262 void WebViewFindHelper::FindInfo::AggregateResults( | 
| 267     int number_of_matches, | 263     int number_of_matches, | 
| 268     const gfx::Rect& selection_rect, | 264     const gfx::Rect& selection_rect, | 
| 269     int active_match_ordinal, | 265     int active_match_ordinal, | 
| 270     bool final_update) { | 266     bool final_update) { | 
| 271   replied_ = true; | 267   replied_ = true; | 
| 272   find_results_.AggregateResults(number_of_matches, selection_rect, | 268   find_results_.AggregateResults(number_of_matches, selection_rect, | 
| 273                                  active_match_ordinal, final_update); | 269                                  active_match_ordinal, final_update); | 
| 274 } | 270 } | 
| 275 | 271 | 
| 276 base::WeakPtr<WebViewFindHelper::FindInfo> | 272 base::WeakPtr<WebViewFindHelper::FindInfo> | 
| 277 WebViewFindHelper::FindInfo::AsWeakPtr() { | 273 WebViewFindHelper::FindInfo::AsWeakPtr() { | 
| 278   return weak_ptr_factory_.GetWeakPtr(); | 274   return weak_ptr_factory_.GetWeakPtr(); | 
| 279 } | 275 } | 
| 280 | 276 | 
| 281 void WebViewFindHelper::FindInfo::SendResponse(bool canceled) { | 277 void WebViewFindHelper::FindInfo::SendResponse(bool canceled) { | 
| 282   // Prepare the find results to pass to the callback function. | 278   // Prepare the find results to pass to the callback function. | 
| 283   base::DictionaryValue results; | 279   base::DictionaryValue results; | 
| 284   find_results_.PrepareResults(&results); | 280   find_results_.PrepareResults(&results); | 
| 285   results.SetBoolean(webview::kFindCanceled, canceled); | 281   results.SetBoolean(webview::kFindCanceled, canceled); | 
| 286 | 282 | 
| 287   // Call the callback. | 283   // Call the callback. | 
| 288   find_function_->SetResult(results.DeepCopy()); | 284   find_function_->SetResult(results.DeepCopy()); | 
| 289   find_function_->SendResponse(true); | 285   find_function_->SendResponse(true); | 
| 290 } | 286 } | 
| 291 | 287 | 
|  | 288 WebViewFindHelper::FindInfo::~FindInfo() {} | 
|  | 289 | 
| 292 }  // namespace extensions | 290 }  // namespace extensions | 
| OLD | NEW | 
|---|