OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/find_request_manager.h" |
| 6 |
| 7 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 8 #include "content/browser/web_contents/web_contents_impl.h" |
| 9 #include "content/common/frame_messages.h" |
| 10 #include "content/common/input_messages.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 FindRequestManager::FindRequestManager(WebContentsImpl* web_contents) |
| 15 : contents_(web_contents), |
| 16 current_session_id_(kInvalidId), |
| 17 number_of_matches_(0), |
| 18 active_match_ordinal_(0) {} |
| 19 |
| 20 FindRequestManager::~FindRequestManager() {} |
| 21 |
| 22 void FindRequestManager::Find(int request_id, |
| 23 const base::string16& search_text, |
| 24 const blink::WebFindOptions& options) { |
| 25 // Every find request must have a unique ID, and these IDs must strictly |
| 26 // increase so that newer requests always have greater IDs than older |
| 27 // requests. |
| 28 DCHECK_GT(request_id, current_request_.id); |
| 29 DCHECK_GT(request_id, current_session_id_); |
| 30 |
| 31 FindRequest request(request_id, search_text, options); |
| 32 |
| 33 if (options.findNext) { |
| 34 // This is a find next operation. |
| 35 |
| 36 // This implies that there is an ongoing find session with the same search |
| 37 // text. |
| 38 DCHECK_GE(current_session_id_, 0); |
| 39 DCHECK_EQ(request.search_text, current_request_.search_text); |
| 40 } else { |
| 41 // This is an initial find operation. |
| 42 Reset(request); |
| 43 } |
| 44 |
| 45 SendFindIPC(request, contents_->GetMainFrame()); |
| 46 } |
| 47 |
| 48 void FindRequestManager::StopFinding(StopFindAction action) { |
| 49 SendStopFindingIPC(action, contents_->GetMainFrame()); |
| 50 current_session_id_ = kInvalidId; |
| 51 } |
| 52 |
| 53 void FindRequestManager::OnFindReply(RenderFrameHost* rfh, |
| 54 int request_id, |
| 55 int number_of_matches, |
| 56 const gfx::Rect& selection_rect, |
| 57 int active_match_ordinal, |
| 58 bool final_update) { |
| 59 // Ignore stale replies from abandoned find sessions. |
| 60 if (current_session_id_ == kInvalidId || request_id < current_session_id_) |
| 61 return; |
| 62 |
| 63 // Update the stored results. |
| 64 number_of_matches_ = number_of_matches; |
| 65 selection_rect_ = selection_rect; |
| 66 active_match_ordinal_ = active_match_ordinal; |
| 67 |
| 68 NotifyFindReply(request_id, final_update); |
| 69 } |
| 70 |
| 71 #if defined(OS_ANDROID) |
| 72 void FindRequestManager::ActivateNearestFindResult(float x, |
| 73 float y) { |
| 74 if (current_session_id_ == kInvalidId) |
| 75 return; |
| 76 |
| 77 auto rfh = contents_->GetMainFrame(); |
| 78 rfh->Send(new InputMsg_ActivateNearestFindResult( |
| 79 rfh->GetRoutingID(), current_session_id_, x, y)); |
| 80 } |
| 81 |
| 82 void FindRequestManager::RequestFindMatchRects(int current_version) { |
| 83 match_rects_.request_version = current_version; |
| 84 SendFindMatchRectsIPC(contents_->GetMainFrame()); |
| 85 } |
| 86 |
| 87 void FindRequestManager::OnFindMatchRectsReply( |
| 88 RenderFrameHost* rfh, |
| 89 int version, |
| 90 const std::vector<gfx::RectF>& rects, |
| 91 const gfx::RectF& active_rect) { |
| 92 contents_->NotifyFindMatchRectsReply(version, rects, active_rect); |
| 93 } |
| 94 #endif |
| 95 |
| 96 void FindRequestManager::Reset(const FindRequest& initial_request) { |
| 97 current_session_id_ = initial_request.id; |
| 98 current_request_ = initial_request; |
| 99 number_of_matches_ = 0; |
| 100 active_match_ordinal_ = 0; |
| 101 selection_rect_ = gfx::Rect(); |
| 102 #if defined(OS_ANDROID) |
| 103 match_rects_ = FindMatchRectsState(); |
| 104 #endif |
| 105 } |
| 106 |
| 107 void FindRequestManager::SendFindIPC(const FindRequest& request, |
| 108 RenderFrameHost* rfh) { |
| 109 rfh->Send(new FrameMsg_Find(rfh->GetRoutingID(), request.id, |
| 110 request.search_text, request.options)); |
| 111 } |
| 112 |
| 113 void FindRequestManager::SendStopFindingIPC(StopFindAction action, |
| 114 RenderFrameHost* rfh) const { |
| 115 rfh->Send(new FrameMsg_StopFinding(rfh->GetRoutingID(), action)); |
| 116 } |
| 117 |
| 118 void FindRequestManager::NotifyFindReply(int request_id, |
| 119 bool final_update) const { |
| 120 if (request_id == kInvalidId) { |
| 121 NOTREACHED(); |
| 122 return; |
| 123 } |
| 124 |
| 125 contents_->NotifyFindReply(request_id, number_of_matches_, selection_rect_, |
| 126 active_match_ordinal_, final_update); |
| 127 } |
| 128 |
| 129 #if defined(OS_ANDROID) |
| 130 void FindRequestManager::SendFindMatchRectsIPC(RenderFrameHost* rfh) { |
| 131 rfh->Send(new FrameMsg_FindMatchRects(rfh->GetRoutingID(), |
| 132 match_rects_.request_version)); |
| 133 } |
| 134 #endif |
| 135 |
| 136 } // namespace content |
OLD | NEW |