diff --git a/browser/ui/webui/brave_rewards_source.cc b/browser/ui/webui/brave_rewards_source.cc index 3f3b0d31b65..33edae55dc3 100644 --- a/browser/ui/webui/brave_rewards_source.cc +++ b/browser/ui/webui/brave_rewards_source.cc @@ -15,9 +15,9 @@ namespace { -typedef base::RepeatingCallback +typedef base::OnceCallback RewardsResourceFetcherCallback; // Calls the specified callback when the requested image is downloaded. This @@ -27,19 +27,23 @@ class RewardsResourceFetcherObserver : public BitmapFetcherService::Observer { public: explicit RewardsResourceFetcherObserver( const GURL& url, - const RewardsResourceFetcherCallback& rewards_resource_fetcher_callback) + RewardsResourceFetcherCallback rewards_resource_fetcher_callback) : url_(url), - rewards_resource_fetcher_callback_(rewards_resource_fetcher_callback) {} + rewards_resource_fetcher_callback_( + std::move(rewards_resource_fetcher_callback)) {} void OnImageChanged(BitmapFetcherService::RequestId request_id, const SkBitmap& image) override { DCHECK(!image.empty()); - rewards_resource_fetcher_callback_.Run(request_id, url_, image); + // BitmapFetcherService does not invoke OnImageChanged more than once, in + // spite of what the method name suggests. + DCHECK(rewards_resource_fetcher_callback_); + std::move(rewards_resource_fetcher_callback_).Run(request_id, url_, image); } private: GURL url_; - const RewardsResourceFetcherCallback rewards_resource_fetcher_callback_; + RewardsResourceFetcherCallback rewards_resource_fetcher_callback_; DISALLOW_COPY_AND_ASSIGN(RewardsResourceFetcherObserver); }; @@ -63,12 +67,11 @@ std::string BraveRewardsSource::GetSource() { } void BraveRewardsSource::StartDataRequest( - const std::string& path, + const GURL& url, const content::WebContents::Getter& wc_getter, - const content::URLDataSource::GotDataCallback& got_data_callback) { - GURL url(path); + content::URLDataSource::GotDataCallback got_data_callback) { if (!url.is_valid()) { - got_data_callback.Run(nullptr); + std::move(got_data_callback).Run(nullptr); return; } @@ -101,14 +104,14 @@ void BraveRewardsSource::StartDataRequest( policy_exception_justification: "Not implemented." })"); - resource_fetchers_.emplace_back(path); + resource_fetchers_.emplace_back(url); request_ids_.push_back(image_service->RequestImage( url, // Image Service takes ownership of the observer. new RewardsResourceFetcherObserver( - url, - base::BindRepeating(&BraveRewardsSource::OnBitmapFetched, - base::Unretained(this), got_data_callback)), + url, base::BindOnce(&BraveRewardsSource::OnBitmapFetched, + base::Unretained(this), + std::move(got_data_callback))), traffic_annotation)); } } @@ -138,20 +141,20 @@ bool BraveRewardsSource::ShouldServiceRequest( } void BraveRewardsSource::OnBitmapFetched( - const content::URLDataSource::GotDataCallback& got_data_callback, + content::URLDataSource::GotDataCallback got_data_callback, BitmapFetcherService::RequestId request_id, const GURL& url, const SkBitmap& bitmap) { if (bitmap.isNull()) { LOG(ERROR) << "Failed to retrieve Brave Rewards resource, url: " << url; - got_data_callback.Run(nullptr); + std::move(got_data_callback).Run(nullptr); return; } - got_data_callback.Run(BitmapToMemory(&bitmap).get()); + std::move(got_data_callback).Run(BitmapToMemory(&bitmap).get()); auto it_url = - find(resource_fetchers_.begin(), resource_fetchers_.end(), url.spec()); + find(resource_fetchers_.begin(), resource_fetchers_.end(), url); if (it_url != resource_fetchers_.end()) { resource_fetchers_.erase(it_url); } diff --git a/browser/ui/webui/brave_rewards_source.h b/browser/ui/webui/brave_rewards_source.h index df79f7b2bea..ff3420e0b79 100644 --- a/browser/ui/webui/brave_rewards_source.h +++ b/browser/ui/webui/brave_rewards_source.h @@ -26,9 +26,9 @@ class BraveRewardsSource : public content::URLDataSource { // content::URLDataSource implementation. std::string GetSource() override; void StartDataRequest( - const std::string& path, + const GURL& url, const content::WebContents::Getter& wc_getter, - const content::URLDataSource::GotDataCallback& got_data_callback) + content::URLDataSource::GotDataCallback got_data_callback) override; std::string GetMimeType(const std::string&) override; bool AllowCaching() override; @@ -39,13 +39,13 @@ class BraveRewardsSource : public content::URLDataSource { private: void OnBitmapFetched( - const content::URLDataSource::GotDataCallback& got_data_callback, + content::URLDataSource::GotDataCallback got_data_callback, BitmapFetcherService::RequestId request_id, const GURL& url, const SkBitmap& bitmap); Profile* profile_; - std::vector resource_fetchers_; + std::vector resource_fetchers_; std::vector request_ids_; DISALLOW_COPY_AND_ASSIGN(BraveRewardsSource); diff --git a/components/ntp_sponsored_images/browser/ntp_sponsored_image_source.cc b/components/ntp_sponsored_images/browser/ntp_sponsored_image_source.cc index c1dfa30d6f7..6a59d9337be 100644 --- a/components/ntp_sponsored_images/browser/ntp_sponsored_image_source.cc +++ b/components/ntp_sponsored_images/browser/ntp_sponsored_image_source.cc @@ -44,11 +44,16 @@ std::string NTPSponsoredImageSource::GetSource() { } void NTPSponsoredImageSource::StartDataRequest( - const std::string& path, + const GURL& url, const content::WebContents::Getter& wc_getter, - const GotDataCallback& callback) { + GotDataCallback callback) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + std::string path = url.path(); + if (!path.empty()) { + path.erase(0,1); + } + if (!IsValidPath(path)) { scoped_refptr bytes; std::move(callback).Run(std::move(bytes)); @@ -69,7 +74,8 @@ void NTPSponsoredImageSource::StartDataRequest( } else { DCHECK(IsWallpaperPath(path)); image_file_path = - images_data->wallpaper_image_files[GetWallpaperIndexFromPath(path)]; + images_data + ->wallpaper_image_files[GetWallpaperIndexFromPath(path)]; } base::PostTaskAndReplyWithResult( @@ -77,11 +83,11 @@ void NTPSponsoredImageSource::StartDataRequest( base::BindOnce(&ReadFileToString, image_file_path), base::BindOnce(&NTPSponsoredImageSource::OnGotImageFile, weak_factory_.GetWeakPtr(), - callback)); + std::move(callback))); } void NTPSponsoredImageSource::OnGotImageFile( - const GotDataCallback& callback, + GotDataCallback callback, base::Optional input) { if (!input) return; diff --git a/components/ntp_sponsored_images/browser/ntp_sponsored_image_source.h b/components/ntp_sponsored_images/browser/ntp_sponsored_image_source.h index 60d419115da..c4b50b6955c 100644 --- a/components/ntp_sponsored_images/browser/ntp_sponsored_image_source.h +++ b/components/ntp_sponsored_images/browser/ntp_sponsored_image_source.h @@ -30,12 +30,12 @@ class NTPSponsoredImageSource : public content::URLDataSource { private: // content::URLDataSource overrides: std::string GetSource() override; - void StartDataRequest(const std::string& path, + void StartDataRequest(const GURL& url, const content::WebContents::Getter& wc_getter, - const GotDataCallback& callback) override; + GotDataCallback callback) override; std::string GetMimeType(const std::string& path) override; - void OnGotImageFile(const GotDataCallback& callback, + void OnGotImageFile(GotDataCallback callback, base::Optional input); bool IsValidPath(const std::string& path) const; bool IsLogoPath(const std::string& path) const; diff --git a/content/browser/webui/brave_shared_resources_data_source.cc b/content/browser/webui/brave_shared_resources_data_source.cc index 3e120381568..b710929bb5b 100644 --- a/content/browser/webui/brave_shared_resources_data_source.cc +++ b/content/browser/webui/brave_shared_resources_data_source.cc @@ -108,9 +108,10 @@ std::string BraveSharedResourcesDataSource::GetSource() { } void BraveSharedResourcesDataSource::StartDataRequest( - const std::string& path, + const GURL& url, const content::WebContents::Getter& wc_getter, - const content::URLDataSource::GotDataCallback& callback) { + content::URLDataSource::GotDataCallback callback) { + const std::string path = URLDataSource::URLToRequestPath(url); int idr = GetIdrForPath(path); DCHECK_NE(-1, idr) << " path: " << path; scoped_refptr bytes; @@ -118,9 +119,9 @@ void BraveSharedResourcesDataSource::StartDataRequest( // Cannot access GetContentClient() from here as that is //content/public // only. Therefore, cannot access ContentClient::GetDataResourceBytes, // so go to the bundle directly. This will work for all content clients apart - // from in a test environment, where this shoudl be mocked. + // from in a test environment, where this should be mocked. bytes = ui::ResourceBundle::GetSharedInstance().LoadDataResourceBytes(idr); - callback.Run(bytes.get()); + std::move(callback).Run(bytes.get()); } bool BraveSharedResourcesDataSource::AllowCaching() { diff --git a/content/browser/webui/brave_shared_resources_data_source.h b/content/browser/webui/brave_shared_resources_data_source.h index 961dd07fbea..7df35789b17 100644 --- a/content/browser/webui/brave_shared_resources_data_source.h +++ b/content/browser/webui/brave_shared_resources_data_source.h @@ -24,9 +24,9 @@ class BraveSharedResourcesDataSource : public content::URLDataSource { // URLDataSource implementation. std::string GetSource() override; void StartDataRequest( - const std::string& path, + const GURL& url, const content::WebContents::Getter& wc_getter, - const content::URLDataSource::GotDataCallback& callback) override; + content::URLDataSource::GotDataCallback callback) override; bool AllowCaching() override; std::string GetMimeType(const std::string& path) override; bool ShouldServeMimeTypeAsContentTypeHeader() override;