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

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: Created 8 years, 1 month 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
« no previous file with comments | « chrome/renderer/favicon_helper.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/chrome_constants.h"
10 #include "chrome/common/favicon_url.h"
11 #include "chrome/common/icon_messages.h"
12 #include "content/public/renderer/render_view.h"
13 #include "net/base/data_url.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 "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"
18 #include "ui/gfx/favicon_size.h"
19 #include "ui/gfx/size.h"
20 #include "ui/gfx/skbitmap_operations.h"
21 #include "webkit/glue/image_decoder.h"
22 #include "webkit/glue/multi_resolution_image_resource_fetcher.h"
23 #include "webkit/glue/webkit_glue.h"
24
25 using WebKit::WebFrame;
26 using WebKit::WebIconURL;
27 using WebKit::WebVector;
28 using WebKit::WebURL;
29 using WebKit::WebURLRequest;
30 using webkit_glue::MultiResolutionImageResourceFetcher;
31
32 static FaviconURL::IconType ToFaviconType(WebIconURL::Type type) {
33 switch (type) {
34 case WebIconURL::TypeFavicon:
35 return FaviconURL::FAVICON;
36 case WebIconURL::TypeTouch:
37 return FaviconURL::TOUCH_ICON;
38 case WebIconURL::TypeTouchPrecomposed:
39 return FaviconURL::TOUCH_PRECOMPOSED_ICON;
40 case WebIconURL::TypeInvalid:
41 return FaviconURL::INVALID_ICON;
42 }
43 return FaviconURL::INVALID_ICON;
44 }
45
46 FaviconHelper::FaviconHelper(content::RenderView* render_view)
47 : content::RenderViewObserver(render_view) {
48 }
49
50 FaviconHelper::~FaviconHelper() {
51 }
52
53 void FaviconHelper::SendUpdateFaviconURL(int32 routing_id,
54 int32 page_id,
55 const std::vector<FaviconURL>& urls) {
56 if (!urls.empty()) {
57 Send(new IconHostMsg_UpdateFaviconURL(routing_id, page_id, urls));
58 }
59 }
60
61 bool FaviconHelper::OnMessageReceived(const IPC::Message& message) {
62 bool handled = true;
63 IPC_BEGIN_MESSAGE_MAP(FaviconHelper, message)
64 IPC_MESSAGE_HANDLER(IconMsg_DownloadFavicon, OnDownloadFavicon)
65 IPC_MESSAGE_UNHANDLED(handled = false)
66 IPC_END_MESSAGE_MAP()
67
68 return handled;
69 }
70
71 void FaviconHelper::OnDownloadFavicon(int id,
72 const GURL& image_url,
73 int image_size) {
74 if (image_url.SchemeIs("data")) {
75 SkBitmap data_image = ImageFromDataUrl(image_url);
76 if (!data_image.empty()) {
77 std::vector<SkBitmap> images(1, data_image);
78 Send(new IconHostMsg_DidDownloadFavicon(
79 routing_id(), id, image_url, false, image_size, images));
80 return;
81 }
joth 2012/10/30 22:14:02 errr Sorry, I think I've introduced a new issue no
Cait (Slow) 2012/11/02 19:14:38 Agree. This is easier to follow. On 2012/10/30 22
82 }
83
84 if (!DownloadFavicon(id, image_url, image_size)) {
85 Send(new IconHostMsg_DidDownloadFavicon(
86 routing_id(), id, image_url, true, image_size,
87 std::vector<SkBitmap>()));
88 }
89 }
90
91 bool FaviconHelper::DownloadFavicon(int id,
92 const GURL& image_url,
93 int image_size) {
94 // Make sure webview was not shut down.
95 if (!render_view()->GetWebView())
96 return false;
97 // Create an image resource fetcher and assign it with a call back object.
98 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher(
99 image_url, render_view()->GetWebView()->mainFrame(), id,
100 WebURLRequest::TargetIsFavicon,
101 base::Bind(&FaviconHelper::DidDownloadFavicon,
102 base::Unretained(this), image_size)));
103 return true;
104 }
105
106 void FaviconHelper::DidDownloadFavicon(
107 int requested_size,
108 MultiResolutionImageResourceFetcher* fetcher,
109 const std::vector<SkBitmap>& images) {
110 // Notify requester of image download status.
111 Send(new IconHostMsg_DidDownloadFavicon(routing_id(),
112 fetcher->id(),
113 fetcher->image_url(),
114 images.empty(),
115 requested_size,
116 images));
117
118 // Remove the image fetcher from our pending list. We're in the callback from
119 // MultiResolutionImageResourceFetcher, best to delay deletion.
120 ImageResourceFetcherList::iterator iter =
121 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher);
122 if (iter != image_fetchers_.end()) {
123 image_fetchers_.erase(iter);
124 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher);
125 }
126 }
127
128 void FaviconHelper::DidStopLoading() {
129 int icon_types = WebIconURL::TypeFavicon;
130 if (chrome::kEnableTouchIcon)
131 icon_types |= WebIconURL::TypeTouchPrecomposed | WebIconURL::TypeTouch;
132
133 WebVector<WebIconURL> icon_urls =
134 render_view()->GetWebView()->mainFrame()->iconURLs(icon_types);
135 std::vector<FaviconURL> urls;
136 for (size_t i = 0; i < icon_urls.size(); i++) {
137 WebURL url = icon_urls[i].iconURL();
138 if (!url.isEmpty())
139 urls.push_back(FaviconURL(url, ToFaviconType(icon_urls[i].iconType())));
140 }
141 SendUpdateFaviconURL(routing_id(), render_view()->GetPageId(), urls);
142 }
143
144 void FaviconHelper::DidChangeIcon(WebKit::WebFrame* frame,
145 WebKit::WebIconURL::Type icon_type) {
146 if (frame->parent())
147 return;
148
149 if (!chrome::kEnableTouchIcon &&
150 icon_type != WebIconURL::TypeFavicon)
151 return;
152
153 WebVector<WebIconURL> icon_urls = frame->iconURLs(icon_type);
154 std::vector<FaviconURL> urls;
155 for (size_t i = 0; i < icon_urls.size(); i++) {
156 urls.push_back(FaviconURL(icon_urls[i].iconURL(),
157 ToFaviconType(icon_urls[i].iconType())));
158 }
159 SendUpdateFaviconURL(routing_id(), render_view()->GetPageId(), urls);
160 }
161
162 SkBitmap FaviconHelper::ImageFromDataUrl(const GURL& url) const {
163 std::string mime_type, char_set, data;
164 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) {
165 // Decode the favicon using WebKit's image decoder.
166 webkit_glue::ImageDecoder decoder(
167 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize));
168 const unsigned char* src_data =
169 reinterpret_cast<const unsigned char*>(&data[0]);
170
171 return decoder.Decode(src_data, data.size());
172 }
173 return SkBitmap();
174 }
OLDNEW
« no previous file with comments | « chrome/renderer/favicon_helper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698