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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 namespace content { | 58 namespace content { |
59 | 59 |
60 namespace { | 60 namespace { |
61 | 61 |
62 static const char kPng[] = "png"; | 62 static const char kPng[] = "png"; |
63 static const char kJpeg[] = "jpeg"; | 63 static const char kJpeg[] = "jpeg"; |
64 static int kDefaultScreenshotQuality = 80; | 64 static int kDefaultScreenshotQuality = 80; |
65 static int kFrameRateThresholdMs = 100; | 65 static int kFrameRateThresholdMs = 100; |
66 static int kCaptureRetryLimit = 2; | 66 static int kCaptureRetryLimit = 2; |
67 | 67 |
68 void ParseGenericInputParams(base::DictionaryValue* params, | |
69 WebInputEvent* event) { | |
70 int modifiers = 0; | |
71 if (params->GetInteger( | |
72 devtools::Input::emulateTouchFromMouseEvent::kParamModifiers, | |
73 &modifiers)) { | |
74 if (modifiers & 1) | |
75 event->modifiers |= WebInputEvent::AltKey; | |
76 if (modifiers & 2) | |
77 event->modifiers |= WebInputEvent::ControlKey; | |
78 if (modifiers & 4) | |
79 event->modifiers |= WebInputEvent::MetaKey; | |
80 if (modifiers & 8) | |
81 event->modifiers |= WebInputEvent::ShiftKey; | |
82 } | |
83 | |
84 params->GetDouble( | |
85 devtools::Input::emulateTouchFromMouseEvent::kParamTimestamp, | |
86 &event->timeStampSeconds); | |
87 } | |
88 | |
89 } // namespace | 68 } // namespace |
90 | 69 |
91 RendererOverridesHandler::RendererOverridesHandler(DevToolsAgentHost* agent) | 70 RendererOverridesHandler::RendererOverridesHandler(DevToolsAgentHost* agent) |
92 : agent_(agent), | 71 : agent_(agent), |
93 has_last_compositor_frame_metadata_(false), | 72 has_last_compositor_frame_metadata_(false), |
94 capture_retry_count_(0), | 73 capture_retry_count_(0), |
95 weak_factory_(this) { | 74 weak_factory_(this) { |
96 RegisterCommandHandler( | 75 RegisterCommandHandler( |
97 devtools::DOM::setFileInputFiles::kName, | 76 devtools::DOM::setFileInputFiles::kName, |
98 base::Bind( | 77 base::Bind( |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 devtools::Page::stopScreencast::kName, | 135 devtools::Page::stopScreencast::kName, |
157 base::Bind( | 136 base::Bind( |
158 &RendererOverridesHandler::PageStopScreencast, | 137 &RendererOverridesHandler::PageStopScreencast, |
159 base::Unretained(this))); | 138 base::Unretained(this))); |
160 RegisterCommandHandler( | 139 RegisterCommandHandler( |
161 devtools::Page::queryUsageAndQuota::kName, | 140 devtools::Page::queryUsageAndQuota::kName, |
162 base::Bind( | 141 base::Bind( |
163 &RendererOverridesHandler::PageQueryUsageAndQuota, | 142 &RendererOverridesHandler::PageQueryUsageAndQuota, |
164 base::Unretained(this))); | 143 base::Unretained(this))); |
165 RegisterCommandHandler( | 144 RegisterCommandHandler( |
166 devtools::Input::dispatchMouseEvent::kName, | |
167 base::Bind( | |
168 &RendererOverridesHandler::InputDispatchMouseEvent, | |
169 base::Unretained(this))); | |
170 RegisterCommandHandler( | |
171 devtools::Input::dispatchGestureEvent::kName, | |
172 base::Bind( | |
173 &RendererOverridesHandler::InputDispatchGestureEvent, | |
174 base::Unretained(this))); | |
175 RegisterCommandHandler( | |
176 devtools::Input::emulateTouchFromMouseEvent::kName, | 145 devtools::Input::emulateTouchFromMouseEvent::kName, |
177 base::Bind( | 146 base::Bind( |
178 &RendererOverridesHandler::InputEmulateTouchFromMouseEvent, | 147 &RendererOverridesHandler::InputEmulateTouchFromMouseEvent, |
179 base::Unretained(this))); | 148 base::Unretained(this))); |
180 } | 149 } |
181 | 150 |
182 RendererOverridesHandler::~RendererOverridesHandler() {} | 151 RendererOverridesHandler::~RendererOverridesHandler() {} |
183 | 152 |
184 void RendererOverridesHandler::OnClientDetached() { | 153 void RendererOverridesHandler::OnClientDetached() { |
185 RenderViewHostImpl* host = static_cast<RenderViewHostImpl*>( | 154 RenderViewHostImpl* host = static_cast<RenderViewHostImpl*>( |
(...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
878 base::DictionaryValue* params = new base::DictionaryValue(); | 847 base::DictionaryValue* params = new base::DictionaryValue(); |
879 params->SetBoolean( | 848 params->SetBoolean( |
880 devtools::Page::screencastVisibilityChanged::kParamVisible, visible); | 849 devtools::Page::screencastVisibilityChanged::kParamVisible, visible); |
881 SendNotification( | 850 SendNotification( |
882 devtools::Page::screencastVisibilityChanged::kName, params); | 851 devtools::Page::screencastVisibilityChanged::kName, params); |
883 } | 852 } |
884 | 853 |
885 // Input agent handlers ------------------------------------------------------ | 854 // Input agent handlers ------------------------------------------------------ |
886 | 855 |
887 scoped_refptr<DevToolsProtocol::Response> | 856 scoped_refptr<DevToolsProtocol::Response> |
888 RendererOverridesHandler::InputDispatchMouseEvent( | |
889 scoped_refptr<DevToolsProtocol::Command> command) { | |
890 base::DictionaryValue* params = command->params(); | |
891 if (!params) | |
892 return NULL; | |
893 | |
894 bool device_space = false; | |
895 if (!params->GetBoolean( | |
896 devtools::Input::dispatchMouseEvent::kParamDeviceSpace, | |
897 &device_space) || | |
898 !device_space) { | |
899 return NULL; | |
900 } | |
901 | |
902 if (!DispatchMouseEventFromCommand(command)) | |
903 return NULL; | |
904 return command->SuccessResponse(NULL); | |
905 } | |
906 | |
907 scoped_refptr<DevToolsProtocol::Response> | |
908 RendererOverridesHandler::InputEmulateTouchFromMouseEvent( | 857 RendererOverridesHandler::InputEmulateTouchFromMouseEvent( |
909 scoped_refptr<DevToolsProtocol::Command> command) { | 858 scoped_refptr<DevToolsProtocol::Command> command) { |
910 if (!screencast_command_) | 859 if (!screencast_command_) |
911 return NULL; | 860 return command->InternalErrorResponse("Screencast should be turned on"); |
912 if (!DispatchMouseEventFromCommand(command)) | |
913 return NULL; | |
914 return command->SuccessResponse(NULL); | |
915 } | |
916 | 861 |
917 bool RendererOverridesHandler::DispatchMouseEventFromCommand( | |
918 scoped_refptr<DevToolsProtocol::Command> command) { | |
919 base::DictionaryValue* params = command->params(); | 862 base::DictionaryValue* params = command->params(); |
920 if (!params) | 863 if (!params) |
921 return false; | 864 return command->NoSuchMethodErrorResponse(); |
922 | 865 |
923 RenderViewHost* host = agent_->GetRenderViewHost(); | 866 RenderViewHost* host = agent_->GetRenderViewHost(); |
924 | 867 |
925 std::string type; | 868 std::string type; |
926 if (!params->GetString( | 869 if (!params->GetString( |
927 devtools::Input::emulateTouchFromMouseEvent::kParamType, | 870 devtools::Input::emulateTouchFromMouseEvent::kParamType, |
928 &type)) { | 871 &type)) { |
929 return false; | 872 return command->InvalidParamResponse( |
| 873 devtools::Input::emulateTouchFromMouseEvent::kParamType); |
930 } | 874 } |
931 | 875 |
932 blink::WebMouseWheelEvent wheel_event; | 876 blink::WebMouseWheelEvent wheel_event; |
933 blink::WebMouseEvent mouse_event; | 877 blink::WebMouseEvent mouse_event; |
934 blink::WebMouseEvent* event = &mouse_event; | 878 blink::WebMouseEvent* event = &mouse_event; |
935 | 879 |
936 if (type == | 880 if (type == |
937 devtools::Input::emulateTouchFromMouseEvent::Type::kEnumMousePressed) { | 881 devtools::Input::emulateTouchFromMouseEvent::Type::kEnumMousePressed) { |
938 event->type = WebInputEvent::MouseDown; | 882 event->type = WebInputEvent::MouseDown; |
939 } else if (type == | 883 } else if (type == |
940 devtools::Input::emulateTouchFromMouseEvent::Type::kEnumMouseReleased) { | 884 devtools::Input::emulateTouchFromMouseEvent::Type::kEnumMouseReleased) { |
941 event->type = WebInputEvent::MouseUp; | 885 event->type = WebInputEvent::MouseUp; |
942 } else if (type == | 886 } else if (type == |
943 devtools::Input::emulateTouchFromMouseEvent::Type::kEnumMouseMoved) { | 887 devtools::Input::emulateTouchFromMouseEvent::Type::kEnumMouseMoved) { |
944 event->type = WebInputEvent::MouseMove; | 888 event->type = WebInputEvent::MouseMove; |
945 } else if (type == | 889 } else if (type == |
946 devtools::Input::emulateTouchFromMouseEvent::Type::kEnumMouseWheel) { | 890 devtools::Input::emulateTouchFromMouseEvent::Type::kEnumMouseWheel) { |
947 double deltaX = 0; | 891 double deltaX = 0; |
948 double deltaY = 0; | 892 double deltaY = 0; |
949 if (!params->GetDouble( | 893 if (!params->GetDouble( |
950 devtools::Input::emulateTouchFromMouseEvent::kParamDeltaX, | 894 devtools::Input::emulateTouchFromMouseEvent::kParamDeltaX, |
951 &deltaX) || | 895 &deltaX)) { |
952 !params->GetDouble( | 896 return command->InvalidParamResponse( |
| 897 devtools::Input::emulateTouchFromMouseEvent::kParamDeltaX); |
| 898 } |
| 899 if (!params->GetDouble( |
953 devtools::Input::emulateTouchFromMouseEvent::kParamDeltaY, | 900 devtools::Input::emulateTouchFromMouseEvent::kParamDeltaY, |
954 &deltaY)) { | 901 &deltaY)) { |
955 return false; | 902 return command->InvalidParamResponse( |
| 903 devtools::Input::emulateTouchFromMouseEvent::kParamDeltaY); |
956 } | 904 } |
957 wheel_event.deltaX = static_cast<float>(deltaX); | 905 wheel_event.deltaX = static_cast<float>(deltaX); |
958 wheel_event.deltaY = static_cast<float>(deltaY); | 906 wheel_event.deltaY = static_cast<float>(deltaY); |
959 event = &wheel_event; | 907 event = &wheel_event; |
960 event->type = WebInputEvent::MouseWheel; | 908 event->type = WebInputEvent::MouseWheel; |
961 } else { | 909 } else { |
962 return false; | 910 return command->InvalidParamResponse( |
| 911 devtools::Input::emulateTouchFromMouseEvent::kParamType); |
963 } | 912 } |
964 | 913 |
965 ParseGenericInputParams(params, event); | 914 int modifiers = 0; |
| 915 if (params->GetInteger( |
| 916 devtools::Input::emulateTouchFromMouseEvent::kParamModifiers, |
| 917 &modifiers)) { |
| 918 if (modifiers & 1) |
| 919 event->modifiers |= WebInputEvent::AltKey; |
| 920 if (modifiers & 2) |
| 921 event->modifiers |= WebInputEvent::ControlKey; |
| 922 if (modifiers & 4) |
| 923 event->modifiers |= WebInputEvent::MetaKey; |
| 924 if (modifiers & 8) |
| 925 event->modifiers |= WebInputEvent::ShiftKey; |
| 926 } |
| 927 |
| 928 params->GetDouble( |
| 929 devtools::Input::emulateTouchFromMouseEvent::kParamTimestamp, |
| 930 &event->timeStampSeconds); |
966 | 931 |
967 if (!params->GetInteger(devtools::Input::emulateTouchFromMouseEvent::kParamX, | 932 if (!params->GetInteger(devtools::Input::emulateTouchFromMouseEvent::kParamX, |
968 &event->x) || | 933 &event->x)) { |
969 !params->GetInteger(devtools::Input::emulateTouchFromMouseEvent::kParamY, | 934 return command->InvalidParamResponse( |
| 935 devtools::Input::emulateTouchFromMouseEvent::kParamX); |
| 936 } |
| 937 |
| 938 if (!params->GetInteger(devtools::Input::emulateTouchFromMouseEvent::kParamY, |
970 &event->y)) { | 939 &event->y)) { |
971 return false; | 940 return command->InvalidParamResponse( |
| 941 devtools::Input::emulateTouchFromMouseEvent::kParamY); |
972 } | 942 } |
973 | 943 |
974 event->windowX = event->x; | 944 event->windowX = event->x; |
975 event->windowY = event->y; | 945 event->windowY = event->y; |
976 event->globalX = event->x; | 946 event->globalX = event->x; |
977 event->globalY = event->y; | 947 event->globalY = event->y; |
978 | 948 |
979 params->GetInteger( | 949 params->GetInteger( |
980 devtools::Input::emulateTouchFromMouseEvent::kParamClickCount, | 950 devtools::Input::emulateTouchFromMouseEvent::kParamClickCount, |
981 &event->clickCount); | 951 &event->clickCount); |
982 | 952 |
983 std::string button; | 953 std::string button; |
984 if (!params->GetString( | 954 if (!params->GetString( |
985 devtools::Input::emulateTouchFromMouseEvent::kParamButton, | 955 devtools::Input::emulateTouchFromMouseEvent::kParamButton, |
986 &button)) { | 956 &button)) { |
987 return false; | 957 return command->InvalidParamResponse( |
| 958 devtools::Input::emulateTouchFromMouseEvent::kParamButton); |
988 } | 959 } |
989 | 960 |
990 if (button == "none") { | 961 if (button == "none") { |
991 event->button = WebMouseEvent::ButtonNone; | 962 event->button = WebMouseEvent::ButtonNone; |
992 } else if (button == "left") { | 963 } else if (button == "left") { |
993 event->button = WebMouseEvent::ButtonLeft; | 964 event->button = WebMouseEvent::ButtonLeft; |
994 event->modifiers |= WebInputEvent::LeftButtonDown; | 965 event->modifiers |= WebInputEvent::LeftButtonDown; |
995 } else if (button == "middle") { | 966 } else if (button == "middle") { |
996 event->button = WebMouseEvent::ButtonMiddle; | 967 event->button = WebMouseEvent::ButtonMiddle; |
997 event->modifiers |= WebInputEvent::MiddleButtonDown; | 968 event->modifiers |= WebInputEvent::MiddleButtonDown; |
998 } else if (button == "right") { | 969 } else if (button == "right") { |
999 event->button = WebMouseEvent::ButtonRight; | 970 event->button = WebMouseEvent::ButtonRight; |
1000 event->modifiers |= WebInputEvent::RightButtonDown; | 971 event->modifiers |= WebInputEvent::RightButtonDown; |
1001 } else { | 972 } else { |
1002 return false; | 973 return command->InvalidParamResponse( |
| 974 devtools::Input::emulateTouchFromMouseEvent::kParamButton); |
1003 } | 975 } |
1004 | 976 |
1005 if (event->type == WebInputEvent::MouseWheel) | 977 if (event->type == WebInputEvent::MouseWheel) |
1006 host->ForwardWheelEvent(wheel_event); | 978 host->ForwardWheelEvent(wheel_event); |
1007 else | 979 else |
1008 host->ForwardMouseEvent(mouse_event); | 980 host->ForwardMouseEvent(mouse_event); |
1009 return true; | |
1010 } | |
1011 | |
1012 scoped_refptr<DevToolsProtocol::Response> | |
1013 RendererOverridesHandler::InputDispatchGestureEvent( | |
1014 scoped_refptr<DevToolsProtocol::Command> command) { | |
1015 base::DictionaryValue* params = command->params(); | |
1016 if (!params) | |
1017 return NULL; | |
1018 | |
1019 RenderViewHostImpl* host = static_cast<RenderViewHostImpl*>( | |
1020 agent_->GetRenderViewHost()); | |
1021 blink::WebGestureEvent event; | |
1022 ParseGenericInputParams(params, &event); | |
1023 event.sourceDevice = blink::WebGestureDeviceTouchscreen; | |
1024 | |
1025 std::string type; | |
1026 if (params->GetString(devtools::Input::dispatchGestureEvent::kParamType, | |
1027 &type)) { | |
1028 if (type == | |
1029 devtools::Input::dispatchGestureEvent::Type::kEnumScrollBegin) | |
1030 event.type = WebInputEvent::GestureScrollBegin; | |
1031 else if (type == | |
1032 devtools::Input::dispatchGestureEvent::Type::kEnumScrollUpdate) | |
1033 event.type = WebInputEvent::GestureScrollUpdate; | |
1034 else if (type == | |
1035 devtools::Input::dispatchGestureEvent::Type::kEnumScrollEnd) | |
1036 event.type = WebInputEvent::GestureScrollEnd; | |
1037 else if (type == | |
1038 devtools::Input::dispatchGestureEvent::Type::kEnumTapDown) | |
1039 event.type = WebInputEvent::GestureTapDown; | |
1040 else if (type == | |
1041 devtools::Input::dispatchGestureEvent::Type::kEnumTap) | |
1042 event.type = WebInputEvent::GestureTap; | |
1043 else if (type == | |
1044 devtools::Input::dispatchGestureEvent::Type::kEnumPinchBegin) | |
1045 event.type = WebInputEvent::GesturePinchBegin; | |
1046 else if (type == | |
1047 devtools::Input::dispatchGestureEvent::Type::kEnumPinchUpdate) | |
1048 event.type = WebInputEvent::GesturePinchUpdate; | |
1049 else if (type == | |
1050 devtools::Input::dispatchGestureEvent::Type::kEnumPinchEnd) | |
1051 event.type = WebInputEvent::GesturePinchEnd; | |
1052 else | |
1053 return NULL; | |
1054 } else { | |
1055 return NULL; | |
1056 } | |
1057 | |
1058 if (!params->GetInteger(devtools::Input::dispatchGestureEvent::kParamX, | |
1059 &event.x) || | |
1060 !params->GetInteger(devtools::Input::dispatchGestureEvent::kParamY, | |
1061 &event.y)) { | |
1062 return NULL; | |
1063 } | |
1064 event.globalX = event.x; | |
1065 event.globalY = event.y; | |
1066 | |
1067 if (type == "scrollUpdate") { | |
1068 int dx = 0; | |
1069 int dy = 0; | |
1070 if (!params->GetInteger( | |
1071 devtools::Input::dispatchGestureEvent::kParamDeltaX, &dx) || | |
1072 !params->GetInteger( | |
1073 devtools::Input::dispatchGestureEvent::kParamDeltaY, &dy)) { | |
1074 return NULL; | |
1075 } | |
1076 event.data.scrollUpdate.deltaX = dx; | |
1077 event.data.scrollUpdate.deltaY = dy; | |
1078 } | |
1079 | |
1080 if (type == "pinchUpdate") { | |
1081 double scale; | |
1082 if (!params->GetDouble( | |
1083 devtools::Input::dispatchGestureEvent::kParamPinchScale, | |
1084 &scale)) { | |
1085 return NULL; | |
1086 } | |
1087 event.data.pinchUpdate.scale = static_cast<float>(scale); | |
1088 } | |
1089 | |
1090 host->ForwardGestureEvent(event); | |
1091 return command->SuccessResponse(NULL); | 981 return command->SuccessResponse(NULL); |
1092 } | 982 } |
1093 | 983 |
1094 } // namespace content | 984 } // namespace content |
OLD | NEW |