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

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

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

Powered by Google App Engine
This is Rietveld 408576698