1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199

content / browser / devtools / protocol / emulation_handler.h [blame]

// Copyright 2015 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_BROWSER_DEVTOOLS_PROTOCOL_EMULATION_HANDLER_H_
#define CONTENT_BROWSER_DEVTOOLS_PROTOCOL_EMULATION_HANDLER_H_

#include <memory>

#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "content/browser/devtools/protocol/devtools_domain_handler.h"
#include "content/browser/devtools/protocol/emulation.h"
#include "content/browser/devtools/protocol/protocol.h"
#include "services/device/public/cpp/compute_pressure/buildflags.h"
#include "services/device/public/mojom/pressure_update.mojom-shared.h"
#include "services/device/public/mojom/sensor.mojom-shared.h"
#include "services/device/public/mojom/sensor_provider.mojom-shared.h"
#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
#include "third_party/blink/public/common/widget/device_emulation_params.h"

namespace net {
class HttpRequestHeaders;
}  // namespace net

namespace download {
class DownloadUrlParameters;
}  // namespace download

namespace content {

class DevToolsAgentHostImpl;
class RenderFrameHostImpl;
class RenderWidgetHostImpl;
class ScopedVirtualSensorForDevTools;
class ScopedVirtualPressureSourceForDevTools;
class WebContentsImpl;

namespace protocol {

class EmulationHandler : public DevToolsDomainHandler,
                         public Emulation::Backend {
 public:
  EmulationHandler();

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

  ~EmulationHandler() override;

  static std::vector<EmulationHandler*> ForAgentHost(
      DevToolsAgentHostImpl* host);

  void Wire(UberDispatcher* dispatcher) override;
  void SetRenderer(int process_host_id,
                   RenderFrameHostImpl* frame_host) override;

  Response Disable() override;

  void GetOverriddenSensorInformation(
      const Emulation::SensorType& type,
      std::unique_ptr<GetOverriddenSensorInformationCallback>) override;
  Response SetSensorOverrideEnabled(
      bool enabled,
      const Emulation::SensorType& type,
      Maybe<Emulation::SensorMetadata> metadata) override;
  void SetSensorOverrideReadings(
      const Emulation::SensorType& type,
      std::unique_ptr<Emulation::SensorReading> reading,
      std::unique_ptr<SetSensorOverrideReadingsCallback>) override;

  Response SetIdleOverride(bool is_user_active,
                           bool is_screen_unlocked) override;
  Response ClearIdleOverride() override;

  Response SetGeolocationOverride(Maybe<double> latitude,
                                  Maybe<double> longitude,
                                  Maybe<double> accuracy) override;
  Response ClearGeolocationOverride() override;

  Response SetEmitTouchEventsForMouse(
      bool enabled,
      Maybe<std::string> configuration) override;

  Response SetUserAgentOverride(
      const std::string& user_agent,
      Maybe<std::string> accept_language,
      Maybe<std::string> platform,
      Maybe<Emulation::UserAgentMetadata> ua_metadata_override) override;

  Response CanEmulate(bool* result) override;
  Response SetDeviceMetricsOverride(
      int width,
      int height,
      double device_scale_factor,
      bool mobile,
      Maybe<double> scale,
      Maybe<int> screen_widget,
      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) override;
  Response ClearDeviceMetricsOverride() override;

  Response SetVisibleSize(int width, int height) override;

  Response SetFocusEmulationEnabled(bool) override;

  Response SetEmulatedMedia(
      Maybe<std::string> media,
      Maybe<protocol::Array<protocol::Emulation::MediaFeature>> features)
      override;

  blink::DeviceEmulationParams GetDeviceEmulationParams();
  void SetDeviceEmulationParams(const blink::DeviceEmulationParams& params);

  bool device_emulation_enabled() { return device_emulation_enabled_; }

  // Applies the network request header overrides on `headers`.  If the
  // User-Agent header was overridden, `user_agent_overridden` is set to true;
  // otherwise, it's set to false. If the Accept-Language header was overridden,
  // `accept_language_overridden` is set to true; otherwise, it's set to false.
  void ApplyOverrides(net::HttpRequestHeaders* headers,
                      bool* user_agent_overridden,
                      bool* accept_language_overridden);
  bool ApplyUserAgentMetadataOverrides(
      std::optional<blink::UserAgentMetadata>* override_out);
  void ApplyNetworkOverridesForDownload(
      download::DownloadUrlParameters* parameters);

 private:
  WebContentsImpl* GetWebContents();

  void UpdateTouchEventEmulationState();
  void UpdateDeviceEmulationState();
  void UpdateDeviceEmulationStateForHost(
      RenderWidgetHostImpl* render_widget_host);

  Response SetDevicePostureOverride(
      std::unique_ptr<protocol::Emulation::DevicePosture> posture) override;
  Response ClearDevicePostureOverride() override;

  Response SetPressureSourceOverrideEnabled(
      bool enabled,
      const Emulation::PressureSource& source,
      Maybe<Emulation::PressureMetadata> metadata) override;
  void SetPressureStateOverride(
      const Emulation::PressureSource& source,
      const Emulation::PressureState& state,
      std::unique_ptr<SetPressureStateOverrideCallback>) override;

  bool touch_emulation_enabled_;
  std::string touch_emulation_configuration_;
  bool device_emulation_enabled_;
  bool focus_emulation_enabled_;
  blink::DeviceEmulationParams device_emulation_params_;
  std::string user_agent_;

  // |user_agent_metadata_| is meaningful if |user_agent_| is non-empty.
  // In that case nullopt will disable sending of client hints, and a
  // non-nullopt value will be sent.
  std::optional<blink::UserAgentMetadata> user_agent_metadata_;
  std::string accept_language_;
  // If |prefers_color_scheme_| is either "light" or "dark", it is used to
  // override the "prefers-color-scheme" client hint header, when present.
  std::string prefers_color_scheme_;
  // If |prefers_reduced_motion_| is "reduce", it is used to override the
  // "prefers-reduced-motion" client hint header, when present.
  std::string prefers_reduced_motion_;
  // If |prefers_reduced_transparency_| is "reduce", it is used to override the
  // "prefers-reduced-transparency" client hint header, when present.
  std::string prefers_reduced_transparency_;

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

#if BUILDFLAG(ENABLE_COMPUTE_PRESSURE)
  base::flat_map<device::mojom::PressureSource,
                 std::unique_ptr<ScopedVirtualPressureSourceForDevTools>>
      pressure_overrides_;
#endif  // BUILDFLAG(ENABLE_COMPUTE_PRESSURE)

  // True when SetDevicePostureOverride() has been called.
  bool device_posture_emulation_enabled_ = false;

  raw_ptr<RenderFrameHostImpl> host_;

  base::ScopedClosureRunner capture_handle_;
};

}  // namespace protocol
}  // namespace content

#endif  // CONTENT_BROWSER_DEVTOOLS_PROTOCOL_EMULATION_HANDLER_H_