From 75566663b192bfed6c69f0978f91ee05a1127881 Mon Sep 17 00:00:00 2001 From: mkarolin Date: Wed, 5 Feb 2020 12:42:01 -0500 Subject: [PATCH] Fixes rewards-image handler post C80 bump. The handler now gets a full url instead of just path. In the past the path was the actual url we wanted retrieved, but now the url includes the chrome://rewards-image prefix, so we need to extract path from the url. URLDataSource::URLToRequestPath is used to do that. Also updated NTP Sponsored Image handler to use URLDataSource::URLToRequestPath instead of extracting path manually. Fixes brave/brave-browser#8074 --- browser/ui/webui/brave_rewards_source.cc | 17 +++++++++++------ .../browser/ntp_sponsored_image_source.cc | 7 +------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/browser/ui/webui/brave_rewards_source.cc b/browser/ui/webui/brave_rewards_source.cc index 17d1622dd0e0..1a882951aabf 100644 --- a/browser/ui/webui/brave_rewards_source.cc +++ b/browser/ui/webui/brave_rewards_source.cc @@ -72,15 +72,20 @@ void BraveRewardsSource::StartDataRequest( const GURL& url, const content::WebContents::Getter& wc_getter, content::URLDataSource::GotDataCallback got_data_callback) { - if (!url.is_valid()) { + // URL here comes in the form of + // chrome://rewards-image/https://rewards.brave.com/... + // We need to take the path and make it into a URL. + GURL actual_url(URLDataSource::URLToRequestPath(url)); + if (!actual_url.is_valid()) { std::move(got_data_callback).Run(nullptr); return; } - auto it = find(resource_fetchers_.begin(), resource_fetchers_.end(), url); + auto it = + find(resource_fetchers_.begin(), resource_fetchers_.end(), actual_url); if (it != resource_fetchers_.end()) { LOG(WARNING) << "Already fetching specified Brave Rewards resource, url: " - << url; + << actual_url; return; } @@ -106,12 +111,12 @@ void BraveRewardsSource::StartDataRequest( policy_exception_justification: "Not implemented." })"); - resource_fetchers_.emplace_back(url); + resource_fetchers_.emplace_back(actual_url); request_ids_.push_back(image_service->RequestImage( - url, + actual_url, // Image Service takes ownership of the observer. new RewardsResourceFetcherObserver( - url, base::BindOnce(&BraveRewardsSource::OnBitmapFetched, + actual_url, base::BindOnce(&BraveRewardsSource::OnBitmapFetched, base::Unretained(this), std::move(got_data_callback))), traffic_annotation)); 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 d04900f6bf29..497a6cc0feba 100644 --- a/components/ntp_sponsored_images/browser/ntp_sponsored_image_source.cc +++ b/components/ntp_sponsored_images/browser/ntp_sponsored_image_source.cc @@ -49,12 +49,7 @@ void NTPSponsoredImageSource::StartDataRequest( GotDataCallback callback) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); - // GURL::path() returns path with the leading slash, which we don't want. - std::string path = url.path(); - if (!path.empty()) { - path.erase(0, 1); - } - + const std::string path = URLDataSource::URLToRequestPath(url); if (!IsValidPath(path)) { scoped_refptr bytes; std::move(callback).Run(std::move(bytes));