| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/renderer_overrides_handler.h" | 5 #include "content/browser/devtools/renderer_overrides_handler.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/barrier_closure.h" | 10 #include "base/barrier_closure.h" |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 devtools::Page::getNavigationHistory::kName, | 128 devtools::Page::getNavigationHistory::kName, |
| 129 base::Bind( | 129 base::Bind( |
| 130 &RendererOverridesHandler::PageGetNavigationHistory, | 130 &RendererOverridesHandler::PageGetNavigationHistory, |
| 131 base::Unretained(this))); | 131 base::Unretained(this))); |
| 132 RegisterCommandHandler( | 132 RegisterCommandHandler( |
| 133 devtools::Page::navigateToHistoryEntry::kName, | 133 devtools::Page::navigateToHistoryEntry::kName, |
| 134 base::Bind( | 134 base::Bind( |
| 135 &RendererOverridesHandler::PageNavigateToHistoryEntry, | 135 &RendererOverridesHandler::PageNavigateToHistoryEntry, |
| 136 base::Unretained(this))); | 136 base::Unretained(this))); |
| 137 RegisterCommandHandler( | 137 RegisterCommandHandler( |
| 138 devtools::Page::captureScreenshot::kName, | |
| 139 base::Bind( | |
| 140 &RendererOverridesHandler::PageCaptureScreenshot, | |
| 141 base::Unretained(this))); | |
| 142 RegisterCommandHandler( | |
| 143 devtools::Page::setTouchEmulationEnabled::kName, | 138 devtools::Page::setTouchEmulationEnabled::kName, |
| 144 base::Bind( | 139 base::Bind( |
| 145 &RendererOverridesHandler::PageSetTouchEmulationEnabled, | 140 &RendererOverridesHandler::PageSetTouchEmulationEnabled, |
| 146 base::Unretained(this))); | 141 base::Unretained(this))); |
| 147 RegisterCommandHandler( | 142 RegisterCommandHandler( |
| 148 devtools::Page::canEmulate::kName, | 143 devtools::Page::canEmulate::kName, |
| 149 base::Bind( | 144 base::Bind( |
| 150 &RendererOverridesHandler::PageCanEmulate, | 145 &RendererOverridesHandler::PageCanEmulate, |
| 151 base::Unretained(this))); | 146 base::Unretained(this))); |
| 152 RegisterCommandHandler( | 147 RegisterCommandHandler( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 167 RegisterCommandHandler( | 162 RegisterCommandHandler( |
| 168 devtools::Page::queryUsageAndQuota::kName, | 163 devtools::Page::queryUsageAndQuota::kName, |
| 169 base::Bind( | 164 base::Bind( |
| 170 &RendererOverridesHandler::PageQueryUsageAndQuota, | 165 &RendererOverridesHandler::PageQueryUsageAndQuota, |
| 171 base::Unretained(this))); | 166 base::Unretained(this))); |
| 172 RegisterCommandHandler( | 167 RegisterCommandHandler( |
| 173 devtools::Page::setColorPickerEnabled::kName, | 168 devtools::Page::setColorPickerEnabled::kName, |
| 174 base::Bind( | 169 base::Bind( |
| 175 &RendererOverridesHandler::PageSetColorPickerEnabled, | 170 &RendererOverridesHandler::PageSetColorPickerEnabled, |
| 176 base::Unretained(this))); | 171 base::Unretained(this))); |
| 177 RegisterCommandHandler( | |
| 178 devtools::Input::emulateTouchFromMouseEvent::kName, | |
| 179 base::Bind( | |
| 180 &RendererOverridesHandler::InputEmulateTouchFromMouseEvent, | |
| 181 base::Unretained(this))); | |
| 182 mouse_event_callback_ = base::Bind( | 172 mouse_event_callback_ = base::Bind( |
| 183 &RendererOverridesHandler::HandleMouseEvent, | 173 &RendererOverridesHandler::HandleMouseEvent, |
| 184 base::Unretained(this)); | 174 base::Unretained(this)); |
| 185 } | 175 } |
| 186 | 176 |
| 187 RendererOverridesHandler::~RendererOverridesHandler() {} | 177 RendererOverridesHandler::~RendererOverridesHandler() {} |
| 188 | 178 |
| 189 void RendererOverridesHandler::OnClientDetached() { | 179 void RendererOverridesHandler::OnClientDetached() { |
| 190 touch_emulation_enabled_ = false; | 180 touch_emulation_enabled_ = false; |
| 191 screencast_command_ = NULL; | 181 screencast_command_ = NULL; |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 controller.GoToIndex(i); | 501 controller.GoToIndex(i); |
| 512 return command->SuccessResponse(new base::DictionaryValue()); | 502 return command->SuccessResponse(new base::DictionaryValue()); |
| 513 } | 503 } |
| 514 } | 504 } |
| 515 return command->InvalidParamResponse(param); | 505 return command->InvalidParamResponse(param); |
| 516 } | 506 } |
| 517 return command->InternalErrorResponse("No WebContents to navigate"); | 507 return command->InternalErrorResponse("No WebContents to navigate"); |
| 518 } | 508 } |
| 519 | 509 |
| 520 scoped_refptr<DevToolsProtocol::Response> | 510 scoped_refptr<DevToolsProtocol::Response> |
| 521 RendererOverridesHandler::PageCaptureScreenshot( | |
| 522 scoped_refptr<DevToolsProtocol::Command> command) { | |
| 523 if (!host_ || !host_->GetView()) | |
| 524 return command->InternalErrorResponse("Could not connect to view"); | |
| 525 | |
| 526 host_->GetSnapshotFromBrowser( | |
| 527 base::Bind(&RendererOverridesHandler::ScreenshotCaptured, | |
| 528 weak_factory_.GetWeakPtr(), command)); | |
| 529 return command->AsyncResponsePromise(); | |
| 530 } | |
| 531 | |
| 532 void RendererOverridesHandler::ScreenshotCaptured( | |
| 533 scoped_refptr<DevToolsProtocol::Command> command, | |
| 534 const unsigned char* png_data, | |
| 535 size_t png_size) { | |
| 536 if (!png_data || !png_size) { | |
| 537 SendAsyncResponse( | |
| 538 command->InternalErrorResponse("Unable to capture screenshot")); | |
| 539 return; | |
| 540 } | |
| 541 | |
| 542 std::string base_64_data; | |
| 543 base::Base64Encode( | |
| 544 base::StringPiece(reinterpret_cast<const char*>(png_data), png_size), | |
| 545 &base_64_data); | |
| 546 | |
| 547 base::DictionaryValue* response = new base::DictionaryValue(); | |
| 548 response->SetString(devtools::Page::screencastFrame::kParamData, | |
| 549 base_64_data); | |
| 550 | |
| 551 SendAsyncResponse(command->SuccessResponse(response)); | |
| 552 } | |
| 553 | |
| 554 scoped_refptr<DevToolsProtocol::Response> | |
| 555 RendererOverridesHandler::PageSetTouchEmulationEnabled( | 511 RendererOverridesHandler::PageSetTouchEmulationEnabled( |
| 556 scoped_refptr<DevToolsProtocol::Command> command) { | 512 scoped_refptr<DevToolsProtocol::Command> command) { |
| 557 base::DictionaryValue* params = command->params(); | 513 base::DictionaryValue* params = command->params(); |
| 558 bool enabled = false; | 514 bool enabled = false; |
| 559 if (!params || !params->GetBoolean( | 515 if (!params || !params->GetBoolean( |
| 560 devtools::Page::setTouchEmulationEnabled::kParamEnabled, | 516 devtools::Page::setTouchEmulationEnabled::kParamEnabled, |
| 561 &enabled)) { | 517 &enabled)) { |
| 562 // Pass to renderer. | 518 // Pass to renderer. |
| 563 return NULL; | 519 return NULL; |
| 564 } | 520 } |
| (...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1182 kHotspotOffset * device_scale_factor); | 1138 kHotspotOffset * device_scale_factor); |
| 1183 #if defined(OS_WIN) | 1139 #if defined(OS_WIN) |
| 1184 cursor_info.external_handle = 0; | 1140 cursor_info.external_handle = 0; |
| 1185 #endif | 1141 #endif |
| 1186 | 1142 |
| 1187 cursor.InitFromCursorInfo(cursor_info); | 1143 cursor.InitFromCursorInfo(cursor_info); |
| 1188 DCHECK(host_); | 1144 DCHECK(host_); |
| 1189 host_->SetCursor(cursor); | 1145 host_->SetCursor(cursor); |
| 1190 } | 1146 } |
| 1191 | 1147 |
| 1192 // Input agent handlers ------------------------------------------------------ | |
| 1193 | |
| 1194 scoped_refptr<DevToolsProtocol::Response> | |
| 1195 RendererOverridesHandler::InputEmulateTouchFromMouseEvent( | |
| 1196 scoped_refptr<DevToolsProtocol::Command> command) { | |
| 1197 if (!screencast_command_.get()) | |
| 1198 return command->InternalErrorResponse("Screencast should be turned on"); | |
| 1199 | |
| 1200 base::DictionaryValue* params = command->params(); | |
| 1201 if (!params) | |
| 1202 return command->NoSuchMethodErrorResponse(); | |
| 1203 | |
| 1204 std::string type; | |
| 1205 if (!params->GetString( | |
| 1206 devtools::Input::emulateTouchFromMouseEvent::kParamType, | |
| 1207 &type)) { | |
| 1208 return command->InvalidParamResponse( | |
| 1209 devtools::Input::emulateTouchFromMouseEvent::kParamType); | |
| 1210 } | |
| 1211 | |
| 1212 blink::WebMouseWheelEvent wheel_event; | |
| 1213 blink::WebMouseEvent mouse_event; | |
| 1214 blink::WebMouseEvent* event = &mouse_event; | |
| 1215 | |
| 1216 if (type == | |
| 1217 devtools::Input::emulateTouchFromMouseEvent::Type::kEnumMousePressed) { | |
| 1218 event->type = WebInputEvent::MouseDown; | |
| 1219 } else if (type == | |
| 1220 devtools::Input::emulateTouchFromMouseEvent::Type::kEnumMouseReleased) { | |
| 1221 event->type = WebInputEvent::MouseUp; | |
| 1222 } else if (type == | |
| 1223 devtools::Input::emulateTouchFromMouseEvent::Type::kEnumMouseMoved) { | |
| 1224 event->type = WebInputEvent::MouseMove; | |
| 1225 } else if (type == | |
| 1226 devtools::Input::emulateTouchFromMouseEvent::Type::kEnumMouseWheel) { | |
| 1227 double deltaX = 0; | |
| 1228 double deltaY = 0; | |
| 1229 if (!params->GetDouble( | |
| 1230 devtools::Input::emulateTouchFromMouseEvent::kParamDeltaX, | |
| 1231 &deltaX)) { | |
| 1232 return command->InvalidParamResponse( | |
| 1233 devtools::Input::emulateTouchFromMouseEvent::kParamDeltaX); | |
| 1234 } | |
| 1235 if (!params->GetDouble( | |
| 1236 devtools::Input::emulateTouchFromMouseEvent::kParamDeltaY, | |
| 1237 &deltaY)) { | |
| 1238 return command->InvalidParamResponse( | |
| 1239 devtools::Input::emulateTouchFromMouseEvent::kParamDeltaY); | |
| 1240 } | |
| 1241 wheel_event.deltaX = static_cast<float>(deltaX); | |
| 1242 wheel_event.deltaY = static_cast<float>(deltaY); | |
| 1243 event = &wheel_event; | |
| 1244 event->type = WebInputEvent::MouseWheel; | |
| 1245 } else { | |
| 1246 return command->InvalidParamResponse( | |
| 1247 devtools::Input::emulateTouchFromMouseEvent::kParamType); | |
| 1248 } | |
| 1249 | |
| 1250 int modifiers = 0; | |
| 1251 if (params->GetInteger( | |
| 1252 devtools::Input::emulateTouchFromMouseEvent::kParamModifiers, | |
| 1253 &modifiers)) { | |
| 1254 if (modifiers & 1) | |
| 1255 event->modifiers |= WebInputEvent::AltKey; | |
| 1256 if (modifiers & 2) | |
| 1257 event->modifiers |= WebInputEvent::ControlKey; | |
| 1258 if (modifiers & 4) | |
| 1259 event->modifiers |= WebInputEvent::MetaKey; | |
| 1260 if (modifiers & 8) | |
| 1261 event->modifiers |= WebInputEvent::ShiftKey; | |
| 1262 } | |
| 1263 | |
| 1264 params->GetDouble( | |
| 1265 devtools::Input::emulateTouchFromMouseEvent::kParamTimestamp, | |
| 1266 &event->timeStampSeconds); | |
| 1267 | |
| 1268 if (!params->GetInteger(devtools::Input::emulateTouchFromMouseEvent::kParamX, | |
| 1269 &event->x)) { | |
| 1270 return command->InvalidParamResponse( | |
| 1271 devtools::Input::emulateTouchFromMouseEvent::kParamX); | |
| 1272 } | |
| 1273 | |
| 1274 if (!params->GetInteger(devtools::Input::emulateTouchFromMouseEvent::kParamY, | |
| 1275 &event->y)) { | |
| 1276 return command->InvalidParamResponse( | |
| 1277 devtools::Input::emulateTouchFromMouseEvent::kParamY); | |
| 1278 } | |
| 1279 | |
| 1280 event->windowX = event->x; | |
| 1281 event->windowY = event->y; | |
| 1282 event->globalX = event->x; | |
| 1283 event->globalY = event->y; | |
| 1284 | |
| 1285 params->GetInteger( | |
| 1286 devtools::Input::emulateTouchFromMouseEvent::kParamClickCount, | |
| 1287 &event->clickCount); | |
| 1288 | |
| 1289 std::string button; | |
| 1290 if (!params->GetString( | |
| 1291 devtools::Input::emulateTouchFromMouseEvent::kParamButton, | |
| 1292 &button)) { | |
| 1293 return command->InvalidParamResponse( | |
| 1294 devtools::Input::emulateTouchFromMouseEvent::kParamButton); | |
| 1295 } | |
| 1296 | |
| 1297 if (button == "none") { | |
| 1298 event->button = WebMouseEvent::ButtonNone; | |
| 1299 } else if (button == "left") { | |
| 1300 event->button = WebMouseEvent::ButtonLeft; | |
| 1301 event->modifiers |= WebInputEvent::LeftButtonDown; | |
| 1302 } else if (button == "middle") { | |
| 1303 event->button = WebMouseEvent::ButtonMiddle; | |
| 1304 event->modifiers |= WebInputEvent::MiddleButtonDown; | |
| 1305 } else if (button == "right") { | |
| 1306 event->button = WebMouseEvent::ButtonRight; | |
| 1307 event->modifiers |= WebInputEvent::RightButtonDown; | |
| 1308 } else { | |
| 1309 return command->InvalidParamResponse( | |
| 1310 devtools::Input::emulateTouchFromMouseEvent::kParamButton); | |
| 1311 } | |
| 1312 | |
| 1313 if (!host_) | |
| 1314 return command->InternalErrorResponse("Could not connect to view"); | |
| 1315 | |
| 1316 if (event->type == WebInputEvent::MouseWheel) | |
| 1317 host_->ForwardWheelEvent(wheel_event); | |
| 1318 else | |
| 1319 host_->ForwardMouseEvent(mouse_event); | |
| 1320 return command->SuccessResponse(NULL); | |
| 1321 } | |
| 1322 | |
| 1323 void RendererOverridesHandler::UpdateTouchEventEmulationState() { | 1148 void RendererOverridesHandler::UpdateTouchEventEmulationState() { |
| 1324 if (!host_) | 1149 if (!host_) |
| 1325 return; | 1150 return; |
| 1326 bool enabled = touch_emulation_enabled_ || screencast_command_.get(); | 1151 bool enabled = touch_emulation_enabled_ || screencast_command_.get(); |
| 1327 host_->SetTouchEventEmulationEnabled(enabled); | 1152 host_->SetTouchEventEmulationEnabled(enabled); |
| 1328 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( | 1153 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( |
| 1329 WebContents::FromRenderViewHost(host_)); | 1154 WebContents::FromRenderViewHost(host_)); |
| 1330 if (web_contents) | 1155 if (web_contents) |
| 1331 web_contents->SetForceDisableOverscrollContent(enabled); | 1156 web_contents->SetForceDisableOverscrollContent(enabled); |
| 1332 } | 1157 } |
| 1333 | 1158 |
| 1334 } // namespace content | 1159 } // namespace content |
| OLD | NEW |