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"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/common/prerender_messages.h"
9 #include "chrome/common/print_messages.h"
10 #include "chrome/common/render_messages.h"
11 #include "chrome/renderer/prerender/prerender_helper.h"
12 #include "chrome/renderer/printing/print_web_view_helper.h"
13 #include "content/public/renderer/render_frame.h"
14 #include "skia/ext/image_operations.h"
15 #include "skia/ext/platform_canvas.h"
16 #include "third_party/WebKit/public/platform/WebImage.h"
17 #include "third_party/WebKit/public/web/WebElement.h"
18 #include "third_party/WebKit/public/web/WebNode.h"
20 using blink::WebElement
;
25 // If the source image is null or occupies less area than
26 // |thumbnail_min_area_pixels|, we return the image unmodified. Otherwise, we
27 // scale down the image so that the width and height do not exceed
28 // |thumbnail_max_size_pixels|, preserving the original aspect ratio.
29 SkBitmap
Downscale(const blink::WebImage
& image
,
30 int thumbnail_min_area_pixels
,
31 const gfx::Size
& thumbnail_max_size_pixels
) {
35 gfx::Size image_size
= image
.size();
37 if (image_size
.GetArea() < thumbnail_min_area_pixels
)
38 return image
.getSkBitmap();
40 if (image_size
.width() <= thumbnail_max_size_pixels
.width() &&
41 image_size
.height() <= thumbnail_max_size_pixels
.height())
42 return image
.getSkBitmap();
44 gfx::SizeF scaled_size
= image_size
;
46 if (scaled_size
.width() > thumbnail_max_size_pixels
.width()) {
47 scaled_size
.Scale(thumbnail_max_size_pixels
.width() / scaled_size
.width());
50 if (scaled_size
.height() > thumbnail_max_size_pixels
.height()) {
52 thumbnail_max_size_pixels
.height() / scaled_size
.height());
55 return skia::ImageOperations::Resize(image
.getSkBitmap(),
56 skia::ImageOperations::RESIZE_GOOD
,
57 static_cast<int>(scaled_size
.width()),
58 static_cast<int>(scaled_size
.height()));
63 ChromeRenderFrameObserver::ChromeRenderFrameObserver(
64 content::RenderFrame
* render_frame
)
65 : content::RenderFrameObserver(render_frame
) {
68 ChromeRenderFrameObserver::~ChromeRenderFrameObserver() {
71 bool ChromeRenderFrameObserver::OnMessageReceived(const IPC::Message
& message
) {
74 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver
, message
)
75 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering
, OnSetIsPrerendering
)
76 IPC_MESSAGE_UNHANDLED(handled
= false)
81 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver
, message
)
82 IPC_MESSAGE_HANDLER(ChromeViewMsg_RequestThumbnailForContextNode
,
83 OnRequestThumbnailForContextNode
)
84 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu
,
85 OnPrintNodeUnderContextMenu
)
86 IPC_MESSAGE_UNHANDLED(handled
= false)
92 void ChromeRenderFrameObserver::OnSetIsPrerendering(bool is_prerendering
) {
93 if (is_prerendering
) {
94 // If the PrerenderHelper for this frame already exists, don't create it. It
95 // can already be created for subframes during handling of
96 // RenderFrameCreated, if the parent frame was prerendering at time of
98 if (prerender::PrerenderHelper::Get(render_frame()))
101 // The PrerenderHelper will destroy itself either after recording histograms
102 // or on destruction of the RenderView.
103 new prerender::PrerenderHelper(render_frame());
107 void ChromeRenderFrameObserver::OnRequestThumbnailForContextNode(
108 int thumbnail_min_area_pixels
,
109 const gfx::Size
& thumbnail_max_size_pixels
) {
110 WebNode context_node
= render_frame()->GetContextMenuNode();
112 gfx::Size original_size
;
113 if (!context_node
.isNull() && context_node
.isElementNode()) {
114 blink::WebImage image
= context_node
.to
<WebElement
>().imageContents();
115 original_size
= image
.size();
116 thumbnail
= Downscale(image
,
117 thumbnail_min_area_pixels
,
118 thumbnail_max_size_pixels
);
120 Send(new ChromeViewHostMsg_RequestThumbnailForContextNode_ACK(
121 routing_id(), thumbnail
, original_size
));
124 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() {
125 printing::PrintWebViewHelper
* helper
=
126 printing::PrintWebViewHelper::Get(render_frame()->GetRenderView());
128 helper
->PrintNode(render_frame()->GetContextMenuNode());