OLD | NEW |
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 | |
9 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
10 #include "chrome/common/automation_messages.h" | 8 #include "chrome/common/automation_messages.h" |
11 #include "content/public/renderer/render_view.h" | |
12 #include "skia/ext/platform_canvas.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | 10 #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" | |
20 | 11 |
21 using WebKit::WebFrame; | 12 using WebKit::WebFrame; |
22 using WebKit::WebSize; | |
23 using WebKit::WebURL; | 13 using WebKit::WebURL; |
24 using WebKit::WebView; | |
25 | 14 |
26 AutomationRendererHelper::AutomationRendererHelper( | 15 AutomationRendererHelper::AutomationRendererHelper( |
27 content::RenderView* render_view) | 16 content::RenderView* render_view) |
28 : content::RenderViewObserver(render_view), | 17 : content::RenderViewObserver(render_view) { |
29 content::RenderViewObserverTracker<AutomationRendererHelper>( | |
30 render_view) { | |
31 } | 18 } |
32 | 19 |
33 AutomationRendererHelper::~AutomationRendererHelper() { } | 20 AutomationRendererHelper::~AutomationRendererHelper() { } |
34 | 21 |
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 | |
121 void AutomationRendererHelper::WillPerformClientRedirect( | 22 void AutomationRendererHelper::WillPerformClientRedirect( |
122 WebFrame* frame, const WebURL& from, const WebURL& to, double interval, | 23 WebFrame* frame, const WebURL& from, const WebURL& to, double interval, |
123 double fire_time) { | 24 double fire_time) { |
124 Send(new AutomationMsg_WillPerformClientRedirect( | 25 Send(new AutomationMsg_WillPerformClientRedirect( |
125 routing_id(), frame->identifier(), interval)); | 26 routing_id(), frame->identifier(), interval)); |
126 } | 27 } |
127 | 28 |
128 void AutomationRendererHelper::DidCancelClientRedirect(WebFrame* frame) { | 29 void AutomationRendererHelper::DidCancelClientRedirect(WebFrame* frame) { |
129 Send(new AutomationMsg_DidCompleteOrCancelClientRedirect( | 30 Send(new AutomationMsg_DidCompleteOrCancelClientRedirect( |
130 routing_id(), frame->identifier())); | 31 routing_id(), frame->identifier())); |
131 } | 32 } |
132 | 33 |
133 void AutomationRendererHelper::DidCompleteClientRedirect( | 34 void AutomationRendererHelper::DidCompleteClientRedirect( |
134 WebFrame* frame, const WebURL& from) { | 35 WebFrame* frame, const WebURL& from) { |
135 Send(new AutomationMsg_DidCompleteOrCancelClientRedirect( | 36 Send(new AutomationMsg_DidCompleteOrCancelClientRedirect( |
136 routing_id(), frame->identifier())); | 37 routing_id(), frame->identifier())); |
137 } | 38 } |
OLD | NEW |