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

Side by Side Diff: chrome/renderer/automation/automation_renderer_helper.cc

Issue 8294030: Fix snapshotting on linux by creating a separate automation path for (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address Pawel's comments Created 9 years, 2 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/renderer/automation/automation_renderer_helper.h" 5 #include "chrome/renderer/automation/automation_renderer_helper.h"
6 6
7 #include <algorithm>
8
7 #include "base/basictypes.h" 9 #include "base/basictypes.h"
8 #include "chrome/common/automation_messages.h" 10 #include "chrome/common/automation_messages.h"
11 #include "content/public/renderer/render_view.h"
12 #include "skia/ext/platform_canvas.h"
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
17 #include "ui/gfx/codec/png_codec.h"
18 #include "ui/gfx/point.h"
19 #include "ui/gfx/rect.h"
20 #include "ui/gfx/size.h"
21 #include "webkit/glue/webkit_glue.h"
11 22
12 using WebKit::WebFrame; 23 using WebKit::WebFrame;
24 using WebKit::WebSize;
13 using WebKit::WebURL; 25 using WebKit::WebURL;
26 using WebKit::WebView;
14 27
15 AutomationRendererHelper::AutomationRendererHelper( 28 AutomationRendererHelper::AutomationRendererHelper(
16 content::RenderView* render_view) 29 content::RenderView* render_view)
17 : content::RenderViewObserver(render_view) { 30 : content::RenderViewObserver(render_view),
31 content::RenderViewObserverTracker<AutomationRendererHelper>(
32 render_view) {
18 } 33 }
19 34
20 AutomationRendererHelper::~AutomationRendererHelper() { } 35 AutomationRendererHelper::~AutomationRendererHelper() { }
21 36
37 bool AutomationRendererHelper::SnapshotEntirePage(
38 WebView* view,
39 std::vector<unsigned char>* png_data,
40 std::string* error_msg) {
41 // We don't want to change the widget's layout by resizing. Just scroll
42 // the main frame and incremently capture the full page contents.
43 WebFrame* frame = view->mainFrame();
44 WebSize old_scroll = frame->scrollOffset();
45 // For RTL, the minimum scroll offset may be negative and the maximum zero.
46 WebSize min_scroll = frame->minimumScrollOffset();
47 WebSize max_scroll = frame->maximumScrollOffset();
48 WebSize scroll_size(max_scroll.width - min_scroll.width,
49 max_scroll.height - min_scroll.height);
50 WebSize content_size = frame->contentsSize();
51 // This is the size of the content that is visible in the scroll view (i.e.,
52 // the window size minus scrollbars).
53 WebSize viewport_size(content_size.width - scroll_size.width,
54 content_size.height - scroll_size.height);
55 if (viewport_size.width <= 0 || viewport_size.height <= 0) {
56 *error_msg = "cannot take snapshot because viewport is too small";
57 return false;
58 }
59
60 skia::PlatformCanvas canvas(
61 content_size.width, content_size.height, true /* is_opaque */);
62 for (int y = 0; y < content_size.height; y += viewport_size.height) {
63 int constrain_y = std::min(y, content_size.height - viewport_size.height);
64 for (int x = 0; x < content_size.width; x += viewport_size.width) {
65 int constrain_x = std::min(x, content_size.width - viewport_size.width);
66 frame->setScrollOffset(WebSize(constrain_x, constrain_y));
67 canvas.save();
68 canvas.translate(static_cast<SkScalar>(constrain_x),
69 static_cast<SkScalar>(constrain_y));
70 gfx::Point offset(x - constrain_x, y - constrain_y);
71 gfx::Rect view_region(offset, gfx::Size(
72 viewport_size.width - offset.x(), viewport_size.height - offset.y()));
73 view->paint(webkit_glue::ToWebCanvas(&canvas), view_region);
74 canvas.restore();
75 }
76 }
77 // One would think scrollOffset and setScrollOffset would deal in the
78 // same type of coordinates, but using just |old_scroll| here doesn't work
79 // for RTL pages.
80 frame->setScrollOffset(WebSize(old_scroll.width - min_scroll.width,
81 old_scroll.height - min_scroll.height));
82
83 const SkBitmap& bmp = skia::GetTopDevice(canvas)->accessBitmap(false);
84 SkAutoLockPixels lock_pixels(bmp);
85 // EncodeBGRA uses FORMAT_SkBitmap, which doesn't work on windows for some
86 // cases dealing with transparency. See crbug.com/96317. Use FORMAT_BGRA.
87 bool encode_success = gfx::PNGCodec::Encode(
88 reinterpret_cast<unsigned char*>(bmp.getPixels()),
89 gfx::PNGCodec::FORMAT_BGRA,
90 gfx::Size(bmp.width(), bmp.height()),
91 bmp.rowBytes(),
92 true, // discard_transparency
93 std::vector<gfx::PNGCodec::Comment>(),
94 png_data);
95 if (!encode_success)
96 *error_msg = "failed to encode image as png";
97 return encode_success;
98 }
99
100 void AutomationRendererHelper::OnSnapshotEntirePage() {
101 std::vector<unsigned char> png_data;
102 std::string error_msg;
103 bool success = false;
104 if (render_view()->GetWebView()) {
105 success = SnapshotEntirePage(
106 render_view()->GetWebView(), &png_data, &error_msg);
107 } else {
108 error_msg = "cannot snapshot page because webview is null";
109 }
110
111 // Check that the image is not too large, allowing a 1kb buffer for other
112 // message data.
113 if (success && png_data.size() > IPC::Channel::kMaximumMessageSize - 1024) {
114 png_data.clear();
115 success = false;
116 error_msg = "image is too large to be transferred over ipc";
117 }
118 Send(new AutomationMsg_SnapshotEntirePageACK(
119 routing_id(), success, png_data, error_msg));
120 }
121
122 bool AutomationRendererHelper::OnMessageReceived(const IPC::Message& message) {
123 bool handled = true;
124 bool deserialize_success = true;
125 IPC_BEGIN_MESSAGE_MAP_EX(AutomationRendererHelper, message,
126 deserialize_success)
127 IPC_MESSAGE_HANDLER(AutomationMsg_SnapshotEntirePage, OnSnapshotEntirePage)
128 IPC_MESSAGE_UNHANDLED(handled = false)
129 IPC_END_MESSAGE_MAP_EX()
130 if (!deserialize_success) {
131 LOG(ERROR) << "Failed to deserialize an IPC message";
132 }
133 return handled;
134 }
135
22 void AutomationRendererHelper::WillPerformClientRedirect( 136 void AutomationRendererHelper::WillPerformClientRedirect(
23 WebFrame* frame, const WebURL& from, const WebURL& to, double interval, 137 WebFrame* frame, const WebURL& from, const WebURL& to, double interval,
24 double fire_time) { 138 double fire_time) {
25 Send(new AutomationMsg_WillPerformClientRedirect( 139 Send(new AutomationMsg_WillPerformClientRedirect(
26 routing_id(), frame->identifier(), interval)); 140 routing_id(), frame->identifier(), interval));
27 } 141 }
28 142
29 void AutomationRendererHelper::DidCancelClientRedirect(WebFrame* frame) { 143 void AutomationRendererHelper::DidCancelClientRedirect(WebFrame* frame) {
30 Send(new AutomationMsg_DidCompleteOrCancelClientRedirect( 144 Send(new AutomationMsg_DidCompleteOrCancelClientRedirect(
31 routing_id(), frame->identifier())); 145 routing_id(), frame->identifier()));
32 } 146 }
33 147
34 void AutomationRendererHelper::DidCompleteClientRedirect( 148 void AutomationRendererHelper::DidCompleteClientRedirect(
35 WebFrame* frame, const WebURL& from) { 149 WebFrame* frame, const WebURL& from) {
36 Send(new AutomationMsg_DidCompleteOrCancelClientRedirect( 150 Send(new AutomationMsg_DidCompleteOrCancelClientRedirect(
37 routing_id(), frame->identifier())); 151 routing_id(), frame->identifier()));
38 } 152 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698