alloy: Implement Find() using find_in_page::FindTabHelper (fixes issue #3098, see issue #3047)

The find behavior should now match Chrome.
This commit is contained in:
Marshall Greenblatt
2022-02-17 13:17:29 -05:00
parent 758022006a
commit 171d525aa4
17 changed files with 114 additions and 96 deletions

View File

@@ -9,6 +9,7 @@
#include <utility>
#include "libcef/browser/alloy/alloy_browser_context.h"
#include "libcef/browser/alloy/browser_platform_delegate_alloy.h"
#include "libcef/browser/audio_capturer.h"
#include "libcef/browser/browser_context.h"
#include "libcef/browser/browser_info.h"
@@ -450,21 +451,19 @@ void AlloyBrowserHostImpl::PrintToPDF(const CefString& path,
}
}
void AlloyBrowserHostImpl::Find(int identifier,
const CefString& searchText,
void AlloyBrowserHostImpl::Find(const CefString& searchText,
bool forward,
bool matchCase,
bool findNext) {
if (!CEF_CURRENTLY_ON_UIT()) {
CEF_POST_TASK(CEF_UIT,
base::BindOnce(&AlloyBrowserHostImpl::Find, this, identifier,
searchText, forward, matchCase, findNext));
base::BindOnce(&AlloyBrowserHostImpl::Find, this, searchText,
forward, matchCase, findNext));
return;
}
if (platform_delegate_) {
platform_delegate_->Find(identifier, searchText, forward, matchCase,
findNext);
platform_delegate_->Find(searchText, forward, matchCase, findNext);
}
}
@@ -860,13 +859,21 @@ void AlloyBrowserHostImpl::FindReply(content::WebContents* web_contents,
const gfx::Rect& selection_rect,
int active_match_ordinal,
bool final_update) {
if (client_.get()) {
CefRefPtr<CefFindHandler> handler = client_->GetFindHandler();
if (handler.get()) {
CefRect rect(selection_rect.x(), selection_rect.y(),
selection_rect.width(), selection_rect.height());
handler->OnFindResult(this, request_id, number_of_matches, rect,
active_match_ordinal, final_update);
auto alloy_delegate =
static_cast<CefBrowserPlatformDelegateAlloy*>(platform_delegate());
if (alloy_delegate->HandleFindReply(request_id, number_of_matches,
selection_rect, active_match_ordinal,
final_update)) {
if (client_) {
if (auto handler = client_->GetFindHandler()) {
const auto& details = alloy_delegate->last_search_result();
CefRect rect(details.selection_rect().x(), details.selection_rect().y(),
details.selection_rect().width(),
details.selection_rect().height());
handler->OnFindResult(
this, details.request_id(), details.number_of_matches(), rect,
details.active_match_ordinal(), details.final_update());
}
}
}
}

View File

@@ -94,8 +94,7 @@ class AlloyBrowserHostImpl : public CefBrowserHostBase,
void PrintToPDF(const CefString& path,
const CefPdfPrintSettings& settings,
CefRefPtr<CefPdfPrintCallback> callback) override;
void Find(int identifier,
const CefString& searchText,
void Find(const CefString& searchText,
bool forward,
bool matchCase,
bool findNext) override;

View File

@@ -18,6 +18,8 @@
#include "base/logging.h"
#include "chrome/browser/printing/print_view_manager.h"
#include "chrome/browser/ui/prefs/prefs_tab_helper.h"
#include "components/find_in_page/find_tab_helper.h"
#include "components/find_in_page/find_types.h"
#include "components/zoom/zoom_controller.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
@@ -108,6 +110,7 @@ void CefBrowserPlatformDelegateAlloy::WebContentsCreated(
content::WebContents* web_contents,
bool owned) {
CefBrowserPlatformDelegate::WebContentsCreated(web_contents, owned);
find_in_page::FindTabHelper::CreateForWebContents(web_contents);
if (owned) {
SetOwnedWebContents(web_contents);
@@ -387,37 +390,47 @@ void CefBrowserPlatformDelegateAlloy::PrintToPDF(
settings, std::move(pdf_callback));
}
void CefBrowserPlatformDelegateAlloy::Find(int identifier,
const CefString& searchText,
void CefBrowserPlatformDelegateAlloy::Find(const CefString& searchText,
bool forward,
bool matchCase,
bool findNext) {
if (!web_contents_)
return;
// Every find request must have a unique ID and these IDs must strictly
// increase so that newer requests always have greater IDs than older
// requests.
if (identifier <= find_request_id_counter_)
identifier = ++find_request_id_counter_;
else
find_request_id_counter_ = identifier;
auto options = blink::mojom::FindOptions::New();
options->forward = forward;
options->match_case = matchCase;
options->find_match = findNext;
web_contents_->Find(identifier, searchText, std::move(options));
find_in_page::FindTabHelper::FromWebContents(web_contents_)
->StartFinding(searchText.ToString16(), forward, matchCase, findNext,
/*run_synchronously_for_testing=*/false);
}
void CefBrowserPlatformDelegateAlloy::StopFinding(bool clearSelection) {
if (!web_contents_)
return;
content::StopFindAction action =
clearSelection ? content::STOP_FIND_ACTION_CLEAR_SELECTION
: content::STOP_FIND_ACTION_KEEP_SELECTION;
web_contents_->StopFinding(action);
last_search_result_ = find_in_page::FindNotificationDetails();
find_in_page::FindTabHelper::FromWebContents(web_contents_)
->StopFinding(clearSelection ? find_in_page::SelectionAction::kClear
: find_in_page::SelectionAction::kKeep);
}
bool CefBrowserPlatformDelegateAlloy::HandleFindReply(
int request_id,
int number_of_matches,
const gfx::Rect& selection_rect,
int active_match_ordinal,
bool final_update) {
if (!web_contents_)
return false;
auto find_in_page =
find_in_page::FindTabHelper::FromWebContents(web_contents_);
find_in_page->HandleFindReply(request_id, number_of_matches, selection_rect,
active_match_ordinal, final_update);
if (!(find_in_page->find_result() == last_search_result_)) {
last_search_result_ = find_in_page->find_result();
return true;
}
return false;
}
base::RepeatingClosure

View File

@@ -10,6 +10,7 @@
#include "libcef/browser/web_contents_dialog_helper.h"
#include "base/memory/weak_ptr.h"
#include "components/find_in_page/find_notification_details.h"
#include "content/public/browser/web_contents.h"
#include "ui/gfx/geometry/size.h"
@@ -58,13 +59,23 @@ class CefBrowserPlatformDelegateAlloy : public CefBrowserPlatformDelegate {
void PrintToPDF(const CefString& path,
const CefPdfPrintSettings& settings,
CefRefPtr<CefPdfPrintCallback> callback) override;
void Find(int identifier,
const CefString& searchText,
void Find(const CefString& searchText,
bool forward,
bool matchCase,
bool findNext) override;
void StopFinding(bool clearSelection) override;
// Called from AlloyBrowserHostImpl::FindReply().
bool HandleFindReply(int request_id,
int number_of_matches,
const gfx::Rect& selection_rect,
int active_match_ordinal,
bool final_update);
const find_in_page::FindNotificationDetails& last_search_result() const {
return last_search_result_;
}
protected:
CefBrowserPlatformDelegateAlloy();
@@ -96,8 +107,9 @@ class CefBrowserPlatformDelegateAlloy : public CefBrowserPlatformDelegate {
// Used for the print preview dialog.
std::unique_ptr<CefWebContentsDialogHelper> web_contents_dialog_helper_;
// Used to provide unique incremental IDs for each find request.
int find_request_id_counter_ = 0;
// The last find result. This object contains details about the number of
// matches, the find selection rectangle, etc.
find_in_page::FindNotificationDetails last_search_result_;
// Used when the browser is hosting an extension.
extensions::ExtensionHost* extension_host_ = nullptr;

View File

@@ -381,8 +381,7 @@ void CefBrowserPlatformDelegate::PrintToPDF(
NOTIMPLEMENTED();
}
void CefBrowserPlatformDelegate::Find(int identifier,
const CefString& searchText,
void CefBrowserPlatformDelegate::Find(const CefString& searchText,
bool forward,
bool matchCase,
bool findNext) {

View File

@@ -353,8 +353,7 @@ class CefBrowserPlatformDelegate {
virtual void PrintToPDF(const CefString& path,
const CefPdfPrintSettings& settings,
CefRefPtr<CefPdfPrintCallback> callback);
virtual void Find(int identifier,
const CefString& searchText,
virtual void Find(const CefString& searchText,
bool forward,
bool matchCase,
bool findNext);

View File

@@ -215,8 +215,7 @@ void ChromeBrowserHostImpl::PrintToPDF(
callback->OnPdfPrintFinished(CefString(), false);
}
void ChromeBrowserHostImpl::Find(int identifier,
const CefString& searchText,
void ChromeBrowserHostImpl::Find(const CefString& searchText,
bool forward,
bool matchCase,
bool findNext) {

View File

@@ -74,8 +74,7 @@ class ChromeBrowserHostImpl : public CefBrowserHostBase {
void PrintToPDF(const CefString& path,
const CefPdfPrintSettings& settings,
CefRefPtr<CefPdfPrintCallback> callback) override;
void Find(int identifier,
const CefString& searchText,
void Find(const CefString& searchText,
bool forward,
bool matchCase,
bool findNext) override;