Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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. | |
| 4 | |
| 5 #include "content/renderer/image_loading_helper.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "content/common/image_messages.h" | |
| 10 #include "content/public/renderer/render_view.h" | |
| 11 #include "net/base/data_url.h" | |
| 12 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h" | |
| 13 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h" | |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
| 16 #include "ui/gfx/favicon_size.h" | |
| 17 #include "ui/gfx/size.h" | |
| 18 #include "ui/gfx/skbitmap_operations.h" | |
| 19 #include "webkit/glue/image_decoder.h" | |
| 20 #include "webkit/glue/multi_resolution_image_resource_fetcher.h" | |
| 21 #include "webkit/glue/webkit_glue.h" | |
| 22 | |
| 23 using WebKit::WebFrame; | |
| 24 using WebKit::WebVector; | |
| 25 using WebKit::WebURL; | |
| 26 using WebKit::WebURLRequest; | |
| 27 using webkit_glue::MultiResolutionImageResourceFetcher; | |
| 28 | |
| 29 namespace content { | |
| 30 | |
| 31 ImageLoadingHelper::ImageLoadingHelper(RenderView* render_view) | |
| 32 : RenderViewObserver(render_view) { | |
| 33 } | |
| 34 | |
| 35 ImageLoadingHelper::~ImageLoadingHelper() { | |
| 36 } | |
| 37 | |
| 38 void ImageLoadingHelper::OnDownloadImage(int id, | |
| 39 const GURL& image_url, | |
| 40 int image_size) { | |
| 41 std::vector<SkBitmap> result_images; | |
| 42 if (image_url.SchemeIs("data")) { | |
|
brettw
2013/03/20 23:25:56
Can you use kDataScheme from content/public/common
Dmitry Titov
2013/03/27 22:41:13
Done.
| |
| 43 SkBitmap data_image = ImageFromDataUrl(image_url); | |
| 44 if (!data_image.empty()) | |
| 45 result_images.push_back(data_image); | |
| 46 } else { | |
| 47 if (DownloadImage(id, image_url, image_size)) { | |
| 48 // Will complete asynchronously via ImageLoadingHelper::DidDownloadImage | |
| 49 return; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 Send(new ImageHostMsg_DidDownloadImage(routing_id(), | |
| 54 id, | |
| 55 image_url, | |
| 56 image_size, | |
| 57 result_images)); | |
| 58 } | |
| 59 | |
| 60 bool ImageLoadingHelper::DownloadImage(int id, | |
| 61 const GURL& image_url, | |
| 62 int image_size) { | |
| 63 // Make sure webview was not shut down. | |
| 64 if (!render_view()->GetWebView()) | |
| 65 return false; | |
| 66 // Create an image resource fetcher and assign it with a call back object. | |
| 67 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher( | |
| 68 image_url, render_view()->GetWebView()->mainFrame(), id, | |
| 69 WebURLRequest::TargetIsFavicon, | |
| 70 base::Bind(&ImageLoadingHelper::DidDownloadImage, | |
| 71 base::Unretained(this), image_size))); | |
| 72 return true; | |
| 73 } | |
| 74 | |
| 75 void ImageLoadingHelper::DidDownloadImage( | |
| 76 int requested_size, | |
| 77 MultiResolutionImageResourceFetcher* fetcher, | |
| 78 const std::vector<SkBitmap>& images) { | |
| 79 // Notify requester of image download status. | |
| 80 Send(new ImageHostMsg_DidDownloadImage(routing_id(), | |
| 81 fetcher->id(), | |
| 82 fetcher->image_url(), | |
| 83 requested_size, | |
| 84 images)); | |
| 85 | |
| 86 // Remove the image fetcher from our pending list. We're in the callback from | |
| 87 // MultiResolutionImageResourceFetcher, best to delay deletion. | |
| 88 ImageResourceFetcherList::iterator iter = | |
| 89 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher); | |
| 90 if (iter != image_fetchers_.end()) { | |
| 91 image_fetchers_.weak_erase(iter); | |
| 92 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 SkBitmap ImageLoadingHelper::ImageFromDataUrl(const GURL& url) const { | |
| 97 std::string mime_type, char_set, data; | |
| 98 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) { | |
| 99 // Decode the image using WebKit's image decoder. | |
| 100 webkit_glue::ImageDecoder decoder( | |
| 101 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)); | |
| 102 const unsigned char* src_data = | |
| 103 reinterpret_cast<const unsigned char*>(&data[0]); | |
| 104 | |
| 105 return decoder.Decode(src_data, data.size()); | |
| 106 } | |
| 107 return SkBitmap(); | |
| 108 } | |
| 109 | |
| 110 bool ImageLoadingHelper::OnMessageReceived(const IPC::Message& message) { | |
| 111 bool handled = true; | |
| 112 IPC_BEGIN_MESSAGE_MAP(ImageLoadingHelper, message) | |
| 113 IPC_MESSAGE_HANDLER(ImageMsg_DownloadImage, OnDownloadImage) | |
| 114 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 115 IPC_END_MESSAGE_MAP() | |
| 116 | |
| 117 return handled; | |
| 118 } | |
| 119 | |
| 120 } // namespace content | |
| 121 | |
| OLD | NEW |