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

Side by Side Diff: chrome/browser/favicon_tab_helper.cc

Issue 6814029: Revert 80519 - Move favicon from TabContents to TabContentsWrapper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 8 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
« no previous file with comments | « chrome/browser/favicon_tab_helper.h ('k') | chrome/browser/prerender/prerender_manager.cc » ('j') | 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) 2011 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/browser/favicon_tab_helper.h"
6
7 #include "build/build_config.h"
8
9 #include <vector>
10
11 #include "base/callback.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "chrome/browser/bookmarks/bookmark_model.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/render_messages.h"
16 #include "content/browser/renderer_host/render_view_host.h"
17 #include "content/browser/tab_contents/navigation_controller.h"
18 #include "content/browser/tab_contents/navigation_entry.h"
19 #include "content/browser/tab_contents/tab_contents_delegate.h"
20 #include "content/browser/tab_contents/tab_contents.h"
21 #include "content/browser/webui/web_ui.h"
22 #include "skia/ext/image_operations.h"
23 #include "ui/gfx/codec/png_codec.h"
24 #include "ui/gfx/favicon_size.h"
25
26 FaviconTabHelper::FaviconTabHelper(TabContents* tab_contents)
27 : TabContentsObserver(tab_contents),
28 got_favicon_url_(false),
29 got_favicon_from_history_(false),
30 favicon_expired_(false) {
31 }
32
33 FaviconTabHelper::~FaviconTabHelper() {
34 SkBitmap empty_image;
35
36 // Call pending download callbacks with error to allow caller to clean up.
37 for (DownloadRequests::iterator i = download_requests_.begin();
38 i != download_requests_.end(); ++i) {
39 if (i->second.callback) {
40 i->second.callback->Run(i->first, true, empty_image);
41 }
42 }
43 }
44
45 SkBitmap FaviconTabHelper::GetFavicon() const {
46 // Like GetTitle(), we also want to use the favicon for the last committed
47 // entry rather than a pending navigation entry.
48 NavigationEntry* entry = tab_contents()->controller().GetTransientEntry();
49 if (entry)
50 return entry->favicon().bitmap();
51
52 entry = tab_contents()->controller().GetLastCommittedEntry();
53 if (entry)
54 return entry->favicon().bitmap();
55 return SkBitmap();
56 }
57
58 bool FaviconTabHelper::FaviconIsValid() const {
59 const NavigationController& controller = tab_contents()->controller();
60 NavigationEntry* entry = controller.GetTransientEntry();
61 if (entry)
62 return entry->favicon().is_valid();
63
64 entry = controller.GetLastCommittedEntry();
65 if (entry)
66 return entry->favicon().is_valid();
67
68 return false;
69 }
70
71 bool FaviconTabHelper::ShouldDisplayFavicon() {
72 // Always display a throbber during pending loads.
73 const NavigationController& controller = tab_contents()->controller();
74 if (controller.GetLastCommittedEntry() && controller.pending_entry())
75 return true;
76
77 WebUI* web_ui = tab_contents()->GetWebUIForCurrentState();
78 if (web_ui)
79 return !web_ui->hide_favicon();
80 return true;
81 }
82
83 void FaviconTabHelper::FetchFavicon(const GURL& url) {
84 cancelable_consumer_.CancelAllRequests();
85
86 url_ = url;
87
88 favicon_expired_ = got_favicon_from_history_ = got_favicon_url_ = false;
89
90 // Request the favicon from the history service. In parallel to this the
91 // renderer is going to notify us (well TabContents) when the favicon url is
92 // available.
93 if (GetFaviconService()) {
94 GetFaviconService()->GetFaviconForURL(url_, history::FAVICON,
95 &cancelable_consumer_,
96 NewCallback(this, &FaviconTabHelper::OnFaviconDataForInitialURL));
97 }
98 }
99
100 int FaviconTabHelper::DownloadImage(const GURL& image_url,
101 int image_size,
102 ImageDownloadCallback* callback) {
103 DCHECK(callback); // Must provide a callback.
104 return ScheduleDownload(GURL(), image_url, image_size, callback);
105 }
106
107 FaviconService* FaviconTabHelper::GetFaviconService() {
108 return tab_contents()->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS);
109 }
110
111 void FaviconTabHelper::SetFavicon(
112 const GURL& url,
113 const GURL& image_url,
114 const SkBitmap& image) {
115 const SkBitmap& sized_image =
116 (image.width() == kFaviconSize && image.height() == kFaviconSize)
117 ? image : ConvertToFaviconSize(image);
118
119 if (GetFaviconService() && ShouldSaveFavicon(url)) {
120 std::vector<unsigned char> image_data;
121 gfx::PNGCodec::EncodeBGRASkBitmap(sized_image, false, &image_data);
122 GetFaviconService()->SetFavicon(url, image_url, image_data,
123 history::FAVICON);
124 }
125
126 if (url == url_) {
127 NavigationEntry* entry = GetEntry();
128 if (entry)
129 UpdateFavicon(entry, sized_image);
130 }
131 }
132
133 void FaviconTabHelper::SaveFavicon() {
134 NavigationEntry* entry = tab_contents()->controller().GetActiveEntry();
135 if (!entry || entry->url().is_empty())
136 return;
137
138 // Make sure the page is in history, otherwise adding the favicon does
139 // nothing.
140 HistoryService* history =
141 tab_contents()->profile()->GetOriginalProfile()->GetHistoryService(
142 Profile::IMPLICIT_ACCESS);
143 if (!history)
144 return;
145 history->AddPageNoVisitForBookmark(entry->url());
146
147 FaviconService* service = tab_contents()->profile()->
148 GetOriginalProfile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
149 if (!service)
150 return;
151 const NavigationEntry::FaviconStatus& favicon(entry->favicon());
152 if (!favicon.is_valid() || favicon.url().is_empty() ||
153 favicon.bitmap().empty()) {
154 return;
155 }
156 std::vector<unsigned char> image_data;
157 gfx::PNGCodec::EncodeBGRASkBitmap(favicon.bitmap(), false, &image_data);
158 service->SetFavicon(
159 entry->url(), favicon.url(), image_data, history::FAVICON);
160 }
161
162 void FaviconTabHelper::UpdateFavicon(NavigationEntry* entry,
163 scoped_refptr<RefCountedMemory> data) {
164 SkBitmap image;
165 gfx::PNGCodec::Decode(data->front(), data->size(), &image);
166 UpdateFavicon(entry, image);
167 }
168
169 void FaviconTabHelper::UpdateFavicon(NavigationEntry* entry,
170 const SkBitmap& image) {
171 // No matter what happens, we need to mark the favicon as being set.
172 entry->favicon().set_is_valid(true);
173
174 if (image.empty())
175 return;
176
177 entry->favicon().set_bitmap(image);
178 tab_contents()->NotifyNavigationStateChanged(TabContents::INVALIDATE_TAB);
179 }
180
181 void FaviconTabHelper::OnUpdateFaviconURL(int32 page_id, const GURL& icon_url) {
182 // TODO(davemoore) Should clear on empty url. Currently we ignore it.
183 // This appears to be what FF does as well.
184 if (icon_url.is_empty())
185 return;
186
187 NavigationEntry* entry = GetEntry();
188 if (!entry)
189 return;
190
191 got_favicon_url_ = true;
192
193 if (!GetFaviconService())
194 return;
195
196 if (!favicon_expired_ && entry->favicon().is_valid() &&
197 entry->favicon().url() == icon_url) {
198 // We already have the icon, no need to proceed.
199 return;
200 }
201
202 entry->favicon().set_url(icon_url);
203
204 if (got_favicon_from_history_)
205 DownloadFaviconOrAskHistory(entry);
206 }
207
208 void FaviconTabHelper::NavigateToPendingEntry(
209 const GURL& url,
210 NavigationController::ReloadType reload_type) {
211 if (reload_type != NavigationController::NO_RELOAD &&
212 !tab_contents()->profile()->IsOffTheRecord()) {
213 FaviconService* favicon_service =
214 tab_contents()->profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
215 if (favicon_service)
216 favicon_service->SetFaviconOutOfDateForPage(url);
217 }
218 }
219
220 void FaviconTabHelper::DidNavigateMainFramePostCommit(
221 const NavigationController::LoadCommittedDetails& details,
222 const ViewHostMsg_FrameNavigate_Params& /*params*/) {
223 // Get the favicon, either from history or request it from the net.
224 FetchFavicon(details.entry->url());
225 }
226
227 bool FaviconTabHelper::OnMessageReceived(const IPC::Message& message) {
228 bool handled = true;
229 IPC_BEGIN_MESSAGE_MAP(FaviconTabHelper, message)
230 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateFaviconURL, OnUpdateFaviconURL)
231 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDownloadFavicon, OnDidDownloadFavicon)
232 IPC_MESSAGE_UNHANDLED(handled = false)
233 IPC_END_MESSAGE_MAP()
234 return handled;
235 }
236
237 void FaviconTabHelper::OnDidDownloadFavicon(int id,
238 const GURL& image_url,
239 bool errored,
240 const SkBitmap& image) {
241 DownloadRequests::iterator i = download_requests_.find(id);
242 if (i == download_requests_.end()) {
243 // Currently TabContents notifies us of ANY downloads so that it is
244 // possible to get here.
245 return;
246 }
247
248 if (i->second.callback) {
249 i->second.callback->Run(id, errored, image);
250 } else if (!errored) {
251 SetFavicon(i->second.url, image_url, image);
252 }
253
254 download_requests_.erase(i);
255 }
256
257 NavigationEntry* FaviconTabHelper::GetEntry() {
258 NavigationEntry* entry = tab_contents()->controller().GetActiveEntry();
259 if (entry && entry->url() == url_ &&
260 tab_contents()->IsActiveEntry(entry->page_id())) {
261 return entry;
262 }
263 // If the URL has changed out from under us (as will happen with redirects)
264 // return NULL.
265 return NULL;
266 }
267
268 void FaviconTabHelper::OnFaviconDataForInitialURL(
269 FaviconService::Handle handle,
270 history::FaviconData favicon) {
271 NavigationEntry* entry = GetEntry();
272 if (!entry)
273 return;
274
275 got_favicon_from_history_ = true;
276
277 favicon_expired_ = (favicon.known_icon && favicon.expired);
278
279 if (favicon.known_icon && !entry->favicon().is_valid() &&
280 (!got_favicon_url_ || entry->favicon().url() == favicon.icon_url)) {
281 // The db knows the favicon (although it may be out of date) and the entry
282 // doesn't have an icon. Set the favicon now, and if the favicon turns out
283 // to be expired (or the wrong url) we'll fetch later on. This way the
284 // user doesn't see a flash of the default favicon.
285 entry->favicon().set_url(favicon.icon_url);
286 if (favicon.is_valid())
287 UpdateFavicon(entry, favicon.image_data);
288 entry->favicon().set_is_valid(true);
289 }
290
291 if (favicon.known_icon && !favicon.expired) {
292 if (got_favicon_url_ && entry->favicon().url() != favicon.icon_url) {
293 // Mapping in the database is wrong. DownloadFaviconOrAskHistory will
294 // update the mapping for this url and download the favicon if we don't
295 // already have it.
296 DownloadFaviconOrAskHistory(entry);
297 }
298 } else if (got_favicon_url_) {
299 // We know the official url for the favicon, by either don't have the
300 // favicon or its expired. Continue on to DownloadFaviconOrAskHistory to
301 // either download or check history again.
302 DownloadFaviconOrAskHistory(entry);
303 }
304 // else we haven't got the icon url. When we get it we'll ask the
305 // renderer to download the icon.
306 }
307
308 void FaviconTabHelper::DownloadFaviconOrAskHistory(NavigationEntry* entry) {
309 DCHECK(entry); // We should only get here if entry is valid.
310 if (favicon_expired_) {
311 // We have the mapping, but the favicon is out of date. Download it now.
312 ScheduleDownload(entry->url(), entry->favicon().url(), kFaviconSize, NULL);
313 } else if (GetFaviconService()) {
314 // We don't know the favicon, but we may have previously downloaded the
315 // favicon for another page that shares the same favicon. Ask for the
316 // favicon given the favicon URL.
317 if (tab_contents()->profile()->IsOffTheRecord()) {
318 GetFaviconService()->GetFavicon(
319 entry->favicon().url(),
320 history::FAVICON,
321 &cancelable_consumer_,
322 NewCallback(this, &FaviconTabHelper::OnFaviconData));
323 } else {
324 // Ask the history service for the icon. This does two things:
325 // 1. Attempts to fetch the favicon data from the database.
326 // 2. If the favicon exists in the database, this updates the database to
327 // include the mapping between the page url and the favicon url.
328 // This is asynchronous. The history service will call back when done.
329 // Issue the request and associate the current page ID with it.
330 GetFaviconService()->UpdateFaviconMappingAndFetch(
331 entry->url(),
332 entry->favicon().url(),
333 history::FAVICON,
334 &cancelable_consumer_,
335 NewCallback(this, &FaviconTabHelper::OnFaviconData));
336 }
337 }
338 }
339
340 void FaviconTabHelper::OnFaviconData(
341 FaviconService::Handle handle,
342 history::FaviconData favicon) {
343 NavigationEntry* entry = GetEntry();
344 if (!entry)
345 return;
346
347 // No need to update the favicon url. By the time we get here
348 // UpdateFaviconURL will have set the favicon url.
349
350 if (favicon.is_valid()) {
351 // There is a favicon, set it now. If expired we'll download the current
352 // one again, but at least the user will get some icon instead of the
353 // default and most likely the current one is fine anyway.
354 UpdateFavicon(entry, favicon.image_data);
355 }
356
357 if (!favicon.known_icon || favicon.expired) {
358 // We don't know the favicon, or it is out of date. Request the current one.
359 ScheduleDownload(entry->url(), entry->favicon().url(), kFaviconSize, NULL);
360 }
361 }
362
363 int FaviconTabHelper::ScheduleDownload(const GURL& url,
364 const GURL& image_url,
365 int image_size,
366 ImageDownloadCallback* callback) {
367 const int download_id = tab_contents()->render_view_host()->DownloadFavicon(
368 image_url, image_size);
369
370 if (download_id) {
371 // Download ids should be unique.
372 DCHECK(download_requests_.find(download_id) == download_requests_.end());
373 download_requests_[download_id] = DownloadRequest(url, image_url, callback);
374 }
375
376 return download_id;
377 }
378
379 SkBitmap FaviconTabHelper::ConvertToFaviconSize(const SkBitmap& image) {
380 int width = image.width();
381 int height = image.height();
382 if (width > 0 && height > 0) {
383 calc_favicon_target_size(&width, &height);
384 return skia::ImageOperations::Resize(
385 image, skia::ImageOperations::RESIZE_LANCZOS3,
386 width, height);
387 }
388 return image;
389 }
390
391 bool FaviconTabHelper::ShouldSaveFavicon(const GURL& url) {
392 if (!tab_contents()->profile()->IsOffTheRecord())
393 return true;
394
395 // Otherwise store the favicon if the page is bookmarked.
396 BookmarkModel* bookmark_model = tab_contents()->profile()->GetBookmarkModel();
397 return bookmark_model && bookmark_model->IsBookmarked(url);
398 }
OLDNEW
« no previous file with comments | « chrome/browser/favicon_tab_helper.h ('k') | chrome/browser/prerender/prerender_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698