diff --git a/components/api_request_helper/api_request_helper.cc b/components/api_request_helper/api_request_helper.cc index 4ebcb71ac07..fc8c941b844 100644 --- a/components/api_request_helper/api_request_helper.cc +++ b/components/api_request_helper/api_request_helper.cc @@ -252,8 +252,23 @@ void APIRequestHelper::OnResponse( void APIRequestHelper::OnDownload(SimpleURLLoaderList::iterator iter, DownloadCallback callback, base::FilePath path) { + auto* loader = iter->get(); + base::flat_map headers; + if (loader->ResponseInfo()) { + auto headers_list = loader->ResponseInfo()->headers; + if (headers_list) { + size_t header_iter = 0; + std::string key; + std::string value; + while (headers_list->EnumerateHeaderLines(&header_iter, &key, &value)) { + key = base::ToLowerASCII(key); + headers[key] = value; + } + } + } + url_loaders_.erase(iter); - std::move(callback).Run(path); + std::move(callback).Run(path, std::move(headers)); } } // namespace api_request_helper diff --git a/components/api_request_helper/api_request_helper.h b/components/api_request_helper/api_request_helper.h index 1797a105692..b4c8c41b10a 100644 --- a/components/api_request_helper/api_request_helper.h +++ b/components/api_request_helper/api_request_helper.h @@ -104,7 +104,9 @@ class APIRequestHelper { size_t max_body_size = -1u, ResponseConversionCallback conversion_callback = base::NullCallback()); - using DownloadCallback = base::OnceCallback; + using DownloadCallback = base::OnceCallback& /*response_headers*/)>; Ticket Download(const GURL& url, const std::string& payload, const std::string& payload_content_type, diff --git a/components/playlist/playlist_thumbnail_downloader.cc b/components/playlist/playlist_thumbnail_downloader.cc index 00a56419ff9..2b981469c52 100644 --- a/components/playlist/playlist_thumbnail_downloader.cc +++ b/components/playlist/playlist_thumbnail_downloader.cc @@ -7,6 +7,8 @@ #include +#include "base/files/file_util.h" +#include "base/task/thread_pool.h" #include "content/public/browser/browser_context.h" #include "content/public/browser/storage_partition.h" #include "services/network/public/cpp/resource_request.h" @@ -77,10 +79,54 @@ void PlaylistThumbnailDownloader::CancelAllDownloadRequests() { ticket_map_.clear(); } -void PlaylistThumbnailDownloader::OnThumbnailDownloaded(const std::string& id, - base::FilePath path) { +void PlaylistThumbnailDownloader::OnThumbnailDownloaded( + const std::string& id, + base::FilePath path, + const base::flat_map& response_headers) { VLOG(2) << __func__ << " id: " << id; +#if BUILDFLAG(IS_ANDROID) + if (!path.empty()) { + // Anroid requires spedific format for thumbnail file. + std::string extension = "png"; + if (response_headers.contains("content-type")) { + std::string content_type = response_headers.at("content-type"); + if (base::StartsWith(content_type, "image/")) { + extension = content_type.substr(6); + } + } + RenameFilePerFormat(id, path, extension); + return; + } +#endif DCHECK(!ticket_map_.empty()); ticket_map_.erase(id); delegate_->OnThumbnailDownloaded(id, path); } + +#if BUILDFLAG(IS_ANDROID) +void PlaylistThumbnailDownloader::RenameFilePerFormat( + const std::string& id, + const base::FilePath& path, + const std::string& extension) { + if (!task_runner_) { + task_runner_ = base::ThreadPool::CreateSequencedTaskRunner( + {base::MayBlock(), base::TaskPriority::USER_VISIBLE, + base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}); + } + base::FilePath new_path = path.AddExtension(extension); + task_runner_->PostTaskAndReplyWithResult( + FROM_HERE, base::BindOnce(&base::ReplaceFile, path, new_path, nullptr), + base::BindOnce(&PlaylistThumbnailDownloader::OnRenameFilePerFormat, + base::Unretained(this), id, new_path)); +} + +void PlaylistThumbnailDownloader::OnRenameFilePerFormat( + const std::string& id, + const base::FilePath& new_path, + bool result) { + DCHECK(!ticket_map_.empty()); + ticket_map_.erase(id); + delegate_->OnThumbnailDownloaded( + id, result ? new_path : new_path.RemoveExtension()); +} +#endif diff --git a/components/playlist/playlist_thumbnail_downloader.h b/components/playlist/playlist_thumbnail_downloader.h index eaa82e9f27a..79d51734428 100644 --- a/components/playlist/playlist_thumbnail_downloader.h +++ b/components/playlist/playlist_thumbnail_downloader.h @@ -22,6 +22,12 @@ namespace network { class SharedURLLoaderFactory; } // namespace network +#if BUILDFLAG(IS_ANDROID) +namespace base { +class SequencedTaskRunner; +} // namespace base +#endif + class GURL; class PlaylistThumbnailDownloader { @@ -36,7 +42,6 @@ class PlaylistThumbnailDownloader { PlaylistThumbnailDownloader(content::BrowserContext* context, Delegate* delegate); virtual ~PlaylistThumbnailDownloader(); - PlaylistThumbnailDownloader(const PlaylistThumbnailDownloader&) = delete; PlaylistThumbnailDownloader& operator=(const PlaylistThumbnailDownloader&) = delete; @@ -51,7 +56,21 @@ class PlaylistThumbnailDownloader { using APIRequestHelper = api_request_helper::APIRequestHelper; using TicketMap = base::flat_map; - void OnThumbnailDownloaded(const std::string& id, base::FilePath path); + void OnThumbnailDownloaded( + const std::string& id, + base::FilePath path, + const base::flat_map& response_headers); + +#if BUILDFLAG(IS_ANDROID) + void RenameFilePerFormat(const std::string& id, + const base::FilePath& path, + const std::string& extension); + void OnRenameFilePerFormat(const std::string& id, + const base::FilePath& new_path, + bool result); + + scoped_refptr task_runner_; +#endif scoped_refptr url_loader_factory_;