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

Side by Side Diff: content/browser/devtools/renderer_overrides_handler.cc

Issue 474243003: DevTools: make eye dropper live (refresh on frame update). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review comments addressed. Created 6 years, 4 months 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 | Annotate | Revision Log
« no previous file with comments | « content/browser/devtools/renderer_overrides_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 static int kDefaultScreenshotQuality = 80; 68 static int kDefaultScreenshotQuality = 80;
69 static int kFrameRateThresholdMs = 100; 69 static int kFrameRateThresholdMs = 100;
70 static int kCaptureRetryLimit = 2; 70 static int kCaptureRetryLimit = 2;
71 71
72 } // namespace 72 } // namespace
73 73
74 RendererOverridesHandler::RendererOverridesHandler() 74 RendererOverridesHandler::RendererOverridesHandler()
75 : has_last_compositor_frame_metadata_(false), 75 : has_last_compositor_frame_metadata_(false),
76 capture_retry_count_(0), 76 capture_retry_count_(0),
77 color_picker_enabled_(false), 77 color_picker_enabled_(false),
78 last_cursor_x_(-1),
79 last_cursor_y_(-1),
78 weak_factory_(this) { 80 weak_factory_(this) {
79 RegisterCommandHandler( 81 RegisterCommandHandler(
80 devtools::DOM::setFileInputFiles::kName, 82 devtools::DOM::setFileInputFiles::kName,
81 base::Bind( 83 base::Bind(
82 &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles, 84 &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles,
83 base::Unretained(this))); 85 base::Unretained(this)));
84 RegisterCommandHandler( 86 RegisterCommandHandler(
85 devtools::Network::clearBrowserCache::kName, 87 devtools::Network::clearBrowserCache::kName,
86 base::Bind( 88 base::Bind(
87 &RendererOverridesHandler::ClearBrowserCache, 89 &RendererOverridesHandler::ClearBrowserCache,
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 184
183 void RendererOverridesHandler::OnVisibilityChanged(bool visible) { 185 void RendererOverridesHandler::OnVisibilityChanged(bool visible) {
184 if (!screencast_command_) 186 if (!screencast_command_)
185 return; 187 return;
186 NotifyScreencastVisibility(visible); 188 NotifyScreencastVisibility(visible);
187 } 189 }
188 190
189 void RendererOverridesHandler::SetRenderViewHost( 191 void RendererOverridesHandler::SetRenderViewHost(
190 RenderViewHostImpl* host) { 192 RenderViewHostImpl* host) {
191 host_ = host; 193 host_ = host;
192 if (screencast_command_ && host) 194 if (!host)
195 return;
196 if (screencast_command_)
193 host->SetTouchEventEmulationEnabled(true, true); 197 host->SetTouchEventEmulationEnabled(true, true);
194 if (color_picker_enabled_) 198 if (color_picker_enabled_)
195 host->AddMouseEventCallback(mouse_event_callback_); 199 host->AddMouseEventCallback(mouse_event_callback_);
196 } 200 }
197 201
198 void RendererOverridesHandler::ClearRenderViewHost() { 202 void RendererOverridesHandler::ClearRenderViewHost() {
199 if (host_) 203 if (host_)
200 host_->RemoveMouseEventCallback(mouse_event_callback_); 204 host_->RemoveMouseEventCallback(mouse_event_callback_);
201 host_ = NULL; 205 host_ = NULL;
206 ResetColorPickerFrame();
202 } 207 }
203 208
204 bool RendererOverridesHandler::OnSetTouchEventEmulationEnabled() { 209 bool RendererOverridesHandler::OnSetTouchEventEmulationEnabled() {
205 return screencast_command_.get() != NULL; 210 return screencast_command_.get() != NULL;
206 } 211 }
207 212
208 void RendererOverridesHandler::InnerSwapCompositorFrame() { 213 void RendererOverridesHandler::InnerSwapCompositorFrame() {
209 if ((base::TimeTicks::Now() - last_frame_time_).InMilliseconds() < 214 if ((base::TimeTicks::Now() - last_frame_time_).InMilliseconds() <
210 kFrameRateThresholdMs) { 215 kFrameRateThresholdMs) {
211 return; 216 return;
(...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after
890 color_picker_enabled_ = enabled; 895 color_picker_enabled_ = enabled;
891 896
892 if (!host_) 897 if (!host_)
893 return; 898 return;
894 899
895 if (enabled) { 900 if (enabled) {
896 host_->AddMouseEventCallback(mouse_event_callback_); 901 host_->AddMouseEventCallback(mouse_event_callback_);
897 UpdateColorPickerFrame(); 902 UpdateColorPickerFrame();
898 } else { 903 } else {
899 host_->RemoveMouseEventCallback(mouse_event_callback_); 904 host_->RemoveMouseEventCallback(mouse_event_callback_);
900 color_picker_frame_.reset(); 905 ResetColorPickerFrame();
901 906
902 WebCursor pointer_cursor; 907 WebCursor pointer_cursor;
903 WebCursor::CursorInfo cursor_info; 908 WebCursor::CursorInfo cursor_info;
904 cursor_info.type = blink::WebCursorInfo::TypePointer; 909 cursor_info.type = blink::WebCursorInfo::TypePointer;
905 pointer_cursor.InitFromCursorInfo(cursor_info); 910 pointer_cursor.InitFromCursorInfo(cursor_info);
906 host_->SetCursor(pointer_cursor); 911 host_->SetCursor(pointer_cursor);
907 } 912 }
908 } 913 }
909 914
910 void RendererOverridesHandler::UpdateColorPickerFrame() { 915 void RendererOverridesHandler::UpdateColorPickerFrame() {
911 if (!host_) 916 if (!host_)
912 return; 917 return;
913 RenderWidgetHostViewBase* view = 918 RenderWidgetHostViewBase* view =
914 static_cast<RenderWidgetHostViewBase*>(host_->GetView()); 919 static_cast<RenderWidgetHostViewBase*>(host_->GetView());
915 if (!view) 920 if (!view)
916 return; 921 return;
917 922
918 gfx::Size size = view->GetViewBounds().size(); 923 gfx::Size size = view->GetViewBounds().size();
919 view->CopyFromCompositingSurface( 924 view->CopyFromCompositingSurface(
920 gfx::Rect(size), size, 925 gfx::Rect(size), size,
921 base::Bind(&RendererOverridesHandler::ColorPickerFrameUpdated, 926 base::Bind(&RendererOverridesHandler::ColorPickerFrameUpdated,
922 weak_factory_.GetWeakPtr()), 927 weak_factory_.GetWeakPtr()),
923 kN32_SkColorType); 928 kN32_SkColorType);
924 } 929 }
925 930
931 void RendererOverridesHandler::ResetColorPickerFrame() {
932 color_picker_frame_.reset();
933 last_cursor_x_ = -1;
934 last_cursor_y_ = -1;
935 }
936
926 void RendererOverridesHandler::ColorPickerFrameUpdated( 937 void RendererOverridesHandler::ColorPickerFrameUpdated(
927 bool succeeded, 938 bool succeeded,
928 const SkBitmap& bitmap) { 939 const SkBitmap& bitmap) {
929 if (succeeded) 940 if (!color_picker_enabled_)
941 return;
942
943 if (succeeded) {
930 color_picker_frame_ = bitmap; 944 color_picker_frame_ = bitmap;
945 UpdateColorPickerCursor();
946 }
931 } 947 }
932 948
933 bool RendererOverridesHandler::HandleMouseEvent( 949 bool RendererOverridesHandler::HandleMouseEvent(
934 const blink::WebMouseEvent& event) { 950 const blink::WebMouseEvent& event) {
951 last_cursor_x_ = event.x;
952 last_cursor_y_ = event.y;
935 if (color_picker_frame_.drawsNothing()) 953 if (color_picker_frame_.drawsNothing())
936 return true; 954 return true;
937 955
938 if (event.button == blink::WebMouseEvent::ButtonLeft) { 956 if (event.button == blink::WebMouseEvent::ButtonLeft) {
939 color_picker_frame_.lockPixels(); 957 if (last_cursor_x_ < 0 || last_cursor_x_ >= color_picker_frame_.width() ||
940 SkColor color = color_picker_frame_.getColor(event.x, event.y); 958 last_cursor_y_ < 0 || last_cursor_y_ >= color_picker_frame_.height()) {
941 color_picker_frame_.unlockPixels(); 959 return true;
960 }
961
962 SkAutoLockPixels lock_image(color_picker_frame_);
963 SkColor color = color_picker_frame_.getColor(last_cursor_x_,
964 last_cursor_y_);
942 base::DictionaryValue* color_dict = new base::DictionaryValue(); 965 base::DictionaryValue* color_dict = new base::DictionaryValue();
943 color_dict->SetInteger("r", SkColorGetR(color)); 966 color_dict->SetInteger("r", SkColorGetR(color));
944 color_dict->SetInteger("g", SkColorGetG(color)); 967 color_dict->SetInteger("g", SkColorGetG(color));
945 color_dict->SetInteger("b", SkColorGetB(color)); 968 color_dict->SetInteger("b", SkColorGetB(color));
946 color_dict->SetInteger("a", SkColorGetA(color)); 969 color_dict->SetInteger("a", SkColorGetA(color));
947 base::DictionaryValue* response = new base::DictionaryValue(); 970 base::DictionaryValue* response = new base::DictionaryValue();
948 response->Set(devtools::Page::colorPicked::kParamColor, color_dict); 971 response->Set(devtools::Page::colorPicked::kParamColor, color_dict);
949 SendNotification(devtools::Page::colorPicked::kName, response); 972 SendNotification(devtools::Page::colorPicked::kName, response);
950 } 973 }
974 UpdateColorPickerCursor();
975 return true;
976 }
951 977
952 if (!host_) 978 void RendererOverridesHandler::UpdateColorPickerCursor() {
953 return true; 979 if (!host_ || color_picker_frame_.drawsNothing())
980 return;
981
982 if (last_cursor_x_ < 0 || last_cursor_x_ >= color_picker_frame_.width() ||
983 last_cursor_y_ < 0 || last_cursor_y_ >= color_picker_frame_.height()) {
984 return;
985 }
986
954 RenderWidgetHostViewBase* view = static_cast<RenderWidgetHostViewBase*>( 987 RenderWidgetHostViewBase* view = static_cast<RenderWidgetHostViewBase*>(
955 host_->GetView()); 988 host_->GetView());
956 if (!view) 989 if (!view)
957 return true; 990 return;
958 991
959 // Due to platform limitations, we are using two different cursors 992 // Due to platform limitations, we are using two different cursors
960 // depending on the platform. Mac and Win have large cursors with two circles 993 // depending on the platform. Mac and Win have large cursors with two circles
961 // for original spot and its magnified projection; Linux gets smaller (64 px) 994 // for original spot and its magnified projection; Linux gets smaller (64 px)
962 // magnified projection only with centered hotspot. 995 // magnified projection only with centered hotspot.
963 // Mac Retina requires cursor to be > 120px in order to render smoothly. 996 // Mac Retina requires cursor to be > 120px in order to render smoothly.
964 997
965 #if defined(OS_LINUX) 998 #if defined(OS_LINUX)
966 const float kCursorSize = 63; 999 const float kCursorSize = 63;
967 const float kDiameter = 63; 1000 const float kDiameter = 63;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1004 1037
1005 // Clip circle for magnified projection. 1038 // Clip circle for magnified projection.
1006 float padding = (kCursorSize - kDiameter) / 2; 1039 float padding = (kCursorSize - kDiameter) / 2;
1007 SkPath clip_path; 1040 SkPath clip_path;
1008 clip_path.addOval(SkRect::MakeXYWH(padding, padding, kDiameter, kDiameter)); 1041 clip_path.addOval(SkRect::MakeXYWH(padding, padding, kDiameter, kDiameter));
1009 clip_path.close(); 1042 clip_path.close();
1010 canvas->clipPath(clip_path, SkRegion::kIntersect_Op, true); 1043 canvas->clipPath(clip_path, SkRegion::kIntersect_Op, true);
1011 1044
1012 // Project pixels. 1045 // Project pixels.
1013 int pixel_count = kDiameter / kPixelSize; 1046 int pixel_count = kDiameter / kPixelSize;
1014 SkRect src_rect = SkRect::MakeXYWH(event.x - pixel_count / 2, 1047 SkRect src_rect = SkRect::MakeXYWH(last_cursor_x_ - pixel_count / 2,
1015 event.y - pixel_count / 2, 1048 last_cursor_y_ - pixel_count / 2,
1016 pixel_count, pixel_count); 1049 pixel_count, pixel_count);
1017 SkRect dst_rect = SkRect::MakeXYWH(padding, padding, kDiameter, kDiameter); 1050 SkRect dst_rect = SkRect::MakeXYWH(padding, padding, kDiameter, kDiameter);
1018 canvas->drawBitmapRectToRect(color_picker_frame_, &src_rect, dst_rect); 1051 canvas->drawBitmapRectToRect(color_picker_frame_, &src_rect, dst_rect);
1019 1052
1020 // Paint grid. 1053 // Paint grid.
1021 paint.setStrokeWidth(1); 1054 paint.setStrokeWidth(1);
1022 paint.setAntiAlias(false); 1055 paint.setAntiAlias(false);
1023 paint.setColor(SK_ColorGRAY); 1056 paint.setColor(SK_ColorGRAY);
1024 for (int i = 0; i < pixel_count; ++i) { 1057 for (int i = 0; i < pixel_count; ++i) {
1025 canvas->drawLine(padding + i * kPixelSize, padding, 1058 canvas->drawLine(padding + i * kPixelSize, padding,
1026 padding + i * kPixelSize, kCursorSize - padding, paint); 1059 padding + i * kPixelSize, kCursorSize - padding, paint);
1027 canvas->drawLine(padding, padding + i * kPixelSize, 1060 canvas->drawLine(padding, padding + i * kPixelSize,
1028 kCursorSize - padding, padding + i * kPixelSize, paint); 1061 kCursorSize - padding, padding + i * kPixelSize, paint);
1029 } 1062 }
1030 1063
1031 // Paint central pixel in red. 1064 // Paint central pixel in red.
1032 SkRect pixel = SkRect::MakeXYWH((kCursorSize - kPixelSize) / 2, 1065 SkRect pixel = SkRect::MakeXYWH((kCursorSize - kPixelSize) / 2,
1033 (kCursorSize - kPixelSize) / 2, 1066 (kCursorSize - kPixelSize) / 2,
1034 kPixelSize, kPixelSize); 1067 kPixelSize, kPixelSize);
1035 paint.setColor(SK_ColorRED); 1068 paint.setColor(SK_ColorRED);
1069 paint.setStyle(SkPaint::kStroke_Style);
1036 canvas->drawRect(pixel, paint); 1070 canvas->drawRect(pixel, paint);
1037 1071
1038 // Paint outline. 1072 // Paint outline.
1039 paint.setStrokeWidth(2); 1073 paint.setStrokeWidth(2);
1040 paint.setColor(SK_ColorDKGRAY); 1074 paint.setColor(SK_ColorDKGRAY);
1041 paint.setAntiAlias(true); 1075 paint.setAntiAlias(true);
1042 paint.setStyle(SkPaint::kStroke_Style);
1043 canvas->drawCircle(kCursorSize / 2, kCursorSize / 2, kDiameter / 2, paint); 1076 canvas->drawCircle(kCursorSize / 2, kCursorSize / 2, kDiameter / 2, paint);
1044 1077
1045 SkBitmap result; 1078 SkBitmap result;
1046 result.allocN32Pixels(kCursorSize * device_scale_factor, 1079 result.allocN32Pixels(kCursorSize * device_scale_factor,
1047 kCursorSize * device_scale_factor); 1080 kCursorSize * device_scale_factor);
1048 canvas->readPixels(&result, 0, 0); 1081 canvas->readPixels(&result, 0, 0);
1049 1082
1050 WebCursor cursor; 1083 WebCursor cursor;
1051 WebCursor::CursorInfo cursor_info; 1084 WebCursor::CursorInfo cursor_info;
1052 cursor_info.type = blink::WebCursorInfo::TypeCustom; 1085 cursor_info.type = blink::WebCursorInfo::TypeCustom;
1053 cursor_info.image_scale_factor = device_scale_factor; 1086 cursor_info.image_scale_factor = device_scale_factor;
1054 cursor_info.custom_image = result; 1087 cursor_info.custom_image = result;
1055 cursor_info.hotspot = 1088 cursor_info.hotspot =
1056 gfx::Point(kHotspotOffset * device_scale_factor, 1089 gfx::Point(kHotspotOffset * device_scale_factor,
1057 kHotspotOffset * device_scale_factor); 1090 kHotspotOffset * device_scale_factor);
1058 #if defined(OS_WIN) 1091 #if defined(OS_WIN)
1059 cursor_info.external_handle = 0; 1092 cursor_info.external_handle = 0;
1060 #endif 1093 #endif
1061 1094
1062 cursor.InitFromCursorInfo(cursor_info); 1095 cursor.InitFromCursorInfo(cursor_info);
1063 DCHECK(host_); 1096 DCHECK(host_);
1064 host_->SetCursor(cursor); 1097 host_->SetCursor(cursor);
1065 return true;
1066 } 1098 }
1067 1099
1068 // Input agent handlers ------------------------------------------------------ 1100 // Input agent handlers ------------------------------------------------------
1069 1101
1070 scoped_refptr<DevToolsProtocol::Response> 1102 scoped_refptr<DevToolsProtocol::Response>
1071 RendererOverridesHandler::InputEmulateTouchFromMouseEvent( 1103 RendererOverridesHandler::InputEmulateTouchFromMouseEvent(
1072 scoped_refptr<DevToolsProtocol::Command> command) { 1104 scoped_refptr<DevToolsProtocol::Command> command) {
1073 if (!screencast_command_) 1105 if (!screencast_command_)
1074 return command->InternalErrorResponse("Screencast should be turned on"); 1106 return command->InternalErrorResponse("Screencast should be turned on");
1075 1107
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1190 return command->InternalErrorResponse("Could not connect to view"); 1222 return command->InternalErrorResponse("Could not connect to view");
1191 1223
1192 if (event->type == WebInputEvent::MouseWheel) 1224 if (event->type == WebInputEvent::MouseWheel)
1193 host_->ForwardWheelEvent(wheel_event); 1225 host_->ForwardWheelEvent(wheel_event);
1194 else 1226 else
1195 host_->ForwardMouseEvent(mouse_event); 1227 host_->ForwardMouseEvent(mouse_event);
1196 return command->SuccessResponse(NULL); 1228 return command->SuccessResponse(NULL);
1197 } 1229 }
1198 1230
1199 } // namespace content 1231 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/devtools/renderer_overrides_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698