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

Side by Side Diff: chrome/renderer/favicon_helper.cc

Issue 11232068: Extract renderer-side favicon downloading code into separate helper class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: move UpdateFaviconURL code into helper Created 8 years, 2 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "chrome/renderer/favicon_helper.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop.h"
9 #include "chrome/common/favicon_url.h"
10 #include "chrome/common/icon_messages.h"
11 #include "content/public/renderer/render_view.h"
12 #include "net/base/data_url.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"
17 #include "ui/gfx/favicon_size.h"
18 #include "ui/gfx/size.h"
19 #include "ui/gfx/skbitmap_operations.h"
20 #include "webkit/glue/image_decoder.h"
21 #include "webkit/glue/multi_resolution_image_resource_fetcher.h"
22 #include "webkit/glue/webkit_glue.h"
23
24 using WebKit::WebURLRequest;
25 using webkit_glue::MultiResolutionImageResourceFetcher;
26
27 FaviconHelper::FaviconHelper(content::RenderView* render_view)
28 : content::RenderViewObserver(render_view) {
29 }
30
31 FaviconHelper::~FaviconHelper() {
32 }
33
34 void FaviconHelper::SendUpdateFaviconURL(int32 routing_id,
35 int32 page_id,
36 const std::vector<FaviconURL>& urls) {
37 if (!urls.empty()) {
38 Send(new IconHostMsg_UpdateFaviconURL(routing_id, page_id, urls));
39 }
40 }
41
42 bool FaviconHelper::OnMessageReceived(const IPC::Message& message) {
43 bool handled = true;
44 IPC_BEGIN_MESSAGE_MAP(FaviconHelper, message)
45 IPC_MESSAGE_HANDLER(IconMsg_DownloadFavicon, OnDownloadFavicon)
46 IPC_MESSAGE_UNHANDLED(handled = false)
47 IPC_END_MESSAGE_MAP()
48
49 return handled;
50 }
51
52 void FaviconHelper::OnDownloadFavicon(int id,
53 const GURL& image_url,
54 int image_size) {
55 bool data_image_failed = false;
56 if (image_url.SchemeIs("data")) {
57 SkBitmap data_image = ImageFromDataUrl(image_url);
58 data_image_failed = data_image.empty();
59 if (!data_image_failed) {
60 std::vector<SkBitmap> images(1, data_image);
61 Send(new IconHostMsg_DidDownloadFavicon(
62 routing_id(), id, image_url, false, image_size, images));
63 }
64 }
65
66 if (data_image_failed ||
67 !DownloadFavicon(id, image_url, image_size)) {
68 Send(new IconHostMsg_DidDownloadFavicon(
69 routing_id(), id, image_url, true, image_size,
70 std::vector<SkBitmap>()));
71 }
72 }
73
74 bool FaviconHelper::DownloadFavicon(int id,
75 const GURL& image_url,
76 int image_size) {
77 // Make sure webview was not shut down.
78 if (!render_view()->GetWebView())
79 return false;
80 // Create an image resource fetcher and assign it with a call back object.
81 image_fetchers_.push_back(linked_ptr<MultiResolutionImageResourceFetcher>(
82 new MultiResolutionImageResourceFetcher(
83 image_url, render_view()->GetWebView()->mainFrame(), id,
84 WebURLRequest::TargetIsFavicon,
85 base::Bind(&FaviconHelper::DidDownloadFavicon,
86 base::Unretained(this), image_size))));
87 return true;
88 }
89
90 void FaviconHelper::DidDownloadFavicon(
91 int requested_size,
92 MultiResolutionImageResourceFetcher* fetcher,
93 const std::vector<SkBitmap>& images) {
94 // Notify requester of image download status.
95 Send(new IconHostMsg_DidDownloadFavicon(routing_id(),
96 fetcher->id(),
97 fetcher->image_url(),
98 images.empty(),
99 requested_size,
100 images));
101
102 // Remove the image fetcher from our pending list. We're in the callback from
103 // MultiResolutionImageResourceFetcher, best to delay deletion.
104 ImageResourceFetcherList::iterator iter;
105 for (iter = image_fetchers_.begin(); iter != image_fetchers_.end(); ++iter) {
joth 2012/10/24 22:02:23 fwiw, if image_fetchers_ is ScopedVector I think w
Cait (Slow) 2012/10/25 20:29:23 Done.
106 if (iter->get() == fetcher) {
107 iter->release();
108 image_fetchers_.erase(iter);
109 break;
110 }
111 }
112 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher);
113 }
114
115 SkBitmap FaviconHelper::ImageFromDataUrl(const GURL& url) const {
116 std::string mime_type, char_set, data;
117 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) {
118 // Decode the favicon using WebKit's image decoder.
119 webkit_glue::ImageDecoder decoder(
120 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize));
121 const unsigned char* src_data =
122 reinterpret_cast<const unsigned char*>(&data[0]);
123
124 return decoder.Decode(src_data, data.size());
125 }
126 return SkBitmap();
127 }
OLDNEW
« chrome/renderer/favicon_helper.h ('K') | « chrome/renderer/favicon_helper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698