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

Side by Side Diff: chrome/browser/favicon_helper.h

Issue 6735042: Move favicon from TabContents to TabContentsWrapper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase 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/extensions/extension_tabs_module.cc ('k') | chrome/browser/favicon_helper.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 #ifndef CHROME_BROWSER_FAVICON_HELPER_H__
6 #define CHROME_BROWSER_FAVICON_HELPER_H__
7 #pragma once
8
9 #include <map>
10
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "chrome/browser/favicon_service.h"
15 #include "chrome/common/ref_counted_util.h"
16 #include "content/browser/cancelable_request.h"
17 #include "content/browser/tab_contents/tab_contents_observer.h"
18 #include "googleurl/src/gurl.h"
19
20 class NavigationEntry;
21 class Profile;
22 class RefCountedMemory;
23 class SkBitmap;
24 class TabContents;
25
26 // FaviconHelper is used to fetch the favicon for TabContents.
27 //
28 // FetchFavicon requests the favicon from the favicon service which in turn
29 // requests the favicon from the history database. At this point
30 // we only know the URL of the page, and not necessarily the url of the
31 // favicon. To ensure we handle reloading stale favicons as well as
32 // reloading a favicon on page reload we always request the favicon from
33 // history regardless of whether the NavigationEntry has a favicon.
34 //
35 // After the navigation two types of events are delivered (which is
36 // first depends upon who is faster): notification from the history
37 // db on our request for the favicon (OnFaviconDataForInitialURL),
38 // or a message from the renderer giving us the URL of the favicon for
39 // the page (SetFaviconURL).
40 // . If the history db has a valid up to date favicon for the page, we update
41 // the NavigationEntry and use the favicon.
42 // . When we receive the favicon url if it matches that of the NavigationEntry
43 // and the NavigationEntry's favicon is set, we do nothing (everything is
44 // ok).
45 // . On the other hand if the database does not know the favicon for url, or
46 // the favicon is out date, or the URL from the renderer does not match that
47 // NavigationEntry we proceed to DownloadFaviconOrAskHistory. Before we
48 // invoke DownloadFaviconOrAskHistory we wait until we've received both
49 // the favicon url and the callback from history. We wait to ensure we
50 // truly know both the favicon url and the state of the database.
51 //
52 // DownloadFaviconOrAskHistory does the following:
53 // . If we have a valid favicon, but it is expired we ask the renderer to
54 // download the favicon.
55 // . Otherwise we ask the history database to update the mapping from
56 // page url to favicon url and call us back with the favicon. Remember, it is
57 // possible for the db to already have the favicon, just not the mapping
58 // between page to favicon url. The callback for this is OnFaviconData.
59 //
60 // OnFaviconData either updates the favicon of the NavigationEntry (if the
61 // db knew about the favicon), or requests the renderer to download the
62 // favicon.
63 //
64 // When the renderer downloads the favicon SetFaviconImageData is invoked,
65 // at which point we update the favicon of the NavigationEntry and notify
66 // the database to save the favicon.
67
68 class FaviconHelper : public TabContentsObserver {
69 public:
70 explicit FaviconHelper(TabContents* tab_contents);
71 virtual ~FaviconHelper();
72
73 // Initiates loading the favicon for the specified url.
74 void FetchFavicon(const GURL& url);
75
76 // Initiates loading an image from given |image_url|. Returns a download id
77 // for caller to track the request. When download completes, |callback| is
78 // called with the three params: the download_id, a boolean flag to indicate
79 // whether the download succeeds and a SkBitmap as the downloaded image.
80 // Note that |image_size| is a hint for images with multiple sizes. The
81 // downloaded image is not resized to the given image_size. If 0 is passed,
82 // the first frame of the image is returned.
83 typedef Callback3<int, bool, const SkBitmap&>::Type ImageDownloadCallback;
84 int DownloadImage(const GURL& image_url, int image_size,
85 ImageDownloadCallback* callback);
86
87 // Message Handler. Must be public, because also called from
88 // PrerenderContents.
89 void OnUpdateFaviconURL(int32 page_id, const GURL& icon_url);
90
91 private:
92 struct DownloadRequest {
93 DownloadRequest() : callback(NULL) {}
94 DownloadRequest(const GURL& url,
95 const GURL& image_url,
96 ImageDownloadCallback* callback)
97 : url(url),
98 image_url(image_url),
99 callback(callback) {}
100
101 GURL url;
102 GURL image_url;
103 ImageDownloadCallback* callback;
104 };
105
106 // TabContentsObserver implementation.
107 virtual bool OnMessageReceived(const IPC::Message& message);
108
109 void OnDidDownloadFavicon(int id,
110 const GURL& image_url,
111 bool errored,
112 const SkBitmap& image);
113
114 // Return the NavigationEntry for the active entry, or NULL if the active
115 // entries URL does not match that of the URL last passed to FetchFavicon.
116 NavigationEntry* GetEntry();
117
118 Profile* profile();
119
120 FaviconService* GetFaviconService();
121
122 // See description above class for details.
123 void OnFaviconDataForInitialURL(FaviconService::Handle handle,
124 history::FaviconData favicon);
125
126 // If the favicon has expired, asks the renderer to download the favicon.
127 // Otherwise asks history to update the mapping between page url and icon
128 // url with a callback to OnFaviconData when done.
129 void DownloadFaviconOrAskHistory(NavigationEntry* entry);
130
131 // See description above class for details.
132 void OnFaviconData(FaviconService::Handle handle,
133 history::FaviconData favicon);
134
135 // Schedules a download for the specified entry. This adds the request to
136 // download_requests_.
137 int ScheduleDownload(const GURL& url, const GURL& image_url, int image_size,
138 ImageDownloadCallback* callback);
139
140 // Sets the image data for the favicon. This is invoked asynchronously after
141 // we request the TabContents to download the favicon.
142 void SetFavicon(const GURL& url, const GURL& icon_url, const SkBitmap& image);
143
144 // Converts the image data to an SkBitmap and sets it on the NavigationEntry.
145 // If the TabContents has a delegate, it is notified of the new favicon
146 // (INVALIDATE_FAVICON).
147 void UpdateFavicon(NavigationEntry* entry,
148 scoped_refptr<RefCountedMemory> data);
149 void UpdateFavicon(NavigationEntry* entry, const SkBitmap& image);
150
151 // Scales the image such that either the width and/or height is 16 pixels
152 // wide. Does nothing if the image is empty.
153 SkBitmap ConvertToFaviconSize(const SkBitmap& image);
154
155 // Returns true if the favicon should be saved.
156 bool ShouldSaveFavicon(const GURL& url);
157
158 // Used for history requests.
159 CancelableRequestConsumer cancelable_consumer_;
160
161 // URL of the page we're requesting the favicon for.
162 GURL url_;
163
164 // Whether we got the url for the page back from the renderer.
165 // See "Favicon Details" in tab_contents.cc for more details.
166 bool got_favicon_url_;
167
168 // Whether we got the initial response for the favicon back from the renderer.
169 // See "Favicon Details" in tab_contents.cc for more details.
170 bool got_favicon_from_history_;
171
172 // Whether the favicon is out of date. If true, it means history knows about
173 // the favicon, but we need to download the favicon because the icon has
174 // expired.
175 // See "Favicon Details" in tab_contents.cc for more details.
176 bool favicon_expired_;
177
178 // Requests to the renderer to download favicons.
179 typedef std::map<int, DownloadRequest> DownloadRequests;
180 DownloadRequests download_requests_;
181
182 DISALLOW_COPY_AND_ASSIGN(FaviconHelper);
183 };
184
185 #endif // CHROME_BROWSER_FAVICON_HELPER_H__
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_tabs_module.cc ('k') | chrome/browser/favicon_helper.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698