Skip to content

Commit

Permalink
Merge pull request #939 from brave/publisher-branded-site-banners
Browse files Browse the repository at this point in the history
Support publisher branded site banners
  • Loading branch information
NejcZdovc authored Dec 4, 2018
2 parents 3f0f487 + bf3a138 commit b0c4a7b
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ deps = {
"vendor/python-patch": "https://github.com/svn2github/python-patch@a336a458016ced89aba90dfc3f4c8222ae3b1403",
"vendor/omaha": "https://github.com/brave/omaha.git@e4263050ed24e92f4d3ed0b6874538f811011c5b",
"vendor/sparkle": "https://github.com/brave/Sparkle.git@c0759cce415d7c0feae45005c8a013b1898711f0",
"vendor/bat-native-ledger": "https://github.com/brave-intl/bat-native-ledger@b4fb7f9ea3d1ce9161d92e30091788830267211f",
"vendor/bat-native-ledger": "https://github.com/brave-intl/bat-native-ledger@c07de55c1f8cb8ea4259f1ff01c0ef3c0dc39150",
"vendor/bat-native-rapidjson": "https://github.com/brave-intl/bat-native-rapidjson.git@86aafe2ef89835ae71c9ed7c2527e3bb3000930e",
"vendor/bip39wally-core-native": "https://github.com/brave-intl/bip39wally-core-native.git@9b119931c702d55be994117eb505d56310720b1d",
"vendor/bat-native-anonize": "https://github.com/brave-intl/bat-native-anonize.git@adeff3254bb90ccdc9699040d5a4e1cd6b8393b7",
Expand Down
2 changes: 2 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ source_set("ui") {

if (brave_rewards_enabled) {
sources += [
"webui/brave_rewards_source.cc",
"webui/brave_rewards_source.h",
"webui/brave_rewards_ui.cc",
"webui/brave_rewards_ui.h",
]
Expand Down
162 changes: 162 additions & 0 deletions browser/ui/webui/brave_rewards_source.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/webui/brave_rewards_source.h"

#include "base/memory/ref_counted_memory.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "url/gurl.h"

namespace {

typedef base::RepeatingCallback<void(BitmapFetcherService::RequestId request_id,
const GURL& url,
const SkBitmap& bitmap)>
RewardsResourceFetcherCallback;

// Calls the specified callback when the requested image is downloaded. This
// is a separate class instead of being implemented on BraveRewardsSource
// because BitmapFetcherService currently takes ownership of this object.
class RewardsResourceFetcherObserver : public BitmapFetcherService::Observer {
public:
explicit RewardsResourceFetcherObserver(
const GURL& url,
const RewardsResourceFetcherCallback& rewards_resource_fetcher_callback)
: url_(url),
rewards_resource_fetcher_callback_(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);
}

private:
GURL url_;
const RewardsResourceFetcherCallback rewards_resource_fetcher_callback_;

DISALLOW_COPY_AND_ASSIGN(RewardsResourceFetcherObserver);
};

scoped_refptr<base::RefCountedMemory> BitmapToMemory(const SkBitmap* image) {
base::RefCountedBytes* image_bytes = new base::RefCountedBytes;
gfx::PNGCodec::EncodeBGRASkBitmap(*image, false, &image_bytes->data());
return image_bytes;
}

} // namespace

BraveRewardsSource::BraveRewardsSource(Profile* profile)
: profile_(profile->GetOriginalProfile()) {}

BraveRewardsSource::~BraveRewardsSource() {
}

std::string BraveRewardsSource::GetSource() const {
return "rewards-image";
}

void BraveRewardsSource::StartDataRequest(
const std::string& path,
const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
const content::URLDataSource::GotDataCallback& got_data_callback) {
GURL url(path);
if (!url.is_valid()) {
got_data_callback.Run(nullptr);
return;
}

auto it = find(resource_fetchers_.begin(), resource_fetchers_.end(), url);
if (it != resource_fetchers_.end()) {
LOG(WARNING) << "Already fetching specified Brave Rewards resource, url: "
<< url;
return;
}

BitmapFetcherService* image_service =
BitmapFetcherServiceFactory::GetForBrowserContext(profile_);
if (image_service) {
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("brave_rewards_resource_fetcher", R"(
semantics {
sender:
"Brave Rewards resource fetcher"
description:
"Fetches resources related to Brave Rewards."
trigger:
"User visits a media publisher's site."
data: "Brave Rewards related resources."
destination: WEBSITE
}
policy {
cookies_allowed: NO
setting:
"This feature cannot be disabled by settings."
policy_exception_justification:
"Not implemented."
})");
resource_fetchers_.emplace_back(path);
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)),
traffic_annotation));
}
}

