#include "content/browser/devtools/protocol/emulation_handler.h"
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "base/types/expected_macros.h"
#include "build/build_config.h"
#include "components/download/public/common/download_url_parameters.h"
#include "content/browser/compute_pressure/web_contents_pressure_manager_proxy.h"
#include "content/browser/device_posture/device_posture_provider_impl.h"
#include "content/browser/devtools/devtools_agent_host_impl.h"
#include "content/browser/generic_sensor/web_contents_sensor_provider_proxy.h"
#include "content/browser/idle/idle_manager_impl.h"
#include "content/browser/renderer_host/input/touch_emulator_impl.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/common/content_client.h"
#include "content/public/common/url_constants.h"
#include "net/http/http_util.h"
#include "services/device/public/cpp/generic_sensor/sensor_reading.h"
#include "services/device/public/cpp/geolocation/geoposition.h"
#include "services/device/public/mojom/geolocation_context.mojom.h"
#include "services/device/public/mojom/geoposition.mojom.h"
#include "services/device/public/mojom/pressure_manager.mojom.h"
#include "services/device/public/mojom/pressure_update.mojom.h"
#include "services/device/public/mojom/sensor.mojom-shared.h"
#include "services/network/public/cpp/client_hints.h"
#include "third_party/blink/public/mojom/device_posture/device_posture_provider.mojom.h"
#include "ui/display/mojom/screen_orientation.mojom.h"
#include "ui/events/gesture_detection/gesture_provider_config_helper.h"
namespace content {
namespace protocol {
namespace {
constexpr char kCommandIsOnlyAvailableAtTopTarget[] = …;
#if BUILDFLAG(ENABLE_COMPUTE_PRESSURE)
constexpr char kPressureSourceIsAlreadyOverridden[] = …;
constexpr char kPressureSourceIsNotOverridden[] = …;
#endif
constexpr char kSensorIsAlreadyOverridden[] = …;
constexpr char kSensorIsNotOverridden[] = …;
display::mojom::ScreenOrientation WebScreenOrientationTypeFromString(
const std::string& type) { … }
std::optional<content::DisplayFeature::Orientation>
DisplayFeatureOrientationTypeFromString(const std::string& type) { … }
base::expected<blink::mojom::DevicePostureType, protocol::Response>
DevicePostureTypeFromString(const std::string& type) { … }
ui::GestureProviderConfigType TouchEmulationConfigurationToType(
const std::string& protocol_value) { … }
bool ValidateClientHintString(const std::string& s) { … }
}
EmulationHandler::EmulationHandler()
: … { … }
EmulationHandler::~EmulationHandler() = default;
std::vector<EmulationHandler*> EmulationHandler::ForAgentHost(
DevToolsAgentHostImpl* host) { … }
void EmulationHandler::SetRenderer(int process_host_id,
RenderFrameHostImpl* frame_host) { … }
void EmulationHandler::Wire(UberDispatcher* dispatcher) { … }
Response EmulationHandler::Disable() { … }
namespace {
Response ConvertSensorType(const Emulation::SensorType& type,
device::mojom::SensorType* out_type) { … }
Response ConvertSensorReading(device::mojom::SensorType type,
Emulation::SensorReading* const reading,
device::SensorReading* out_reading) { … }
base::expected<device::mojom::VirtualSensorMetadataPtr, Response>
ParseSensorMetadata(Maybe<Emulation::SensorMetadata>& metadata) { … }
}
void EmulationHandler::GetOverriddenSensorInformation(
const Emulation::SensorType& type,
std::unique_ptr<GetOverriddenSensorInformationCallback> callback) { … }
Response EmulationHandler::SetSensorOverrideEnabled(
bool enabled,
const Emulation::SensorType& type,
Maybe<Emulation::SensorMetadata> metadata) { … }
void EmulationHandler::SetSensorOverrideReadings(
const Emulation::SensorType& type,
std::unique_ptr<Emulation::SensorReading> reading,
std::unique_ptr<SetSensorOverrideReadingsCallback> callback) { … }
#if BUILDFLAG(ENABLE_COMPUTE_PRESSURE)
namespace {
device::mojom::VirtualPressureSourceMetadataPtr ConvertPressureMetadata(
Maybe<Emulation::PressureMetadata>& metadata) { … }
Response ConvertPressureSource(const Emulation::PressureSource& source,
device::mojom::PressureSource* out_type) { … }
Response ConvertPressureState(const Emulation::PressureState& state,
device::mojom::PressureState* out_type) { … }
}
#endif
Response EmulationHandler::SetPressureSourceOverrideEnabled(
bool enabled,
const Emulation::PressureSource& source,
Maybe<Emulation::PressureMetadata> metadata) { … }
void EmulationHandler::SetPressureStateOverride(
const Emulation::PressureSource& source,
const Emulation::PressureState& state,
std::unique_ptr<SetPressureStateOverrideCallback> callback) { … }
Response EmulationHandler::SetIdleOverride(bool is_user_active,
bool is_screen_unlocked) { … }
Response EmulationHandler::ClearIdleOverride() { … }
Response EmulationHandler::SetGeolocationOverride(Maybe<double> latitude,
Maybe<double> longitude,
Maybe<double> accuracy) { … }
Response EmulationHandler::ClearGeolocationOverride() { … }
Response EmulationHandler::SetEmitTouchEventsForMouse(
bool enabled,
Maybe<std::string> configuration) { … }
Response EmulationHandler::CanEmulate(bool* result) { … }
Response EmulationHandler::SetDeviceMetricsOverride(
int width,
int height,
double device_scale_factor,
bool mobile,
Maybe<double> scale,
Maybe<int> screen_width,
Maybe<int> screen_height,
Maybe<int> position_x,
Maybe<int> position_y,
Maybe<bool> dont_set_visible_size,
Maybe<Emulation::ScreenOrientation> screen_orientation,
Maybe<protocol::Page::Viewport> viewport,
Maybe<protocol::Emulation::DisplayFeature> display_feature,
Maybe<protocol::Emulation::DevicePosture> device_posture) { … }
Response EmulationHandler::ClearDeviceMetricsOverride() { … }
Response EmulationHandler::SetVisibleSize(int width, int height) { … }
Response EmulationHandler::SetUserAgentOverride(
const std::string& user_agent,
Maybe<std::string> accept_language,
Maybe<std::string> platform,
Maybe<Emulation::UserAgentMetadata> ua_metadata_override) { … }
Response EmulationHandler::SetFocusEmulationEnabled(bool enabled) { … }
Response EmulationHandler::SetEmulatedMedia(
Maybe<std::string> media,
Maybe<protocol::Array<protocol::Emulation::MediaFeature>> features) { … }
blink::DeviceEmulationParams EmulationHandler::GetDeviceEmulationParams() { … }
void EmulationHandler::SetDeviceEmulationParams(
const blink::DeviceEmulationParams& params) { … }
WebContentsImpl* EmulationHandler::GetWebContents() { … }
void EmulationHandler::UpdateTouchEventEmulationState() { … }
void EmulationHandler::UpdateDeviceEmulationState() { … }
void EmulationHandler::UpdateDeviceEmulationStateForHost(
RenderWidgetHostImpl* render_widget_host) { … }
Response EmulationHandler::SetDevicePostureOverride(
std::unique_ptr<protocol::Emulation::DevicePosture> posture) { … }
Response EmulationHandler::ClearDevicePostureOverride() { … }
void EmulationHandler::ApplyOverrides(net::HttpRequestHeaders* headers,
bool* user_agent_overridden,
bool* accept_language_overridden) { … }
bool EmulationHandler::ApplyUserAgentMetadataOverrides(
std::optional<blink::UserAgentMetadata>* override_out) { … }
void EmulationHandler::ApplyNetworkOverridesForDownload(
download::DownloadUrlParameters* parameters) { … }
}
}