Gallery: Add animation when zooming pictures.
[chromium-blink-merge.git] / chrome / renderer / chrome_render_frame_observer.cc
blob2a9b594a734e40d6cf846592e82fbc400598e211
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;
21 using blink::WebNode;
23 namespace {
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) {
32 if (image.isNull())
33 return SkBitmap();
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()) {
51 scaled_size.Scale(
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()));
61 } // namespace
63 ChromeRenderFrameObserver::ChromeRenderFrameObserver(
64 content::RenderFrame* render_frame)
65 : content::RenderFrameObserver(render_frame) {
68 ChromeRenderFrameObserver::~ChromeRenderFrameObserver() {
71 bool ChromeRenderFrameObserver::OnMessageReceived(const IPC::Message& message) {
72 // Filter only.
73 bool handled = true;
74 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver, message)
75 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering, OnSetIsPrerendering)
76 IPC_MESSAGE_UNHANDLED(handled = false)
77 IPC_END_MESSAGE_MAP()
78 if (handled)
79 return 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)
87 IPC_END_MESSAGE_MAP()
89 return handled;
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
97 // subframe creation.
98 if (prerender::PrerenderHelper::Get(render_frame()))
99 return;
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();
111 SkBitmap thumbnail;
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());
127 if (helper)
128 helper->PrintNode(render_frame()->GetContextMenuNode());