std::string BraveRewardsSource::GetMimeType(const std::string&) const {
// 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 BraveRewardsSource::AllowCaching() const {
return false;
}

bool BraveRewardsSource::ShouldReplaceExistingSource() const {
// Leave the existing DataSource in place, otherwise we'll drop any pending
// requests on the floor.
return false;
}

bool BraveRewardsSource::ShouldServiceRequest(
const GURL& url,
content::ResourceContext* resource_context,
int render_process_id) const {
return URLDataSource::ShouldServiceRequest(url, resource_context,
render_process_id);
}

void BraveRewardsSource::OnBitmapFetched(
const 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);
return;
}

got_data_callback.Run(BitmapToMemory(&bitmap).get());

auto it_url =
find(resource_fetchers_.begin(), resource_fetchers_.end(), url.spec());
if (it_url != resource_fetchers_.end()) {
resource_fetchers_.erase(it_url);
}

auto it_ids = find(request_ids_.begin(), request_ids_.end(), request_id);
if (it_ids != request_ids_.end()) {
request_ids_.erase(it_ids);
}
}
53 changes: 53 additions & 0 deletions browser/ui/webui/brave_rewards_source.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_UI_WEBUI_BRAVE_REWARDS_SOURCE_H_
#define BRAVE_BROWSER_UI_WEBUI_BRAVE_REWARDS_SOURCE_H_

#include <string>
#include <vector>

#include "base/macros.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h"
#include "content/public/browser/url_data_source.h"

class GURL;
class Profile;
class SkBitmap;

class BraveRewardsSource : public content::URLDataSource {
public:
explicit BraveRewardsSource(Profile* profile);

~BraveRewardsSource() override;

// content::URLDataSource implementation.
std::string GetSource() const override;
void StartDataRequest(
const std::string& path,
const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
const content::URLDataSource::GotDataCallback& got_data_callback)
override;
std::string GetMimeType(const std::string&) const override;
bool AllowCaching() const override;
bool ShouldReplaceExistingSource() const override;
bool ShouldServiceRequest(const GURL& url,
content::ResourceContext* resource_context,
int render_process_id) const override;

private:
void OnBitmapFetched(
const content::URLDataSource::GotDataCallback& got_data_callback,
BitmapFetcherService::RequestId request_id,
const GURL& url,
const SkBitmap& bitmap);

Profile* profile_;
std::vector<std::string> resource_fetchers_;
std::vector<BitmapFetcherService::RequestId> request_ids_;

DISALLOW_COPY_AND_ASSIGN(BraveRewardsSource);
};

#endif // BRAVE_BROWSER_UI_WEBUI_BRAVE_REWARDS_SOURCE_H_
6 changes: 6 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "bat/ledger/media_publisher_info.h"
#include "bat/ledger/publisher_info.h"
#include "bat/ledger/wallet_info.h"
#include "brave/browser/ui/webui/brave_rewards_source.h"
#include "brave/common/brave_switches.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_rewards/browser/balance_report.h"
Expand All @@ -44,6 +45,7 @@
#include "components/favicon_base/favicon_types.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/url_data_source.h"
#include "content_site.h"
#include "extensions/buildflags/buildflags.h"
#include "net/base/escape.h"
Expand Down Expand Up @@ -317,6 +319,10 @@ RewardsServiceImpl::RewardsServiceImpl(Profile* profile)
HandleFlags(options);
}
}

// Set up the rewards data source
content::URLDataSource::Add(profile_,
std::make_unique<BraveRewardsSource>(profile_));
}

RewardsServiceImpl::~RewardsServiceImpl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ class Banner extends React.Component<Props, State> {

if (publisher) {
title = publisher.title
// TODO re-enable when IsWebUIAllowedToMakeNetworkRequests crash is fixed
// background = publisher.background
// logo = publisher.logo
background = publisher.background
logo = publisher.logo
publisherKey = publisher.publisherKey
description = publisher.description
name = publisher.name
Expand Down

0 comments on commit b0c4a7b

Please sign in to comment.