1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/renderer/chrome_render_frame_observer.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/common/prerender_messages.h"
13 #include "chrome/common/render_messages.h"
14 #include "chrome/renderer/prerender/prerender_helper.h"
15 #include "components/printing/common/print_messages.h"
16 #include "components/printing/renderer/print_web_view_helper.h"
17 #include "content/public/renderer/render_frame.h"
18 #include "skia/ext/image_operations.h"
19 #include "third_party/WebKit/public/platform/WebImage.h"
20 #include "third_party/WebKit/public/web/WebElement.h"
21 #include "third_party/WebKit/public/web/WebNode.h"
22 #include "third_party/skia/include/core/SkBitmap.h"
23 #include "ui/gfx/codec/jpeg_codec.h"
25 using blink::WebElement
;
30 // If the source image is null or occupies less area than
31 // |thumbnail_min_area_pixels|, we return the image unmodified. Otherwise, we
32 // scale down the image so that the width and height do not exceed
33 // |thumbnail_max_size_pixels|, preserving the original aspect ratio.
34 SkBitmap
Downscale(const blink::WebImage
& image
,
35 int thumbnail_min_area_pixels
,
36 const gfx::Size
& thumbnail_max_size_pixels
) {
40 gfx::Size image_size
= image
.size();
42 if (image_size
.GetArea() < thumbnail_min_area_pixels
)
43 return image
.getSkBitmap();
45 if (image_size
.width() <= thumbnail_max_size_pixels
.width() &&
46 image_size
.height() <= thumbnail_max_size_pixels
.height())
47 return image
.getSkBitmap();
49 gfx::SizeF scaled_size
= image_size
;
51 if (scaled_size
.width() > thumbnail_max_size_pixels
.width()) {
52 scaled_size
.Scale(thumbnail_max_size_pixels
.width() / scaled_size
.width());
55 if (scaled_size
.height() > thumbnail_max_size_pixels
.height()) {
57 thumbnail_max_size_pixels
.height() / scaled_size
.height());
60 return skia::ImageOperations::Resize(image
.getSkBitmap(),
61 skia::ImageOperations::RESIZE_GOOD
,
62 static_cast<int>(scaled_size
.width()),
63 static_cast<int>(scaled_size
.height()));
68 ChromeRenderFrameObserver::ChromeRenderFrameObserver(
69 content::RenderFrame
* render_frame
)
70 : content::RenderFrameObserver(render_frame
) {
73 ChromeRenderFrameObserver::~ChromeRenderFrameObserver() {
76 bool ChromeRenderFrameObserver::OnMessageReceived(const IPC::Message
& message
) {
79 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver
, message
)
80 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering
, OnSetIsPrerendering
)
81 IPC_MESSAGE_UNHANDLED(handled
= false)
86 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver
, message
)
87 IPC_MESSAGE_HANDLER(ChromeViewMsg_RequestThumbnailForContextNode
,
88 OnRequestThumbnailForContextNode
)
89 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu
,
90 OnPrintNodeUnderContextMenu
)
91 IPC_MESSAGE_UNHANDLED(handled
= false)
97 void ChromeRenderFrameObserver::OnSetIsPrerendering(bool is_prerendering
) {
98 if (is_prerendering
) {
99 // If the PrerenderHelper for this frame already exists, don't create it. It
100 // can already be created for subframes during handling of
101 // RenderFrameCreated, if the parent frame was prerendering at time of
102 // subframe creation.
103 if (prerender::PrerenderHelper::Get(render_frame()))
106 // The PrerenderHelper will destroy itself either after recording histograms
107 // or on destruction of the RenderView.
108 new prerender::PrerenderHelper(render_frame());
112 void ChromeRenderFrameObserver::OnRequestThumbnailForContextNode(
113 int thumbnail_min_area_pixels
,
114 const gfx::Size
& thumbnail_max_size_pixels
) {
115 WebNode context_node
= render_frame()->GetContextMenuNode();
117 gfx::Size original_size
;
118 if (!context_node
.isNull() && context_node
.isElementNode()) {
119 blink::WebImage image
= context_node
.to
<WebElement
>().imageContents();
120 original_size
= image
.size();
121 thumbnail
= Downscale(image
,
122 thumbnail_min_area_pixels
,
123 thumbnail_max_size_pixels
);
127 if (thumbnail
.colorType() == kN32_SkColorType
)
130 thumbnail
.copyTo(&bitmap
, kN32_SkColorType
);
132 std::string thumbnail_data
;
133 SkAutoLockPixels
lock(bitmap
);
134 if (bitmap
.getPixels()) {
135 const int kDefaultQuality
= 90;
136 std::vector
<unsigned char> data
;
137 if (gfx::JPEGCodec::Encode(
138 reinterpret_cast<unsigned char*>(bitmap
.getAddr32(0, 0)),
139 gfx::JPEGCodec::FORMAT_SkBitmap
, bitmap
.width(), bitmap
.height(),
140 static_cast<int>(bitmap
.rowBytes()), kDefaultQuality
, &data
))
141 thumbnail_data
= std::string(data
.begin(), data
.end());
144 Send(new ChromeViewHostMsg_RequestThumbnailForContextNode_ACK(
145 routing_id(), thumbnail_data
, original_size
));
148 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() {
149 printing::PrintWebViewHelper
* helper
=
150 printing::PrintWebViewHelper::Get(render_frame()->GetRenderView());
152 helper
->PrintNode(render_frame()->GetContextMenuNode());