diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2019-08-30 10:22:43 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2019-08-30 12:36:28 +0000 |
commit | 271a6c3487a14599023a9106329505597638d793 (patch) | |
tree | e040d58ffc86c1480b79ca8528020ca9ec919bf8 /chromium/chrome/browser/ui/webui/favicon_source.cc | |
parent | 7b2ffa587235a47d4094787d72f38102089f402a (diff) |
BASELINE: Update Chromium to 77.0.3865.59
Change-Id: I1e89a5f3b009a9519a6705102ad65c92fe736f21
Reviewed-by: Michael BrĂ¼ning <michael.bruning@qt.io>
Diffstat (limited to 'chromium/chrome/browser/ui/webui/favicon_source.cc')
-rw-r--r-- | chromium/chrome/browser/ui/webui/favicon_source.cc | 188 |
1 files changed, 94 insertions, 94 deletions
diff --git a/chromium/chrome/browser/ui/webui/favicon_source.cc b/chromium/chrome/browser/ui/webui/favicon_source.cc index c93c9e6bd63..5175c36ac4d 100644 --- a/chromium/chrome/browser/ui/webui/favicon_source.cc +++ b/chromium/chrome/browser/ui/webui/favicon_source.cc @@ -11,17 +11,16 @@ #include "base/metrics/histogram_macros.h" #include "base/strings/string_number_conversions.h" #include "chrome/browser/favicon/favicon_service_factory.h" -#include "chrome/browser/favicon/large_icon_service_factory.h" +#include "chrome/browser/favicon/history_ui_favicon_request_handler_factory.h" #include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search/instant_io_context.h" -#include "chrome/browser/sync/profile_sync_service_factory.h" #include "chrome/browser/sync/session_sync_service_factory.h" #include "chrome/common/url_constants.h" #include "chrome/common/webui_url_constants.h" +#include "components/favicon/core/history_ui_favicon_request_handler.h" #include "components/favicon_base/favicon_url_parser.h" #include "components/history/core/browser/top_sites.h" -#include "components/sync/driver/sync_service_utils.h" #include "components/sync_sessions/open_tabs_ui_delegate.h" #include "components/sync_sessions/session_sync_service.h" #include "content/public/browser/web_contents.h" @@ -34,71 +33,49 @@ #include "url/gurl.h" namespace { -favicon::FaviconRequestOrigin ParseFaviconRequestOrigin(const GURL& url) { - GURL history_url(chrome::kChromeUIHistoryURL); - if (url == history_url.Resolve(chrome::kChromeUIHistorySyncedTabs)) - return favicon::FaviconRequestOrigin::HISTORY_SYNCED_TABS; - if (url == history_url) - return favicon::FaviconRequestOrigin::HISTORY; - return favicon::FaviconRequestOrigin::UNKNOWN; -} -sync_sessions::OpenTabsUIDelegate* GetOpenTabsUIDelegate(Profile* profile) { - sync_sessions::SessionSyncService* session_sync_service = - SessionSyncServiceFactory::GetInstance()->GetForProfile(profile); - DCHECK(session_sync_service); - return session_sync_service->GetOpenTabsUIDelegate(); -} - -scoped_refptr<base::RefCountedMemory> GetSyncedFaviconForPageURL( - Profile* profile, - const GURL& page_url) { - sync_sessions::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile); - return open_tabs ? open_tabs->GetSyncedFaviconForPageURL(page_url.spec()) - : nullptr; +// web_contents->GetLastCommittedURL in general will not necessarily yield the +// original URL that started the request, but we're only interested in verifying +// if it was issued by a history page, for whom this is the case. If it is not +// possible to obtain the URL, we return the empty GURL. +GURL GetUnsafeRequestOrigin( + const content::ResourceRequestInfo::WebContentsGetter& wc_getter) { + content::WebContents* web_contents = wc_getter.Run(); + return web_contents ? web_contents->GetLastCommittedURL() : GURL(); } -// Check if user settings allow querying a Google server using history -// information. -bool CanSendHistoryDataToServer(Profile* profile) { - return syncer::GetUploadToGoogleState( - ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile), - syncer::ModelType::HISTORY_DELETE_DIRECTIVES) == - syncer::UploadState::ACTIVE; +bool ParseHistoryUiOrigin(const GURL& url, + favicon::HistoryUiFaviconRequestOrigin* origin) { + GURL history_url(chrome::kChromeUIHistoryURL); + if (url == history_url) { + *origin = favicon::HistoryUiFaviconRequestOrigin::kHistory; + return true; + } + if (url == history_url.Resolve(chrome::kChromeUIHistorySyncedTabs)) { + *origin = favicon::HistoryUiFaviconRequestOrigin::kHistorySyncedTabs; + return true; + } + return false; } } // namespace -FaviconSource::IconRequest::IconRequest() - : size_in_dip(gfx::kFaviconSize), - device_scale_factor(1.0f), - icon_request_origin(favicon::FaviconRequestOrigin::UNKNOWN) {} - -FaviconSource::IconRequest::IconRequest( - const content::URLDataSource::GotDataCallback& cb, - const GURL& path, - int size, - float scale, - favicon::FaviconRequestOrigin origin) - : callback(cb), - request_path(path), - size_in_dip(size), - device_scale_factor(scale), - icon_request_origin(origin) {} - -FaviconSource::IconRequest::IconRequest(const IconRequest& other) = default; - -FaviconSource::IconRequest::~IconRequest() { -} - -FaviconSource::FaviconSource(Profile* profile) - : profile_(profile->GetOriginalProfile()) {} +FaviconSource::FaviconSource(Profile* profile, + chrome::FaviconUrlFormat url_format) + : profile_(profile->GetOriginalProfile()), url_format_(url_format) {} FaviconSource::~FaviconSource() { } -std::string FaviconSource::GetSource() const { - return chrome::kChromeUIFaviconHost; +std::string FaviconSource::GetSource() { + switch (url_format_) { + case chrome::FaviconUrlFormat::kFaviconLegacy: + return chrome::kChromeUIFaviconHost; + case chrome::FaviconUrlFormat::kFavicon2: + return chrome::kChromeUIFavicon2Host; + } + NOTREACHED(); + return ""; } void FaviconSource::StartDataRequest( @@ -114,7 +91,7 @@ void FaviconSource::StartDataRequest( } chrome::ParsedFaviconPath parsed; - bool success = chrome::ParseFaviconPath(path, &parsed); + bool success = chrome::ParseFaviconPath(path, url_format_, &parsed); if (!success) { SendDefaultResponse(callback); return; @@ -129,22 +106,14 @@ void FaviconSource::StartDataRequest( int desired_size_in_pixel = std::ceil(parsed.size_in_dip * parsed.device_scale_factor); - content::WebContents* web_contents = wc_getter.Run(); - // web_contents->GetURL will not necessarily yield the original URL that - // started the request, but for collecting metrics for chrome://history and - // chrome://history/syncedTabs this will be ok. - favicon::FaviconRequestOrigin unsafe_request_origin = - web_contents ? ParseFaviconRequestOrigin(web_contents->GetURL()) - : favicon::FaviconRequestOrigin::UNKNOWN; if (parsed.is_icon_url) { // TODO(michaelbai): Change GetRawFavicon to support combination of // IconType. favicon_service->GetRawFavicon( url, favicon_base::IconType::kFavicon, desired_size_in_pixel, - base::BindRepeating( - &FaviconSource::OnFaviconDataAvailable, base::Unretained(this), - IconRequest(callback, url, parsed.size_in_dip, - parsed.device_scale_factor, unsafe_request_origin)), + base::BindRepeating(&FaviconSource::OnFaviconDataAvailable, + base::Unretained(this), callback, + parsed.size_in_dip, parsed.device_scale_factor), &cancelable_task_tracker_); } else { // Intercept requests for prepopulated pages if TopSites exists. @@ -163,35 +132,62 @@ void FaviconSource::StartDataRequest( } } } + + favicon::HistoryUiFaviconRequestOrigin parsed_history_ui_origin; + if (!parsed.allow_favicon_server_fallback || + !ParseHistoryUiOrigin(GetUnsafeRequestOrigin(wc_getter), + &parsed_history_ui_origin)) { + // Request from local storage only. + // TODO(victorvianna): Expose fallback_to_host in FaviconRequestHandler + // API and move the explanatory comment for |fallback_to_host| here. + const bool fallback_to_host = true; + favicon_service->GetRawFaviconForPageURL( + url, {favicon_base::IconType::kFavicon}, desired_size_in_pixel, + fallback_to_host, + base::Bind(&FaviconSource::OnFaviconDataAvailable, + base::Unretained(this), callback, parsed.size_in_dip, + parsed.device_scale_factor), + &cancelable_task_tracker_); + return; + } + + // Request from both local storage and favicon server using + // HistoryUiFaviconRequestHandler. + favicon::HistoryUiFaviconRequestHandler* + history_ui_favicon_request_handler = + HistoryUiFaviconRequestHandlerFactory::GetForBrowserContext( + profile_); + if (!history_ui_favicon_request_handler) { + SendDefaultResponse(callback); + return; + } + sync_sessions::SessionSyncService* session_sync_service = + SessionSyncServiceFactory::GetInstance()->GetForProfile(profile_); sync_sessions::OpenTabsUIDelegate* open_tabs = - GetOpenTabsUIDelegate(profile_); - favicon_request_handler_.GetRawFaviconForPageURL( + session_sync_service->GetOpenTabsUIDelegate(); + history_ui_favicon_request_handler->GetRawFaviconForPageURL( url, desired_size_in_pixel, - base::BindOnce( - &FaviconSource::OnFaviconDataAvailable, base::Unretained(this), - IconRequest(callback, url, parsed.size_in_dip, - parsed.device_scale_factor, unsafe_request_origin)), - unsafe_request_origin, favicon::FaviconRequestPlatform::kDesktop, - favicon_service, - LargeIconServiceFactory::GetForBrowserContext(profile_), + base::BindOnce(&FaviconSource::OnFaviconDataAvailable, + base::Unretained(this), callback, parsed.size_in_dip, + parsed.device_scale_factor), + favicon::FaviconRequestPlatform::kDesktop, parsed_history_ui_origin, /*icon_url_for_uma=*/ open_tabs ? open_tabs->GetIconUrlForPageUrl(url) : GURL(), - base::BindOnce(&GetSyncedFaviconForPageURL, base::Unretained(profile_)), - CanSendHistoryDataToServer(profile_), &cancelable_task_tracker_); + &cancelable_task_tracker_); } } -std::string FaviconSource::GetMimeType(const std::string&) const { +std::string FaviconSource::GetMimeType(const std::string&) { // We need to explicitly return a mime type, otherwise if the user tries to // drag the image they get no extension. return "image/png"; } -bool FaviconSource::AllowCaching() const { +bool FaviconSource::AllowCaching() { return false; } -bool FaviconSource::ShouldReplaceExistingSource() const { +bool FaviconSource::ShouldReplaceExistingSource() { // Leave the existing DataSource in place, otherwise we'll drop any pending // requests on the floor. return false; @@ -200,7 +196,7 @@ bool FaviconSource::ShouldReplaceExistingSource() const { bool FaviconSource::ShouldServiceRequest( const GURL& url, content::ResourceContext* resource_context, - int render_process_id) const { + int render_process_id) { if (url.SchemeIs(chrome::kChromeSearchScheme)) { return InstantIOContext::ShouldServiceRequest(url, resource_context, render_process_id); @@ -214,26 +210,30 @@ ui::NativeTheme* FaviconSource::GetNativeTheme() { } void FaviconSource::OnFaviconDataAvailable( - const IconRequest& request, + const content::URLDataSource::GotDataCallback& callback, + int size_in_dip, + float scale_factor, const favicon_base::FaviconRawBitmapResult& bitmap_result) { if (bitmap_result.is_valid()) { // Forward the data along to the networking system. - request.callback.Run(bitmap_result.bitmap_data.get()); + callback.Run(bitmap_result.bitmap_data.get()); } else { - SendDefaultResponse(request); + SendDefaultResponse(callback, size_in_dip, scale_factor); } } void FaviconSource::SendDefaultResponse( const content::URLDataSource::GotDataCallback& callback) { - SendDefaultResponse(IconRequest(callback, GURL(), 16, 1.0f, - favicon::FaviconRequestOrigin::UNKNOWN)); + SendDefaultResponse(callback, 16, 1.0f); } -void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { +void FaviconSource::SendDefaultResponse( + const content::URLDataSource::GotDataCallback& callback, + int size_in_dip, + float scale_factor) { const bool dark = GetNativeTheme()->SystemDarkModeEnabled(); int resource_id; - switch (icon_request.size_in_dip) { + switch (size_in_dip) { case 64: resource_id = dark ? IDR_DEFAULT_FAVICON_DARK_64 : IDR_DEFAULT_FAVICON_64; break; @@ -244,11 +244,11 @@ void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { resource_id = dark ? IDR_DEFAULT_FAVICON_DARK : IDR_DEFAULT_FAVICON; break; } - icon_request.callback.Run(LoadIconBytes(icon_request, resource_id)); + callback.Run(LoadIconBytes(scale_factor, resource_id)); } -base::RefCountedMemory* FaviconSource::LoadIconBytes(const IconRequest& request, +base::RefCountedMemory* FaviconSource::LoadIconBytes(float scale_factor, int resource_id) { return ui::ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( - resource_id, ui::GetSupportedScaleFactor(request.device_scale_factor)); + resource_id, ui::GetSupportedScaleFactor(scale_factor)); } |