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

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 remaining favicon functionality to FaviconHelper 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
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 bool data_image_failed = false;
75 if (image_url.SchemeIs("data")) {
76 SkBitmap data_image = ImageFromDataUrl(image_url);
77 data_image_failed = data_image.empty();
78 if (!data_image_failed) {
79 std::vector<SkBitmap> images(1, data_image);
80 Send(new IconHostMsg_DidDownloadFavicon(
81 routing_id(), id, image_url, false, image_size, images));
joth 2012/10/30 20:22:57 I'm now looking at this wondering why we don't hav
Cait (Slow) 2012/10/30 21:39:40 Done.
82 }
83 }
84
85 if (data_image_failed ||
86 !DownloadFavicon(id, image_url, image_size)) {
87 Send(new IconHostMsg_DidDownloadFavicon(
88 routing_id(), id, image_url, true, image_size,
89 std::vector<SkBitmap>()));
90 }
91 }
92
93 bool FaviconHelper::DownloadFavicon(int id,
94 const GURL& image_url,
95 int image_size) {
96 // Make sure webview was not shut down.
97 if (!render_view()->GetWebView())
98 return false;
99 // Create an image resource fetcher and assign it with a call back object.
100 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher(
101 image_url, render_view()->GetWebView()->mainFrame(), id,
102 WebURLRequest::TargetIsFavicon,
103 base::Bind(&FaviconHelper::DidDownloadFavicon,
104 base::Unretained(this), image_size)));
105 return true;
106 }
107
108 void FaviconHelper::DidDownloadFavicon(
109 int requested_size,
110 MultiResolutionImageResourceFetcher* fetcher,
111 const std::vector<SkBitmap>& images) {
112 // Notify requester of image download status.
113 Send(new IconHostMsg_DidDownloadFavicon(routing_id(),
114 fetcher->id(),
115 fetcher->image_url(),
116 images.empty(),
117 requested_size,
118 images));
119
120 // Remove the image fetcher from our pending list. We're in the callback from
121 // MultiResolutionImageResourceFetcher, best to delay deletion.
122 ImageResourceFetcherList::iterator iter =
123 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher);
124 if (iter != image_fetchers_.end()) {
125 image_fetchers_.erase(iter);
126 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher);
127 }
128 }
129
130 void FaviconHelper::DidStopLoading() {
131 int icon_types = WebIconURL::TypeFavicon;
132 if (chrome::kEnableTouchIcon)
133 icon_types |= WebIconURL::TypeTouchPrecomposed | WebIconURL::TypeTouch;
134
135 WebVector<WebIconURL> icon_urls =
136 render_view()->GetWebView()->mainFrame()->iconURLs(icon_types);
137 std::vector<FaviconURL> urls;
138 for (size_t i = 0; i < icon_urls.size(); i++) {
139 WebURL url = icon_urls[i].iconURL();
140 if (!url.isEmpty())
141 urls.push_back(FaviconURL(url, ToFaviconType(icon_urls[i].iconType())));
142 }
143 SendUpdateFaviconURL(routing_id(), render_view()->GetPageId(), urls);
144 }
145
146 void FaviconHelper::DidChangeIcon(WebKit::WebFrame* frame,
147 WebKit::WebIconURL::Type icon_type) {
148 if (frame->parent())
149 return;
150
151 if (!chrome::kEnableTouchIcon &&
152 icon_type != WebIconURL::TypeFavicon)
153 return;
154
155 WebVector<WebIconURL> icon_urls = frame->iconURLs(icon_type);
156 std::vector<FaviconURL> urls;
157 for (size_t i = 0; i < icon_urls.size(); i++) {
158 urls.push_back(FaviconURL(icon_urls[i].iconURL(),
159 ToFaviconType(icon_urls[i].iconType())));
160 }
161 SendUpdateFaviconURL(routing_id(), render_view()->GetPageId(), urls);
162 }
163
164 SkBitmap FaviconHelper::ImageFromDataUrl(const GURL& url) const {
165 std::string mime_type, char_set, data;
166 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) {
167 // Decode the favicon using WebKit's image decoder.
168 webkit_glue::ImageDecoder decoder(
169 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize));
170 const unsigned char* src_data =
171 reinterpret_cast<const unsigned char*>(&data[0]);
172
173 return decoder.Decode(src_data, data.size());
174 }
175 return SkBitmap();
176 }
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