OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/favicon/favicon_tab_helper.h" | 5 #include "chrome/browser/favicon/favicon_tab_helper.h" |
6 | 6 |
7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
8 #include "chrome/browser/favicon/chrome_favicon_client.h" | 8 #include "chrome/browser/favicon/chrome_favicon_client.h" |
9 #include "chrome/browser/favicon/chrome_favicon_client_factory.h" | 9 #include "chrome/browser/favicon/chrome_favicon_client_factory.h" |
10 #include "chrome/browser/favicon/favicon_handler.h" | 10 #include "chrome/browser/favicon/favicon_handler.h" |
11 #include "chrome/browser/favicon/favicon_service.h" | 11 #include "chrome/browser/favicon/favicon_service.h" |
12 #include "chrome/browser/favicon/favicon_service_factory.h" | 12 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 13 #include "chrome/browser/favicon/favicon_tab_helper_observer.h" |
13 #include "chrome/browser/history/history_service.h" | 14 #include "chrome/browser/history/history_service.h" |
14 #include "chrome/browser/history/history_service_factory.h" | 15 #include "chrome/browser/history/history_service_factory.h" |
15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/search/search.h" | 17 #include "chrome/browser/search/search.h" |
17 #include "chrome/common/chrome_constants.h" | 18 #include "chrome/common/chrome_constants.h" |
18 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
19 #include "components/favicon_base/favicon_types.h" | 20 #include "components/favicon_base/favicon_types.h" |
20 #include "content/public/browser/favicon_status.h" | 21 #include "content/public/browser/favicon_status.h" |
21 #include "content/public/browser/invalidate_type.h" | 22 #include "content/public/browser/invalidate_type.h" |
22 #include "content/public/browser/navigation_controller.h" | 23 #include "content/public/browser/navigation_controller.h" |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 return; | 130 return; |
130 const FaviconStatus& favicon(entry->GetFavicon()); | 131 const FaviconStatus& favicon(entry->GetFavicon()); |
131 if (!favicon.valid || favicon.url.is_empty() || | 132 if (!favicon.valid || favicon.url.is_empty() || |
132 favicon.image.IsEmpty()) { | 133 favicon.image.IsEmpty()) { |
133 return; | 134 return; |
134 } | 135 } |
135 service->SetFavicons( | 136 service->SetFavicons( |
136 entry->GetURL(), favicon.url, favicon_base::FAVICON, favicon.image); | 137 entry->GetURL(), favicon.url, favicon_base::FAVICON, favicon.image); |
137 } | 138 } |
138 | 139 |
| 140 void FaviconTabHelper::AddObserver(FaviconTabHelperObserver* observer) { |
| 141 observer_list_.AddObserver(observer); |
| 142 } |
| 143 |
| 144 void FaviconTabHelper::RemoveObserver(FaviconTabHelperObserver* observer) { |
| 145 observer_list_.RemoveObserver(observer); |
| 146 } |
| 147 |
139 int FaviconTabHelper::StartDownload(const GURL& url, int max_image_size) { | 148 int FaviconTabHelper::StartDownload(const GURL& url, int max_image_size) { |
140 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( | 149 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( |
141 profile_->GetOriginalProfile(), Profile::IMPLICIT_ACCESS); | 150 profile_->GetOriginalProfile(), Profile::IMPLICIT_ACCESS); |
142 if (favicon_service && favicon_service->WasUnableToDownloadFavicon(url)) { | 151 if (favicon_service && favicon_service->WasUnableToDownloadFavicon(url)) { |
143 DVLOG(1) << "Skip Failed FavIcon: " << url; | 152 DVLOG(1) << "Skip Failed FavIcon: " << url; |
144 return 0; | 153 return 0; |
145 } | 154 } |
146 | 155 |
147 return web_contents()->DownloadImage( | 156 return web_contents()->DownloadImage( |
148 url, | 157 url, |
149 true, | 158 true, |
150 max_image_size, | 159 max_image_size, |
151 base::Bind(&FaviconTabHelper::DidDownloadFavicon,base::Unretained(this))); | 160 base::Bind(&FaviconTabHelper::DidDownloadFavicon,base::Unretained(this))); |
152 } | 161 } |
153 | 162 |
154 void FaviconTabHelper::NotifyFaviconUpdated(bool icon_url_changed) { | |
155 content::NotificationService::current()->Notify( | |
156 chrome::NOTIFICATION_FAVICON_UPDATED, | |
157 content::Source<WebContents>(web_contents()), | |
158 content::Details<bool>(&icon_url_changed)); | |
159 web_contents()->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); | |
160 } | |
161 | |
162 bool FaviconTabHelper::IsOffTheRecord() { | 163 bool FaviconTabHelper::IsOffTheRecord() { |
163 DCHECK(web_contents()); | 164 DCHECK(web_contents()); |
164 return web_contents()->GetBrowserContext()->IsOffTheRecord(); | 165 return web_contents()->GetBrowserContext()->IsOffTheRecord(); |
165 } | 166 } |
166 | 167 |
167 const gfx::Image FaviconTabHelper::GetActiveFaviconImage() { | 168 const gfx::Image FaviconTabHelper::GetActiveFaviconImage() { |
168 return GetFaviconStatus().image; | 169 return GetFaviconStatus().image; |
169 } | 170 } |
170 | 171 |
171 const GURL FaviconTabHelper::GetActiveFaviconURL() { | 172 const GURL FaviconTabHelper::GetActiveFaviconURL() { |
(...skipping 16 matching lines...) Expand all Loading... |
188 } | 189 } |
189 | 190 |
190 void FaviconTabHelper::SetActiveFaviconURL(GURL url) { | 191 void FaviconTabHelper::SetActiveFaviconURL(GURL url) { |
191 GetFaviconStatus().url = url; | 192 GetFaviconStatus().url = url; |
192 } | 193 } |
193 | 194 |
194 void FaviconTabHelper::SetActiveFaviconValidity(bool validity) { | 195 void FaviconTabHelper::SetActiveFaviconValidity(bool validity) { |
195 GetFaviconStatus().valid = validity; | 196 GetFaviconStatus().valid = validity; |
196 } | 197 } |
197 | 198 |
| 199 void FaviconTabHelper::OnFaviconAvailable(const gfx::Image& image, |
| 200 const GURL& icon_url, |
| 201 bool is_active_favicon) { |
| 202 if (is_active_favicon) { |
| 203 // No matter what happens, we need to mark the favicon as being set. |
| 204 SetActiveFaviconValidity(true); |
| 205 bool icon_url_changed = GetActiveFaviconURL() != icon_url; |
| 206 SetActiveFaviconURL(icon_url); |
| 207 |
| 208 if (image.IsEmpty()) |
| 209 return; |
| 210 |
| 211 SetActiveFaviconImage(image); |
| 212 content::NotificationService::current()->Notify( |
| 213 chrome::NOTIFICATION_FAVICON_UPDATED, |
| 214 content::Source<WebContents>(web_contents()), |
| 215 content::Details<bool>(&icon_url_changed)); |
| 216 web_contents()->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); |
| 217 } |
| 218 if (!image.IsEmpty()) |
| 219 FOR_EACH_OBSERVER(FaviconTabHelperObserver, observer_list_, |
| 220 OnFaviconAvailable(image)); |
| 221 } |
| 222 |
198 content::FaviconStatus& FaviconTabHelper::GetFaviconStatus() { | 223 content::FaviconStatus& FaviconTabHelper::GetFaviconStatus() { |
199 DCHECK(web_contents()->GetController().GetActiveEntry()); | 224 DCHECK(web_contents()->GetController().GetActiveEntry()); |
200 return web_contents()->GetController().GetActiveEntry()->GetFavicon(); | 225 return web_contents()->GetController().GetActiveEntry()->GetFavicon(); |
201 } | 226 } |
202 | 227 |
203 void FaviconTabHelper::DidStartNavigationToPendingEntry( | 228 void FaviconTabHelper::DidStartNavigationToPendingEntry( |
204 const GURL& url, | 229 const GURL& url, |
205 NavigationController::ReloadType reload_type) { | 230 NavigationController::ReloadType reload_type) { |
206 if (reload_type != NavigationController::NO_RELOAD && | 231 if (reload_type != NavigationController::NO_RELOAD && |
207 !profile_->IsOffTheRecord()) { | 232 !profile_->IsOffTheRecord()) { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 favicon_service->UnableToDownloadFavicon(image_url); | 299 favicon_service->UnableToDownloadFavicon(image_url); |
275 } | 300 } |
276 | 301 |
277 favicon_handler_->OnDidDownloadFavicon( | 302 favicon_handler_->OnDidDownloadFavicon( |
278 id, image_url, bitmaps, original_bitmap_sizes); | 303 id, image_url, bitmaps, original_bitmap_sizes); |
279 if (touch_icon_handler_.get()) { | 304 if (touch_icon_handler_.get()) { |
280 touch_icon_handler_->OnDidDownloadFavicon( | 305 touch_icon_handler_->OnDidDownloadFavicon( |
281 id, image_url, bitmaps, original_bitmap_sizes); | 306 id, image_url, bitmaps, original_bitmap_sizes); |
282 } | 307 } |
283 } | 308 } |
OLD | NEW |