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 @@
#include "content/browser/find_request_manager.h"
#include <algorithm>
#include <utility>
#include "base/bind.h"
#include "base/containers/queue.h"
......@@ -32,6 +33,7 @@ namespace {
// Returns all child frames of |node|.
std::vector<FrameTreeNode*> GetChildren(FrameTreeNode* node) {
std::vector<FrameTreeNode*> children;
children.reserve(node->child_count());
for (size_t i = 0; i != node->child_count(); ++i) {
if (auto* contents = static_cast<WebContentsImpl*>(
WebContentsImpl::FromOuterFrameTreeNode(node->child_at(i)))) {
......@@ -95,20 +97,16 @@ FrameTreeNode* GetParent(FrameTreeNode* node) {
return node->parent();
auto* contents = WebContentsImpl::FromFrameTreeNode(node);
if (!node->IsMainFrame() || !contents->GetOuterWebContents())
return nullptr;
if (node->IsMainFrame() && contents->GetOuterWebContents()) {
if (!GuestMode::IsCrossProcessFrameGuest(contents) &&
contents->GetBrowserPluginGuest() &&
contents->GetBrowserPluginGuest()->GetEmbedderFrame()) {
return contents->GetBrowserPluginGuest()
->GetEmbedderFrame()
->frame_tree_node();
} else {
if (!GuestMode::IsCrossProcessFrameGuest(contents)) {
auto* guest = contents->GetBrowserPluginGuest();
if (guest && guest->GetEmbedderFrame())
return guest->GetEmbedderFrame()->frame_tree_node();
}
return GetParent(FrameTreeNode::GloballyFindByID(
contents->GetOuterDelegateFrameTreeNodeId()));
}
}
return nullptr;
}
// Returns the previous sibling FrameTreeNode of |node|, if one exists, or
......@@ -197,7 +195,7 @@ class FindRequestManager::FrameObserver : public WebContentsObserver {
FrameObserver(WebContentsImpl* web_contents, FindRequestManager* manager)
: WebContentsObserver(web_contents), manager_(manager) {}
~FrameObserver() override {}
~FrameObserver() override = default;
void DidFinishLoad(RenderFrameHost* rfh, const GURL& validated_url) override {
if (manager_->current_session_id_ == kInvalidId)
......@@ -236,7 +234,7 @@ class FindRequestManager::FrameObserver : public WebContentsObserver {
DISALLOW_COPY_AND_ASSIGN(FrameObserver);
};
FindRequestManager::FindRequest::FindRequest() {}
FindRequestManager::FindRequest::FindRequest() = default;
FindRequestManager::FindRequest::FindRequest(
int id,
......@@ -249,7 +247,7 @@ FindRequestManager::FindRequest::FindRequest(const FindRequest& request)
search_text(request.search_text),
options(request.options.Clone()) {}
FindRequestManager::FindRequest::~FindRequest() {}
FindRequestManager::FindRequest::~FindRequest() = default;
FindRequestManager::FindRequest& FindRequestManager::FindRequest::operator=(
const FindRequest& request) {
......@@ -266,33 +264,25 @@ FindRequestManager::ActivateNearestFindResultState::
ActivateNearestFindResultState(float x, float y)
: current_request_id(GetNextID()), point(x, y) {}
FindRequestManager::ActivateNearestFindResultState::
~ActivateNearestFindResultState() {}
~ActivateNearestFindResultState() = default;
FindRequestManager::FrameRects::FrameRects() = default;
FindRequestManager::FrameRects::FrameRects(const std::vector<gfx::RectF>& rects,
int version)
: rects(rects), version(version) {}
FindRequestManager::FrameRects::~FrameRects() {}
FindRequestManager::FrameRects::~FrameRects() = default;
FindRequestManager::FindMatchRectsState::FindMatchRectsState() = default;
FindRequestManager::FindMatchRectsState::~FindMatchRectsState() {}
FindRequestManager::FindMatchRectsState::~FindMatchRectsState() = default;
#endif
// static
const int FindRequestManager::kInvalidId = -1;
FindRequestManager::FindRequestManager(WebContentsImpl* web_contents)
: contents_(web_contents),
current_session_id_(kInvalidId),
pending_find_next_reply_(nullptr),
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() {}
: contents_(web_contents) {}
FindRequestManager::~FindRequestManager() = default;
void FindRequestManager::Find(int request_id,
const base::string16& search_text,
......@@ -443,10 +433,8 @@ void FindRequestManager::RemoveFrame(RenderFrameHost* rfh) {
activate_.nearest_frame = nullptr;
// Match rects in the removed frame are no longer relevant.
if (match_rects_.frame_rects.count(rfh)) {
match_rects_.frame_rects.erase(rfh);
if (match_rects_.frame_rects.erase(rfh) != 0)
++match_rects_.known_version;
}
// A reply should not be expected from the removed frame.
RemoveNearestFindResultPendingReply(rfh);
......@@ -455,16 +443,16 @@ void FindRequestManager::RemoveFrame(RenderFrameHost* rfh) {
// If no pending find replies are expected for the removed frame, then just
// 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 =
pending_initial_replies_.empty() && !pending_find_next_reply_;
NotifyFindReply(current_session_id_, final_update);
return;
}
if (pending_initial_replies_.count(rfh)) {
if (pending_initial_replies_.erase(rfh) != 0) {
// A reply should not be expected from the removed frame.
pending_initial_replies_.erase(rfh);
if (pending_initial_replies_.empty()) {
FinalUpdateReceived(current_session_id_, rfh);
}
......@@ -513,7 +501,7 @@ void FindRequestManager::OnGetNearestFindResultReply(RenderFrameHostImpl* rfh,
int request_id,
float distance) {
if (request_id != activate_.current_request_id ||
!activate_.pending_replies.count(rfh)) {
!base::Contains(activate_.pending_replies, rfh)) {
return;
}
......@@ -680,10 +668,10 @@ RenderFrameHost* FindRequestManager::Traverse(RenderFrameHost* from_rfh,
DCHECK(from_rfh);
// 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.
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;
FrameTreeNode* node =
static_cast<RenderFrameHostImpl*>(from_rfh)->frame_tree_node();
FrameTreeNode* node = from_rfh_impl->frame_tree_node();
FrameTreeNode* last_node = node;
while ((node = TraverseNode(node, forward, wrap)) != nullptr) {
if (!CheckFrame(node->current_frame_host())) {
......@@ -697,7 +685,7 @@ RenderFrameHost* FindRequestManager::Traverse(RenderFrameHost* from_rfh,
RenderFrameHost* current_rfh = node->current_frame_host();
if (!matches_only ||
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,
// then it may have unaccounted matches and will not be skipped via
// |matches_only|.
......@@ -728,7 +716,7 @@ void FindRequestManager::AddFrame(RenderFrameHost* rfh, bool force) {
}
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() {
......@@ -833,30 +821,29 @@ void FindRequestManager::RemoveFindMatchRectsPendingReply(
return;
match_rects_.pending_replies.erase(it);
if (match_rects_.pending_replies.empty()) {
if (!match_rects_.pending_replies.empty())
return;
// All replies are in.
std::vector<gfx::RectF> aggregate_rects;
if (match_rects_.request_version != match_rects_.known_version) {
// Request version is stale, so aggregate and report the newer find
// match rects. The rects should be aggregated in search order.
for (RenderFrameHost* frame = GetInitialFrame(true /* forward */); frame;
frame = Traverse(frame,
true /* forward */,
true /* matches_only */,
frame = Traverse(frame, true /* forward */, true /* matches_only */,
false /* wrap */)) {
auto frame_it = match_rects_.frame_rects.find(frame);
if (frame_it == match_rects_.frame_rects.end())
continue;
std::vector<gfx::RectF>& frame_rects = frame_it->second.rects;
aggregate_rects.insert(
aggregate_rects.end(), frame_rects.begin(), frame_rects.end());
aggregate_rects.insert(aggregate_rects.end(), frame_rects.begin(),
frame_rects.end());
}
}
contents_->NotifyFindMatchRectsReply(
match_rects_.known_version, aggregate_rects, match_rects_.active_rect);
}
}
#endif
#endif // defined(OS_ANDROID)
} // namespace content
......@@ -5,9 +5,9 @@
#ifndef CONTENT_BROWSER_FIND_REQUEST_MANAGER_H_
#define CONTENT_BROWSER_FIND_REQUEST_MANAGER_H_
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "base/containers/queue.h"
......@@ -277,7 +277,7 @@ class CONTENT_EXPORT FindRequestManager {
// in all find-related IPCs, which allows reply IPCs containing results from
// previous sessions (with |request_id| < |current_session_id_|) to be easily
// identified and ignored.
int current_session_id_;
int current_session_id_ = kInvalidId;
// The current find request.
FindRequest current_request_;
......@@ -290,13 +290,13 @@ class CONTENT_EXPORT FindRequestManager {
// The frame (if any) that is still expected to reply to the last pending
// "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
// 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
// |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
// entries in this map for every frame that is being (or has been) searched in
......@@ -307,16 +307,16 @@ class CONTENT_EXPORT FindRequestManager {
// 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
// |matches_per_frame_|.
int number_of_matches_;
int number_of_matches_ = 0;
// 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.
int relative_active_match_ordinal_;
int relative_active_match_ordinal_ = 0;
// 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.
gfx::Rect selection_rect_;
......@@ -327,7 +327,7 @@ class CONTENT_EXPORT FindRequestManager {
// Keeps track of the find request ID of the last find reply reported via
// NotifyFindReply().
int last_reported_id_;
int last_reported_id_ = kInvalidId;
// WebContentsObservers to observe frame changes in |contents_| and its inner
// 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