Commit 7b275389 authored by Rakina Zata Amni's avatar Rakina Zata Amni Committed by Commit Bot

Introduce find_in_page.h for find-in-page related functions

We are moving find-in-page related functions out of
web_local_frame_impl_find_in_page.cc to its own class
(FindInPage) for easier mojoification and so that we can add
new find-in-page related functions more easily.

In this CL, we are introducing find_in_page.h. WebLocalFrameImpl
still have the find-in-page functions, but TextFinder ownership is
moved to the FindInPage class. The functions in
web_local_frame_impl_find_in_page.cc are changed to call the
FindInPage functions (that are in the same file for smaller CL),
and will be removed entirely in the next CLs.

Previous CL: crrev.com/c/956648
In the previous CL, we moved find-in-page related functions to a new
file, but it's still in the WebLocalFrameImpl class. After more
discussions, we decided to do the old plan of refactoring the functions
to its own class.

Next CLs:
Move functions from "web_local_frame_impl_find_in_page.cc" to "find_in_page.cc"
Remove find-in-page functions from WebLocalFrame

Refactor plan doc: https://goo.gl/CtLsFR
Further plans:
Mojoification https://goo.gl/JenyGm
Find-in-page APIs https://github.com/rakina/find-in-page-api/

Bug: 819919
Change-Id: Icc8da3b321c72f2e7a210d5b350711b673eedfec
Reviewed-on: https://chromium-review.googlesource.com/1025242Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarTakayoshi Kochi <kochi@chromium.org>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Commit-Queue: Rakina Zata Amni <rakina@chromium.org>
Cr-Commit-Position: refs/heads/master@{#553459}
parent f87f4419
...@@ -210,6 +210,7 @@ ...@@ -210,6 +210,7 @@
#include "third_party/blink/renderer/core/loader/navigation_scheduler.h" #include "third_party/blink/renderer/core/loader/navigation_scheduler.h"
#include "third_party/blink/renderer/core/messaging/message_port.h" #include "third_party/blink/renderer/core/messaging/message_port.h"
#include "third_party/blink/renderer/core/page/context_menu_controller.h" #include "third_party/blink/renderer/core/page/context_menu_controller.h"
#include "third_party/blink/renderer/core/page/find_in_page.h"
#include "third_party/blink/renderer/core/page/focus_controller.h" #include "third_party/blink/renderer/core/page/focus_controller.h"
#include "third_party/blink/renderer/core/page/frame_tree.h" #include "third_party/blink/renderer/core/page/frame_tree.h"
#include "third_party/blink/renderer/core/page/page.h" #include "third_party/blink/renderer/core/page/page.h"
...@@ -1749,6 +1750,7 @@ WebLocalFrameImpl::WebLocalFrameImpl( ...@@ -1749,6 +1750,7 @@ WebLocalFrameImpl::WebLocalFrameImpl(
client_(client), client_(client),
local_frame_client_(LocalFrameClientImpl::Create(this)), local_frame_client_(LocalFrameClientImpl::Create(this)),
autofill_client_(nullptr), autofill_client_(nullptr),
find_in_page_(FindInPage::Create(*this)),
input_events_scale_factor_for_emulation_(1), input_events_scale_factor_for_emulation_(1),
interface_registry_(interface_registry), interface_registry_(interface_registry),
input_method_controller_(*this), input_method_controller_(*this),
...@@ -1777,10 +1779,10 @@ WebLocalFrameImpl::~WebLocalFrameImpl() { ...@@ -1777,10 +1779,10 @@ WebLocalFrameImpl::~WebLocalFrameImpl() {
void WebLocalFrameImpl::Trace(blink::Visitor* visitor) { void WebLocalFrameImpl::Trace(blink::Visitor* visitor) {
visitor->Trace(local_frame_client_); visitor->Trace(local_frame_client_);
visitor->Trace(find_in_page_);
visitor->Trace(frame_); visitor->Trace(frame_);
visitor->Trace(dev_tools_agent_); visitor->Trace(dev_tools_agent_);
visitor->Trace(frame_widget_); visitor->Trace(frame_widget_);
visitor->Trace(text_finder_);
visitor->Trace(print_context_); visitor->Trace(print_context_);
visitor->Trace(input_method_controller_); visitor->Trace(input_method_controller_);
WebFrame::TraceFrames(visitor, this); WebFrame::TraceFrames(visitor, this);
...@@ -1862,8 +1864,8 @@ LocalFrame* WebLocalFrameImpl::CreateChildFrame( ...@@ -1862,8 +1864,8 @@ LocalFrame* WebLocalFrameImpl::CreateChildFrame(
} }
void WebLocalFrameImpl::DidChangeContentsSize(const IntSize& size) { void WebLocalFrameImpl::DidChangeContentsSize(const IntSize& size) {
if (text_finder_ && text_finder_->TotalMatchCount() > 0) if (GetTextFinder() && GetTextFinder()->TotalMatchCount() > 0)
text_finder_->IncreaseMarkerVersion(); GetTextFinder()->IncreaseMarkerVersion();
} }
void WebLocalFrameImpl::CreateFrameView() { void WebLocalFrameImpl::CreateFrameView() {
...@@ -2060,8 +2062,8 @@ void WebLocalFrameImpl::Load( ...@@ -2060,8 +2062,8 @@ void WebLocalFrameImpl::Load(
DCHECK(!request.Url().ProtocolIs("javascript")); DCHECK(!request.Url().ProtocolIs("javascript"));
const ResourceRequest& resource_request = request.ToResourceRequest(); const ResourceRequest& resource_request = request.ToResourceRequest();
if (text_finder_) if (GetTextFinder())
text_finder_->ClearActiveFindMatch(); GetTextFinder()->ClearActiveFindMatch();
FrameLoadRequest frame_request = FrameLoadRequest frame_request =
FrameLoadRequest(nullptr, resource_request, /*frame_name=*/AtomicString(), FrameLoadRequest(nullptr, resource_request, /*frame_name=*/AtomicString(),
...@@ -2341,12 +2343,12 @@ void WebLocalFrameImpl::WillBeDetached() { ...@@ -2341,12 +2343,12 @@ void WebLocalFrameImpl::WillBeDetached() {
void WebLocalFrameImpl::WillDetachParent() { void WebLocalFrameImpl::WillDetachParent() {
// Do not expect string scoping results from any frames that got detached // Do not expect string scoping results from any frames that got detached
// in the middle of the operation. // in the middle of the operation.
if (text_finder_ && text_finder_->ScopingInProgress()) { if (GetTextFinder() && GetTextFinder()->ScopingInProgress()) {
// There is a possibility that the frame being detached was the only // There is a possibility that the frame being detached was the only
// pending one. We need to make sure final replies can be sent. // pending one. We need to make sure final replies can be sent.
text_finder_->FlushCurrentScoping(); GetTextFinder()->FlushCurrentScoping();
text_finder_->CancelPendingScopingEffort(); GetTextFinder()->CancelPendingScopingEffort();
} }
} }
...@@ -2408,10 +2410,6 @@ WebSandboxFlags WebLocalFrameImpl::EffectiveSandboxFlags() const { ...@@ -2408,10 +2410,6 @@ WebSandboxFlags WebLocalFrameImpl::EffectiveSandboxFlags() const {
GetFrame()->Loader().EffectiveSandboxFlags()); GetFrame()->Loader().EffectiveSandboxFlags());
} }
void WebLocalFrameImpl::ClearActiveFindMatch() {
EnsureTextFinder().ClearActiveFindMatch();
}
void WebLocalFrameImpl::UsageCountChromeLoadTimes(const WebString& metric) { void WebLocalFrameImpl::UsageCountChromeLoadTimes(const WebString& metric) {
WebFeature feature = WebFeature::kChromeLoadTimesUnknown; WebFeature feature = WebFeature::kChromeLoadTimesUnknown;
if (metric == "requestTime") { if (metric == "requestTime") {
......
...@@ -50,6 +50,7 @@ ...@@ -50,6 +50,7 @@
namespace blink { namespace blink {
class ChromePrintContext; class ChromePrintContext;
class FindInPage;
class IntSize; class IntSize;
class LocalFrameClientImpl; class LocalFrameClientImpl;
class ScrollableArea; class ScrollableArea;
...@@ -441,6 +442,9 @@ class CORE_EXPORT WebLocalFrameImpl final ...@@ -441,6 +442,9 @@ class CORE_EXPORT WebLocalFrameImpl final
// useful. // useful.
WebFrameWidgetBase* LocalRootFrameWidget(); WebFrameWidgetBase* LocalRootFrameWidget();
// Returns true if the frame is focused.
bool IsFocused() const;
virtual void Trace(blink::Visitor*); virtual void Trace(blink::Visitor*);
private: private:
...@@ -468,9 +472,6 @@ class CORE_EXPORT WebLocalFrameImpl final ...@@ -468,9 +472,6 @@ class CORE_EXPORT WebLocalFrameImpl final
WebPlugin* FocusedPluginIfInputMethodSupported(); WebPlugin* FocusedPluginIfInputMethodSupported();
ScrollableArea* LayoutViewportScrollableArea() const; ScrollableArea* LayoutViewportScrollableArea() const;
// Returns true if the frame is focused.
bool IsFocused() const;
// A helper for DispatchBeforePrintEvent() and DispatchAfterPrintEvent(). // A helper for DispatchBeforePrintEvent() and DispatchAfterPrintEvent().
void DispatchPrintEventRecursively(const AtomicString& event_type); void DispatchPrintEventRecursively(const AtomicString& event_type);
...@@ -499,8 +500,7 @@ class CORE_EXPORT WebLocalFrameImpl final ...@@ -499,8 +500,7 @@ class CORE_EXPORT WebLocalFrameImpl final
std::unique_ptr<SharedWorkerRepositoryClientImpl> std::unique_ptr<SharedWorkerRepositoryClientImpl>
shared_worker_repository_client_; shared_worker_repository_client_;
// Will be initialized after first call to ensureTextFinder(). Member<FindInPage> find_in_page_;
Member<TextFinder> text_finder_;
// Valid between calls to BeginPrint() and EndPrint(). Containts the print // Valid between calls to BeginPrint() and EndPrint(). Containts the print
// information. Is used by PrintPage(). // information. Is used by PrintPage().
......
...@@ -34,35 +34,42 @@ ...@@ -34,35 +34,42 @@
#include "third_party/blink/public/web/web_find_options.h" #include "third_party/blink/public/web/web_find_options.h"
#include "third_party/blink/public/web/web_frame_client.h" #include "third_party/blink/public/web/web_frame_client.h"
#include "third_party/blink/renderer/core/editing/finder/text_finder.h" #include "third_party/blink/renderer/core/editing/finder/text_finder.h"
#include "third_party/blink/renderer/core/page/find_in_page.h"
namespace blink { namespace blink {
void WebLocalFrameImpl::RequestFind(int identifier, void WebLocalFrameImpl::RequestFind(int identifier,
const WebString& search_text, const WebString& search_text,
const WebFindOptions& options) { const WebFindOptions& options) {
find_in_page_->RequestFind(identifier, search_text, options);
}
void FindInPage::RequestFind(int identifier,
const WebString& search_text,
const WebFindOptions& options) {
// Send "no results" if this frame has no visible content. // Send "no results" if this frame has no visible content.
if (!HasVisibleContent() && !options.force) { if (!frame_->HasVisibleContent() && !options.force) {
Client()->ReportFindInPageMatchCount(identifier, 0 /* count */, frame_->Client()->ReportFindInPageMatchCount(identifier, 0 /* count */,
true /* finalUpdate */); true /* finalUpdate */);
return; return;
} }
WebRange current_selection = SelectionRange(); WebRange current_selection = frame_->SelectionRange();
bool result = false; bool result = false;
bool active_now = false; bool active_now = false;
// Search for an active match only if this frame is focused or if this is a // Search for an active match only if this frame is focused or if this is a
// find next request. // find next request.
if (IsFocused() || options.find_next) { if (frame_->IsFocused() || options.find_next) {
result = Find(identifier, search_text, options, false /* wrapWithinFrame */, result = frame_->Find(identifier, search_text, options,
&active_now); false /* wrapWithinFrame */, &active_now);
} }
if (result && !options.find_next) { if (result && !options.find_next) {
// Indicate that at least one match has been found. 1 here means // Indicate that at least one match has been found. 1 here means
// possibly more matches could be coming. // possibly more matches could be coming.
Client()->ReportFindInPageMatchCount(identifier, 1 /* count */, frame_->Client()->ReportFindInPageMatchCount(identifier, 1 /* count */,
false /* finalUpdate */); false /* finalUpdate */);
} }
// There are three cases in which scoping is needed: // There are three cases in which scoping is needed:
...@@ -86,7 +93,7 @@ void WebLocalFrameImpl::RequestFind(int identifier, ...@@ -86,7 +93,7 @@ void WebLocalFrameImpl::RequestFind(int identifier,
if (/* (1) */ options.find_next && /* (2) */ current_selection.IsNull() && if (/* (1) */ options.find_next && /* (2) */ current_selection.IsNull() &&
/* (3) */ !(result && !active_now)) { /* (3) */ !(result && !active_now)) {
// Force report of the actual count. // Force report of the actual count.
IncreaseMatchCount(0, identifier); frame_->IncreaseMatchCount(0, identifier);
return; return;
} }
...@@ -101,33 +108,49 @@ bool WebLocalFrameImpl::Find(int identifier, ...@@ -101,33 +108,49 @@ bool WebLocalFrameImpl::Find(int identifier,
const WebFindOptions& options, const WebFindOptions& options,
bool wrap_within_frame, bool wrap_within_frame,
bool* active_now) { bool* active_now) {
if (!GetFrame()) return find_in_page_->Find(identifier, search_text, options,
wrap_within_frame, active_now);
}
bool FindInPage::Find(int identifier,
const WebString& search_text,
const WebFindOptions& options,
bool wrap_within_frame,
bool* active_now) {
if (!frame_->GetFrame())
return false; return false;
// Unlikely, but just in case we try to find-in-page on a detached frame. // Unlikely, but just in case we try to find-in-page on a detached frame.
DCHECK(GetFrame()->GetPage()); DCHECK(frame_->GetFrame()->GetPage());
// Up-to-date, clean tree is required for finding text in page, since it // Up-to-date, clean tree is required for finding text in page, since it
// relies on TextIterator to look over the text. // relies on TextIterator to look over the text.
GetFrame()->GetDocument()->UpdateStyleAndLayoutIgnorePendingStylesheets(); frame_->GetFrame()
->GetDocument()
->UpdateStyleAndLayoutIgnorePendingStylesheets();
return EnsureTextFinder().Find(identifier, search_text, options, return EnsureTextFinder().Find(identifier, search_text, options,
wrap_within_frame, active_now); wrap_within_frame, active_now);
} }
void WebLocalFrameImpl::StopFinding(StopFindAction action) { void WebLocalFrameImpl::StopFinding(StopFindAction action) {
bool clear_selection = action == kStopFindActionClearSelection; find_in_page_->StopFinding(action);
}
void FindInPage::StopFinding(WebLocalFrame::StopFindAction action) {
bool clear_selection = action == WebLocalFrame::kStopFindActionClearSelection;
if (clear_selection) if (clear_selection)
ExecuteCommand(WebString::FromUTF8("Unselect")); frame_->ExecuteCommand(WebString::FromUTF8("Unselect"));
if (text_finder_) { if (GetTextFinder()) {
if (!clear_selection) if (!clear_selection)
text_finder_->SetFindEndstateFocusAndSelection(); GetTextFinder()->SetFindEndstateFocusAndSelection();
text_finder_->StopFindingAndClearSelection(); GetTextFinder()->StopFindingAndClearSelection();
} }
if (action == kStopFindActionActivateSelection && IsFocused()) { if (action == WebLocalFrame::kStopFindActionActivateSelection &&
WebDocument doc = GetDocument(); frame_->IsFocused()) {
WebDocument doc = frame_->GetDocument();
if (!doc.IsNull()) { if (!doc.IsNull()) {
WebElement element = doc.FocusedElement(); WebElement element = doc.FocusedElement();
if (!element.IsNull()) if (!element.IsNull())
...@@ -137,53 +160,98 @@ void WebLocalFrameImpl::StopFinding(StopFindAction action) { ...@@ -137,53 +160,98 @@ void WebLocalFrameImpl::StopFinding(StopFindAction action) {
} }
void WebLocalFrameImpl::IncreaseMatchCount(int count, int identifier) { void WebLocalFrameImpl::IncreaseMatchCount(int count, int identifier) {
EnsureTextFinder().IncreaseMatchCount(identifier, count); find_in_page_->IncreaseMatchCount(count, identifier);
} }
int WebLocalFrameImpl::SelectNearestFindMatch(const WebFloatPoint& point, void FindInPage::IncreaseMatchCount(int count, int identifier) {
WebRect* selection_rect) { EnsureTextFinder().IncreaseMatchCount(identifier, count);
return EnsureTextFinder().SelectNearestFindMatch(point, selection_rect);
} }
int WebLocalFrameImpl::FindMatchMarkersVersion() const { int WebLocalFrameImpl::FindMatchMarkersVersion() const {
if (text_finder_) return find_in_page_->FindMatchMarkersVersion();
return text_finder_->FindMatchMarkersVersion(); };
int FindInPage::FindMatchMarkersVersion() const {
if (GetTextFinder())
return GetTextFinder()->FindMatchMarkersVersion();
return 0; return 0;
} }
float WebLocalFrameImpl::DistanceToNearestFindMatch( WebFloatRect WebLocalFrameImpl::ActiveFindMatchRect() {
const WebFloatPoint& point) { return find_in_page_->ActiveFindMatchRect();
float nearest_distance;
EnsureTextFinder().NearestFindMatch(point, &nearest_distance);
return nearest_distance;
} }
WebFloatRect WebLocalFrameImpl::ActiveFindMatchRect() { WebFloatRect FindInPage::ActiveFindMatchRect() {
if (text_finder_) if (GetTextFinder())
return text_finder_->ActiveFindMatchRect(); return GetTextFinder()->ActiveFindMatchRect();
return WebFloatRect(); return WebFloatRect();
} }
void WebLocalFrameImpl::FindMatchRects(WebVector<WebFloatRect>& output_rects) { void WebLocalFrameImpl::FindMatchRects(WebVector<WebFloatRect>& output_rects) {
find_in_page_->FindMatchRects(output_rects);
};
void FindInPage::FindMatchRects(WebVector<WebFloatRect>& output_rects) {
EnsureTextFinder().FindMatchRects(output_rects); EnsureTextFinder().FindMatchRects(output_rects);
} }
int WebLocalFrameImpl::SelectNearestFindMatch(const WebFloatPoint& point,
WebRect* selection_rect) {
return find_in_page_->SelectNearestFindMatch(point, selection_rect);
};
int FindInPage::SelectNearestFindMatch(const WebFloatPoint& point,
WebRect* selection_rect) {
return EnsureTextFinder().SelectNearestFindMatch(point, selection_rect);
}
float WebLocalFrameImpl::DistanceToNearestFindMatch(
const WebFloatPoint& point) {
return find_in_page_->DistanceToNearestFindMatch(point);
}
float FindInPage::DistanceToNearestFindMatch(const WebFloatPoint& point) {
float nearest_distance;
EnsureTextFinder().NearestFindMatch(point, &nearest_distance);
return nearest_distance;
}
void WebLocalFrameImpl::SetTickmarks(const WebVector<WebRect>& tickmarks) { void WebLocalFrameImpl::SetTickmarks(const WebVector<WebRect>& tickmarks) {
if (GetFrameView()) { find_in_page_->SetTickmarks(tickmarks);
}
void FindInPage::SetTickmarks(const WebVector<WebRect>& tickmarks) {
if (frame_->GetFrameView()) {
Vector<IntRect> tickmarks_converted(tickmarks.size()); Vector<IntRect> tickmarks_converted(tickmarks.size());
for (size_t i = 0; i < tickmarks.size(); ++i) for (size_t i = 0; i < tickmarks.size(); ++i)
tickmarks_converted[i] = tickmarks[i]; tickmarks_converted[i] = tickmarks[i];
GetFrameView()->SetTickmarks(tickmarks_converted); frame_->GetFrameView()->SetTickmarks(tickmarks_converted);
} }
} }
void WebLocalFrameImpl::ClearActiveFindMatch() {
find_in_page_->ClearActiveFindMatch();
}
void FindInPage::ClearActiveFindMatch() {
EnsureTextFinder().ClearActiveFindMatch();
}
TextFinder* WebLocalFrameImpl::GetTextFinder() const { TextFinder* WebLocalFrameImpl::GetTextFinder() const {
return find_in_page_->GetTextFinder();
}
TextFinder* FindInPage::GetTextFinder() const {
return text_finder_; return text_finder_;
} }
TextFinder& WebLocalFrameImpl::EnsureTextFinder() { TextFinder& WebLocalFrameImpl::EnsureTextFinder() {
return find_in_page_->EnsureTextFinder();
}
TextFinder& FindInPage::EnsureTextFinder() {
if (!text_finder_) if (!text_finder_)
text_finder_ = TextFinder::Create(*this); text_finder_ = TextFinder::Create(*frame_);
return *text_finder_; return *text_finder_;
} }
......
...@@ -25,6 +25,7 @@ blink_core_sources("page") { ...@@ -25,6 +25,7 @@ blink_core_sources("page") {
"drag_session.h", "drag_session.h",
"drag_state.h", "drag_state.h",
"event_with_hit_test_results.h", "event_with_hit_test_results.h",
"find_in_page.h",
"focus_changed_observer.cc", "focus_changed_observer.cc",
"focus_changed_observer.h", "focus_changed_observer.h",
"focus_controller.cc", "focus_controller.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.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_PAGE_FIND_IN_PAGE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_PAGE_FIND_IN_PAGE_H_
#include "third_party/blink/public/platform/web_common.h"
#include "third_party/blink/public/web/web_local_frame.h"
namespace blink {
class TextFinder;
class WebLocalFrameImpl;
class WebString;
struct WebFindOptions;
struct WebFloatRect;
class FindInPage : public GarbageCollected<FindInPage> {
public:
static FindInPage* Create(WebLocalFrameImpl& frame) {
return new FindInPage(frame);
}
void RequestFind(int identifier,
const WebString& search_text,
const WebFindOptions&);
bool Find(int identifier,
const WebString& search_text,
const WebFindOptions&,
bool wrap_within_frame,
bool* active_now = nullptr);
void StopFinding(WebLocalFrame::StopFindAction);
void IncreaseMatchCount(int count, int identifier);
int FindMatchMarkersVersion() const;
WebFloatRect ActiveFindMatchRect();
void FindMatchRects(WebVector<WebFloatRect>&);
int SelectNearestFindMatch(const WebFloatPoint&, WebRect* selection_rect);
float DistanceToNearestFindMatch(const WebFloatPoint&);
void SetTickmarks(const WebVector<WebRect>&);
void ClearActiveFindMatch();
TextFinder* GetTextFinder() const;
// Returns the text finder object if it already exists.
// Otherwise creates it and then returns.
TextFinder& EnsureTextFinder();
void Trace(blink::Visitor* visitor) {
visitor->Trace(text_finder_);
visitor->Trace(frame_);
}
private:
FindInPage(WebLocalFrameImpl& frame) : frame_(&frame) {}
// Will be initialized after first call to ensureTextFinder().
Member<TextFinder> text_finder_;
const Member<WebLocalFrameImpl> frame_;
DISALLOW_COPY_AND_ASSIGN(FindInPage);
};
} // namespace blink
#endif
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