Commit 724f3efe authored by Rakina Zata Amni's avatar Rakina Zata Amni Committed by Commit Bot

Introduce FindInPageClient, with SetNumberOfMatches and SetActiveMatch

FindInPageClient is a per-frame client for FindInPage.
FindRequestManager has a map of frame -> FindInPageClient.
FindInPageClient manages per-frame number of matches, and also
updates the global number of matches and the current active match
rect and ordinal.

In this CL, we are changing ActivateNearestFindResult to call methods
on FindInPageClient instead of calling a callback. SetNumberOfMatches
is called to update the corresponding frame's number of find-in-page
matches and also update the total number of matches in a tab.
SetActiveMatch updates the tab's active match rect and ordinal.

When we mojoify FindInPage::Find, we'll use FindInPageClient to
receive the find replies and remove FindRequestManager::OnFindReply.

Mojoifying doc: https://goo.gl/JenyGm

Previous CLs:
Mojoify FindInPage::StopFinding
crrev.com/c/1058731
(other CLs linked in the CL above)

Bug: 819919
Change-Id: I25a6d81037655801db1b1337f335073f11aa7862
Reviewed-on: https://chromium-review.googlesource.com/1080568
Commit-Queue: Rakina Zata Amni <rakina@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarAlex Moshchuk <alexmos@chromium.org>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577106}
parent 48286672
......@@ -789,6 +789,8 @@ jumbo_source_set("browser") {
"fileapi/file_system_url_loader_factory.h",
"fileapi/fileapi_message_filter.cc",
"fileapi/fileapi_message_filter.h",
"find_in_page_client.cc",
"find_in_page_client.h",
"find_request_manager.cc",
"find_request_manager.h",
"font_list_async.cc",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/find_in_page_client.h"
#include "content/browser/find_request_manager.h"
#include "content/browser/frame_host/render_frame_host_impl.h"
namespace content {
FindInPageClient::FindInPageClient(FindRequestManager* find_request_manager,
RenderFrameHostImpl* rfh)
: frame_(rfh), find_request_manager_(find_request_manager), binding_(this) {
blink::mojom::FindInPageClientPtr client;
binding_.Bind(MakeRequest(&client));
frame_->GetFindInPage()->SetClient(std::move(client));
}
FindInPageClient::~FindInPageClient() {}
void FindInPageClient::SetNumberOfMatches(
int request_id,
unsigned int number_of_matches,
blink::mojom::FindMatchUpdateType update_type) {
const int old_matches = number_of_matches_;
number_of_matches_ = number_of_matches;
find_request_manager_->UpdatedFrameNumberOfMatches(frame_, old_matches,
number_of_matches);
// If this is the final update for this frame, it might be the final update
// for the find request out of all the frames, so we need to handle it.
// Otherwise just notify directly while saying this is not the final update
// for the request.
if (update_type == blink::mojom::FindMatchUpdateType::kFinalUpdate)
find_request_manager_->HandleFinalUpdateForFrame(frame_, request_id);
else
find_request_manager_->NotifyFindReply(request_id,
false /* final_update */);
}
void FindInPageClient::SetActiveMatch(int request_id,
const gfx::Rect& active_match_rect,
int active_match_ordinal) {
find_request_manager_->SetActiveMatchRect(active_match_rect);
find_request_manager_->SetActiveMatchOrdinal(frame_, request_id,
active_match_ordinal);
find_request_manager_->HandleFinalUpdateForFrame(frame_, request_id);
}
void FindInPageClient::ActivateNearestFindResult(int request_id,
const gfx::PointF& point) {
frame_->GetFindInPage()->ActivateNearestFindResult(request_id, point);
}
} // namespace content
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_FIND_IN_PAGE_CLIENT_H_
#define CONTENT_BROWSER_FIND_IN_PAGE_CLIENT_H_
#include "mojo/public/cpp/bindings/binding.h"
#include "third_party/blink/public/mojom/frame/find_in_page.mojom.h"
namespace content {
class RenderFrameHostImpl;
class FindRequestManager;
// Per-frame client of FindInPage, owned by FindRequestManager.
// Keeps track of the current match count for the frame.
class FindInPageClient final : public blink::mojom::FindInPageClient {
public:
FindInPageClient(FindRequestManager* find_request_manager,
RenderFrameHostImpl* rfh);
~FindInPageClient() override;
void ActivateNearestFindResult(int request_id, const gfx::PointF& point);
// Current number of matches for this frame.
int number_of_matches() { return number_of_matches_; }
// blink::mojom::FindInPageClient overrides
void SetNumberOfMatches(int request_id,
unsigned int current_number_of_matches,
blink::mojom::FindMatchUpdateType final_update) final;
void SetActiveMatch(int request_id,
const gfx::Rect& active_match_rect,
int active_match_ordinal) final;
private:
RenderFrameHostImpl* const frame_;
FindRequestManager* const find_request_manager_;
mojo::Binding<blink::mojom::FindInPageClient> binding_;
int number_of_matches_ = 0;
DISALLOW_COPY_AND_ASSIGN(FindInPageClient);
};
} // namespace content
#endif // CONTENT_BROWSER_FIND_IN_PAGE_CLIENT_H_
This diff is collapsed.
......@@ -20,6 +20,7 @@
namespace content {
class FindInPageClient;
class RenderFrameHost;
class RenderFrameHostImpl;
class WebContentsImpl;
......@@ -55,13 +56,27 @@ class CONTENT_EXPORT FindRequestManager {
int active_match_ordinal,
bool final_update);
// Called when a reply for ActivateNearestFindResult is received.
void OnActivateNearestFindResultReply(RenderFrameHostImpl* rfh,
int request_id,
const gfx::Rect& active_match_rect,
int number_of_matches,
int active_match_ordinal,
bool final_update);
// Handles the final update from |rfh| for the find request with id
// |request_id|.
void HandleFinalUpdateForFrame(RenderFrameHostImpl* rfh, int request_id);
// The number of matches on |rfh| has changed from |old_count| to |new_count|.
// This method updates the total number of matches and also updates
// |active_match_ordinal_| accordingly.
void UpdatedFrameNumberOfMatches(RenderFrameHostImpl* rfh,
unsigned int old_count,
unsigned int new_count);
void SetActiveMatchRect(const gfx::Rect& active_match_rect);
void SetActiveMatchOrdinal(RenderFrameHostImpl* rfh,
int request_id,
int active_match_ordinal);
// Sends the find results (as they currently are) to the WebContents.
// |final_update| is true if we have received all of the updates from
// every frame for this request.
void NotifyFindReply(int request_id, bool final_update);
// Removes a frame from the set of frames being searched. This should be
// called whenever a frame is discovered to no longer exist.
......@@ -132,9 +147,6 @@ class CONTENT_EXPORT FindRequestManager {
// associated with |rfh|.
void SendFindIPC(const FindRequest& request, RenderFrameHost* rfh);
// Sends the find results (as they currently are) to the WebContents.
void NotifyFindReply(int request_id, bool final_update);
// Returns the initial frame in search order. This will be either the first
// frame, if searching forward, or the last frame, if searching backward.
RenderFrameHost* GetInitialFrame(bool forward) const;
......@@ -283,10 +295,11 @@ class CONTENT_EXPORT FindRequestManager {
// |current_request_.id| (the latest request).
bool pending_active_match_ordinal_;
// The number of matches found in 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
// the current find session, and no other frames.
std::unordered_map<RenderFrameHost*, int> matches_per_frame_;
std::unordered_map<RenderFrameHost*, std::unique_ptr<FindInPageClient>>
find_in_page_clients_;
// 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
......
......@@ -24,26 +24,16 @@ interface FindInPage {
GetNearestFindResult(gfx.mojom.PointF point) => (float distance);
// Activates a find result nearest to |point|, which is in fractions of the
// content document's width and height.
//
// Return values:
//
// |active_match_rect| will contain the bounding box of the activated
// find-in-page match marker. Might be an empty rect if there is none.
//
// |number_of_matches| is the current number of matches, or -1 if we
// shouldn't update number of matches.
//
// |active_match_ordinal| will contain the activated match's ordinal
// or -1 if we shouldn't update the active match ordinal.
//
// |final_update| is true if it's the final find reply for the frame.
//
ActivateNearestFindResult(gfx.mojom.PointF point) =>
(gfx.mojom.Rect active_match_rect,
int32 number_of_matches,
int32 active_match_ordinal,
bool final_update);
// content document's width and height. This function will call either the
// SetActiveMatch or SetNumberOfMatches on the FindInPageClient for this
// FindInPage instance.
// TODO(rakina): Find a way to remove |request_id|
ActivateNearestFindResult(int32 request_id, gfx.mojom.PointF point);
// Sets the client for this FindInPage instance. Should be called before
// calling ActivateNearestFindResult.
// TODO(rakina): Remove the need for this?
SetClient(FindInPageClient client);
// Returns the bounding boxes of the find-in-page match markers from the
// frame. The bounding boxes are returned in find-in-page coordinates.
......@@ -68,6 +58,21 @@ interface FindInPage {
gfx.mojom.RectF active_match_rect);
};
// Per-frame client of FindInPage.
interface FindInPageClient {
// Sets the number of matches of the frame to |number_of_matches|.
// If |final_update| is true, there will be no more update to the number of
// matches or active match for this frame.
SetNumberOfMatches(int32 request_id, uint32 number_of_matches,
FindMatchUpdateType update_type);
// Sets the current active match rect and ordinal. This is the final
// find update for the frame, notifying the browser side that there will be
// no more update to number of matches or active match for this frame.
SetActiveMatch(int32 request_id, gfx.mojom.Rect active_match_rect,
int32 active_match_ordinal);
};
// This enum defines what actions the renderer should take next when
// the user has completed a find-in-page;
enum StopFindAction {
......@@ -78,3 +83,9 @@ enum StopFindAction {
// The active match selection will be activated.
kStopFindActionActivateSelection
};
// Type of updates for FindInPageClient::SetNumberOfMatches
enum FindMatchUpdateType {
kFinalUpdate,
kMoreUpdatesComing
};
......@@ -197,9 +197,8 @@ WebFloatRect FindInPage::ActiveFindMatchRect() {
return WebFloatRect();
}
void FindInPage::ActivateNearestFindResult(
const WebFloatPoint& point,
ActivateNearestFindResultCallback callback) {
void FindInPage::ActivateNearestFindResult(int request_id,
const WebFloatPoint& point) {
WebRect active_match_rect;
const int ordinal =
EnsureTextFinder().SelectNearestFindMatch(point, &active_match_rect);
......@@ -208,16 +207,18 @@ void FindInPage::ActivateNearestFindResult(
// the current match count) in case the host is waiting for a response due
// to rate-limiting.
int number_of_matches = EnsureTextFinder().TotalMatchCount();
std::move(callback).Run(WebRect(), number_of_matches,
-1 /* active_match_ordinal */,
!EnsureTextFinder().FrameScoping() ||
!number_of_matches /* final_reply */);
mojom::blink::FindMatchUpdateType update_type =
mojom::blink::FindMatchUpdateType::kMoreUpdatesComing;
if (!EnsureTextFinder().FrameScoping() || !number_of_matches)
update_type = mojom::blink::FindMatchUpdateType::kFinalUpdate;
client_->SetNumberOfMatches(request_id, number_of_matches, update_type);
return;
}
// Call callback with current active match's rect and its ordinal,
// and don't update total number of matches.
std::move(callback).Run(active_match_rect, -1 /* number_of_matches */,
ordinal, true /* final_reply*/);
client_->SetActiveMatch(request_id, active_match_rect, ordinal);
}
void FindInPage::SetClient(mojom::blink::FindInPageClientPtr client) {
client_ = std::move(client);
}
void FindInPage::GetNearestFindResult(const WebFloatPoint& point,
......
......@@ -56,8 +56,9 @@ class CORE_EXPORT FindInPage final
// mojom::blink::FindInPage overrides
void ActivateNearestFindResult(const WebFloatPoint&,
ActivateNearestFindResultCallback) final;
void SetClient(mojom::blink::FindInPageClientPtr) final;
void ActivateNearestFindResult(int request_id, const WebFloatPoint&) final;
// Stops the current find-in-page, following the given |action|
void StopFinding(mojom::StopFindAction action) final;
......@@ -108,6 +109,8 @@ class CORE_EXPORT FindInPage final
const Member<WebLocalFrameImpl> frame_;
mojom::blink::FindInPageClientPtr client_;
mojo::AssociatedBinding<mojom::blink::FindInPage> binding_;
DISALLOW_COPY_AND_ASSIGN(FindInPage);
......
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