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 | |
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
-rw-r--r-- | chrome/common/render_messages.h | 14 | ||||
-rw-r--r-- | chrome/renderer/chrome_render_view_observer.cc | 54 | ||||
-rw-r--r-- | chrome/renderer/chrome_render_view_observer.h | 3 |
3 files changed, 71 insertions, 0 deletions
diff --git a/chrome/common/render_messages.h b/chrome/common/render_messages.h index 6001df2..547057c 100644 --- a/chrome/common/render_messages.h +++ b/chrome/common/render_messages.h @@ -366,6 +366,15 @@ IPC_MESSAGE_ROUTED1(ChromeViewMsg_SetClientSidePhishingDetection, IPC_MESSAGE_ROUTED1(ChromeViewMsg_StartFrameSniffer, string16 /* frame-name */) +// Asks the renderer for a thumbnail of the image selected by the most +// recently opened context menu, if there is one. If the image's area +// is greater than thumbnail_min_area it will be downscaled to +// be within thumbnail_max_size. The possibly downsampled image will be +// returned in a ChromeViewHostMsg_RequestThumbnailForContextNode_ACK message. +IPC_MESSAGE_ROUTED2(ChromeViewMsg_RequestThumbnailForContextNode, + int /* thumbnail_min_area_pixels */, + gfx::Size /* thumbnail_max_size_pixels */) + // Notifies the renderer whether hiding/showing the top controls is enabled, // what the current state should be, and whether or not to animate to the // proper state. @@ -374,10 +383,15 @@ IPC_MESSAGE_ROUTED3(ChromeViewMsg_UpdateTopControlsState, content::TopControlsState /* current */, bool /* animate */) + // Updates the window features of the render view. IPC_MESSAGE_ROUTED1(ChromeViewMsg_SetWindowFeatures, WebKit::WebWindowFeatures /* window_features */) +IPC_MESSAGE_ROUTED1(ChromeViewHostMsg_RequestThumbnailForContextNode_ACK, + SkBitmap /* thumbnail */) + + // JavaScript related messages ----------------------------------------------- // Notify the JavaScript engine in the render to change its parameters 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); } diff --git a/chrome/renderer/chrome_render_view_observer.h b/chrome/renderer/chrome_render_view_observer.h index 288a910..82f36c0 100644 --- a/chrome/renderer/chrome_render_view_observer.h +++ b/chrome/renderer/chrome_render_view_observer.h @@ -16,6 +16,7 @@ #include "content/public/common/top_controls_state.h" #include "content/public/renderer/render_view_observer.h" #include "third_party/WebKit/public/web/WebPermissionClient.h" +#include "ui/gfx/size.h" #include "url/gurl.h" class ChromeRenderProcessObserver; @@ -130,6 +131,8 @@ class ChromeRenderViewObserver : public content::RenderViewObserver, void OnSetAllowRunningInsecureContent(bool allow); void OnSetClientSidePhishingDetection(bool enable_phishing_detection); void OnSetVisuallyDeemphasized(bool deemphasized); + void OnRequestThumbnailForContextNode(int thumbnail_min_area_pixels, + gfx::Size thumbnail_max_size_pixels); void OnStartFrameSniffer(const string16& frame_name); void OnGetFPS(); void OnAddStrictSecurityHost(const std::string& host); |