Commit 58f116da authored by Mario Sanchez Prada's avatar Mario Sanchez Prada Committed by Commit Bot

Migrate blink::mojom::UnhandledTapNotifier to BrowserInterfaceBroker

This CL migrates the GestureManager mojo client in Blink to the new
BrowserInterfaceBroker, and adapts the code in the browser process
to register the binder from PopulateChromeFrameBinders(), instead of
doing it right inside UnhandledTapWebContentsObserver.
UnhandledTapNotifierImpl.

Additionally it also adapts ShowUnhandledTapTest helper class so
that it properly set the mock overrides for BrowserInterfaceBroker,
as it was done before for InterfaceProvider.

Bug: 936482,1006361
Change-Id: I9f369e41cb8441043646b34fd67e1902f7010383
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1887615
Commit-Queue: Mario Sanchez Prada <mario@igalia.com>
Reviewed-by: default avatarOksana Zhuravlova <oksamyt@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarDonn Denman <donnd@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712780}
parent 42710d41
......@@ -18,6 +18,7 @@
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;
using contextual_search::UnhandledTapWebContentsObserver;
ContextualSearchTabHelper::ContextualSearchTabHelper(JNIEnv* env,
jobject obj,
......@@ -62,15 +63,21 @@ void ContextualSearchTabHelper::InstallUnhandledTapNotifierIfNeeded(
content::WebContents* base_web_contents =
content::WebContents::FromJavaWebContents(j_base_web_contents);
DCHECK(base_web_contents);
if (!unhandled_tap_web_contents_observer_ ||
base_web_contents !=
unhandled_tap_web_contents_observer_->web_contents()) {
unhandled_tap_web_contents_observer_.reset(
new contextual_search::UnhandledTapWebContentsObserver(
base_web_contents, device_scale_factor,
base::BindRepeating(
&ContextualSearchTabHelper::OnShowUnhandledTapUIIfNeeded,
weak_factory_.GetWeakPtr())));
if (!UnhandledTapWebContentsObserver::FromWebContents(base_web_contents)) {
// Create an UnhandledTapWebContentsObserver owned by |base_web_contents|.
UnhandledTapWebContentsObserver::CreateForWebContents(base_web_contents);
// As per WebContentsUserData::CreateForWebContents(), the constructor of
// UnhandledTapWebContentsObserver must only accept one parameter holding a
// pointer to the WebContents that will own it (i.e. |base_web_contents|),
// forcing us to defer the rest of the initialization to the setters below.
auto* utwc_observer =
UnhandledTapWebContentsObserver::FromWebContents(base_web_contents);
utwc_observer->set_device_scale_factor(device_scale_factor);
utwc_observer->set_unhandled_tap_callback(base::BindRepeating(
&ContextualSearchTabHelper::OnShowUnhandledTapUIIfNeeded,
weak_factory_.GetWeakPtr()));
}
}
......
......@@ -15,10 +15,6 @@
class Profile;
namespace contextual_search {
class UnhandledTapWebContentsObserver;
}
class ContextualSearchTabHelper {
public:
ContextualSearchTabHelper(JNIEnv* env, jobject obj, Profile* profile);
......@@ -50,11 +46,6 @@ class ContextualSearchTabHelper {
JavaObjectWeakGlobalRef weak_java_ref_;
std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
// The unhandled tap WebContentsObserver for the current tab.
// Installs a mojo handler for ShowUnhandledTapUIIfNeeded.
std::unique_ptr<contextual_search::UnhandledTapWebContentsObserver>
unhandled_tap_web_contents_observer_;
base::WeakPtrFactory<ContextualSearchTabHelper> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(ContextualSearchTabHelper);
};
......
......@@ -4,39 +4,14 @@
#include "chrome/browser/android/contextualsearch/unhandled_tap_web_contents_observer.h"
#include <utility>
#include "base/bind.h"
#include "build/build_config.h"
#include "third_party/blink/public/public_buildflags.h"
#if BUILDFLAG(ENABLE_UNHANDLED_TAP)
#include "chrome/browser/android/contextualsearch/unhandled_tap_notifier_impl.h"
#endif // BUILDFLAG(ENABLE_UNHANDLED_TAP)
namespace contextual_search {
UnhandledTapWebContentsObserver::UnhandledTapWebContentsObserver(
content::WebContents* web_contents,
float device_scale_factor,
UnhandledTapCallback callback)
: content::WebContentsObserver(web_contents) {
#if BUILDFLAG(ENABLE_UNHANDLED_TAP)
registry_.AddInterface(
base::BindRepeating(&contextual_search::CreateUnhandledTapNotifierImpl,
device_scale_factor, std::move(callback)));
#endif // BUILDFLAG(ENABLE_UNHANDLED_TAP)
}
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) {}
UnhandledTapWebContentsObserver::~UnhandledTapWebContentsObserver() {}
void UnhandledTapWebContentsObserver::OnInterfaceRequestFromFrame(
content::RenderFrameHost* render_frame_host,
const std::string& interface_name,
mojo::ScopedMessagePipeHandle* interface_pipe) {
#if BUILDFLAG(ENABLE_UNHANDLED_TAP)
registry_.TryBindInterface(interface_name, interface_pipe);
#endif // BUILDFLAG(ENABLE_UNHANDLED_TAP)
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(UnhandledTapWebContentsObserver)
} // namespace contextual_search
......@@ -6,9 +6,8 @@
#define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_UNHANDLED_TAP_WEB_CONTENTS_OBSERVER_H_
#include "base/macros.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents_observer.h"
#include "services/service_manager/public/cpp/binder_registry.h"
#include "content/public/browser/web_contents_user_data.h"
namespace contextual_search {
......@@ -18,27 +17,39 @@ typedef base::RepeatingCallback<
// Binds a Mojo unhandled-tap notifier message-handler to the frame host
// observed by this observer.
class UnhandledTapWebContentsObserver : public content::WebContentsObserver {
class UnhandledTapWebContentsObserver
: public content::WebContentsObserver,
public content::WebContentsUserData<UnhandledTapWebContentsObserver> {
public:
// Creates an observer for the given |web_contents| that binds a Mojo request
// for an endpoint to the UnhandledTapNotifier service. This will create an
// instance of the contextual_search::CreateUnhandledTapNotifierImpl to handle
// those messages. May use the given |scale_factor| to convert from dips to
// pixels for tap coordinates when calling back through the given |callback|.
UnhandledTapWebContentsObserver(content::WebContents* web_contents,
float device_scale_factor,
UnhandledTapCallback callback);
// those messages.
explicit UnhandledTapWebContentsObserver(content::WebContents* web_contents);
~UnhandledTapWebContentsObserver() override;
void set_device_scale_factor(float factor) { device_scale_factor_ = factor; }
float device_scale_factor() const { return device_scale_factor_; }
void set_unhandled_tap_callback(UnhandledTapCallback callback) {
unhandled_tap_callback_ = callback;
}
UnhandledTapCallback unhandled_tap_callback() const {
return unhandled_tap_callback_;
}
private:
// content::WebContentsObserver implementation.
void OnInterfaceRequestFromFrame(
content::RenderFrameHost* render_frame_host,
const std::string& interface_name,
mojo::ScopedMessagePipeHandle* interface_pipe) override;
friend class content::WebContentsUserData<UnhandledTapWebContentsObserver>;
// Scale factor to convert from dips to pixels for tap coordinates when
// calling back through the given |unhandled_tap_callback_|.
float device_scale_factor_;
UnhandledTapCallback unhandled_tap_callback_;
service_manager::BinderRegistry registry_;
WEB_CONTENTS_USER_DATA_KEY_DECL();
DISALLOW_COPY_AND_ASSIGN(UnhandledTapWebContentsObserver);
};
......
......@@ -4,6 +4,8 @@
#include "chrome/browser/chrome_browser_interface_binders.h"
#include <utility>
#include "base/feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/accessibility/accessibility_labels_service.h"
......@@ -21,6 +23,13 @@
#include "third_party/blink/public/mojom/insecure_input/insecure_input_service.mojom.h"
#include "third_party/blink/public/mojom/loader/navigation_predictor.mojom.h"
#include "third_party/blink/public/mojom/payments/payment_request.mojom.h"
#include "third_party/blink/public/public_buildflags.h"
#if BUILDFLAG(ENABLE_UNHANDLED_TAP)
#include "chrome/browser/android/contextualsearch/unhandled_tap_notifier_impl.h"
#include "chrome/browser/android/contextualsearch/unhandled_tap_web_contents_observer.h"
#include "third_party/blink/public/mojom/unhandled_tap_notifier/unhandled_tap_notifier.mojom.h"
#endif // BUILDFLAG(ENABLE_UNHANDLED_TAP)
#if defined(OS_ANDROID)
#include "content/public/browser/web_contents.h"
......@@ -38,6 +47,20 @@
namespace chrome {
namespace internal {
#if BUILDFLAG(ENABLE_UNHANDLED_TAP)
void BindUnhandledTapWebContentsObserver(
content::RenderFrameHost* const host,
mojo::PendingReceiver<blink::mojom::UnhandledTapNotifier> receiver) {
auto* unhandled_tap_notifier_observer =
contextual_search::UnhandledTapWebContentsObserver::FromWebContents(
content::WebContents::FromRenderFrameHost(host));
contextual_search::CreateUnhandledTapNotifierImpl(
unhandled_tap_notifier_observer->device_scale_factor(),
unhandled_tap_notifier_observer->unhandled_tap_callback(),
std::move(receiver));
}
#endif // BUILDFLAG(ENABLE_UNHANDLED_TAP)
// Forward image Annotator requests to the profile's AccessibilityLabelsService.
void BindImageAnnotator(
content::RenderFrameHost* const frame_host,
......@@ -92,6 +115,12 @@ void PopulateChromeFrameBinders(
}
map->Add<blink::mojom::ShareService>(base::BindRepeating(
&ForwardToJavaWebContents<blink::mojom::ShareService>));
#if BUILDFLAG(ENABLE_UNHANDLED_TAP)
map->Add<blink::mojom::UnhandledTapNotifier>(
base::BindRepeating(&BindUnhandledTapWebContentsObserver));
#endif // BUILDFLAG(ENABLE_UNHANDLED_TAP)
#if defined(ENABLE_SPATIAL_NAVIGATION_HOST)
map->Add<blink::mojom::SpatialNavigationHost>(base::BindRepeating(
&ForwardToJavaWebContents<blink::mojom::SpatialNavigationHost>));
......
......@@ -206,7 +206,6 @@ const service_manager::Manifest& GetContentBrowserManifest() {
"blink.mojom.PrefetchURLLoaderService",
"blink.mojom.QuotaDispatcherHost",
"blink.mojom.SharedWorkerConnector",
"blink.mojom.UnhandledTapNotifier",
"content.mojom.BrowserTarget",
"content.mojom.InputInjector",
"content.mojom.RendererAudioInputStreamFactory",
......
......@@ -46,6 +46,7 @@
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
#include "third_party/blink/public/common/page/page_zoom.h"
#include "third_party/blink/public/mojom/frame/document_interface_broker.mojom-blink.h"
......@@ -4372,16 +4373,6 @@ class MojoTestHelper {
web_view_helper_.Reset(); // Remove dependency on locally scoped client.
}
// Bind the test API to a service with the given |name| and repeating Bind
// method given by |callback|.
void BindTestApi(
const String& name,
base::RepeatingCallback<void(mojo::ScopedMessagePipeHandle)> callback) {
// Set up our Mock Mojo API.
test_api_.reset(new service_manager::InterfaceProvider::TestApi(
web_frame_client_.GetInterfaceProvider()));
test_api_->SetBinderForName(name.Utf8(), callback);
}
WebViewImpl* WebView() const { return web_view_; }
private:
......@@ -4450,10 +4441,22 @@ class ShowUnhandledTapTest : public WebViewTest {
WebWidget::LifecycleUpdateReason::kTest);
RunPendingTasks();
mojo_test_helper_->BindTestApi(
mojom::blink::UnhandledTapNotifier::Name_,
WTF::BindRepeating(&MockUnhandledTapNotifierImpl::Bind,
WTF::Unretained(&mock_notifier_)));
WebLocalFrameImpl* web_local_frame = web_view_->MainFrameImpl();
web_local_frame->GetFrame()
->GetBrowserInterfaceBroker()
.SetBinderForTesting(
mojom::blink::UnhandledTapNotifier::Name_,
WTF::BindRepeating(&MockUnhandledTapNotifierImpl::Bind,
WTF::Unretained(&mock_notifier_)));
}
void TearDown() override {
WebLocalFrameImpl* web_local_frame = web_view_->MainFrameImpl();
web_local_frame->GetFrame()
->GetBrowserInterfaceBroker()
.SetBinderForTesting(mojom::blink::UnhandledTapNotifier::Name_, {});
WebViewTest::TearDown();
}
protected:
......
......@@ -26,7 +26,7 @@
#include "third_party/blink/renderer/core/scroll/scroll_animator_base.h"
#if BUILDFLAG(ENABLE_UNHANDLED_TAP)
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
#include "third_party/blink/public/mojom/unhandled_tap_notifier/unhandled_tap_notifier.mojom-blink.h"
#include "third_party/blink/public/web/web_node.h"
#include "third_party/blink/renderer/core/editing/frame_selection.h"
......@@ -472,7 +472,7 @@ void GestureManager::ShowUnhandledTapUIIfNeeded(
if (should_trigger) {
// Start setting up the Mojo interface connection.
mojo::Remote<mojom::blink::UnhandledTapNotifier> provider;
frame_->Client()->GetInterfaceProvider()->GetInterface(
frame_->GetBrowserInterfaceBroker().GetInterface(
provider.BindNewPipeAndPassReceiver());
// Extract text run-length.
......
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