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/geolocation/geolocation_service_context.h" | 8 #include "content/browser/geolocation/geolocation_service_context.h" |
9 #include "content/browser/renderer_host/render_view_host_impl.h" | 9 #include "content/browser/renderer_host/render_view_host_impl.h" |
10 #include "content/browser/web_contents/web_contents_impl.h" | 10 #include "content/browser/web_contents/web_contents_impl.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 } | 33 } |
34 return result; | 34 return result; |
35 } | 35 } |
36 | 36 |
37 } // namespace | 37 } // namespace |
38 | 38 |
39 EmulationHandler::EmulationHandler(page::PageHandler* page_handler) | 39 EmulationHandler::EmulationHandler(page::PageHandler* page_handler) |
40 : touch_emulation_enabled_(false), | 40 : touch_emulation_enabled_(false), |
41 device_emulation_enabled_(false), | 41 device_emulation_enabled_(false), |
42 page_handler_(page_handler), | 42 page_handler_(page_handler), |
43 host_(nullptr) | 43 web_contents_(nullptr) |
44 { | 44 { |
45 page_handler->SetScreencastListener(this); | 45 page_handler->SetScreencastListener(this); |
46 } | 46 } |
47 | 47 |
48 EmulationHandler::~EmulationHandler() { | 48 EmulationHandler::~EmulationHandler() { |
49 } | 49 } |
50 | 50 |
51 void EmulationHandler::ScreencastEnabledChanged() { | 51 void EmulationHandler::ScreencastEnabledChanged() { |
52 UpdateTouchEventEmulationState(); | 52 UpdateTouchEventEmulationState(); |
53 } | 53 } |
54 | 54 |
55 void EmulationHandler::SetRenderViewHost(RenderViewHostImpl* host) { | 55 void EmulationHandler::SetWebContents(WebContentsImpl* web_contents) { |
56 if (host_ == host) | 56 if (web_contents_ == web_contents) |
57 return; | 57 return; |
58 | 58 |
59 host_ = host; | 59 web_contents_ = web_contents; |
60 UpdateTouchEventEmulationState(); | 60 UpdateTouchEventEmulationState(); |
61 UpdateDeviceEmulationState(); | 61 UpdateDeviceEmulationState(); |
62 } | 62 } |
63 | 63 |
64 void EmulationHandler::Detached() { | 64 void EmulationHandler::Detached() { |
65 touch_emulation_enabled_ = false; | 65 touch_emulation_enabled_ = false; |
66 device_emulation_enabled_ = false; | 66 device_emulation_enabled_ = false; |
67 UpdateTouchEventEmulationState(); | 67 UpdateTouchEventEmulationState(); |
68 UpdateDeviceEmulationState(); | 68 UpdateDeviceEmulationState(); |
69 } | 69 } |
70 | 70 |
71 Response EmulationHandler::SetGeolocationOverride( | 71 Response EmulationHandler::SetGeolocationOverride( |
72 double* latitude, double* longitude, double* accuracy) { | 72 double* latitude, double* longitude, double* accuracy) { |
73 if (!host_) | 73 if (!web_contents_) |
74 return Response::InternalError("Could not connect to view"); | 74 return Response::InternalError("Could not connect to view"); |
75 | 75 |
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 = | 76 GeolocationServiceContext* geolocation_context = |
82 web_contents->GetGeolocationServiceContext(); | 77 web_contents_->GetGeolocationServiceContext(); |
83 scoped_ptr<Geoposition> geoposition(new Geoposition()); | 78 scoped_ptr<Geoposition> geoposition(new Geoposition()); |
84 if (latitude && longitude && accuracy) { | 79 if (latitude && longitude && accuracy) { |
85 geoposition->latitude = *latitude; | 80 geoposition->latitude = *latitude; |
86 geoposition->longitude = *longitude; | 81 geoposition->longitude = *longitude; |
87 geoposition->accuracy = *accuracy; | 82 geoposition->accuracy = *accuracy; |
88 geoposition->timestamp = base::Time::Now(); | 83 geoposition->timestamp = base::Time::Now(); |
89 if (!geoposition->Validate()) { | 84 if (!geoposition->Validate()) { |
90 return Response::InternalError("Invalid geolocation"); | 85 return Response::InternalError("Invalid geolocation"); |
91 } | 86 } |
92 } else { | 87 } else { |
93 geoposition->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 88 geoposition->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
94 } | 89 } |
95 geolocation_context->SetOverride(geoposition.Pass()); | 90 geolocation_context->SetOverride(geoposition.Pass()); |
96 return Response::OK(); | 91 return Response::OK(); |
97 } | 92 } |
98 | 93 |
99 Response EmulationHandler::ClearGeolocationOverride() { | 94 Response EmulationHandler::ClearGeolocationOverride() { |
100 if (!host_) | 95 if (!web_contents_) |
101 return Response::InternalError("Could not connect to view"); | 96 return Response::InternalError("Could not connect to view"); |
102 | 97 |
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 = | 98 GeolocationServiceContext* geolocation_context = |
109 web_contents->GetGeolocationServiceContext(); | 99 web_contents_->GetGeolocationServiceContext(); |
110 geolocation_context->ClearOverride(); | 100 geolocation_context->ClearOverride(); |
111 return Response::OK(); | 101 return Response::OK(); |
112 } | 102 } |
113 | 103 |
114 Response EmulationHandler::SetTouchEmulationEnabled( | 104 Response EmulationHandler::SetTouchEmulationEnabled( |
115 bool enabled, const std::string* configuration) { | 105 bool enabled, const std::string* configuration) { |
116 touch_emulation_enabled_ = enabled; | 106 touch_emulation_enabled_ = enabled; |
117 touch_emulation_configuration_ = | 107 touch_emulation_configuration_ = |
118 configuration ? *configuration : std::string(); | 108 configuration ? *configuration : std::string(); |
119 UpdateTouchEventEmulationState(); | 109 UpdateTouchEventEmulationState(); |
120 return Response::FallThrough(); | 110 return Response::FallThrough(); |
121 } | 111 } |
122 | 112 |
123 Response EmulationHandler::CanEmulate(bool* result) { | 113 Response EmulationHandler::CanEmulate(bool* result) { |
124 #if defined(OS_ANDROID) | 114 #if defined(OS_ANDROID) |
125 *result = false; | 115 *result = false; |
126 #else | 116 #else |
127 if (host_) { | 117 if (web_contents_) { |
128 if (WebContents* web_contents = WebContents::FromRenderViewHost(host_)) { | 118 *result = true; |
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 *result &= !web_contents_->GetVisibleURL().SchemeIs(kChromeDevToolsScheme); |
132 #endif // defined(DEBUG_DEVTOOLS) | 121 #endif // defined(DEBUG_DEVTOOLS) |
133 } else { | |
134 *result = true; | |
135 } | |
136 } else { | 122 } else { |
137 *result = true; | 123 *result = true; |
138 } | 124 } |
139 #endif // defined(OS_ANDROID) | 125 #endif // defined(OS_ANDROID) |
140 return Response::OK(); | 126 return Response::OK(); |
141 } | 127 } |
142 | 128 |
143 Response EmulationHandler::SetDeviceMetricsOverride( | 129 Response EmulationHandler::SetDeviceMetricsOverride( |
144 int width, int height, double device_scale_factor, bool mobile, | 130 int width, int height, double device_scale_factor, bool mobile, |
145 bool fit_window, const double* optional_scale, | 131 bool fit_window, const double* optional_scale, |
146 const double* optional_offset_x, const double* optional_offset_y) { | 132 const double* optional_offset_x, const double* optional_offset_y) { |
147 const static int max_size = 10000000; | 133 const static int max_size = 10000000; |
148 const static double max_scale = 10; | 134 const static double max_scale = 10; |
149 | 135 |
150 if (!host_) | 136 if (!web_contents_) |
151 return Response::InternalError("Could not connect to view"); | 137 return Response::InternalError("Could not connect to view"); |
152 | 138 |
153 if (width < 0 || height < 0 || width > max_size || height > max_size) { | 139 if (width < 0 || height < 0 || width > max_size || height > max_size) { |
154 return Response::InvalidParams( | 140 return Response::InvalidParams( |
155 "Width and height values must be positive, not greater than " + | 141 "Width and height values must be positive, not greater than " + |
156 base::IntToString(max_size)); | 142 base::IntToString(max_size)); |
157 } | 143 } |
158 | 144 |
159 if (device_scale_factor < 0) | 145 if (device_scale_factor < 0) |
160 return Response::InvalidParams("deviceScaleFactor must be non-negative"); | 146 return Response::InvalidParams("deviceScaleFactor must be non-negative"); |
(...skipping 26 matching lines...) Expand all Loading... | |
187 | 173 |
188 Response EmulationHandler::ClearDeviceMetricsOverride() { | 174 Response EmulationHandler::ClearDeviceMetricsOverride() { |
189 if (!device_emulation_enabled_) | 175 if (!device_emulation_enabled_) |
190 return Response::OK(); | 176 return Response::OK(); |
191 | 177 |
192 device_emulation_enabled_ = false; | 178 device_emulation_enabled_ = false; |
193 UpdateDeviceEmulationState(); | 179 UpdateDeviceEmulationState(); |
194 return Response::OK(); | 180 return Response::OK(); |
195 } | 181 } |
196 | 182 |
183 RenderWidgetHostImpl* EmulationHandler::GetRenderWidgetHost() { | |
184 // TODO(dgozman): there should be a way to get main RenderWidgetHost from | |
185 // WebContents without RenderViewHost. | |
nasko
2015/03/31 14:43:36
WebContentsImpl::GetMainFrame()->GetRenderWidgetHo
dgozman
2015/03/31 15:05:23
So, do you think it's a property of frame? This cl
dgozman
2015/03/31 16:44:59
Done.
| |
186 return web_contents_ ? | |
187 static_cast<RenderViewHostImpl*>(web_contents_->GetRenderViewHost()) : | |
188 nullptr; | |
189 } | |
190 | |
197 void EmulationHandler::UpdateTouchEventEmulationState() { | 191 void EmulationHandler::UpdateTouchEventEmulationState() { |
198 if (!host_) | 192 if (!web_contents_) |
199 return; | 193 return; |
200 bool enabled = touch_emulation_enabled_ || | 194 bool enabled = touch_emulation_enabled_ || |
201 page_handler_->screencast_enabled(); | 195 page_handler_->screencast_enabled(); |
202 ui::GestureProviderConfigType config_type = | 196 ui::GestureProviderConfigType config_type = |
203 TouchEmulationConfigurationToType(touch_emulation_configuration_); | 197 TouchEmulationConfigurationToType(touch_emulation_configuration_); |
204 host_->SetTouchEventEmulationEnabled(enabled, config_type); | 198 GetRenderWidgetHost()->SetTouchEventEmulationEnabled(enabled, config_type); |
205 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( | 199 web_contents_->SetForceDisableOverscrollContent(enabled); |
206 WebContents::FromRenderViewHost(host_)); | |
207 if (web_contents) | |
208 web_contents->SetForceDisableOverscrollContent(enabled); | |
209 } | 200 } |
210 | 201 |
211 void EmulationHandler::UpdateDeviceEmulationState() { | 202 void EmulationHandler::UpdateDeviceEmulationState() { |
212 if (!host_) | 203 if (!web_contents_) |
213 return; | 204 return; |
205 RenderWidgetHostImpl* widget_host = GetRenderWidgetHost(); | |
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 |