OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/favicon/content/content_favicon_driver.h" | 5 #include "components/favicon/content/content_favicon_driver.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "components/favicon/content/favicon_url_util.h" | 8 #include "components/favicon/content/favicon_url_util.h" |
9 #include "components/favicon/core/favicon_service.h" | 9 #include "components/favicon/core/favicon_service.h" |
10 #include "components/favicon/core/favicon_url.h" | 10 #include "components/favicon/core/favicon_url.h" |
11 #include "components/history/core/browser/history_service.h" | 11 #include "components/history/core/browser/history_service.h" |
12 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
13 #include "content/public/browser/favicon_status.h" | 13 #include "content/public/browser/favicon_status.h" |
14 #include "content/public/browser/navigation_controller.h" | 14 #include "content/public/browser/navigation_controller.h" |
15 #include "content/public/browser/navigation_details.h" | 15 #include "content/public/browser/navigation_details.h" |
16 #include "content/public/browser/navigation_entry.h" | 16 #include "content/public/browser/navigation_entry.h" |
17 #include "content/public/browser/navigation_handle.h" | 17 #include "content/public/browser/navigation_handle.h" |
18 #include "content/public/common/favicon_url.h" | 18 #include "content/public/common/favicon_url.h" |
| 19 #include "content/public/common/manifest.h" |
19 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
20 | 21 |
21 DEFINE_WEB_CONTENTS_USER_DATA_KEY(favicon::ContentFaviconDriver); | 22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(favicon::ContentFaviconDriver); |
22 | 23 |
23 namespace favicon { | 24 namespace favicon { |
| 25 namespace { |
| 26 |
| 27 void ExtractManifestIcons( |
| 28 ContentFaviconDriver::ManifestDownloadCallback callback, |
| 29 const GURL& manifest_url, |
| 30 const content::Manifest& manifest) { |
| 31 // TODO/DONOTSUBMIT(mastiz): This should distinguish 404s from other status |
| 32 // codes. |
| 33 std::vector<FaviconURL> candidates; |
| 34 for (const content::Manifest::Icon& icon : manifest.icons) { |
| 35 candidates.emplace_back(icon.src, favicon_base::FAVICON, icon.sizes); |
| 36 } |
| 37 callback.Run(/*status_code=*/200, candidates); |
| 38 } |
| 39 |
| 40 } // namespace |
24 | 41 |
25 // static | 42 // static |
26 void ContentFaviconDriver::CreateForWebContents( | 43 void ContentFaviconDriver::CreateForWebContents( |
27 content::WebContents* web_contents, | 44 content::WebContents* web_contents, |
28 FaviconService* favicon_service, | 45 FaviconService* favicon_service, |
29 history::HistoryService* history_service, | 46 history::HistoryService* history_service, |
30 bookmarks::BookmarkModel* bookmark_model) { | 47 bookmarks::BookmarkModel* bookmark_model) { |
31 if (FromWebContents(web_contents)) | 48 if (FromWebContents(web_contents)) |
32 return; | 49 return; |
33 | 50 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 int ContentFaviconDriver::DownloadImage(const GURL& url, | 126 int ContentFaviconDriver::DownloadImage(const GURL& url, |
110 int max_image_size, | 127 int max_image_size, |
111 ImageDownloadCallback callback) { | 128 ImageDownloadCallback callback) { |
112 bool bypass_cache = (bypass_cache_page_url_ == GetActiveURL()); | 129 bool bypass_cache = (bypass_cache_page_url_ == GetActiveURL()); |
113 bypass_cache_page_url_ = GURL(); | 130 bypass_cache_page_url_ = GURL(); |
114 | 131 |
115 return web_contents()->DownloadImage(url, true, max_image_size, bypass_cache, | 132 return web_contents()->DownloadImage(url, true, max_image_size, bypass_cache, |
116 callback); | 133 callback); |
117 } | 134 } |
118 | 135 |
| 136 void ContentFaviconDriver::DownloadManifest(const GURL& url, |
| 137 ManifestDownloadCallback callback) { |
| 138 web_contents()->GetManifest(base::Bind(&ExtractManifestIcons, callback)); |
| 139 } |
| 140 |
119 bool ContentFaviconDriver::IsOffTheRecord() { | 141 bool ContentFaviconDriver::IsOffTheRecord() { |
120 DCHECK(web_contents()); | 142 DCHECK(web_contents()); |
121 return web_contents()->GetBrowserContext()->IsOffTheRecord(); | 143 return web_contents()->GetBrowserContext()->IsOffTheRecord(); |
122 } | 144 } |
123 | 145 |
124 void ContentFaviconDriver::OnFaviconUpdated( | 146 void ContentFaviconDriver::OnFaviconUpdated( |
125 const GURL& page_url, | 147 const GURL& page_url, |
126 FaviconDriverObserver::NotificationIconType notification_icon_type, | 148 FaviconDriverObserver::NotificationIconType notification_icon_type, |
127 const GURL& icon_url, | 149 const GURL& icon_url, |
128 bool icon_url_changed, | 150 bool icon_url_changed, |
(...skipping 18 matching lines...) Expand all Loading... |
147 DCHECK(!candidates.empty()); | 169 DCHECK(!candidates.empty()); |
148 | 170 |
149 // Ignore the update if there is no last committed navigation entry. This can | 171 // Ignore the update if there is no last committed navigation entry. This can |
150 // occur when loading an initially blank page. | 172 // occur when loading an initially blank page. |
151 content::NavigationEntry* entry = | 173 content::NavigationEntry* entry = |
152 web_contents()->GetController().GetLastCommittedEntry(); | 174 web_contents()->GetController().GetLastCommittedEntry(); |
153 if (!entry) | 175 if (!entry) |
154 return; | 176 return; |
155 | 177 |
156 favicon_urls_ = candidates; | 178 favicon_urls_ = candidates; |
157 OnUpdateFaviconURL(entry->GetURL(), | 179 } |
158 FaviconURLsFromContentFaviconURLs(candidates)); | 180 |
| 181 void ContentFaviconDriver::DidUpdateManifestURL( |
| 182 const base::Optional<GURL>& manifest_url) { |
| 183 // Ignore the update if there is no last committed navigation entry. This can |
| 184 // occur when loading an initially blank page. |
| 185 content::NavigationEntry* entry = |
| 186 web_contents()->GetController().GetLastCommittedEntry(); |
| 187 if (!entry) |
| 188 return; |
| 189 |
| 190 OnUpdateCandidates(entry->GetURL(), |
| 191 FaviconURLsFromContentFaviconURLs(favicon_urls_), |
| 192 manifest_url); |
159 } | 193 } |
160 | 194 |
161 void ContentFaviconDriver::DidStartNavigation( | 195 void ContentFaviconDriver::DidStartNavigation( |
162 content::NavigationHandle* navigation_handle) { | 196 content::NavigationHandle* navigation_handle) { |
163 if (!navigation_handle->IsInMainFrame()) | 197 if (!navigation_handle->IsInMainFrame()) |
164 return; | 198 return; |
165 | 199 |
| 200 favicon_urls_.clear(); |
| 201 |
166 content::ReloadType reload_type = navigation_handle->GetReloadType(); | 202 content::ReloadType reload_type = navigation_handle->GetReloadType(); |
167 if (reload_type == content::ReloadType::NONE || IsOffTheRecord()) | 203 if (reload_type == content::ReloadType::NONE || IsOffTheRecord()) |
168 return; | 204 return; |
169 | 205 |
170 bypass_cache_page_url_ = navigation_handle->GetURL(); | 206 bypass_cache_page_url_ = navigation_handle->GetURL(); |
171 SetFaviconOutOfDateForPage( | 207 SetFaviconOutOfDateForPage( |
172 navigation_handle->GetURL(), | 208 navigation_handle->GetURL(), |
173 reload_type == content::ReloadType::BYPASSING_CACHE); | 209 reload_type == content::ReloadType::BYPASSING_CACHE); |
174 } | 210 } |
175 | 211 |
176 void ContentFaviconDriver::DidFinishNavigation( | 212 void ContentFaviconDriver::DidFinishNavigation( |
177 content::NavigationHandle* navigation_handle) { | 213 content::NavigationHandle* navigation_handle) { |
178 if (!navigation_handle->IsInMainFrame() || | 214 if (!navigation_handle->IsInMainFrame() || |
179 !navigation_handle->HasCommitted() || | 215 !navigation_handle->HasCommitted() || |
180 navigation_handle->IsErrorPage()) { | 216 navigation_handle->IsErrorPage()) { |
181 return; | 217 return; |
182 } | 218 } |
183 | 219 |
184 favicon_urls_.clear(); | |
185 | |
186 // Wait till the user navigates to a new URL to start checking the cache | 220 // Wait till the user navigates to a new URL to start checking the cache |
187 // again. The cache may be ignored for non-reload navigations (e.g. | 221 // again. The cache may be ignored for non-reload navigations (e.g. |
188 // history.replace() in-page navigation). This is allowed to increase the | 222 // history.replace() in-page navigation). This is allowed to increase the |
189 // likelihood that "reloading a page ignoring the cache" redownloads the | 223 // likelihood that "reloading a page ignoring the cache" redownloads the |
190 // favicon. In particular, a page may do an in-page navigation before | 224 // favicon. In particular, a page may do an in-page navigation before |
191 // FaviconHandler has the time to determine that the favicon needs to be | 225 // FaviconHandler has the time to determine that the favicon needs to be |
192 // redownloaded. | 226 // redownloaded. |
193 GURL url = navigation_handle->GetURL(); | 227 GURL url = navigation_handle->GetURL(); |
194 if (url != bypass_cache_page_url_) | 228 if (url != bypass_cache_page_url_) |
195 bypass_cache_page_url_ = GURL(); | 229 bypass_cache_page_url_ = GURL(); |
196 | 230 |
197 // Get the favicon, either from history or request it from the net. | 231 // Get the favicon, either from history or request it from the net. |
198 FetchFavicon(url); | 232 FetchFavicon(url); |
199 } | 233 } |
200 | 234 |
201 } // namespace favicon | 235 } // namespace favicon |
OLD | NEW |