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

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

Issue 12780024: Split FaviconHelper in two: ImageLoadingHelper and FaviconHelper (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix build Created 7 years, 9 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 "content/renderer/favicon_helper.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/message_loop.h"
10 #include "content/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/Platform/chromium/public/WebURLRequest.h"
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
17 #include "ui/base/ui_base_switches.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 namespace content {
33
34 namespace {
35
36 bool TouchEnabled() {
37 // Based on the definition of chrome::kEnableTouchIcon.
38 #if defined(OS_ANDROID)
39 return true;
40 #else
41 return false;
42 #endif
43 }
44
45 } // namespace
46
47
48 static FaviconURL::IconType ToFaviconType(WebIconURL::Type type) {
49 switch (type) {
50 case WebIconURL::TypeFavicon:
51 return FaviconURL::FAVICON;
52 case WebIconURL::TypeTouch:
53 return FaviconURL::TOUCH_ICON;
54 case WebIconURL::TypeTouchPrecomposed:
55 return FaviconURL::TOUCH_PRECOMPOSED_ICON;
56 case WebIconURL::TypeInvalid:
57 return FaviconURL::INVALID_ICON;
58 }
59 return FaviconURL::INVALID_ICON;
60 }
61
62 FaviconHelper::FaviconHelper(RenderView* render_view)
63 : RenderViewObserver(render_view) {
64 }
65
66 void FaviconHelper::DidChangeIcon(WebKit::WebFrame* frame,
67 WebKit::WebIconURL::Type icon_type) {
68 if (frame->parent())
69 return;
70
71 if (!TouchEnabled() && icon_type != WebIconURL::TypeFavicon)
72 return;
73
74 WebVector<WebIconURL> icon_urls = frame->iconURLs(icon_type);
75 std::vector<FaviconURL> urls;
76 for (size_t i = 0; i < icon_urls.size(); i++) {
77 urls.push_back(FaviconURL(icon_urls[i].iconURL(),
78 ToFaviconType(icon_urls[i].iconType())));
79 }
80 SendUpdateFaviconURL(routing_id(), render_view()->GetPageId(), urls);
81 }
82
83 FaviconHelper::~FaviconHelper() {
84 }
85
86 void FaviconHelper::OnDownloadFavicon(int id,
87 const GURL& image_url,
88 int image_size) {
89 std::vector<SkBitmap> result_images;
90 if (image_url.SchemeIs("data")) {
91 SkBitmap data_image = ImageFromDataUrl(image_url);
92 if (!data_image.empty())
93 result_images.push_back(data_image);
94 } else {
95 if (DownloadFavicon(id, image_url, image_size)) {
96 // Will complete asynchronously via FaviconHelper::DidDownloadFavicon
97 return;
98 }
99 }
100
101 Send(new IconHostMsg_DidDownloadFavicon(routing_id(),
102 id,
103 image_url,
104 image_size,
105 result_images));
106 }
107
108 bool FaviconHelper::DownloadFavicon(int id,
109 const GURL& image_url,
110 int image_size) {
111 // Make sure webview was not shut down.
112 if (!render_view()->GetWebView())
113 return false;
114 // Create an image resource fetcher and assign it with a call back object.
115 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher(
116 image_url, render_view()->GetWebView()->mainFrame(), id,
117 WebURLRequest::TargetIsFavicon,
118 base::Bind(&FaviconHelper::DidDownloadFavicon,
119 base::Unretained(this), image_size)));
120 return true;
121 }
122
123 void FaviconHelper::DidDownloadFavicon(
124 int requested_size,
125 MultiResolutionImageResourceFetcher* fetcher,
126 const std::vector<SkBitmap>& images) {
127 // Notify requester of image download status.
128 Send(new IconHostMsg_DidDownloadFavicon(routing_id(),
129 fetcher->id(),
130 fetcher->image_url(),
131 requested_size,
132 images));
133
134 // Remove the image fetcher from our pending list. We're in the callback from
135 // MultiResolutionImageResourceFetcher, best to delay deletion.
136 ImageResourceFetcherList::iterator iter =
137 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher);
138 if (iter != image_fetchers_.end()) {
139 image_fetchers_.weak_erase(iter);
140 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher);
141 }
142 }
143
144 SkBitmap FaviconHelper::ImageFromDataUrl(const GURL& url) const {
145 std::string mime_type, char_set, data;
146 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) {
147 // Decode the favicon using WebKit's image decoder.
148 webkit_glue::ImageDecoder decoder(
149 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize));
150 const unsigned char* src_data =
151 reinterpret_cast<const unsigned char*>(&data[0]);
152
153 return decoder.Decode(src_data, data.size());
154 }
155 return SkBitmap();
156 }
157
158 void FaviconHelper::SendUpdateFaviconURL(int32 routing_id,
159 int32 page_id,
160 const std::vector<FaviconURL>& urls) {
161 if (!urls.empty())
162 Send(new IconHostMsg_UpdateFaviconURL(routing_id, page_id, urls));
163 }
164
165 bool FaviconHelper::OnMessageReceived(const IPC::Message& message) {
166 bool handled = true;
167 IPC_BEGIN_MESSAGE_MAP(FaviconHelper, message)
168 IPC_MESSAGE_HANDLER(IconMsg_DownloadFavicon, OnDownloadFavicon)
169 IPC_MESSAGE_UNHANDLED(handled = false)
170 IPC_END_MESSAGE_MAP()
171
172 return handled;
173 }
174
175 void FaviconHelper::DidStopLoading() {
176 int icon_types = WebIconURL::TypeFavicon;
177 if (TouchEnabled())
178 icon_types |= WebIconURL::TypeTouchPrecomposed | WebIconURL::TypeTouch;
179
180 WebVector<WebIconURL> icon_urls =
181 render_view()->GetWebView()->mainFrame()->iconURLs(icon_types);
182 std::vector<FaviconURL> urls;
183 for (size_t i = 0; i < icon_urls.size(); i++) {
184 WebURL url = icon_urls[i].iconURL();
185 if (!url.isEmpty())
186 urls.push_back(FaviconURL(url, ToFaviconType(icon_urls[i].iconType())));
187 }
188 SendUpdateFaviconURL(routing_id(), render_view()->GetPageId(), urls);
189 }
190
191 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698