Commit 4ea8c209 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Modernize content::FindRequestManager code.

Use more C++11. Along the way, also use base::Contains() in more places
and improve STL usage.

Change-Id: I34e96d3d9801bcdf30f0f10eea2f5ca91ed43508
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1863157Reviewed-by: default avatarRakina Zata Amni <rakina@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706284}
parent 524ee216
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "content/browser/find_request_manager.h" #include "content/browser/find_request_manager.h"
#include <algorithm> #include <algorithm>
#include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/containers/queue.h" #include "base/containers/queue.h"
...@@ -32,6 +33,7 @@ namespace { ...@@ -32,6 +33,7 @@ namespace {
// Returns all child frames of |node|. // Returns all child frames of |node|.
std::vector<FrameTreeNode*> GetChildren(FrameTreeNode* node) { std::vector<FrameTreeNode*> GetChildren(FrameTreeNode* node) {
std::vector<FrameTreeNode*> children; std::vector<FrameTreeNode*> children;
children.reserve(node->child_count());
for (size_t i = 0; i != node->child_count(); ++i) { for (size_t i = 0; i != node->child_count(); ++i) {
if (auto* contents = static_cast<WebContentsImpl*>( if (auto* contents = static_cast<WebContentsImpl*>(
WebContentsImpl::FromOuterFrameTreeNode(node->child_at(i)))) { WebContentsImpl::FromOuterFrameTreeNode(node->child_at(i)))) {
...@@ -95,20 +97,16 @@ FrameTreeNode* GetParent(FrameTreeNode* node) { ...@@ -95,20 +97,16 @@ FrameTreeNode* GetParent(FrameTreeNode* node) {
return node->parent(); return node->parent();
auto* contents = WebContentsImpl::FromFrameTreeNode(node); auto* contents = WebContentsImpl::FromFrameTreeNode(node);
if (!node->IsMainFrame() || !contents->GetOuterWebContents())
return nullptr;
if (node->IsMainFrame() && contents->GetOuterWebContents()) { if (!GuestMode::IsCrossProcessFrameGuest(contents)) {
if (!GuestMode::IsCrossProcessFrameGuest(contents) && auto* guest = contents->GetBrowserPluginGuest();
contents->GetBrowserPluginGuest() && if (guest && guest->GetEmbedderFrame())
contents->GetBrowserPluginGuest()->GetEmbedderFrame()) { return guest->GetEmbedderFrame()->frame_tree_node();
return contents->GetBrowserPluginGuest()
->GetEmbedderFrame()
->frame_tree_node();
} else {
return GetParent(FrameTreeNode::GloballyFindByID(
contents->GetOuterDelegateFrameTreeNodeId()));
}
} }
return nullptr; return GetParent(FrameTreeNode::GloballyFindByID(
contents->GetOuterDelegateFrameTreeNodeId()));
} }
// Returns the previous sibling FrameTreeNode of |node|, if one exists, or // Returns the previous sibling FrameTreeNode of |node|, if one exists, or
...@@ -197,7 +195,7 @@ class FindRequestManager::FrameObserver : public WebContentsObserver { ...@@ -197,7 +195,7 @@ class FindRequestManager::FrameObserver : public WebContentsObserver {
FrameObserver(WebContentsImpl* web_contents, FindRequestManager* manager) FrameObserver(WebContentsImpl* web_contents, FindRequestManager* manager)
: WebContentsObserver(web_contents), manager_(manager) {} : WebContentsObserver(web_contents), manager_(manager) {}
~FrameObserver() override {} ~FrameObserver() override = default;
void DidFinishLoad(RenderFrameHost* rfh, const GURL& validated_url) override { void DidFinishLoad(RenderFrameHost* rfh, const GURL& validated_url) override {
if (manager_->current_session_id_ == kInvalidId) if (manager_->current_session_id_ == kInvalidId)
...@@ -236,7 +234,7 @@ class FindRequestManager::FrameObserver : public WebContentsObserver { ...@@ -236,7 +234,7 @@ class FindRequestManager::FrameObserver : public WebContentsObserver {
DISALLOW_COPY_AND_ASSIGN(FrameObserver); DISALLOW_COPY_AND_ASSIGN(FrameObserver);
}; };
FindRequestManager::FindRequest::FindRequest() {} FindRequestManager::FindRequest::FindRequest() = default;
FindRequestManager::FindRequest::FindRequest( FindRequestManager::FindRequest::FindRequest(
int id, int id,
...@@ -249,7 +247,7 @@ FindRequestManager::FindRequest::FindRequest(const FindRequest& request) ...@@ -249,7 +247,7 @@ FindRequestManager::FindRequest::FindRequest(const FindRequest& request)
search_text(request.search_text), search_text(request.search_text),
options(request.options.Clone()) {} options(request.options.Clone()) {}
FindRequestManager::FindRequest::~FindRequest() {} FindRequestManager::FindRequest::~FindRequest() = default;
FindRequestManager::FindRequest& FindRequestManager::FindRequest::operator=( FindRequestManager::FindRequest& FindRequestManager::FindRequest::operator=(
const FindRequest& request) { const FindRequest& request) {
...@@ -266,33 +264,25 @@ FindRequestManager::ActivateNearestFindResultState:: ...@@ -266,33 +264,25 @@ FindRequestManager::ActivateNearestFindResultState::
ActivateNearestFindResultState(float x, float y) ActivateNearestFindResultState(float x, float y)
: current_request_id(GetNextID()), point(x, y) {} : current_request_id(GetNextID()), point(x, y) {}
FindRequestManager::ActivateNearestFindResultState:: FindRequestManager::ActivateNearestFindResultState::
~ActivateNearestFindResultState() {} ~ActivateNearestFindResultState() = default;
FindRequestManager::FrameRects::FrameRects() = default; FindRequestManager::FrameRects::FrameRects() = default;
FindRequestManager::FrameRects::FrameRects(const std::vector<gfx::RectF>& rects, FindRequestManager::FrameRects::FrameRects(const std::vector<gfx::RectF>& rects,
int version) int version)
: rects(rects), version(version) {} : rects(rects), version(version) {}
FindRequestManager::FrameRects::~FrameRects() {} FindRequestManager::FrameRects::~FrameRects() = default;
FindRequestManager::FindMatchRectsState::FindMatchRectsState() = default; FindRequestManager::FindMatchRectsState::FindMatchRectsState() = default;
FindRequestManager::FindMatchRectsState::~FindMatchRectsState() {} FindRequestManager::FindMatchRectsState::~FindMatchRectsState() = default;
#endif #endif
// static // static
const int FindRequestManager::kInvalidId = -1; const int FindRequestManager::kInvalidId = -1;
FindRequestManager::FindRequestManager(WebContentsImpl* web_contents) FindRequestManager::FindRequestManager(WebContentsImpl* web_contents)
: contents_(web_contents), : contents_(web_contents) {}
current_session_id_(kInvalidId),
pending_find_next_reply_(nullptr), FindRequestManager::~FindRequestManager() = default;
pending_active_match_ordinal_(false),
number_of_matches_(0),
active_frame_(nullptr),
relative_active_match_ordinal_(0),
active_match_ordinal_(0),
last_reported_id_(kInvalidId) {}
FindRequestManager::~FindRequestManager() {}
void FindRequestManager::Find(int request_id, void FindRequestManager::Find(int request_id,
const base::string16& search_text, const base::string16& search_text,
...@@ -443,10 +433,8 @@ void FindRequestManager::RemoveFrame(RenderFrameHost* rfh) { ...@@ -443,10 +433,8 @@ void FindRequestManager::RemoveFrame(RenderFrameHost* rfh) {
activate_.nearest_frame = nullptr; activate_.nearest_frame = nullptr;
// Match rects in the removed frame are no longer relevant. // Match rects in the removed frame are no longer relevant.
if (match_rects_.frame_rects.count(rfh)) { if (match_rects_.frame_rects.erase(rfh) != 0)
match_rects_.frame_rects.erase(rfh);
++match_rects_.known_version; ++match_rects_.known_version;
}
// A reply should not be expected from the removed frame. // A reply should not be expected from the removed frame.
RemoveNearestFindResultPendingReply(rfh); RemoveNearestFindResultPendingReply(rfh);
...@@ -455,16 +443,16 @@ void FindRequestManager::RemoveFrame(RenderFrameHost* rfh) { ...@@ -455,16 +443,16 @@ void FindRequestManager::RemoveFrame(RenderFrameHost* rfh) {
// If no pending find replies are expected for the removed frame, then just // If no pending find replies are expected for the removed frame, then just
// report the updated results. // report the updated results.
if (!pending_initial_replies_.count(rfh) && pending_find_next_reply_ != rfh) { if (!base::Contains(pending_initial_replies_, rfh) &&
pending_find_next_reply_ != rfh) {
bool final_update = bool final_update =
pending_initial_replies_.empty() && !pending_find_next_reply_; pending_initial_replies_.empty() && !pending_find_next_reply_;
NotifyFindReply(current_session_id_, final_update); NotifyFindReply(current_session_id_, final_update);
return; return;
} }
if (pending_initial_replies_.count(rfh)) { if (pending_initial_replies_.erase(rfh) != 0) {
// A reply should not be expected from the removed frame. // A reply should not be expected from the removed frame.
pending_initial_replies_.erase(rfh);
if (pending_initial_replies_.empty()) { if (pending_initial_replies_.empty()) {
FinalUpdateReceived(current_session_id_, rfh); FinalUpdateReceived(current_session_id_, rfh);
} }
...@@ -513,7 +501,7 @@ void FindRequestManager::OnGetNearestFindResultReply(RenderFrameHostImpl* rfh, ...@@ -513,7 +501,7 @@ void FindRequestManager::OnGetNearestFindResultReply(RenderFrameHostImpl* rfh,
int request_id, int request_id,
float distance) { float distance) {
if (request_id != activate_.current_request_id || if (request_id != activate_.current_request_id ||
!activate_.pending_replies.count(rfh)) { !base::Contains(activate_.pending_replies, rfh)) {
return; return;
} }
...@@ -680,10 +668,10 @@ RenderFrameHost* FindRequestManager::Traverse(RenderFrameHost* from_rfh, ...@@ -680,10 +668,10 @@ RenderFrameHost* FindRequestManager::Traverse(RenderFrameHost* from_rfh,
DCHECK(from_rfh); DCHECK(from_rfh);
// If |from_rfh| is being detached, it might already be removed from // If |from_rfh| is being detached, it might already be removed from
// its parent's list of children, meaning we can't traverse it correctly. // its parent's list of children, meaning we can't traverse it correctly.
if (!static_cast<RenderFrameHostImpl*>(from_rfh)->is_active()) auto* from_rfh_impl = static_cast<RenderFrameHostImpl*>(from_rfh);
if (!from_rfh_impl->is_active())
return nullptr; return nullptr;
FrameTreeNode* node = FrameTreeNode* node = from_rfh_impl->frame_tree_node();
static_cast<RenderFrameHostImpl*>(from_rfh)->frame_tree_node();
FrameTreeNode* last_node = node; FrameTreeNode* last_node = node;
while ((node = TraverseNode(node, forward, wrap)) != nullptr) { while ((node = TraverseNode(node, forward, wrap)) != nullptr) {
if (!CheckFrame(node->current_frame_host())) { if (!CheckFrame(node->current_frame_host())) {
...@@ -697,7 +685,7 @@ RenderFrameHost* FindRequestManager::Traverse(RenderFrameHost* from_rfh, ...@@ -697,7 +685,7 @@ RenderFrameHost* FindRequestManager::Traverse(RenderFrameHost* from_rfh,
RenderFrameHost* current_rfh = node->current_frame_host(); RenderFrameHost* current_rfh = node->current_frame_host();
if (!matches_only || if (!matches_only ||
find_in_page_clients_.find(current_rfh)->second->number_of_matches() || find_in_page_clients_.find(current_rfh)->second->number_of_matches() ||
pending_initial_replies_.count(current_rfh)) { base::Contains(pending_initial_replies_, current_rfh)) {
// Note that if there is still a pending reply expected for this frame, // Note that if there is still a pending reply expected for this frame,
// then it may have unaccounted matches and will not be skipped via // then it may have unaccounted matches and will not be skipped via
// |matches_only|. // |matches_only|.
...@@ -728,7 +716,7 @@ void FindRequestManager::AddFrame(RenderFrameHost* rfh, bool force) { ...@@ -728,7 +716,7 @@ void FindRequestManager::AddFrame(RenderFrameHost* rfh, bool force) {
} }
bool FindRequestManager::CheckFrame(RenderFrameHost* rfh) const { bool FindRequestManager::CheckFrame(RenderFrameHost* rfh) const {
return rfh && find_in_page_clients_.count(rfh); return rfh && base::Contains(find_in_page_clients_, rfh);
} }
void FindRequestManager::UpdateActiveMatchOrdinal() { void FindRequestManager::UpdateActiveMatchOrdinal() {
...@@ -833,30 +821,29 @@ void FindRequestManager::RemoveFindMatchRectsPendingReply( ...@@ -833,30 +821,29 @@ void FindRequestManager::RemoveFindMatchRectsPendingReply(
return; return;
match_rects_.pending_replies.erase(it); match_rects_.pending_replies.erase(it);
if (match_rects_.pending_replies.empty()) { if (!match_rects_.pending_replies.empty())
// All replies are in. return;
std::vector<gfx::RectF> aggregate_rects;
if (match_rects_.request_version != match_rects_.known_version) { // All replies are in.
// Request version is stale, so aggregate and report the newer find std::vector<gfx::RectF> aggregate_rects;
// match rects. The rects should be aggregated in search order. if (match_rects_.request_version != match_rects_.known_version) {
for (RenderFrameHost* frame = GetInitialFrame(true /* forward */); frame; // Request version is stale, so aggregate and report the newer find
frame = Traverse(frame, // match rects. The rects should be aggregated in search order.
true /* forward */, for (RenderFrameHost* frame = GetInitialFrame(true /* forward */); frame;
true /* matches_only */, frame = Traverse(frame, true /* forward */, true /* matches_only */,
false /* wrap */)) { false /* wrap */)) {
auto frame_it = match_rects_.frame_rects.find(frame); auto frame_it = match_rects_.frame_rects.find(frame);
if (frame_it == match_rects_.frame_rects.end()) if (frame_it == match_rects_.frame_rects.end())
continue; continue;
std::vector<gfx::RectF>& frame_rects = frame_it->second.rects; std::vector<gfx::RectF>& frame_rects = frame_it->second.rects;
aggregate_rects.insert( aggregate_rects.insert(aggregate_rects.end(), frame_rects.begin(),
aggregate_rects.end(), frame_rects.begin(), frame_rects.end()); frame_rects.end());
}
} }
contents_->NotifyFindMatchRectsReply(
match_rects_.known_version, aggregate_rects, match_rects_.active_rect);
} }
contents_->NotifyFindMatchRectsReply(
match_rects_.known_version, aggregate_rects, match_rects_.active_rect);
} }
#endif #endif // defined(OS_ANDROID)
} // namespace content } // namespace content
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
#ifndef CONTENT_BROWSER_FIND_REQUEST_MANAGER_H_ #ifndef CONTENT_BROWSER_FIND_REQUEST_MANAGER_H_
#define CONTENT_BROWSER_FIND_REQUEST_MANAGER_H_ #define CONTENT_BROWSER_FIND_REQUEST_MANAGER_H_
#include <memory>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <utility>
#include <vector> #include <vector>
#include "base/containers/queue.h" #include "base/containers/queue.h"
...@@ -277,7 +277,7 @@ class CONTENT_EXPORT FindRequestManager { ...@@ -277,7 +277,7 @@ class CONTENT_EXPORT FindRequestManager {
// in all find-related IPCs, which allows reply IPCs containing results from // in all find-related IPCs, which allows reply IPCs containing results from
// previous sessions (with |request_id| < |current_session_id_|) to be easily // previous sessions (with |request_id| < |current_session_id_|) to be easily
// identified and ignored. // identified and ignored.
int current_session_id_; int current_session_id_ = kInvalidId;
// The current find request. // The current find request.
FindRequest current_request_; FindRequest current_request_;
...@@ -290,13 +290,13 @@ class CONTENT_EXPORT FindRequestManager { ...@@ -290,13 +290,13 @@ class CONTENT_EXPORT FindRequestManager {
// The frame (if any) that is still expected to reply to the last pending // The frame (if any) that is still expected to reply to the last pending
// "find next" request. // "find next" request.
RenderFrameHost* pending_find_next_reply_; RenderFrameHost* pending_find_next_reply_ = nullptr;
// Indicates whether an update to the active match ordinal is expected. Once // Indicates whether an update to the active match ordinal is expected. Once
// set, |pending_active_match_ordinal_| will not reset until an update to the // set, |pending_active_match_ordinal_| will not reset until an update to the
// active match ordinal is received in response to the find request with ID // active match ordinal is received in response to the find request with ID
// |current_request_.id| (the latest request). // |current_request_.id| (the latest request).
bool pending_active_match_ordinal_; bool pending_active_match_ordinal_ = false;
// The FindInPageClient associated with each frame. There will necessarily be // The FindInPageClient associated with each frame. There will necessarily be
// entries in this map for every frame that is being (or has been) searched in // entries in this map for every frame that is being (or has been) searched in
...@@ -307,16 +307,16 @@ class CONTENT_EXPORT FindRequestManager { ...@@ -307,16 +307,16 @@ class CONTENT_EXPORT FindRequestManager {
// The total number of matches found in the current find-in-page session. This // The total number of matches found in the current find-in-page session. This
// should always be equal to the sum of all the entries in // should always be equal to the sum of all the entries in
// |matches_per_frame_|. // |matches_per_frame_|.
int number_of_matches_; int number_of_matches_ = 0;
// The frame containing the active match, if one exists, or nullptr otherwise. // The frame containing the active match, if one exists, or nullptr otherwise.
RenderFrameHostImpl* active_frame_; RenderFrameHostImpl* active_frame_ = nullptr;
// The active match ordinal relative to the matches found in its own frame. // The active match ordinal relative to the matches found in its own frame.
int relative_active_match_ordinal_; int relative_active_match_ordinal_ = 0;
// The overall active match ordinal for the current find-in-page session. // The overall active match ordinal for the current find-in-page session.
int active_match_ordinal_; int active_match_ordinal_ = 0;
// The rectangle around the active match, in screen coordinates. // The rectangle around the active match, in screen coordinates.
gfx::Rect selection_rect_; gfx::Rect selection_rect_;
...@@ -327,7 +327,7 @@ class CONTENT_EXPORT FindRequestManager { ...@@ -327,7 +327,7 @@ class CONTENT_EXPORT FindRequestManager {
// Keeps track of the find request ID of the last find reply reported via // Keeps track of the find request ID of the last find reply reported via
// NotifyFindReply(). // NotifyFindReply().
int last_reported_id_; int last_reported_id_ = kInvalidId;
// WebContentsObservers to observe frame changes in |contents_| and its inner // WebContentsObservers to observe frame changes in |contents_| and its inner
// WebContentses. // WebContentses.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment