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

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: fix win Created 9 years, 1 month 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/rect.h"
19 #include "webkit/glue/webkit_glue.h"
11 20
12 using WebKit::WebFrame; 21 using WebKit::WebFrame;
22 using WebKit::WebSize;
13 using WebKit::WebURL; 23 using WebKit::WebURL;
24 using WebKit::WebView;
14 25
15 AutomationRendererHelper::AutomationRendererHelper( 26 AutomationRendererHelper::AutomationRendererHelper(
16 content::RenderView* render_view) 27 content::RenderView* render_view)
17 : content::RenderViewObserver(render_view) { 28 : content::RenderViewObserver(render_view),
29 content::RenderViewObserverTracker<AutomationRendererHelper>(
30 render_view) {
18 } 31 }
19 32
20 AutomationRendererHelper::~AutomationRendererHelper() { } 33 AutomationRendererHelper::~AutomationRendererHelper() { }
21 34
35 bool AutomationRendererHelper::SnapshotEntirePage(
36 WebView* view,
37 std::vector<unsigned char>* png_data,
38 std::string* error_msg) {
39 WebFrame* frame = view->mainFrame();
40 WebSize old_size = view->size();
41 WebSize new_size = frame->contentsSize();
42 // For RTL, the minimum scroll offset may be negative.
43 WebSize min_scroll = frame->minimumScrollOffset();
44 WebSize old_scroll = frame->scrollOffset();
45 bool fixed_layout_enabled = view->isFixedLayoutModeEnabled();
46 WebSize fixed_size = view->fixedLayoutSize();
47
48 frame->setCanHaveScrollbars(false);
49 view->setFixedLayoutSize(old_size);
50 view->enableFixedLayoutMode(true);
51 view->resize(new_size);
52 view->layout();
53 frame->setScrollOffset(WebSize(0, 0));
54
55 skia::PlatformCanvas canvas(
56 new_size.width, new_size.height, true /* is_opaque */);
57 view->paint(webkit_glue::ToWebCanvas(&canvas),
58 gfx::Rect(0, 0, new_size.width, new_size.height));
59
60 frame->setCanHaveScrollbars(true);
61 view->setFixedLayoutSize(fixed_size);
62 view->enableFixedLayoutMode(fixed_layout_enabled);
63 view->resize(old_size);
64 view->layout();
65 frame->setScrollOffset(WebSize(old_scroll.width - min_scroll.width,
66 old_scroll.height - min_scroll.height));
67
68 const SkBitmap& bmp = skia::GetTopDevice(canvas)->accessBitmap(false);
69 SkAutoLockPixels lock_pixels(bmp);
70 // EncodeBGRA uses FORMAT_SkBitmap, which doesn't work on windows for some
71 // cases dealing with transparency. See crbug.com/96317. Use FORMAT_BGRA.
72 bool encode_success = gfx::PNGCodec::Encode(
73 reinterpret_cast<unsigned char*>(bmp.getPixels()),
74 gfx::PNGCodec::FORMAT_BGRA,
75 gfx::Size(bmp.width(), bmp.height()),
76 bmp.rowBytes(),
77 true, // discard_transparency
78 std::vector<gfx::PNGCodec::Comment>(),
79 png_data);
80 if (!encode_success)
81 *error_msg = "failed to encode image as png";
82 return encode_success;
83 }
84
85 void AutomationRendererHelper::OnSnapshotEntirePage() {
86 std::vector<unsigned char> png_data;
87 std::string error_msg;
88 bool success = false;
89 if (render_view()->GetWebView()) {
90 success = SnapshotEntirePage(
91 render_view()->GetWebView(), &png_data, &error_msg);
92 } else {
93 error_msg = "cannot snapshot page because webview is null";
94 }
95
96 // Check that the image is not too large, allowing a 1kb buffer for other
97 // message data.
98 if (success && png_data.size() > IPC::Channel::kMaximumMessageSize - 1024) {
99 png_data.clear();
100 success = false;
101 error_msg = "image is too large to be transferred over ipc";
102 }
103 Send(new AutomationMsg_SnapshotEntirePageACK(
104 routing_id(), success, png_data, error_msg));
105 }
106
107 bool AutomationRendererHelper::OnMessageReceived(const IPC::Message& message) {
108 bool handled = true;
109 bool deserialize_success = true;
110 IPC_BEGIN_MESSAGE_MAP_EX(AutomationRendererHelper, message,
111 deserialize_success)
112 IPC_MESSAGE_HANDLER(AutomationMsg_SnapshotEntirePage, OnSnapshotEntirePage)
113 IPC_MESSAGE_UNHANDLED(handled = false)
114 IPC_END_MESSAGE_MAP_EX()
115 if (!deserialize_success) {
116 LOG(ERROR) << "Failed to deserialize an IPC message";
117 }
118 return handled;
119 }
120
22 void AutomationRendererHelper::WillPerformClientRedirect( 121 void AutomationRendererHelper::WillPerformClientRedirect(
23 WebFrame* frame, const WebURL& from, const WebURL& to, double interval, 122 WebFrame* frame, const WebURL& from, const WebURL& to, double interval,
24 double fire_time) { 123 double fire_time) {
25 Send(new AutomationMsg_WillPerformClientRedirect( 124 Send(new AutomationMsg_WillPerformClientRedirect(
26 routing_id(), frame->identifier(), interval)); 125 routing_id(), frame->identifier(), interval));
27 } 126 }
28 127
29 void AutomationRendererHelper::DidCancelClientRedirect(WebFrame* frame) { 128 void AutomationRendererHelper::DidCancelClientRedirect(WebFrame* frame) {
30 Send(new AutomationMsg_DidCompleteOrCancelClientRedirect( 129 Send(new AutomationMsg_DidCompleteOrCancelClientRedirect(
31 routing_id(), frame->identifier())); 130 routing_id(), frame->identifier()));
32 } 131 }
33 132
34 void AutomationRendererHelper::DidCompleteClientRedirect( 133 void AutomationRendererHelper::DidCompleteClientRedirect(
35 WebFrame* frame, const WebURL& from) { 134 WebFrame* frame, const WebURL& from) {
36 Send(new AutomationMsg_DidCompleteOrCancelClientRedirect( 135 Send(new AutomationMsg_DidCompleteOrCancelClientRedirect(
37 routing_id(), frame->identifier())); 136 routing_id(), frame->identifier()));
38 } 137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698