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

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

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

Powered by Google App Engine
This is Rietveld 408576698