Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / renderer / chrome_render_frame_observer.cc
blob9db49fb73b2a25790d6357aee523eb824b38b3e1
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 "chrome/common/prerender_messages.h"
8 #include "chrome/common/print_messages.h"
9 #include "chrome/common/render_messages.h"
10 #include "chrome/renderer/prerender/prerender_helper.h"
11 #include "chrome/renderer/printing/print_web_view_helper.h"
12 #include "content/public/renderer/render_frame.h"
13 #include "skia/ext/image_operations.h"
14 #include "skia/ext/platform_canvas.h"
15 #include "third_party/WebKit/public/platform/WebImage.h"
16 #include "third_party/WebKit/public/web/WebElement.h"
17 #include "third_party/WebKit/public/web/WebNode.h"
19 using blink::WebElement;
20 using blink::WebNode;
22 namespace {
23 // If the source image is null or occupies less area than
24 // |thumbnail_min_area_pixels|, we return the image unmodified. Otherwise, we
25 // scale down the image so that the width and height do not exceed
26 // |thumbnail_max_size_pixels|, preserving the original aspect ratio.
27 SkBitmap Downscale(blink::WebImage image,
28 int thumbnail_min_area_pixels,
29 gfx::Size thumbnail_max_size_pixels) {
30 if (image.isNull())
31 return SkBitmap();
33 gfx::Size image_size = image.size();
35 if (image_size.GetArea() < thumbnail_min_area_pixels)
36 return image.getSkBitmap();
38 if (image_size.width() <= thumbnail_max_size_pixels.width() &&
39 image_size.height() <= thumbnail_max_size_pixels.height())
40 return image.getSkBitmap();
42 gfx::SizeF scaled_size = image_size;
44 if (scaled_size.width() > thumbnail_max_size_pixels.width()) {
45 scaled_size.Scale(thumbnail_max_size_pixels.width() / scaled_size.width());
48 if (scaled_size.height() > thumbnail_max_size_pixels.height()) {
49 scaled_size.Scale(
50 thumbnail_max_size_pixels.height() / scaled_size.height());
53 return skia::ImageOperations::Resize(image.getSkBitmap(),
54 skia::ImageOperations::RESIZE_GOOD,
55 static_cast<int>(scaled_size.width()),
56 static_cast<int>(scaled_size.height()));
60 ChromeRenderFrameObserver::ChromeRenderFrameObserver(
61 content::RenderFrame* render_frame)
62 : content::RenderFrameObserver(render_frame) {
65 ChromeRenderFrameObserver::~ChromeRenderFrameObserver() {
68 bool ChromeRenderFrameObserver::OnMessageReceived(const IPC::Message& message) {
69 // Filter only.
70 bool handled = true;
71 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver, message)
72 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering, OnSetIsPrerendering)
73 IPC_MESSAGE_UNHANDLED(handled = false)
74 IPC_END_MESSAGE_MAP()
75 if (handled)
76 return false;
78 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver, message)
79 IPC_MESSAGE_HANDLER(ChromeViewMsg_RequestThumbnailForContextNode,
80 OnRequestThumbnailForContextNode)
81 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu,
82 OnPrintNodeUnderContextMenu)
83 IPC_MESSAGE_UNHANDLED(handled = false)
84 IPC_END_MESSAGE_MAP()
86 return handled;
89 void ChromeRenderFrameObserver::OnSetIsPrerendering(bool is_prerendering) {
90 if (is_prerendering) {
91 // If the PrerenderHelper for this frame already exists, don't create it. It
92 // can already be created for subframes during handling of
93 // RenderFrameCreated, if the parent frame was prerendering at time of
94 // subframe creation.
95 if (prerender::PrerenderHelper::Get(render_frame()))
96 return;
98 // The PrerenderHelper will destroy itself either after recording histograms
99 // or on destruction of the RenderView.
100 new prerender::PrerenderHelper(render_frame());
104 void ChromeRenderFrameObserver::OnRequestThumbnailForContextNode(
105 int thumbnail_min_area_pixels,
106 const gfx::Size& thumbnail_max_size_pixels) {
107 WebNode context_node = render_frame()->GetContextMenuNode();
108 SkBitmap thumbnail;
109 gfx::Size original_size;
110 if (!context_node.isNull() && context_node.isElementNode()) {
111 blink::WebImage image = context_node.to<WebElement>().imageContents();
112 original_size = image.size();
113 thumbnail = Downscale(image,
114 thumbnail_min_area_pixels,
115 thumbnail_max_size_pixels);
117 Send(new ChromeViewHostMsg_RequestThumbnailForContextNode_ACK(
118 routing_id(), thumbnail, original_size));
121 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() {
122 printing::PrintWebViewHelper* helper =
123 printing::PrintWebViewHelper::Get(render_frame()->GetRenderView());
124 if (helper)
125 helper->PrintNode(render_frame()->GetContextMenuNode());