-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sensors: Add content_shell code path for virtual sensors manipulation
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
Showing
21 changed files
with
830 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
content/web_test/browser/web_test_sensor_provider_manager.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
content/web_test/browser/web_test_sensor_provider_manager.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
third_party/blink/public/mojom/sensor/web_sensor_provider_automation.mojom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.