This source file includes following definitions.
- Downscale
- OnMessageReceived
- OnSetIsPrerendering
- OnRequestThumbnailForContextNode
- OnPrintNodeUnderContextMenu
#include "chrome/renderer/chrome_render_frame_observer.h"
#include "chrome/common/prerender_messages.h"
#include "chrome/common/print_messages.h"
#include "chrome/common/render_messages.h"
#include "chrome/renderer/prerender/prerender_helper.h"
#include "chrome/renderer/printing/print_web_view_helper.h"
#include "content/public/renderer/render_frame.h"
#include "skia/ext/image_operations.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/WebKit/public/platform/WebImage.h"
#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebNode.h"
using blink::WebElement;
using blink::WebNode;
namespace {
SkBitmap Downscale(blink::WebImage image,
int thumbnail_min_area_pixels,
gfx::Size thumbnail_max_size_pixels) {
if (image.isNull())
return SkBitmap();
gfx::Size image_size = image.size();
if (image_size.GetArea() < thumbnail_min_area_pixels)
return image.getSkBitmap();
if (image_size.width() <= thumbnail_max_size_pixels.width() &&
image_size.height() <= thumbnail_max_size_pixels.height())
return image.getSkBitmap();
gfx::SizeF scaled_size = image_size;
if (scaled_size.width() > thumbnail_max_size_pixels.width()) {
scaled_size.Scale(thumbnail_max_size_pixels.width() / scaled_size.width());
}
if (scaled_size.height() > thumbnail_max_size_pixels.height()) {
scaled_size.Scale(
thumbnail_max_size_pixels.height() / scaled_size.height());
}
return skia::ImageOperations::Resize(image.getSkBitmap(),
skia::ImageOperations::RESIZE_GOOD,
static_cast<int>(scaled_size.width()),
static_cast<int>(scaled_size.height()));
}
}
ChromeRenderFrameObserver::ChromeRenderFrameObserver(
content::RenderFrame* render_frame)
: content::RenderFrameObserver(render_frame) {
}
ChromeRenderFrameObserver::~ChromeRenderFrameObserver() {
}
bool ChromeRenderFrameObserver::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver, message)
IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering, OnSetIsPrerendering)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
if (handled)
return false;
IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver, message)
IPC_MESSAGE_HANDLER(ChromeViewMsg_RequestThumbnailForContextNode,
OnRequestThumbnailForContextNode)
IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu,
OnPrintNodeUnderContextMenu)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void ChromeRenderFrameObserver::OnSetIsPrerendering(bool is_prerendering) {
if (is_prerendering) {
if (prerender::PrerenderHelper::Get(render_frame()))
return;
new prerender::PrerenderHelper(render_frame());
}
}
void ChromeRenderFrameObserver::OnRequestThumbnailForContextNode(
int thumbnail_min_area_pixels,
const gfx::Size& thumbnail_max_size_pixels) {
WebNode context_node = render_frame()->GetContextMenuNode();
SkBitmap thumbnail;
gfx::Size original_size;
if (!context_node.isNull() && context_node.isElementNode()) {
blink::WebImage image = context_node.to<WebElement>().imageContents();
original_size = image.size();
thumbnail = Downscale(image,
thumbnail_min_area_pixels,
thumbnail_max_size_pixels);
}
Send(new ChromeViewHostMsg_RequestThumbnailForContextNode_ACK(
routing_id(), thumbnail, original_size));
}
void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() {
printing::PrintWebViewHelper* helper =
printing::PrintWebViewHelper::Get(render_frame()->GetRenderView());
if (helper)
helper->PrintNode(render_frame()->GetContextMenuNode());
}