Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(705)

Unified Diff: content/renderer/image_downloader/image_downloader_impl.cc

Issue 1085783002: Replace image_messages.h with Mojo service (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase to master@{#333882} Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/renderer/image_downloader/image_downloader_impl.cc
diff --git a/content/renderer/image_loading_helper.cc b/content/renderer/image_downloader/image_downloader_impl.cc
similarity index 62%
rename from content/renderer/image_loading_helper.cc
rename to content/renderer/image_downloader/image_downloader_impl.cc
index afd0fee406d5e7b8e118a2ba1af16c7010e24b38..b452fbcd23c39379302b4c52194e70f1f11af6b9 100644
--- a/content/renderer/image_loading_helper.cc
+++ b/content/renderer/image_downloader/image_downloader_impl.cc
@@ -1,17 +1,20 @@
-// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "content/renderer/image_loading_helper.h"
+#include "content/renderer/image_downloader/image_downloader_impl.h"
#include "base/bind.h"
+#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "content/child/image_decoder.h"
-#include "content/common/image_messages.h"
#include "content/public/renderer/render_frame.h"
#include "content/renderer/fetchers/multi_resolution_image_resource_fetcher.h"
+#include "mojo/common/url_type_converters.h"
+#include "mojo/converters/geometry/geometry_type_converters.h"
#include "net/base/data_url.h"
#include "skia/ext/image_operations.h"
+#include "skia/public/type_converters.h"
#include "third_party/WebKit/public/platform/WebURLRequest.h"
#include "third_party/WebKit/public/platform/WebVector.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
@@ -39,8 +42,7 @@ SkBitmap ResizeImage(const SkBitmap& image, uint32_t max_image_size) {
// Proportionally resize the minimal image to fit in a box of size
// max_image_size.
return skia::ImageOperations::Resize(
- image,
- skia::ImageOperations::RESIZE_BEST,
+ image, skia::ImageOperations::RESIZE_BEST,
static_cast<uint64_t>(image.width()) * max_image_size / max_dimension,
static_cast<uint64_t>(image.height()) * max_image_size / max_dimension);
}
@@ -69,8 +71,7 @@ void FilterAndResizeImagesForMaximalSize(
// Filter the images by |max_image_size|, and also identify the smallest image
// in case all the images are bigger than |max_image_size|.
for (std::vector<SkBitmap>::const_iterator it = unfiltered.begin();
- it != unfiltered.end();
- ++it) {
+ it != unfiltered.end(); ++it) {
const SkBitmap& image = *it;
uint32_t current_size = std::max(it->width(), it->height());
if (current_size < min_image_size) {
@@ -97,21 +98,38 @@ void FilterAndResizeImagesForMaximalSize(
namespace content {
-ImageLoadingHelper::ImageLoadingHelper(RenderFrame* render_frame)
- : RenderFrameObserver(render_frame) {
+ImageDownloaderImpl::ImageDownloaderImpl(
+ RenderFrame* render_frame,
+ mojo::InterfaceRequest<image_downloader::ImageDownloader> request)
+ : RenderFrameObserver(render_frame), binding_(this, request.Pass()) {
+ DCHECK(render_frame);
}
-ImageLoadingHelper::~ImageLoadingHelper() {
+ImageDownloaderImpl::~ImageDownloaderImpl() {
}
-void ImageLoadingHelper::OnDownloadImage(
- int id,
- const GURL& image_url,
- bool is_favicon,
- uint32_t max_image_size,
- bool bypass_cache) {
+// static
+void ImageDownloaderImpl::CreateMojoService(
+ RenderFrame* render_frame,
+ mojo::InterfaceRequest<image_downloader::ImageDownloader> request) {
+ DVLOG(1) << "ImageDownloaderImpl::CreateService";
+ DCHECK(render_frame);
+
+ new ImageDownloaderImpl(render_frame, request.Pass());
+}
+
+// ImageDownloader methods:
+void ImageDownloaderImpl::DownloadImage(
+ image_downloader::DownloadRequestPtr req,
+ const DownloadImageCallback& callback) {
+ const GURL image_url = req->url.To<GURL>();
+ bool is_favicon = req->is_favicon;
+ uint32_t max_image_size = req->max_bitmap_size;
+ bool bypass_cache = req->bypass_cache;
+
std::vector<SkBitmap> result_images;
std::vector<gfx::Size> result_original_image_sizes;
+
if (image_url.SchemeIs(url::kDataScheme)) {
SkBitmap data_image = ImageFromDataUrl(image_url);
if (!data_image.empty()) {
@@ -120,56 +138,61 @@ void ImageLoadingHelper::OnDownloadImage(
gfx::Size(data_image.width(), data_image.height()));
}
} else {
- if (DownloadImage(id, image_url, is_favicon, max_image_size,
- bypass_cache)) {
- // Will complete asynchronously via ImageLoadingHelper::DidDownloadImage
+ if (FetchImage(image_url, is_favicon, max_image_size, bypass_cache,
+ callback)) {
+ // Will complete asynchronously via ImageDownloaderImpl::DidFetchImage
return;
}
}
- Send(new ImageHostMsg_DidDownloadImage(routing_id(),
- id,
- 0,
- image_url,
- result_images,
- result_original_image_sizes));
+ ReplyDownloadResult(0, result_images, result_original_image_sizes, callback);
}
-bool ImageLoadingHelper::DownloadImage(
- int id,
- const GURL& image_url,
- bool is_favicon,
- uint32_t max_image_size,
- bool bypass_cache) {
+SkBitmap ImageDownloaderImpl::ImageFromDataUrl(const GURL& url) const {
Anand Mistry (off Chromium) 2015/06/12 08:19:38 Push this out of this class and into the anonymous
leonhsl(Using Gerrit) 2015/06/15 08:09:43 Done.
+ std::string mime_type, char_set, data;
+ if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) {
+ // Decode the image using WebKit's image decoder.
+ ImageDecoder decoder(gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize));
+ const unsigned char* src_data =
Anand Mistry (off Chromium) 2015/06/12 08:19:38 Not sure why this is like this, but std::string::d
leonhsl(Using Gerrit) 2015/06/15 08:09:43 Yeah data.data() is better than &data[0], modified
+ reinterpret_cast<const unsigned char*>(&data[0]);
+
+ return decoder.Decode(src_data, data.size());
+ }
+ return SkBitmap();
+}
+
+bool ImageDownloaderImpl::FetchImage(const GURL& image_url,
+ bool is_favicon,
+ uint32_t max_image_size,
+ bool bypass_cache,
+ const DownloadImageCallback& callback) {
+ blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
+ if (!frame)
+ return false;
+
// Create an image resource fetcher and assign it with a call back object.
image_fetchers_.push_back(new MultiResolutionImageResourceFetcher(
- image_url, render_frame()->GetWebFrame(), id,
- is_favicon ? WebURLRequest::RequestContextFavicon
- : WebURLRequest::RequestContextImage,
+ image_url, frame, 0, is_favicon ? WebURLRequest::RequestContextFavicon
+ : WebURLRequest::RequestContextImage,
bypass_cache ? WebURLRequest::ReloadBypassingCache
: WebURLRequest::UseProtocolCachePolicy,
- base::Bind(&ImageLoadingHelper::DidDownloadImage, base::Unretained(this),
- max_image_size)));
+ base::Bind(&ImageDownloaderImpl::DidFetchImage, base::Unretained(this),
+ max_image_size, callback)));
return true;
}
-void ImageLoadingHelper::DidDownloadImage(
+void ImageDownloaderImpl::DidFetchImage(
uint32_t max_image_size,
+ const DownloadImageCallback& callback,
MultiResolutionImageResourceFetcher* fetcher,
const std::vector<SkBitmap>& images) {
std::vector<SkBitmap> result_images;
std::vector<gfx::Size> result_original_image_sizes;
FilterAndResizeImagesForMaximalSize(images, max_image_size, &result_images,
- &result_original_image_sizes);
+ &result_original_image_sizes);
- // Notify requester of image download status.
- Send(new ImageHostMsg_DidDownloadImage(
- routing_id(),
- fetcher->id(),
- fetcher->http_status_code(),
- fetcher->image_url(),
- result_images,
- result_original_image_sizes));
+ ReplyDownloadResult(fetcher->http_status_code(), result_images,
+ result_original_image_sizes, callback);
// Remove the image fetcher from our pending list. We're in the callback from
// MultiResolutionImageResourceFetcher, best to delay deletion.
@@ -181,27 +204,20 @@ void ImageLoadingHelper::DidDownloadImage(
}
}
-SkBitmap ImageLoadingHelper::ImageFromDataUrl(const GURL& url) const {
- std::string mime_type, char_set, data;
- if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) {
- // Decode the image using WebKit's image decoder.
- ImageDecoder decoder(gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize));
- const unsigned char* src_data =
- reinterpret_cast<const unsigned char*>(&data[0]);
-
- return decoder.Decode(src_data, data.size());
- }
- return SkBitmap();
-}
+void ImageDownloaderImpl::ReplyDownloadResult(
+ int32_t http_status_code,
+ const std::vector<SkBitmap>& result_images,
+ const std::vector<gfx::Size>& result_original_image_sizes,
+ const DownloadImageCallback& callback) {
+ image_downloader::DownloadResultPtr result =
+ image_downloader::DownloadResult::New();
-bool ImageLoadingHelper::OnMessageReceived(const IPC::Message& message) {
- bool handled = true;
- IPC_BEGIN_MESSAGE_MAP(ImageLoadingHelper, message)
- IPC_MESSAGE_HANDLER(ImageMsg_DownloadImage, OnDownloadImage)
- IPC_MESSAGE_UNHANDLED(handled = false)
- IPC_END_MESSAGE_MAP()
+ result->http_status_code = http_status_code;
+ result->images = mojo::Array<skia::BitmapPtr>::From(result_images);
+ result->original_image_sizes =
+ mojo::Array<mojo::SizePtr>::From(result_original_image_sizes);
- return handled;
+ callback.Run(result.Pass());
}
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698