Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Side by Side Diff: content/browser/devtools/protocol/emulation_handler.cc

Issue 2548263002: [DevTools] Migrate dom, emulation, inspector, network, page and schema handlers to new generator. (Closed)
Patch Set: rebased atop of roll Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/devtools/protocol/emulation_handler.h" 5 #include "content/browser/devtools/protocol/emulation_handler.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
11 #include "content/browser/frame_host/render_frame_host_impl.h" 11 #include "content/browser/frame_host/render_frame_host_impl.h"
12 #include "content/browser/renderer_host/render_widget_host_impl.h" 12 #include "content/browser/renderer_host/render_widget_host_impl.h"
13 #include "content/browser/web_contents/web_contents_impl.h" 13 #include "content/browser/web_contents/web_contents_impl.h"
14 #include "content/common/view_messages.h" 14 #include "content/common/view_messages.h"
15 #include "content/public/common/url_constants.h" 15 #include "content/public/common/url_constants.h"
16 #include "device/geolocation/geolocation_service_context.h" 16 #include "device/geolocation/geolocation_service_context.h"
17 #include "device/geolocation/geoposition.h" 17 #include "device/geolocation/geoposition.h"
18 18
19 namespace content { 19 namespace content {
20 namespace devtools { 20 namespace protocol {
21 namespace emulation {
22 21
23 using Response = DevToolsProtocolClient::Response;
24 using GeolocationServiceContext = device::GeolocationServiceContext; 22 using GeolocationServiceContext = device::GeolocationServiceContext;
25 using Geoposition = device::Geoposition; 23 using Geoposition = device::Geoposition;
26 24
27 namespace { 25 namespace {
28 26
29 blink::WebScreenOrientationType WebScreenOrientationTypeFromString( 27 blink::WebScreenOrientationType WebScreenOrientationTypeFromString(
30 const std::string& type) { 28 const std::string& type) {
31 if (type == screen_orientation::kTypePortraitPrimary) 29 if (type == Emulation::ScreenOrientation::TypeEnum::PortraitPrimary)
32 return blink::WebScreenOrientationPortraitPrimary; 30 return blink::WebScreenOrientationPortraitPrimary;
33 if (type == screen_orientation::kTypePortraitSecondary) 31 if (type == Emulation::ScreenOrientation::TypeEnum::PortraitSecondary)
34 return blink::WebScreenOrientationPortraitSecondary; 32 return blink::WebScreenOrientationPortraitSecondary;
35 if (type == screen_orientation::kTypeLandscapePrimary) 33 if (type == Emulation::ScreenOrientation::TypeEnum::LandscapePrimary)
36 return blink::WebScreenOrientationLandscapePrimary; 34 return blink::WebScreenOrientationLandscapePrimary;
37 if (type == screen_orientation::kTypeLandscapeSecondary) 35 if (type == Emulation::ScreenOrientation::TypeEnum::LandscapeSecondary)
38 return blink::WebScreenOrientationLandscapeSecondary; 36 return blink::WebScreenOrientationLandscapeSecondary;
39 return blink::WebScreenOrientationUndefined; 37 return blink::WebScreenOrientationUndefined;
40 } 38 }
41 39
42 ui::GestureProviderConfigType TouchEmulationConfigurationToType( 40 ui::GestureProviderConfigType TouchEmulationConfigurationToType(
43 const std::string& protocol_value) { 41 const std::string& protocol_value) {
44 ui::GestureProviderConfigType result = 42 ui::GestureProviderConfigType result =
45 ui::GestureProviderConfigType::CURRENT_PLATFORM; 43 ui::GestureProviderConfigType::CURRENT_PLATFORM;
46 if (protocol_value == 44 if (protocol_value ==
47 set_touch_emulation_enabled::kConfigurationMobile) { 45 Emulation::SetTouchEmulationEnabled::ConfigurationEnum::Mobile) {
48 result = ui::GestureProviderConfigType::GENERIC_MOBILE; 46 result = ui::GestureProviderConfigType::GENERIC_MOBILE;
49 } 47 }
50 if (protocol_value == 48 if (protocol_value ==
51 set_touch_emulation_enabled::kConfigurationDesktop) { 49 Emulation::SetTouchEmulationEnabled::ConfigurationEnum::Desktop) {
52 result = ui::GestureProviderConfigType::GENERIC_DESKTOP; 50 result = ui::GestureProviderConfigType::GENERIC_DESKTOP;
53 } 51 }
54 return result; 52 return result;
55 } 53 }
56 54
57 } // namespace 55 } // namespace
58 56
59 EmulationHandler::EmulationHandler() 57 EmulationHandler::EmulationHandler()
60 : touch_emulation_enabled_(false), 58 : touch_emulation_enabled_(false),
61 device_emulation_enabled_(false), 59 device_emulation_enabled_(false),
62 host_(nullptr) { 60 host_(nullptr) {
63 } 61 }
64 62
65 EmulationHandler::~EmulationHandler() { 63 EmulationHandler::~EmulationHandler() {
66 } 64 }
67 65
68 void EmulationHandler::SetRenderFrameHost(RenderFrameHostImpl* host) { 66 void EmulationHandler::SetRenderFrameHost(RenderFrameHostImpl* host) {
69 if (host_ == host) 67 if (host_ == host)
70 return; 68 return;
71 69
72 host_ = host; 70 host_ = host;
73 UpdateTouchEventEmulationState(); 71 UpdateTouchEventEmulationState();
74 UpdateDeviceEmulationState(); 72 UpdateDeviceEmulationState();
75 } 73 }
76 74
77 void EmulationHandler::Detached() { 75 void EmulationHandler::Wire(UberDispatcher* dispatcher) {
76 Emulation::Dispatcher::wire(dispatcher, this);
77 }
78
79 Response EmulationHandler::Disable() {
78 touch_emulation_enabled_ = false; 80 touch_emulation_enabled_ = false;
79 device_emulation_enabled_ = false; 81 device_emulation_enabled_ = false;
80 UpdateTouchEventEmulationState(); 82 UpdateTouchEventEmulationState();
81 UpdateDeviceEmulationState(); 83 UpdateDeviceEmulationState();
84 return Response::OK();
82 } 85 }
83 86
84 Response EmulationHandler::SetGeolocationOverride( 87 Response EmulationHandler::SetGeolocationOverride(
85 double* latitude, double* longitude, double* accuracy) { 88 Maybe<double> latitude, Maybe<double> longitude, Maybe<double> accuracy) {
86 if (!GetWebContents()) 89 if (!GetWebContents())
87 return Response::InternalError("Could not connect to view"); 90 return Response::InternalError();
88 91
89 GeolocationServiceContext* geolocation_context = 92 GeolocationServiceContext* geolocation_context =
90 GetWebContents()->GetGeolocationServiceContext(); 93 GetWebContents()->GetGeolocationServiceContext();
91 std::unique_ptr<Geoposition> geoposition(new Geoposition()); 94 std::unique_ptr<Geoposition> geoposition(new Geoposition());
92 if (latitude && longitude && accuracy) { 95 if (latitude.isJust() && longitude.isJust() && accuracy.isJust()) {
93 geoposition->latitude = *latitude; 96 geoposition->latitude = latitude.fromJust();
94 geoposition->longitude = *longitude; 97 geoposition->longitude = longitude.fromJust();
95 geoposition->accuracy = *accuracy; 98 geoposition->accuracy = accuracy.fromJust();
96 geoposition->timestamp = base::Time::Now(); 99 geoposition->timestamp = base::Time::Now();
97 if (!geoposition->Validate()) { 100 if (!geoposition->Validate())
98 return Response::InternalError("Invalid geolocation"); 101 return Response::Error("Invalid geolocation");
99 }
100 } else { 102 } else {
101 geoposition->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; 103 geoposition->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE;
102 } 104 }
103 geolocation_context->SetOverride(std::move(geoposition)); 105 geolocation_context->SetOverride(std::move(geoposition));
104 return Response::OK(); 106 return Response::OK();
105 } 107 }
106 108
107 Response EmulationHandler::ClearGeolocationOverride() { 109 Response EmulationHandler::ClearGeolocationOverride() {
108 if (!GetWebContents()) 110 if (!GetWebContents())
109 return Response::InternalError("Could not connect to view"); 111 return Response::InternalError();
110 112
111 GeolocationServiceContext* geolocation_context = 113 GeolocationServiceContext* geolocation_context =
112 GetWebContents()->GetGeolocationServiceContext(); 114 GetWebContents()->GetGeolocationServiceContext();
113 geolocation_context->ClearOverride(); 115 geolocation_context->ClearOverride();
114 return Response::OK(); 116 return Response::OK();
115 } 117 }
116 118
117 Response EmulationHandler::SetTouchEmulationEnabled( 119 Response EmulationHandler::SetTouchEmulationEnabled(
118 bool enabled, const std::string* configuration) { 120 bool enabled, Maybe<std::string> configuration) {
119 touch_emulation_enabled_ = enabled; 121 touch_emulation_enabled_ = enabled;
120 touch_emulation_configuration_ = 122 touch_emulation_configuration_ = configuration.fromMaybe("");
121 configuration ? *configuration : std::string();
122 UpdateTouchEventEmulationState(); 123 UpdateTouchEventEmulationState();
123 return Response::FallThrough(); 124 return Response::FallThrough();
124 } 125 }
125 126
126 Response EmulationHandler::CanEmulate(bool* result) { 127 Response EmulationHandler::CanEmulate(bool* result) {
127 #if defined(OS_ANDROID) 128 #if defined(OS_ANDROID)
128 *result = false; 129 *result = false;
129 #else 130 #else
130 *result = true; 131 *result = true;
131 if (WebContentsImpl* web_contents = GetWebContents()) 132 if (WebContentsImpl* web_contents = GetWebContents())
132 *result &= !web_contents->GetVisibleURL().SchemeIs(kChromeDevToolsScheme); 133 *result &= !web_contents->GetVisibleURL().SchemeIs(kChromeDevToolsScheme);
133 if (host_ && host_->GetRenderWidgetHost()) 134 if (host_ && host_->GetRenderWidgetHost())
134 *result &= !host_->GetRenderWidgetHost()->auto_resize_enabled(); 135 *result &= !host_->GetRenderWidgetHost()->auto_resize_enabled();
135 #endif // defined(OS_ANDROID) 136 #endif // defined(OS_ANDROID)
136 return Response::OK(); 137 return Response::OK();
137 } 138 }
138 139
139 Response EmulationHandler::SetDeviceMetricsOverride( 140 Response EmulationHandler::SetDeviceMetricsOverride(
140 int width, 141 int width,
141 int height, 142 int height,
142 double device_scale_factor, 143 double device_scale_factor,
143 bool mobile, 144 bool mobile,
144 bool fit_window, 145 bool fit_window,
145 const double* optional_scale, 146 Maybe<double> scale,
146 const double* optional_offset_x, 147 Maybe<double> offset_x,
147 const double* optional_offset_y, 148 Maybe<double> offset_y,
148 const int* screen_width, 149 Maybe<int> screen_width,
149 const int* screen_height, 150 Maybe<int> screen_height,
150 const int* position_x, 151 Maybe<int> position_x,
151 const int* position_y, 152 Maybe<int> position_y,
152 const std::unique_ptr<base::DictionaryValue>& screen_orientation) { 153 Maybe<Emulation::ScreenOrientation> screen_orientation) {
153 const static int max_size = 10000000; 154 const static int max_size = 10000000;
154 const static double max_scale = 10; 155 const static double max_scale = 10;
155 const static int max_orientation_angle = 360; 156 const static int max_orientation_angle = 360;
156 157
157 if (!host_) 158 if (!host_)
158 return Response::InternalError("Could not connect to view"); 159 return Response::InternalError();
159 160
160 if (screen_width && screen_height && 161 if (screen_width.fromMaybe(0) < 0 || screen_height.fromMaybe(0) < 0 ||
161 (*screen_width < 0 || *screen_height < 0 || 162 screen_width.fromMaybe(0) > max_size ||
162 *screen_width > max_size || *screen_height > max_size)) { 163 screen_height.fromMaybe(0) > max_size) {
163 return Response::InvalidParams( 164 return Response::InvalidParams(
164 "Screen width and height values must be positive, not greater than " + 165 "Screen width and height values must be positive, not greater than " +
165 base::IntToString(max_size)); 166 base::IntToString(max_size));
166 } 167 }
167 168
168 if (screen_width && screen_height && position_x && position_y && 169 if (position_x.fromMaybe(0) < 0 || position_y.fromMaybe(0) < 0 ||
169 (*position_x < 0 || *position_y < 0 || 170 position_x.fromMaybe(0) > screen_width.fromMaybe(0) ||
170 *position_x > *screen_width || *position_y > *screen_height)) { 171 position_y.fromMaybe(0) > screen_height.fromMaybe(0)) {
171 return Response::InvalidParams("View position should be on the screen"); 172 return Response::InvalidParams("View position should be on the screen");
172 } 173 }
173 174
174 if (width < 0 || height < 0 || width > max_size || height > max_size) { 175 if (width < 0 || height < 0 || width > max_size || height > max_size) {
175 return Response::InvalidParams( 176 return Response::InvalidParams(
176 "Width and height values must be positive, not greater than " + 177 "Width and height values must be positive, not greater than " +
177 base::IntToString(max_size)); 178 base::IntToString(max_size));
178 } 179 }
179 180
180 if (device_scale_factor < 0) 181 if (device_scale_factor < 0)
181 return Response::InvalidParams("deviceScaleFactor must be non-negative"); 182 return Response::InvalidParams("deviceScaleFactor must be non-negative");
182 183
183 if (optional_scale && (*optional_scale <= 0 || *optional_scale > max_scale)) { 184 if (scale.fromMaybe(1) <= 0 || scale.fromMaybe(1) > max_scale) {
184 return Response::InvalidParams( 185 return Response::InvalidParams(
185 "scale must be positive, not greater than " + 186 "scale must be positive, not greater than " +
186 base::DoubleToString(max_scale)); 187 base::DoubleToString(max_scale));
187 } 188 }
188 189
189 blink::WebScreenOrientationType orientationType = 190 blink::WebScreenOrientationType orientationType =
190 blink::WebScreenOrientationUndefined; 191 blink::WebScreenOrientationUndefined;
191 int orientationAngle = 0; 192 int orientationAngle = 0;
192 if (screen_orientation) { 193 if (screen_orientation.isJust()) {
193 std::string orientationTypeString; 194 Emulation::ScreenOrientation* orientation = screen_orientation.fromJust();
194 if (!screen_orientation->GetString("type", &orientationTypeString)) { 195 orientationType = WebScreenOrientationTypeFromString(
195 return Response::InvalidParams( 196 orientation->GetType());
196 "Screen orientation type must be a string");
197 }
198 orientationType = WebScreenOrientationTypeFromString(orientationTypeString);
199 if (orientationType == blink::WebScreenOrientationUndefined) 197 if (orientationType == blink::WebScreenOrientationUndefined)
200 return Response::InvalidParams("Invalid screen orientation type value"); 198 return Response::InvalidParams("Invalid screen orientation type value");
201 199 orientationAngle = orientation->GetAngle();
202 if (!screen_orientation->GetInteger("angle", &orientationAngle)) {
203 return Response::InvalidParams(
204 "Screen orientation angle must be a number");
205 }
206 if (orientationAngle < 0 || orientationAngle >= max_orientation_angle) { 200 if (orientationAngle < 0 || orientationAngle >= max_orientation_angle) {
207 return Response::InvalidParams( 201 return Response::InvalidParams(
208 "Screen orientation angle must be non-negative, less than " + 202 "Screen orientation angle must be non-negative, less than " +
209 base::IntToString(max_orientation_angle)); 203 base::IntToString(max_orientation_angle));
210 } 204 }
211 } 205 }
212 206
213 blink::WebDeviceEmulationParams params; 207 blink::WebDeviceEmulationParams params;
214 params.screenPosition = mobile ? blink::WebDeviceEmulationParams::Mobile : 208 params.screenPosition = mobile ? blink::WebDeviceEmulationParams::Mobile :
215 blink::WebDeviceEmulationParams::Desktop; 209 blink::WebDeviceEmulationParams::Desktop;
216 if (screen_width && screen_height) 210 params.screenSize = blink::WebSize(screen_width.fromMaybe(0),
217 params.screenSize = blink::WebSize(*screen_width, *screen_height); 211 screen_height.fromMaybe(0));
218 if (position_x && position_y) 212 params.viewPosition = blink::WebPoint(position_x.fromMaybe(0),
219 params.viewPosition = blink::WebPoint(*position_x, *position_y); 213 position_y.fromMaybe(0));
220 params.deviceScaleFactor = device_scale_factor; 214 params.deviceScaleFactor = device_scale_factor;
221 params.viewSize = blink::WebSize(width, height); 215 params.viewSize = blink::WebSize(width, height);
222 params.fitToView = fit_window; 216 params.fitToView = fit_window;
223 params.scale = optional_scale ? *optional_scale : 1; 217 params.scale = scale.fromMaybe(1);
224 params.screenOrientationType = orientationType; 218 params.screenOrientationType = orientationType;
225 params.screenOrientationAngle = orientationAngle; 219 params.screenOrientationAngle = orientationAngle;
226 220
227 if (device_emulation_enabled_ && params == device_emulation_params_) 221 if (device_emulation_enabled_ && params == device_emulation_params_)
228 return Response::OK(); 222 return Response::OK();
229 223
230 device_emulation_enabled_ = true; 224 device_emulation_enabled_ = true;
231 device_emulation_params_ = params; 225 device_emulation_params_ = params;
232 UpdateDeviceEmulationState(); 226 UpdateDeviceEmulationState();
233 return Response::OK(); 227 return Response::OK();
234 } 228 }
235 229
236 Response EmulationHandler::ClearDeviceMetricsOverride() { 230 Response EmulationHandler::ClearDeviceMetricsOverride() {
237 if (!device_emulation_enabled_) 231 if (!device_emulation_enabled_)
238 return Response::OK(); 232 return Response::OK();
239 233
240 device_emulation_enabled_ = false; 234 device_emulation_enabled_ = false;
241 UpdateDeviceEmulationState(); 235 UpdateDeviceEmulationState();
242 return Response::OK(); 236 return Response::OK();
243 } 237 }
244 238
245 Response EmulationHandler::SetVisibleSize(int width, int height) { 239 Response EmulationHandler::SetVisibleSize(int width, int height) {
246 if (width < 0 || height < 0) 240 if (width < 0 || height < 0)
247 return Response::InvalidParams("Width and height must be non-negative"); 241 return Response::InvalidParams("Width and height must be non-negative");
248 242
249 // Set size of frame by resizing RWHV if available. 243 // Set size of frame by resizing RWHV if available.
250 RenderWidgetHostImpl* widget_host = 244 RenderWidgetHostImpl* widget_host =
251 host_ ? host_->GetRenderWidgetHost() : nullptr; 245 host_ ? host_->GetRenderWidgetHost() : nullptr;
252 if (!widget_host) 246 if (!widget_host)
253 return Response::ServerError("Target does not support setVisibleSize"); 247 return Response::Error("Target does not support setVisibleSize");
254 248
255 widget_host->GetView()->SetSize(gfx::Size(width, height)); 249 widget_host->GetView()->SetSize(gfx::Size(width, height));
256 return Response::OK(); 250 return Response::OK();
257 } 251 }
258 252
259 Response EmulationHandler::ForceViewport(double x, double y, double scale) {
260 return Response::FallThrough();
261 }
262
263 Response EmulationHandler::ResetViewport() {
264 return Response::FallThrough();
265 }
266
267 Response EmulationHandler::ResetPageScaleFactor() {
268 return Response::FallThrough();
269 }
270
271 Response EmulationHandler::SetPageScaleFactor(double page_scale_factor) {
272 return Response::FallThrough();
273 }
274
275 Response EmulationHandler::SetScriptExecutionDisabled(bool disabled) {
276 return Response::FallThrough();
277 }
278
279 Response EmulationHandler::SetEmulatedMedia(const std::string& media) {
280 return Response::FallThrough();
281 }
282
283 Response EmulationHandler::SetCPUThrottlingRate(double rate) {
284 return Response::FallThrough();
285 }
286
287 Response EmulationHandler::SetVirtualTimePolicy(
288 const std::string& policy,
289 const int* budget) {
290 return Response::FallThrough();
291 }
292
293 WebContentsImpl* EmulationHandler::GetWebContents() { 253 WebContentsImpl* EmulationHandler::GetWebContents() {
294 return host_ ? 254 return host_ ?
295 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost(host_)) : 255 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost(host_)) :
296 nullptr; 256 nullptr;
297 } 257 }
298 258
299 void EmulationHandler::UpdateTouchEventEmulationState() { 259 void EmulationHandler::UpdateTouchEventEmulationState() {
300 RenderWidgetHostImpl* widget_host = 260 RenderWidgetHostImpl* widget_host =
301 host_ ? host_->GetRenderWidgetHost() : nullptr; 261 host_ ? host_->GetRenderWidgetHost() : nullptr;
302 if (!widget_host) 262 if (!widget_host)
(...skipping 13 matching lines...) Expand all
316 return; 276 return;
317 if (device_emulation_enabled_) { 277 if (device_emulation_enabled_) {
318 widget_host->Send(new ViewMsg_EnableDeviceEmulation( 278 widget_host->Send(new ViewMsg_EnableDeviceEmulation(
319 widget_host->GetRoutingID(), device_emulation_params_)); 279 widget_host->GetRoutingID(), device_emulation_params_));
320 } else { 280 } else {
321 widget_host->Send(new ViewMsg_DisableDeviceEmulation( 281 widget_host->Send(new ViewMsg_DisableDeviceEmulation(
322 widget_host->GetRoutingID())); 282 widget_host->GetRoutingID()));
323 } 283 }
324 } 284 }
325 285
326 } // namespace emulation 286 } // namespace protocol
327 } // namespace devtools
328 } // namespace content 287 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/devtools/protocol/emulation_handler.h ('k') | content/browser/devtools/protocol/inspector_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698