diff options
author | jamesr@chromium.org <jamesr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-31 21:59:05 +0000 |
---|---|---|
committer | jamesr@chromium.org <jamesr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-31 21:59:05 +0000 |
commit | 31dd25ab9d478176dded4999473fd1cdf4e5fcc6 (patch) | |
tree | a33223129a1d11781d6e842dfe948e0ef4988caf /chrome/renderer/chrome_render_view_observer.cc | |
parent | f7dbc3f9ef3a4d9c1a1f08f180e031fd9181eb50 (diff) | |
download | chromium_src-31dd25ab9d478176dded4999473fd1cdf4e5fcc6.zip chromium_src-31dd25ab9d478176dded4999473fd1cdf4e5fcc6.tar.gz chromium_src-31dd25ab9d478176dded4999473fd1cdf4e5fcc6.tar.bz2 |
Add renderer side of IPC handler for context menu image thumbnail
BUG=89945
Review URL: https://chromiumcodereview.appspot.com/19994008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@214830 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/chrome_render_view_observer.cc')
-rw-r--r-- | chrome/renderer/chrome_render_view_observer.cc | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/chrome/renderer/chrome_render_view_observer.cc b/chrome/renderer/chrome_render_view_observer.cc index 5d67159..c6a45fd 100644 --- a/chrome/renderer/chrome_render_view_observer.cc +++ b/chrome/renderer/chrome_render_view_observer.cc @@ -30,6 +30,7 @@ #include "content/public/renderer/render_view.h" #include "extensions/common/constants.h" #include "net/base/data_url.h" +#include "skia/ext/image_operations.h" #include "skia/ext/platform_canvas.h" #include "third_party/WebKit/public/platform/WebCString.h" #include "third_party/WebKit/public/platform/WebRect.h" @@ -49,6 +50,7 @@ #include "ui/base/ui_base_switches_util.h" #include "ui/gfx/favicon_size.h" #include "ui/gfx/size.h" +#include "ui/gfx/size_f.h" #include "ui/gfx/skbitmap_operations.h" #include "v8/include/v8-testing.h" #include "webkit/glue/webkit_glue.h" @@ -169,6 +171,42 @@ GURL StripRef(const GURL& url) { replacements.ClearRef(); return url.ReplaceComponents(replacements); } + +// If the source image is null or occupies less area than +// |thumbnail_min_area_pixels|, we return the image unmodified. Otherwise, we +// scale down the image so that the width and height do not exceed +// |thumbnail_max_size_pixels|, preserving the original aspect ratio. +static SkBitmap Downscale(WebKit::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())); +} } // namespace ChromeRenderViewObserver::ChromeRenderViewObserver( @@ -211,6 +249,8 @@ bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) { OnSetClientSidePhishingDetection) IPC_MESSAGE_HANDLER(ChromeViewMsg_SetVisuallyDeemphasized, OnSetVisuallyDeemphasized) + IPC_MESSAGE_HANDLER(ChromeViewMsg_RequestThumbnailForContextNode, + OnRequestThumbnailForContextNode) #if defined(OS_CHROMEOS) IPC_MESSAGE_HANDLER(ChromeViewMsg_StartFrameSniffer, OnStartFrameSniffer) #endif @@ -336,6 +376,20 @@ void ChromeRenderViewObserver::OnSetVisuallyDeemphasized(bool deemphasized) { } } +void ChromeRenderViewObserver::OnRequestThumbnailForContextNode( + int thumbnail_min_area_pixels, gfx::Size thumbnail_max_size_pixels) { + WebNode context_node = render_view()->GetContextMenuNode(); + SkBitmap thumbnail; + if (context_node.isElementNode()) { + WebKit::WebImage image = context_node.to<WebElement>().imageContents(); + thumbnail = Downscale(image, + thumbnail_min_area_pixels, + thumbnail_max_size_pixels); + } + Send(new ChromeViewHostMsg_RequestThumbnailForContextNode_ACK(routing_id(), + thumbnail)); +} + void ChromeRenderViewObserver::OnStartFrameSniffer(const string16& frame_name) { new FrameSniffer(render_view(), frame_name); } |