OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_FAVICON_HELPER_H__ | 5 #ifndef CHROME_BROWSER_FAVICON_HELPER_H__ |
6 #define CHROME_BROWSER_FAVICON_HELPER_H__ | 6 #define CHROME_BROWSER_FAVICON_HELPER_H__ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
14 #include "chrome/browser/favicon_service.h" | 14 #include "chrome/browser/favicon_service.h" |
15 #include "chrome/common/icon_messages.h" | |
15 #include "chrome/common/ref_counted_util.h" | 16 #include "chrome/common/ref_counted_util.h" |
16 #include "content/browser/cancelable_request.h" | 17 #include "content/browser/cancelable_request.h" |
17 #include "content/browser/tab_contents/tab_contents_observer.h" | 18 #include "content/browser/tab_contents/tab_contents_observer.h" |
18 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
19 | 20 |
20 class NavigationEntry; | 21 class NavigationEntry; |
21 class Profile; | 22 class Profile; |
22 class RefCountedMemory; | 23 class RefCountedMemory; |
23 class SkBitmap; | 24 class SkBitmap; |
24 class TabContents; | 25 class TabContents; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 // OnFaviconData either updates the favicon of the NavigationEntry (if the | 61 // OnFaviconData either updates the favicon of the NavigationEntry (if the |
61 // db knew about the favicon), or requests the renderer to download the | 62 // db knew about the favicon), or requests the renderer to download the |
62 // favicon. | 63 // favicon. |
63 // | 64 // |
64 // When the renderer downloads the favicon SetFaviconImageData is invoked, | 65 // When the renderer downloads the favicon SetFaviconImageData is invoked, |
65 // at which point we update the favicon of the NavigationEntry and notify | 66 // at which point we update the favicon of the NavigationEntry and notify |
66 // the database to save the favicon. | 67 // the database to save the favicon. |
67 | 68 |
68 class FaviconHelper : public TabContentsObserver { | 69 class FaviconHelper : public TabContentsObserver { |
69 public: | 70 public: |
70 explicit FaviconHelper(TabContents* tab_contents); | 71 // |icon_types| defines this FaviconHelper supported icon types. It can be the |
sky
2011/03/22 19:48:48
|icon_types| is a bitmask of the supported icon ty
michaelbai
2011/03/22 23:59:03
Done.
| |
72 // combination of icon types. |preferred_icon_size| indicates the preferred | |
73 // icon size, the preferred icon is fetched from network and converted if the | |
74 // size is not matched; It can be 0 if there is no preference, the icon from | |
sky
2011/03/22 19:48:48
; -> . or It -> it
michaelbai
2011/03/22 23:59:03
Done.
| |
75 // network will be left as it. | |
76 FaviconHelper(TabContents* tab_contents, | |
77 int icon_types, | |
sky
2011/03/22 19:48:48
I don't think it makes sense to let folks specify
michaelbai
2011/03/22 23:59:03
Done.
| |
78 int preferred_icon_size); | |
71 virtual ~FaviconHelper(); | 79 virtual ~FaviconHelper(); |
72 | 80 |
73 // Initiates loading the favicon for the specified url. | 81 // Initiates loading the favicon for the specified url. |
74 void FetchFavicon(const GURL& url); | 82 void FetchFavicon(const GURL& url); |
75 | 83 |
76 // Initiates loading an image from given |image_url|. Returns a download id | 84 // Initiates loading an image from given |image_url|. Returns a download id |
77 // for caller to track the request. When download completes, |callback| is | 85 // 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 | 86 // 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. | 87 // 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 | 88 // 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, | 89 // downloaded image is not resized to the given image_size. If 0 is passed, |
82 // the first frame of the image is returned. | 90 // the first frame of the image is returned. |
83 typedef Callback3<int, bool, const SkBitmap&>::Type ImageDownloadCallback; | 91 typedef Callback3<int, bool, const SkBitmap&>::Type ImageDownloadCallback; |
84 int DownloadImage(const GURL& image_url, int image_size, | 92 int DownloadImage(const GURL& image_url, |
93 int image_size, | |
94 history::IconType icon_type, | |
85 ImageDownloadCallback* callback); | 95 ImageDownloadCallback* callback); |
86 | 96 |
87 // Message Handler. Must be public, because also called from | 97 // Message Handler. Must be public, because also called from |
88 // PrerenderContents. | 98 // PrerenderContents. |
89 void OnUpdateFaviconURL(int32 page_id, const GURL& icon_url); | 99 void OnUpdateFaviconURL(int32 page_id, |
100 std::vector<FaviconURL> candidates); | |
sky
2011/03/22 19:48:48
const std::vector<>&
michaelbai
2011/03/22 23:59:03
Done.
| |
90 | 101 |
91 private: | 102 private: |
92 struct DownloadRequest { | 103 struct DownloadRequest { |
93 DownloadRequest() {} | 104 DownloadRequest() {} |
sky
2011/03/22 19:48:48
initialize icon_type and callback here.
michaelbai
2011/03/22 23:59:03
Done.
| |
94 DownloadRequest(const GURL& url, | 105 DownloadRequest(const GURL& url, |
95 const GURL& image_url, | 106 const GURL& image_url, |
96 ImageDownloadCallback* callback) | 107 ImageDownloadCallback* callback, |
108 history::IconType icon_type) | |
97 : url(url), | 109 : url(url), |
98 image_url(image_url), | 110 image_url(image_url), |
99 callback(callback) { } | 111 callback(callback), |
112 icon_type(icon_type) { } | |
sky
2011/03/22 19:48:48
{ } -> {}
michaelbai
2011/03/22 23:59:03
Done.
| |
100 | 113 |
101 GURL url; | 114 GURL url; |
102 GURL image_url; | 115 GURL image_url; |
103 ImageDownloadCallback* callback; | 116 ImageDownloadCallback* callback; |
117 history::IconType icon_type; | |
104 }; | 118 }; |
105 | 119 |
120 static bool IsSameFaviconURL(const FaviconURL& favicon_url, | |
sky
2011/03/22 19:48:48
This should be unix_hacker_style. Also, the name i
michaelbai
2011/03/22 23:59:03
Done.
| |
121 const GURL& url, | |
122 history::IconType icon_type) { | |
123 return favicon_url.icon_url == url && | |
124 favicon_url.icon_type == static_cast<IconType>(icon_type); | |
125 } | |
126 | |
106 // TabContentsObserver implementation. | 127 // TabContentsObserver implementation. |
107 virtual bool OnMessageReceived(const IPC::Message& message); | 128 virtual bool OnMessageReceived(const IPC::Message& message); |
108 | 129 |
109 void OnDidDownloadFavicon(int id, | 130 void OnDidDownloadFavicon(int id, |
110 const GURL& image_url, | 131 const GURL& image_url, |
111 bool errored, | 132 bool errored, |
112 const SkBitmap& image); | 133 const SkBitmap& image); |
113 | 134 |
114 // Return the NavigationEntry for the active entry, or NULL if the active | 135 // 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. | 136 // entries URL does not match that of the URL last passed to FetchFavicon. |
116 NavigationEntry* GetEntry(); | 137 NavigationEntry* GetEntry(); |
117 | 138 |
118 Profile* profile(); | 139 Profile* profile(); |
119 | 140 |
120 FaviconService* GetFaviconService(); | 141 FaviconService* GetFaviconService(); |
121 | 142 |
122 // See description above class for details. | 143 // See description above class for details. |
123 void OnFaviconDataForInitialURL(FaviconService::Handle handle, | 144 void OnFaviconDataForInitialURL(FaviconService::Handle handle, |
124 history::FaviconData favicon); | 145 history::FaviconData favicon); |
125 | 146 |
126 // If the favicon has expired, asks the renderer to download the favicon. | 147 // 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 | 148 // Otherwise asks history to update the mapping between page url and icon |
128 // url with a callback to OnFaviconData when done. | 149 // url with a callback to OnFaviconData when done. |
129 void DownloadFaviconOrAskHistory(NavigationEntry* entry); | 150 void DownloadFaviconOrAskHistory(const GURL& page_url, |
151 const GURL& icon_url, | |
152 history::IconType icon_type); | |
130 | 153 |
131 // See description above class for details. | 154 // See description above class for details. |
132 void OnFaviconData(FaviconService::Handle handle, | 155 void OnFaviconData(FaviconService::Handle handle, |
133 history::FaviconData favicon); | 156 history::FaviconData favicon); |
134 | 157 |
135 // Schedules a download for the specified entry. This adds the request to | 158 // Schedules a download for the specified entry. This adds the request to |
136 // download_requests_. | 159 // download_requests_. |
137 int ScheduleDownload(const GURL& url, const GURL& image_url, int image_size, | 160 int ScheduleDownload(const GURL& url, |
161 const GURL& image_url, | |
162 int image_size, | |
163 history::IconType icon_type, | |
138 ImageDownloadCallback* callback); | 164 ImageDownloadCallback* callback); |
139 | 165 |
140 // Sets the image data for the favicon. This is invoked asynchronously after | 166 // Sets the image data for the favicon. This is invoked asynchronously after |
141 // we request the TabContents to download the favicon. | 167 // we request the TabContents to download the favicon. |
142 void SetFavicon(const GURL& url, const GURL& icon_url, const SkBitmap& image); | 168 void SetFavicon(const GURL& url, |
169 const GURL& icon_url, | |
170 const SkBitmap& image, | |
171 history::IconType icon_type); | |
143 | 172 |
144 // Converts the image data to an SkBitmap and sets it on the NavigationEntry. | 173 // Converts the FAVICON's image data to an SkBitmap and sets it on the |
174 // NavigationEntry. | |
145 // If the TabContents has a delegate, it is notified of the new favicon | 175 // If the TabContents has a delegate, it is notified of the new favicon |
146 // (INVALIDATE_FAVICON). | 176 // (INVALIDATE_FAVICON). |
147 void UpdateFavicon(NavigationEntry* entry, | 177 void UpdateFavicon(NavigationEntry* entry, |
148 scoped_refptr<RefCountedMemory> data); | 178 scoped_refptr<RefCountedMemory> data); |
149 void UpdateFavicon(NavigationEntry* entry, const SkBitmap& image); | 179 void UpdateFavicon(NavigationEntry* entry, const SkBitmap& image); |
150 | 180 |
151 // Scales the image such that either the width and/or height is 16 pixels | 181 // Scales the image such that either the width and/or height is 16 pixels |
152 // wide. Does nothing if the image is empty. | 182 // wide. Does nothing if the image is empty. |
153 SkBitmap ConvertToFaviconSize(const SkBitmap& image); | 183 SkBitmap ConvertToFaviconSize(const SkBitmap& image); |
154 | 184 |
155 // Returns true if the favicon should be saved. | 185 // Returns true if the favicon should be saved. |
156 bool ShouldSaveFavicon(const GURL& url); | 186 bool ShouldSaveFavicon(const GURL& url); |
157 | 187 |
188 // Return the current candidate if any. | |
189 FaviconURL* candidate() { | |
sky
2011/03/22 19:48:48
current_candidate()
michaelbai
2011/03/22 23:59:03
Done.
| |
190 return (favicon_candidates_.size() > current_candidate_idx_) ? | |
191 &favicon_candidates_[current_candidate_idx_] : NULL; | |
192 } | |
193 | |
158 // Used for history requests. | 194 // Used for history requests. |
159 CancelableRequestConsumer cancelable_consumer_; | 195 CancelableRequestConsumer cancelable_consumer_; |
160 | 196 |
161 // URL of the page we're requesting the favicon for. | 197 // URL of the page we're requesting the favicon for. |
162 GURL url_; | 198 GURL url_; |
163 | 199 |
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. | 200 // Whether we got the initial response for the favicon back from the renderer. |
169 // See "Favicon Details" in tab_contents.cc for more details. | 201 // See "Favicon Details" in tab_contents.cc for more details. |
170 bool got_favicon_from_history_; | 202 bool got_favicon_from_history_; |
171 | 203 |
172 // Whether the favicon is out of date. If true, it means history knows about | 204 // 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 | 205 // the favicon, but we need to download the favicon because the icon has |
174 // expired. | 206 // expired. |
175 // See "Favicon Details" in tab_contents.cc for more details. | 207 // See "Favicon Details" in tab_contents.cc for more details. |
176 bool favicon_expired_; | 208 bool favicon_expired_; |
177 | 209 |
178 // Requests to the renderer to download favicons. | 210 // Requests to the renderer to download favicons. |
179 typedef std::map<int, DownloadRequest> DownloadRequests; | 211 typedef std::map<int, DownloadRequest> DownloadRequests; |
180 DownloadRequests download_requests_; | 212 DownloadRequests download_requests_; |
181 | 213 |
214 // The combination of the supported icon types. | |
215 const int icon_types_; | |
216 | |
217 // The prioritized favicon candidates from the page back from the renderer. | |
218 std::vector<FaviconURL> favicon_candidates_; | |
sky
2011/03/22 19:48:48
How about naming this urls_ ? If you change it, up
michaelbai
2011/03/22 23:59:03
Done.
| |
219 | |
220 // The current candidate's index in favicon_candidates_. | |
221 size_t current_candidate_idx_; | |
sky
2011/03/22 19:48:48
idx -> index
michaelbai
2011/03/22 23:59:03
Done.
| |
222 | |
223 // The FaviconData from history. | |
224 history::FaviconData history_icon_; | |
225 | |
226 // The preferred icon size for supported icon types. | |
227 int preferred_icon_size_; | |
228 | |
182 DISALLOW_COPY_AND_ASSIGN(FaviconHelper); | 229 DISALLOW_COPY_AND_ASSIGN(FaviconHelper); |
183 }; | 230 }; |
184 | 231 |
185 #endif // CHROME_BROWSER_FAVICON_HELPER_H__ | 232 #endif // CHROME_BROWSER_FAVICON_HELPER_H__ |
OLD | NEW |