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

@ -33,7 +33,7 @@
// by hand. See the translator.README.txt file in the tools directory for
// more information.
//
// $hash=8af93d03e0b2a6b50d7612b145599600285b76d4$
// $hash=b80e84c0039ab45d5c4562d64b67a84766c0dab3$
//
#ifndef CEF_INCLUDE_CAPI_CEF_BROWSER_CAPI_H_
@ -450,18 +450,15 @@ typedef struct _cef_browser_host_t {
struct _cef_pdf_print_callback_t* callback);
///
// Search for |searchText|. |identifier| must be a unique ID and these IDs
// must strictly increase so that newer requests always have greater IDs than
// older requests. If |identifier| is zero or less than the previous ID value
// then it will be automatically assigned a new valid ID. |forward| indicates
// whether to search forward or backward within the page. |matchCase|
// indicates whether the search should be case-sensitive. |findNext| indicates
// whether this is the first request or a follow-up. The cef_find_handler_t
// instance, if any, returned via cef_client_t::GetFindHandler will be called
// to report find results.
// Search for |searchText|. |forward| indicates whether to search forward or
// backward within the page. |matchCase| indicates whether the search should
// be case-sensitive. |findNext| indicates whether this is the first request
// or a follow-up. The search will be restarted if |searchText| or |matchCase|
// change. The search will be stopped if |searchText| is NULL. The
// cef_find_handler_t instance, if any, returned via
// cef_client_t::GetFindHandler will be called to report find results.
///
void(CEF_CALLBACK* find)(struct _cef_browser_host_t* self,
int identifier,
const cef_string_t* searchText,
int forward,
int matchCase,

View File

@ -33,7 +33,7 @@
// by hand. See the translator.README.txt file in the tools directory for
// more information.
//
// $hash=b1fe7f7db5ab92c6ae64dc1288b6fd47c80f9423$
// $hash=f2e80b8637b07f19adea666e554269de4627e399$
//
#ifndef CEF_INCLUDE_CAPI_CEF_FIND_HANDLER_CAPI_H_
@ -59,11 +59,12 @@ typedef struct _cef_find_handler_t {
///
// Called to report find results returned by cef_browser_host_t::find().
// |identifer| is the identifier passed to find(), |count| is the number of
// matches currently identified, |selectionRect| is the location of where the
// match was found (in window coordinates), |activeMatchOrdinal| is the
// current position in the search results, and |finalUpdate| is true (1) if
// this is the last find notification.
// |identifer| is a unique incremental identifier for the currently active
// search, |count| is the number of matches currently identified,
// |selectionRect| is the location of where the match was found (in window
// coordinates), |activeMatchOrdinal| is the current position in the search
// results, and |finalUpdate| is true (1) if this is the last find
// notification.
///
void(CEF_CALLBACK* on_find_result)(struct _cef_find_handler_t* self,
struct _cef_browser_t* browser,

View File

@ -42,13 +42,13 @@
// way that may cause binary incompatibility with other builds. The universal
// hash value will change if any platform is affected whereas the platform hash
// values will change only if that particular platform is affected.
#define CEF_API_HASH_UNIVERSAL "a215713779f760e45d2dea503245d71c0d9d735b"
#define CEF_API_HASH_UNIVERSAL "bb546bf4f8f1347fc6f0811c6642be211e891073"
#if defined(OS_WIN)
#define CEF_API_HASH_PLATFORM "1cf09d99b2740035c8d6d4e19295ec3a8e6743d9"
#define CEF_API_HASH_PLATFORM "e8d30cbf12066e78df3a1d6ee433afb3a16dbe99"
#elif defined(OS_MAC)
#define CEF_API_HASH_PLATFORM "a8e86f2c291f4703ba97cdcef97d31ba0f37ea9d"
#define CEF_API_HASH_PLATFORM "1e7c396539f3d381d03e10d6b21e61ec5112bd76"
#elif defined(OS_LINUX)
#define CEF_API_HASH_PLATFORM "a00e1b122686da8eeca79967ea3bc5809e8707b1"
#define CEF_API_HASH_PLATFORM "89309607fa2eda0ad89bcfc619182bf67a249a8a"
#endif
#ifdef __cplusplus

View File

@ -479,19 +479,16 @@ class CefBrowserHost : public virtual CefBaseRefCounted {
CefRefPtr<CefPdfPrintCallback> callback) = 0;
///
// Search for |searchText|. |identifier| must be a unique ID and these IDs
// must strictly increase so that newer requests always have greater IDs than
// older requests. If |identifier| is zero or less than the previous ID value
// then it will be automatically assigned a new valid ID. |forward| indicates
// whether to search forward or backward within the page. |matchCase|
// indicates whether the search should be case-sensitive. |findNext| indicates
// whether this is the first request or a follow-up. The CefFindHandler
// instance, if any, returned via CefClient::GetFindHandler will be called to
// report find results.
// Search for |searchText|. |forward| indicates whether to search forward or
// backward within the page. |matchCase| indicates whether the search should
// be case-sensitive. |findNext| indicates whether this is the first request
// or a follow-up. The search will be restarted if |searchText| or |matchCase|
// change. The search will be stopped if |searchText| is empty. The
// CefFindHandler instance, if any, returned via CefClient::GetFindHandler
// will be called to report find results.
///
/*--cef()--*/
virtual void Find(int identifier,
const CefString& searchText,
virtual void Find(const CefString& searchText,
bool forward,
bool matchCase,
bool findNext) = 0;

View File

@ -50,11 +50,11 @@ class CefFindHandler : public virtual CefBaseRefCounted {
public:
///
// Called to report find results returned by CefBrowserHost::Find().
// |identifer| is the identifier passed to Find(), |count| is the number of
// matches currently identified, |selectionRect| is the location of where the
// match was found (in window coordinates), |activeMatchOrdinal| is the
// current position in the search results, and |finalUpdate| is true if this
// is the last find notification.
// |identifer| is a unique incremental identifier for the currently active
// search, |count| is the number of matches currently identified,
// |selectionRect| is the location of where the match was found (in window
// coordinates), |activeMatchOrdinal| is the current position in the search
// results, and |finalUpdate| is true if this is the last find notification.
///
/*--cef()--*/
virtual void OnFindResult(CefRefPtr<CefBrowser> browser,

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;

View File

@ -9,7 +9,7 @@
// implementations. See the translator.README.txt file in the tools directory
// for more information.
//
// $hash=7f4782003c961c3bda54d02556dace8d7b5e4e17$
// $hash=289bb032a65bfd1f35a97d93e14df1f5762a9394$
//
#include "libcef_dll/cpptoc/browser_host_cpptoc.h"
@ -445,7 +445,6 @@ browser_host_print_to_pdf(struct _cef_browser_host_t* self,
}
void CEF_CALLBACK browser_host_find(struct _cef_browser_host_t* self,
int identifier,
const cef_string_t* searchText,
int forward,
int matchCase,
@ -464,8 +463,8 @@ void CEF_CALLBACK browser_host_find(struct _cef_browser_host_t* self,
// Execute
CefBrowserHostCppToC::Get(self)->Find(
identifier, CefString(searchText), forward ? true : false,
matchCase ? true : false, findNext ? true : false);
CefString(searchText), forward ? true : false, matchCase ? true : false,
findNext ? true : false);
}
void CEF_CALLBACK browser_host_stop_finding(struct _cef_browser_host_t* self,

View File

@ -9,7 +9,7 @@
// implementations. See the translator.README.txt file in the tools directory
// for more information.
//
// $hash=80f473fbb920009cc911a60c2ed7517eeccad06e$
// $hash=2edab12ab1759213ab9a6b7620ea39a74291abc7$
//
#include "libcef_dll/ctocpp/browser_host_ctocpp.h"
@ -384,8 +384,7 @@ void CefBrowserHostCToCpp::PrintToPDF(const CefString& path,
}
NO_SANITIZE("cfi-icall")
void CefBrowserHostCToCpp::Find(int identifier,
const CefString& searchText,
void CefBrowserHostCToCpp::Find(const CefString& searchText,
bool forward,
bool matchCase,
bool findNext) {
@ -403,8 +402,7 @@ void CefBrowserHostCToCpp::Find(int identifier,
return;
// Execute
_struct->find(_struct, identifier, searchText.GetStruct(), forward, matchCase,
findNext);
_struct->find(_struct, searchText.GetStruct(), forward, matchCase, findNext);
}
NO_SANITIZE("cfi-icall")

View File

@ -9,7 +9,7 @@
// implementations. See the translator.README.txt file in the tools directory
// for more information.
//
// $hash=90e6257bcf2ef9af754ac47c5948ab8cc3171e21$
// $hash=6de4205143b6855e7ccf54da14a0494db0b4aaa3$
//
#ifndef CEF_LIBCEF_DLL_CTOCPP_BROWSER_HOST_CTOCPP_H_
@ -64,8 +64,7 @@ class CefBrowserHostCToCpp : public CefCToCppRefCounted<CefBrowserHostCToCpp,
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

@ -839,7 +839,7 @@ void RootWindowWin::OnFindEvent() {
find_what_last_ = find_buff_;
}
browser->GetHost()->Find(0, find_what,
browser->GetHost()->Find(find_what,
(find_state_.Flags & FR_DOWN) ? true : false,
match_case, find_next_);
if (!find_next_)