Skip to content

Commit

Permalink
sensors: Add content_shell code path for virtual sensors manipulation
Browse files Browse the repository at this point in the history
content_shell does not use ChromeDriver, so we need a separate
implementation for the testdriver.js commands we are adding.

This implementation is anchored on Blink's Internals system, which uses
the newly-added WebSensorProviderAutomation Mojo interface implemented
by content/web_test/browser's WebTestSensorProviderManager to translate
the virtual sensor calls into calls to WebContentsSensorProviderProxy
that will ultimately reach //services.

Bug: 1278377
Change-Id: I13f49d529b36916d1f98b4792839e818fd7e8bdb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4770865
Commit-Queue: Raphael Kubo Da Costa <raphael.kubo.da.costa@intel.com>
Reviewed-by: Matt Reynolds <mattreynolds@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1212214}
  • Loading branch information
rakuco authored and Chromium LUCI CQ committed Oct 19, 2023
1 parent 4c32af9 commit 143ff00
Show file tree
Hide file tree
Showing 21 changed files with 830 additions and 5 deletions.
2 changes: 2 additions & 0 deletions content/web_test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ static_library("web_test_browser") {
"browser/web_test_permission_manager.h",
"browser/web_test_push_messaging_service.cc",
"browser/web_test_push_messaging_service.h",
"browser/web_test_sensor_provider_manager.cc",
"browser/web_test_sensor_provider_manager.h",
"browser/web_test_shell_platform_delegate.cc",
"browser/web_test_shell_platform_delegate.h",
"browser/web_test_storage_access_manager.cc",
Expand Down
1 change: 1 addition & 0 deletions content/web_test/browser/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ include_rules = [
"+content/web_test/common",
"+device/bluetooth",
"+services/device/public/cpp/test",
"+services/device/public/mojom",
"+services/network/public/cpp",
"+services/network/public/mojom",
"+services/proxy_resolver",
Expand Down
15 changes: 15 additions & 0 deletions content/web_test/browser/web_test_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "content/web_test/browser/web_test_fedcm_manager.h"
#include "content/web_test/browser/web_test_origin_trial_throttle.h"
#include "content/web_test/browser/web_test_permission_manager.h"
#include "content/web_test/browser/web_test_sensor_provider_manager.h"
#include "content/web_test/browser/web_test_storage_access_manager.h"
#include "content/web_test/browser/web_test_tts_platform.h"
#include "content/web_test/common/web_test_bluetooth_fake_adapter_setter.mojom.h"
Expand Down Expand Up @@ -547,6 +548,9 @@ void WebTestContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
map->Add<blink::test::mojom::FederatedAuthRequestAutomation>(
base::BindRepeating(&WebTestContentBrowserClient::BindFedCmAutomation,
base::Unretained(this)));
map->Add<blink::test::mojom::WebSensorProviderAutomation>(base::BindRepeating(
&WebTestContentBrowserClient::BindWebSensorProviderAutomation,
base::Unretained(this)));
}

bool WebTestContentBrowserClient::CanAcceptUntrustedExchangesIfNeeded() {
Expand Down Expand Up @@ -607,6 +611,17 @@ void WebTestContentBrowserClient::BindFedCmAutomation(
std::move(receiver));
}

void WebTestContentBrowserClient::BindWebSensorProviderAutomation(
RenderFrameHost* render_frame_host,
mojo::PendingReceiver<blink::test::mojom::WebSensorProviderAutomation>
receiver) {
if (!sensor_provider_manager_) {
sensor_provider_manager_ = std::make_unique<WebTestSensorProviderManager>(
WebContents::FromRenderFrameHost(render_frame_host));
}
sensor_provider_manager_->Bind(std::move(receiver));
}

std::unique_ptr<LoginDelegate> WebTestContentBrowserClient::CreateLoginDelegate(
const net::AuthChallengeInfo& auth_info,
content::WebContents* web_contents,
Expand Down
8 changes: 8 additions & 0 deletions content/web_test/browser/web_test_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "third_party/blink/public/mojom/clipboard/clipboard.mojom.h"
#include "third_party/blink/public/mojom/cookie_manager/cookie_manager_automation.mojom-forward.h"
#include "third_party/blink/public/mojom/permissions/permission_automation.mojom-forward.h"
#include "third_party/blink/public/mojom/sensor/web_sensor_provider_automation.mojom-forward.h"
#include "third_party/blink/public/mojom/storage_access/storage_access_automation.mojom-forward.h"
#include "third_party/blink/public/mojom/webid/federated_auth_request_automation.mojom-forward.h"

Expand All @@ -38,6 +39,7 @@ class FakeBluetoothDelegate;
class MockBadgeService;
class MockClipboardHost;
class WebTestBrowserContext;
class WebTestSensorProviderManager;

class WebTestContentBrowserClient : public ShellContentBrowserClient {
public:
Expand Down Expand Up @@ -156,6 +158,11 @@ class WebTestContentBrowserClient : public ShellContentBrowserClient {
mojo::PendingReceiver<blink::test::mojom::FederatedAuthRequestAutomation>
receiver);

void BindWebSensorProviderAutomation(
RenderFrameHost* render_frame_host,
mojo::PendingReceiver<blink::test::mojom::WebSensorProviderAutomation>
receiver);

void BindWebTestControlHost(
int render_process_id,
mojo::PendingAssociatedReceiver<mojom::WebTestControlHost> receiver);
Expand All @@ -171,6 +178,7 @@ class WebTestContentBrowserClient : public ShellContentBrowserClient {
std::unique_ptr<FakeBluetoothDelegate> fake_bluetooth_delegate_;
std::unique_ptr<MockClipboardHost> mock_clipboard_host_;
std::unique_ptr<MockBadgeService> mock_badge_service_;
std::unique_ptr<WebTestSensorProviderManager> sensor_provider_manager_;
mojo::UniqueReceiverSet<blink::test::mojom::CookieManagerAutomation>
cookie_managers_;
mojo::UniqueReceiverSet<blink::test::mojom::FederatedAuthRequestAutomation>
Expand Down
99 changes: 99 additions & 0 deletions content/web_test/browser/web_test_sensor_provider_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/web_test/browser/web_test_sensor_provider_manager.h"

#include "base/notreached.h"
#include "content/browser/generic_sensor/web_contents_sensor_provider_proxy.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/web_contents.h"

namespace content {

WebTestSensorProviderManager::WebTestSensorProviderManager(
WebContents* web_contents)
: web_contents_(static_cast<WebContentsImpl*>(web_contents)->GetWeakPtr()) {
}

WebTestSensorProviderManager::~WebTestSensorProviderManager() = default;

void WebTestSensorProviderManager::Bind(
mojo::PendingReceiver<blink::test::mojom::WebSensorProviderAutomation>
receiver) {
receivers_.Add(this, std::move(receiver));
}

void WebTestSensorProviderManager::CreateVirtualSensor(
device::mojom::SensorType type,
device::mojom::VirtualSensorMetadataPtr metadata,
device::mojom::SensorProvider::CreateVirtualSensorCallback callback) {
if (!web_contents_) {
return;
}

if (sensor_overrides_.contains(type)) {
std::move(callback).Run(
device::mojom::CreateVirtualSensorResult::kSensorTypeAlreadyOverridden);
return;
}

auto virtual_sensor =
WebContentsSensorProviderProxy::GetOrCreate(web_contents_.get())
->CreateVirtualSensorForDevTools(type, std::move(metadata));
CHECK(virtual_sensor);
sensor_overrides_[type] = std::move(virtual_sensor);

std::move(callback).Run(device::mojom::CreateVirtualSensorResult::kSuccess);
}

void WebTestSensorProviderManager::UpdateVirtualSensor(
device::mojom::SensorType type,
const device::SensorReading& reading,
device::mojom::SensorProvider::UpdateVirtualSensorCallback callback) {
if (!web_contents_) {
return;
}

auto it = sensor_overrides_.find(type);
if (it == sensor_overrides_.end()) {
std::move(callback).Run(
device::mojom::UpdateVirtualSensorResult::kSensorTypeNotOverridden);
return;
}

it->second->UpdateVirtualSensor(std::move(reading), std::move(callback));
}

void WebTestSensorProviderManager::RemoveVirtualSensor(
device::mojom::SensorType type,
device::mojom::SensorProvider::RemoveVirtualSensorCallback callback) {
if (!web_contents_) {
return;
}

sensor_overrides_.erase(type);
std::move(callback).Run();
}

void WebTestSensorProviderManager::GetVirtualSensorInformation(
device::mojom::SensorType type,
device::mojom::SensorProvider::GetVirtualSensorInformationCallback
callback) {
if (!web_contents_) {
return;
}

auto it = sensor_overrides_.find(type);
if (it == sensor_overrides_.end()) {
std::move(callback).Run(
device::mojom::GetVirtualSensorInformationResult::NewError(
device::mojom::GetVirtualSensorInformationError::
kSensorTypeNotOverridden));
return;
}

it->second->GetVirtualSensorInformation(std::move(callback));
}

} // namespace content
62 changes: 62 additions & 0 deletions content/web_test/browser/web_test_sensor_provider_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_WEB_TEST_BROWSER_WEB_TEST_SENSOR_PROVIDER_MANAGER_H_
#define CONTENT_WEB_TEST_BROWSER_WEB_TEST_SENSOR_PROVIDER_MANAGER_H_

#include "base/containers/flat_map.h"
#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "services/device/public/mojom/sensor_provider.mojom-shared.h"
#include "third_party/blink/public/mojom/sensor/web_sensor_provider_automation.mojom.h"

namespace content {

class ScopedVirtualSensorForDevTools;
class WebContents;

// Implementation of the VirtualSensorProvider Mojo interface for use by
// Blink's InternalsSensor. It implements the required method calls exposed by
// testdriver.js by forwarding the calls to WebContentsSensorProviderProxy.
class WebTestSensorProviderManager
: public blink::test::mojom::WebSensorProviderAutomation {
public:
explicit WebTestSensorProviderManager(WebContents* web_contents);

WebTestSensorProviderManager(const WebTestSensorProviderManager&) = delete;
WebTestSensorProviderManager& operator=(const WebTestSensorProviderManager&) =
delete;

~WebTestSensorProviderManager() override;

void Bind(
mojo::PendingReceiver<blink::test::mojom::WebSensorProviderAutomation>
receiver);

// blink::mojom::SensorProviderAutomation overrides.
void CreateVirtualSensor(device::mojom::SensorType type,
device::mojom::VirtualSensorMetadataPtr metadata,
CreateVirtualSensorCallback callback) override;
void UpdateVirtualSensor(device::mojom::SensorType type,
const device::SensorReading& reading,
UpdateVirtualSensorCallback callback) override;
void RemoveVirtualSensor(device::mojom::SensorType type,
RemoveVirtualSensorCallback callback) override;
void GetVirtualSensorInformation(
device::mojom::SensorType type,
GetVirtualSensorInformationCallback callback) override;

private:
base::flat_map<device::mojom::SensorType,
std::unique_ptr<ScopedVirtualSensorForDevTools>>
sensor_overrides_;

mojo::ReceiverSet<blink::test::mojom::WebSensorProviderAutomation> receivers_;

base::WeakPtr<WebContents> web_contents_;
};

} // namespace content

#endif // CONTENT_WEB_TEST_BROWSER_WEB_TEST_SENSOR_PROVIDER_MANAGER_H_
1 change: 1 addition & 0 deletions third_party/blink/public/mojom/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ mojom("mojom_platform") {
"security_context/insecure_request_policy.mojom",
"selection_menu/selection_menu_behavior.mojom",
"sensor/web_sensor_provider.mojom",
"sensor/web_sensor_provider_automation.mojom",
"service_worker/controller_service_worker_mode.mojom",
"service_worker/dispatch_fetch_event_params.mojom",
"service_worker/service_worker_ancestor_frame_type.mojom",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

module blink.test.mojom;

import "services/device/public/mojom/sensor.mojom";
import "services/device/public/mojom/sensor_provider.mojom";

// Provides a way to control virtual sensors as described in
// https://w3c.github.io/sensors/#automation.
//
// This interface exists solely for the content_shell and InternalsSensor
// implementation, as the ChromeDriver-based code path uses CDP to achieve the
// same results.
//
// The method calls are supposed to duplicate the corresponding ones in the
// device.mojom.SensorProvider interface.
interface WebSensorProviderAutomation {
// Creates a new virtual sensor of a given |type|, or fails if one already
// exists.
CreateVirtualSensor(device.mojom.SensorType type,
device.mojom.VirtualSensorMetadata metadata) =>
(device.mojom.CreateVirtualSensorResult result);

// Updates readings for a virtual sensor of given |type|, or fails if one has
// not been created.
UpdateVirtualSensor(device.mojom.SensorType type,
device.mojom.SensorReadingRaw reading) =>
(device.mojom.UpdateVirtualSensorResult result);

// Removes a virtual sensor of given |type|, or does nothing if one has not
// been created.
RemoveVirtualSensor(device.mojom.SensorType type) => ();

// Retrieves information about a virtual sensor of given |type|. It fails if
// one has not been created. If the sensor is not active, all information
// will be null or zeroed out.
GetVirtualSensorInformation(device.mojom.SensorType type) =>
(device.mojom.GetVirtualSensorInformationResult result);
};
12 changes: 8 additions & 4 deletions third_party/blink/renderer/bindings/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,16 @@ generate_bindings("generate_bindings_all") {
targets += [ "dictionary" ]
outputs += generated_dictionary_sources_in_core +
generated_dictionary_sources_in_modules +
generated_dictionary_sources_for_testing_in_core
generated_dictionary_sources_for_testing_in_core +
generated_dictionary_sources_for_testing_in_modules
if (use_blink_extensions_chromeos) {
outputs += generated_dictionary_sources_in_extensions_chromeos
}
targets += [ "enumeration" ]
outputs += generated_enumeration_sources_in_core +
generated_enumeration_sources_in_modules +
generated_enumeration_sources_for_testing_in_core
generated_enumeration_sources_for_testing_in_core +
generated_enumeration_sources_for_testing_in_modules
if (use_blink_extensions_chromeos) {
outputs += generated_enumeration_sources_in_extensions_chromeos
}
Expand Down Expand Up @@ -332,12 +334,14 @@ action_with_pydeps("check_generated_file_list") {
"--kind",
"dictionary",
]
file_list += generated_dictionary_sources_for_testing_in_core
file_list += generated_dictionary_sources_for_testing_in_core +
generated_dictionary_sources_for_testing_in_modules
file_list += [
"--kind",
"enumeration",
]
file_list += generated_enumeration_sources_for_testing_in_core
file_list += generated_enumeration_sources_for_testing_in_core +
generated_enumeration_sources_for_testing_in_modules
file_list += [
"--kind",
"interface",
Expand Down
14 changes: 14 additions & 0 deletions third_party/blink/renderer/bindings/generated_in_modules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -3209,6 +3209,20 @@ generated_interface_extra_sources_in_modules = [

# Generated sources for testing

generated_dictionary_sources_for_testing_in_modules = [
"$root_gen_dir/third_party/blink/renderer/bindings/modules/v8/v8_create_virtual_sensor_options.cc",
"$root_gen_dir/third_party/blink/renderer/bindings/modules/v8/v8_create_virtual_sensor_options.h",
"$root_gen_dir/third_party/blink/renderer/bindings/modules/v8/v8_virtual_sensor_information.cc",
"$root_gen_dir/third_party/blink/renderer/bindings/modules/v8/v8_virtual_sensor_information.h",
"$root_gen_dir/third_party/blink/renderer/bindings/modules/v8/v8_virtual_sensor_reading.cc",
"$root_gen_dir/third_party/blink/renderer/bindings/modules/v8/v8_virtual_sensor_reading.h",
]

generated_enumeration_sources_for_testing_in_modules = [
"$root_gen_dir/third_party/blink/renderer/bindings/modules/v8/v8_virtual_sensor_type.cc",
"$root_gen_dir/third_party/blink/renderer/bindings/modules/v8/v8_virtual_sensor_type.h",
]

generated_interface_sources_for_testing_in_modules = [
"$root_gen_dir/third_party/blink/renderer/bindings/modules/v8/v8_internals.cc",
"$root_gen_dir/third_party/blink/renderer/bindings/modules/v8/v8_internals.h",
Expand Down
4 changes: 4 additions & 0 deletions third_party/blink/renderer/bindings/idl_in_modules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,10 @@ static_idl_files_in_modules_for_testing = [
"//third_party/blink/renderer/modules/peerconnection/testing/internals_rtc_certificate.idl",
"//third_party/blink/renderer/modules/peerconnection/testing/internals_rtc_peer_connection.idl",
"//third_party/blink/renderer/modules/permissions/testing/internals_permission.idl",
"//third_party/blink/renderer/modules/sensor/testing/internals_sensor.idl",
"//third_party/blink/renderer/modules/sensor/testing/create_virtual_sensor_options.idl",
"//third_party/blink/renderer/modules/sensor/testing/virtual_sensor_information.idl",
"//third_party/blink/renderer/modules/sensor/testing/virtual_sensor_reading.idl",
"//third_party/blink/renderer/modules/service_worker/testing/internals_service_worker.idl",
"//third_party/blink/renderer/modules/speech/testing/internals_speech_synthesis.idl",
"//third_party/blink/renderer/modules/vibration/testing/internals_vibration.idl",
Expand Down
4 changes: 3 additions & 1 deletion third_party/blink/renderer/bindings/modules/v8/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ source_set("testing") {
"//third_party/blink/renderer:inside_blink",
]

sources = generated_interface_sources_for_testing_in_modules +
sources = generated_dictionary_sources_for_testing_in_modules +
generated_enumeration_sources_for_testing_in_modules +
generated_interface_sources_for_testing_in_modules +
generated_interface_extra_sources_for_testing_in_modules

deps = [
Expand Down
2 changes: 2 additions & 0 deletions third_party/blink/renderer/modules/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ source_set("modules_testing") {
"peerconnection/testing/mock_rtp_sender.h",
"permissions/testing/internals_permission.cc",
"permissions/testing/internals_permission.h",
"sensor/testing/internals_sensor.cc",
"sensor/testing/internals_sensor.h",
"service_worker/testing/internals_service_worker.cc",
"service_worker/testing/internals_service_worker.h",
"speech/testing/internals_speech_synthesis.cc",
Expand Down

0 comments on commit 143ff00

Please sign in to comment.