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

Side by Side Diff: components/favicon/content/content_favicon_driver.cc

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

Powered by Google App Engine
This is Rietveld 408576698