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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -9,7 +9,7 @@
// implementations. See the translator.README.txt file in the tools directory // implementations. See the translator.README.txt file in the tools directory
// for more information. // for more information.
// //
// $hash=7f4782003c961c3bda54d02556dace8d7b5e4e17$ // $hash=289bb032a65bfd1f35a97d93e14df1f5762a9394$
// //
#include "libcef_dll/cpptoc/browser_host_cpptoc.h" #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, void CEF_CALLBACK browser_host_find(struct _cef_browser_host_t* self,
int identifier,
const cef_string_t* searchText, const cef_string_t* searchText,
int forward, int forward,
int matchCase, int matchCase,
@ -464,8 +463,8 @@ void CEF_CALLBACK browser_host_find(struct _cef_browser_host_t* self,
// Execute // Execute
CefBrowserHostCppToC::Get(self)->Find( CefBrowserHostCppToC::Get(self)->Find(
identifier, CefString(searchText), forward ? true : false, CefString(searchText), forward ? true : false, matchCase ? true : false,
matchCase ? true : false, findNext ? true : false); findNext ? true : false);
} }
void CEF_CALLBACK browser_host_stop_finding(struct _cef_browser_host_t* self, 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 // implementations. See the translator.README.txt file in the tools directory
// for more information. // for more information.
// //
// $hash=80f473fbb920009cc911a60c2ed7517eeccad06e$ // $hash=2edab12ab1759213ab9a6b7620ea39a74291abc7$
// //
#include "libcef_dll/ctocpp/browser_host_ctocpp.h" #include "libcef_dll/ctocpp/browser_host_ctocpp.h"
@ -384,8 +384,7 @@ void CefBrowserHostCToCpp::PrintToPDF(const CefString& path,
} }
NO_SANITIZE("cfi-icall") NO_SANITIZE("cfi-icall")
void CefBrowserHostCToCpp::Find(int identifier, void CefBrowserHostCToCpp::Find(const CefString& searchText,
const CefString& searchText,
bool forward, bool forward,
bool matchCase, bool matchCase,
bool findNext) { bool findNext) {
@ -403,8 +402,7 @@ void CefBrowserHostCToCpp::Find(int identifier,
return; return;
// Execute // Execute
_struct->find(_struct, identifier, searchText.GetStruct(), forward, matchCase, _struct->find(_struct, searchText.GetStruct(), forward, matchCase, findNext);
findNext);
} }
NO_SANITIZE("cfi-icall") NO_SANITIZE("cfi-icall")

View File

@ -9,7 +9,7 @@
// implementations. See the translator.README.txt file in the tools directory // implementations. See the translator.README.txt file in the tools directory
// for more information. // for more information.
// //
// $hash=90e6257bcf2ef9af754ac47c5948ab8cc3171e21$ // $hash=6de4205143b6855e7ccf54da14a0494db0b4aaa3$
// //
#ifndef CEF_LIBCEF_DLL_CTOCPP_BROWSER_HOST_CTOCPP_H_ #ifndef CEF_LIBCEF_DLL_CTOCPP_BROWSER_HOST_CTOCPP_H_
@ -64,8 +64,7 @@ class CefBrowserHostCToCpp : public CefCToCppRefCounted<CefBrowserHostCToCpp,
void PrintToPDF(const CefString& path, void PrintToPDF(const CefString& path,
const CefPdfPrintSettings& settings, const CefPdfPrintSettings& settings,
CefRefPtr<CefPdfPrintCallback> callback) override; CefRefPtr<CefPdfPrintCallback> callback) override;
void Find(int identifier, void Find(const CefString& searchText,
const CefString& searchText,
bool forward, bool forward,
bool matchCase, bool matchCase,
bool findNext) override; bool findNext) override;

View File

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