OLD | NEW |
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 "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "content/browser/frame_host/render_frame_host_impl.h" |
8 #include "content/browser/geolocation/geolocation_service_context.h" | 9 #include "content/browser/geolocation/geolocation_service_context.h" |
9 #include "content/browser/renderer_host/render_view_host_impl.h" | 10 #include "content/browser/renderer_host/render_widget_host_impl.h" |
10 #include "content/browser/web_contents/web_contents_impl.h" | 11 #include "content/browser/web_contents/web_contents_impl.h" |
11 #include "content/common/view_messages.h" | 12 #include "content/common/view_messages.h" |
12 #include "content/public/common/url_constants.h" | 13 #include "content/public/common/url_constants.h" |
13 | 14 |
14 namespace content { | 15 namespace content { |
15 namespace devtools { | 16 namespace devtools { |
16 namespace emulation { | 17 namespace emulation { |
17 | 18 |
18 using Response = DevToolsProtocolClient::Response; | 19 using Response = DevToolsProtocolClient::Response; |
19 | 20 |
(...skipping 25 matching lines...) Expand all Loading... |
45 page_handler->SetScreencastListener(this); | 46 page_handler->SetScreencastListener(this); |
46 } | 47 } |
47 | 48 |
48 EmulationHandler::~EmulationHandler() { | 49 EmulationHandler::~EmulationHandler() { |
49 } | 50 } |
50 | 51 |
51 void EmulationHandler::ScreencastEnabledChanged() { | 52 void EmulationHandler::ScreencastEnabledChanged() { |
52 UpdateTouchEventEmulationState(); | 53 UpdateTouchEventEmulationState(); |
53 } | 54 } |
54 | 55 |
55 void EmulationHandler::SetRenderViewHost(RenderViewHostImpl* host) { | 56 void EmulationHandler::SetRenderFrameHost(RenderFrameHostImpl* host) { |
56 if (host_ == host) | 57 if (host_ == host) |
57 return; | 58 return; |
58 | 59 |
59 host_ = host; | 60 host_ = host; |
60 UpdateTouchEventEmulationState(); | 61 UpdateTouchEventEmulationState(); |
61 UpdateDeviceEmulationState(); | 62 UpdateDeviceEmulationState(); |
62 } | 63 } |
63 | 64 |
64 void EmulationHandler::Detached() { | 65 void EmulationHandler::Detached() { |
65 touch_emulation_enabled_ = false; | 66 touch_emulation_enabled_ = false; |
66 device_emulation_enabled_ = false; | 67 device_emulation_enabled_ = false; |
67 UpdateTouchEventEmulationState(); | 68 UpdateTouchEventEmulationState(); |
68 UpdateDeviceEmulationState(); | 69 UpdateDeviceEmulationState(); |
69 } | 70 } |
70 | 71 |
71 Response EmulationHandler::SetGeolocationOverride( | 72 Response EmulationHandler::SetGeolocationOverride( |
72 double* latitude, double* longitude, double* accuracy) { | 73 double* latitude, double* longitude, double* accuracy) { |
73 if (!host_) | 74 if (!GetWebContents()) |
74 return Response::InternalError("Could not connect to view"); | 75 return Response::InternalError("Could not connect to view"); |
75 | 76 |
76 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( | |
77 WebContents::FromRenderViewHost(host_)); | |
78 if (!web_contents) | |
79 return Response::InternalError("No WebContents to override"); | |
80 | |
81 GeolocationServiceContext* geolocation_context = | 77 GeolocationServiceContext* geolocation_context = |
82 web_contents->GetGeolocationServiceContext(); | 78 GetWebContents()->GetGeolocationServiceContext(); |
83 scoped_ptr<Geoposition> geoposition(new Geoposition()); | 79 scoped_ptr<Geoposition> geoposition(new Geoposition()); |
84 if (latitude && longitude && accuracy) { | 80 if (latitude && longitude && accuracy) { |
85 geoposition->latitude = *latitude; | 81 geoposition->latitude = *latitude; |
86 geoposition->longitude = *longitude; | 82 geoposition->longitude = *longitude; |
87 geoposition->accuracy = *accuracy; | 83 geoposition->accuracy = *accuracy; |
88 geoposition->timestamp = base::Time::Now(); | 84 geoposition->timestamp = base::Time::Now(); |
89 if (!geoposition->Validate()) { | 85 if (!geoposition->Validate()) { |
90 return Response::InternalError("Invalid geolocation"); | 86 return Response::InternalError("Invalid geolocation"); |
91 } | 87 } |
92 } else { | 88 } else { |
93 geoposition->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 89 geoposition->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
94 } | 90 } |
95 geolocation_context->SetOverride(geoposition.Pass()); | 91 geolocation_context->SetOverride(geoposition.Pass()); |
96 return Response::OK(); | 92 return Response::OK(); |
97 } | 93 } |
98 | 94 |
99 Response EmulationHandler::ClearGeolocationOverride() { | 95 Response EmulationHandler::ClearGeolocationOverride() { |
100 if (!host_) | 96 if (!GetWebContents()) |
101 return Response::InternalError("Could not connect to view"); | 97 return Response::InternalError("Could not connect to view"); |
102 | 98 |
103 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( | |
104 WebContents::FromRenderViewHost(host_)); | |
105 if (!web_contents) | |
106 return Response::InternalError("No WebContents to override"); | |
107 | |
108 GeolocationServiceContext* geolocation_context = | 99 GeolocationServiceContext* geolocation_context = |
109 web_contents->GetGeolocationServiceContext(); | 100 GetWebContents()->GetGeolocationServiceContext(); |
110 geolocation_context->ClearOverride(); | 101 geolocation_context->ClearOverride(); |
111 return Response::OK(); | 102 return Response::OK(); |
112 } | 103 } |
113 | 104 |
114 Response EmulationHandler::SetTouchEmulationEnabled( | 105 Response EmulationHandler::SetTouchEmulationEnabled( |
115 bool enabled, const std::string* configuration) { | 106 bool enabled, const std::string* configuration) { |
116 touch_emulation_enabled_ = enabled; | 107 touch_emulation_enabled_ = enabled; |
117 touch_emulation_configuration_ = | 108 touch_emulation_configuration_ = |
118 configuration ? *configuration : std::string(); | 109 configuration ? *configuration : std::string(); |
119 UpdateTouchEventEmulationState(); | 110 UpdateTouchEventEmulationState(); |
120 return Response::FallThrough(); | 111 return Response::FallThrough(); |
121 } | 112 } |
122 | 113 |
123 Response EmulationHandler::CanEmulate(bool* result) { | 114 Response EmulationHandler::CanEmulate(bool* result) { |
124 #if defined(OS_ANDROID) | 115 #if defined(OS_ANDROID) |
125 *result = false; | 116 *result = false; |
126 #else | 117 #else |
127 if (host_) { | 118 *result = true; |
128 if (WebContents* web_contents = WebContents::FromRenderViewHost(host_)) { | |
129 *result = web_contents->GetMainFrame()->GetRenderViewHost() == host_; | |
130 #if defined(DEBUG_DEVTOOLS) | 119 #if defined(DEBUG_DEVTOOLS) |
131 *result &= !web_contents->GetVisibleURL().SchemeIs(kChromeDevToolsScheme); | 120 if (WebContentsImpl* web_contents = GetWebContents()) |
| 121 *result &= !web_contents->GetVisibleURL().SchemeIs(kChromeDevToolsScheme); |
132 #endif // defined(DEBUG_DEVTOOLS) | 122 #endif // defined(DEBUG_DEVTOOLS) |
133 } else { | |
134 *result = true; | |
135 } | |
136 } else { | |
137 *result = true; | |
138 } | |
139 #endif // defined(OS_ANDROID) | 123 #endif // defined(OS_ANDROID) |
140 return Response::OK(); | 124 return Response::OK(); |
141 } | 125 } |
142 | 126 |
143 Response EmulationHandler::SetDeviceMetricsOverride( | 127 Response EmulationHandler::SetDeviceMetricsOverride( |
144 int width, int height, double device_scale_factor, bool mobile, | 128 int width, int height, double device_scale_factor, bool mobile, |
145 bool fit_window, const double* optional_scale, | 129 bool fit_window, const double* optional_scale, |
146 const double* optional_offset_x, const double* optional_offset_y) { | 130 const double* optional_offset_x, const double* optional_offset_y) { |
147 const static int max_size = 10000000; | 131 const static int max_size = 10000000; |
148 const static double max_scale = 10; | 132 const static double max_scale = 10; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 | 171 |
188 Response EmulationHandler::ClearDeviceMetricsOverride() { | 172 Response EmulationHandler::ClearDeviceMetricsOverride() { |
189 if (!device_emulation_enabled_) | 173 if (!device_emulation_enabled_) |
190 return Response::OK(); | 174 return Response::OK(); |
191 | 175 |
192 device_emulation_enabled_ = false; | 176 device_emulation_enabled_ = false; |
193 UpdateDeviceEmulationState(); | 177 UpdateDeviceEmulationState(); |
194 return Response::OK(); | 178 return Response::OK(); |
195 } | 179 } |
196 | 180 |
| 181 WebContentsImpl* EmulationHandler::GetWebContents() { |
| 182 return host_ ? |
| 183 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost(host_)) : |
| 184 nullptr; |
| 185 } |
| 186 |
197 void EmulationHandler::UpdateTouchEventEmulationState() { | 187 void EmulationHandler::UpdateTouchEventEmulationState() { |
| 188 RenderWidgetHostImpl* widget_host = |
| 189 host_ ? host_->GetRenderWidgetHost() : nullptr; |
198 if (!host_) | 190 if (!host_) |
199 return; | 191 return; |
200 bool enabled = touch_emulation_enabled_ || | 192 bool enabled = touch_emulation_enabled_ || |
201 page_handler_->screencast_enabled(); | 193 page_handler_->screencast_enabled(); |
202 ui::GestureProviderConfigType config_type = | 194 ui::GestureProviderConfigType config_type = |
203 TouchEmulationConfigurationToType(touch_emulation_configuration_); | 195 TouchEmulationConfigurationToType(touch_emulation_configuration_); |
204 host_->SetTouchEventEmulationEnabled(enabled, config_type); | 196 widget_host->SetTouchEventEmulationEnabled(enabled, config_type); |
205 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( | 197 if (GetWebContents()) |
206 WebContents::FromRenderViewHost(host_)); | 198 GetWebContents()->SetForceDisableOverscrollContent(enabled); |
207 if (web_contents) | |
208 web_contents->SetForceDisableOverscrollContent(enabled); | |
209 } | 199 } |
210 | 200 |
211 void EmulationHandler::UpdateDeviceEmulationState() { | 201 void EmulationHandler::UpdateDeviceEmulationState() { |
| 202 RenderWidgetHostImpl* widget_host = |
| 203 host_ ? host_->GetRenderWidgetHost() : nullptr; |
212 if (!host_) | 204 if (!host_) |
213 return; | 205 return; |
214 if (device_emulation_enabled_) { | 206 if (device_emulation_enabled_) { |
215 host_->Send(new ViewMsg_EnableDeviceEmulation( | 207 widget_host->Send(new ViewMsg_EnableDeviceEmulation( |
216 host_->GetRoutingID(), device_emulation_params_)); | 208 widget_host->GetRoutingID(), device_emulation_params_)); |
217 } else { | 209 } else { |
218 host_->Send(new ViewMsg_DisableDeviceEmulation(host_->GetRoutingID())); | 210 widget_host->Send(new ViewMsg_DisableDeviceEmulation( |
| 211 widget_host->GetRoutingID())); |
219 } | 212 } |
220 } | 213 } |
221 | 214 |
222 } // namespace emulation | 215 } // namespace emulation |
223 } // namespace devtools | 216 } // namespace devtools |
224 } // namespace content | 217 } // namespace content |
OLD | NEW |