Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for referral promos #428

Merged
merged 13 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ group("create_dist") {
}
if (is_mac) {
deps += [ "build/mac:create_dist_mac" ]
if (brave_channel == "") {
deps += [
"build/mac:create_pkg",
"build/mac:sign_pkg",
]
}
}
if (is_linux) {
deps += [
Expand Down
8 changes: 5 additions & 3 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ source_set("browser_process") {
deps = [
":version_info",
"//base",
"//brave/components/brave_rewards/browser",
"//brave/browser/referrals",
"//brave/browser/tor",
"//brave/common",
"//brave/components/brave_rewards/browser",
"//brave/components/brave_shields/browser:brave_shields",
"//brave/components/content_settings/core/browser",
"//brave/common",
"//chrome/common",
"//components/component_updater",
"//components/prefs",
Expand Down Expand Up @@ -113,11 +114,12 @@ source_set("browser") {
"//brave/browser/resources:brave_extension_grit",
"//chrome/browser",
"extensions",
"tor",
"net",
"permissions",
"profiles",
"referrals",
"renderer_host",
"tor",
"ui",
"//ui/base",
]
Expand Down
12 changes: 12 additions & 0 deletions browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "brave/browser/extensions/brave_tor_client_updater.h"
#include "brave/browser/profiles/brave_profile_manager.h"
#include "brave/browser/profile_creation_monitor.h"
#include "brave/browser/referrals/brave_referrals_service.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service.h"
#include "brave/components/brave_shields/browser/https_everywhere_service.h"
Expand All @@ -35,6 +36,17 @@ BraveBrowserProcessImpl::BraveBrowserProcessImpl(scoped_refptr<PersistentPrefSto
profile_creation_monitor_(new ProfileCreationMonitor) {
g_browser_process = this;
g_brave_browser_process = this;

brave_referrals_service_ = brave::BraveReferralsServiceFactory(local_state());
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](brave::BraveReferralsService* referrals_service) {
referrals_service->Start();
},
base::Unretained(brave_referrals_service_.get())),
base::TimeDelta::FromSeconds(30));

brave_stats_updater_ = brave::BraveStatsUpdaterFactory(local_state());
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
Expand Down
2 changes: 2 additions & 0 deletions browser/brave_browser_process_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class ProfileCreationMonitor;

namespace brave {
class BraveReferralsService;
class BraveStatsUpdater;
}

Expand Down Expand Up @@ -51,6 +52,7 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
std::unique_ptr<brave_shields::HTTPSEverywhereService>
https_everywhere_service_;
std::unique_ptr<brave::BraveStatsUpdater> brave_stats_updater_;
std::unique_ptr<brave::BraveReferralsService> brave_referrals_service_;
std::unique_ptr<extensions::BraveTorClientUpdater> tor_client_updater_;
std::unique_ptr<ProfileCreationMonitor> profile_creation_monitor_;

Expand Down
2 changes: 2 additions & 0 deletions browser/brave_local_state_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/values.h"
#include "brave/browser/brave_stats_updater.h"
#include "brave/browser/referrals/brave_referrals_service.h"
#include "brave/browser/tor/tor_profile_service.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
Expand All @@ -14,6 +15,7 @@ namespace brave {

void RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
RegisterPrefsForBraveStatsUpdater(registry);
RegisterPrefsForBraveReferralsService(registry);
#if defined(OS_MACOSX)
// Turn off super annoying 'Hold to quit'
registry->SetDefaultPrefValue(prefs::kConfirmToQuitEnabled,
Expand Down
3 changes: 2 additions & 1 deletion browser/brave_stats_updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ GURL GetUpdateURL(const brave::BraveStatsUpdaterParams& stats_updater_params) {
update_url, "first", stats_updater_params.GetFirstCheckMadeParam());
update_url = net::AppendQueryParameter(
update_url, "woi", stats_updater_params.GetWeekOfInstallationParam());
update_url = net::AppendQueryParameter(update_url, "ref", "none");
update_url = net::AppendQueryParameter(
update_url, "ref", stats_updater_params.GetReferralCodeParam());
return update_url;
}

Expand Down
5 changes: 5 additions & 0 deletions browser/brave_stats_updater_params.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ std::string BraveStatsUpdaterParams::GetWeekOfInstallationParam() const {
return week_of_installation_;
}

std::string BraveStatsUpdaterParams::GetReferralCodeParam() const {
return referral_promo_code_.empty() ? "none" : referral_promo_code_;
}

void BraveStatsUpdaterParams::LoadPrefs() {
last_check_ymd_ = pref_service_->GetString(kLastCheckYMD);
last_check_woy_ = pref_service_->GetInteger(kLastCheckWOY);
Expand All @@ -62,6 +66,7 @@ void BraveStatsUpdaterParams::LoadPrefs() {
week_of_installation_ = pref_service_->GetString(kWeekOfInstallation);
if (week_of_installation_.empty())
week_of_installation_ = GetLastMondayAsYMD();
referral_promo_code_ = pref_service_->GetString(kReferralPromoCode);
}

void BraveStatsUpdaterParams::SavePrefs() {
Expand Down
2 changes: 2 additions & 0 deletions browser/brave_stats_updater_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BraveStatsUpdaterParams {
std::string GetMonthlyParam() const;
std::string GetFirstCheckMadeParam() const;
std::string GetWeekOfInstallationParam() const;
std::string GetReferralCodeParam() const;

void SavePrefs();

Expand All @@ -44,6 +45,7 @@ class BraveStatsUpdaterParams {
int last_check_month_;
bool first_check_made_;
std::string week_of_installation_;
std::string referral_promo_code_;

void LoadPrefs();

Expand Down
3 changes: 2 additions & 1 deletion browser/brave_stats_updater_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include "brave/browser/brave_stats_updater.h"

#include "brave/browser/brave_stats_updater.h"
#include "brave/browser/brave_stats_updater_params.h"
#include "brave/browser/referrals/brave_referrals_service.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/browser_process.h"
#include "components/prefs/testing_pref_service.h"
Expand All @@ -32,6 +32,7 @@ class BraveStatsUpdaterTest: public testing::Test {
~BraveStatsUpdaterTest() override {}
void SetUp() override {
brave::RegisterPrefsForBraveStatsUpdater(testing_local_state_.registry());
brave::RegisterPrefsForBraveReferralsService(testing_local_state_.registry());
}

PrefService* GetLocalState() { return &testing_local_state_; }
Expand Down
6 changes: 4 additions & 2 deletions browser/net/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ source_set("net") {
sources = [
"brave_common_static_redirect_network_delegate_helper.cc",
"brave_common_static_redirect_network_delegate_helper.h",
"brave_network_delegate_base.cc",
"brave_network_delegate_base.h",
"brave_httpse_network_delegate_helper.cc",
"brave_httpse_network_delegate_helper.h",
"brave_network_delegate_base.cc",
"brave_network_delegate_base.h",
"brave_profile_network_delegate.cc",
"brave_profile_network_delegate.h",
"brave_referrals_network_delegate_helper.cc",
"brave_referrals_network_delegate_helper.h",
"brave_site_hacks_network_delegate_helper.cc",
"brave_site_hacks_network_delegate_helper.h",
"brave_static_redirect_network_delegate_helper.cc",
Expand Down
18 changes: 16 additions & 2 deletions browser/net/brave_network_delegate_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,33 @@
#include <algorithm>

#include "brave/browser/net/url_context.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/browser_process.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
#include "net/url_request/url_request.h"


using content::BrowserThread;

BraveNetworkDelegateBase::BraveNetworkDelegateBase(
extensions::EventRouterForwarder* event_router) :
ChromeNetworkDelegate(event_router) {
extensions::EventRouterForwarder* event_router)
: ChromeNetworkDelegate(event_router), referral_headers_list_(nullptr) {
// Retrieve the current referral headers, if any.
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&BraveNetworkDelegateBase::GetReferralHeaders,
base::Unretained(this)));
}

BraveNetworkDelegateBase::~BraveNetworkDelegateBase() {
}

void BraveNetworkDelegateBase::GetReferralHeaders() {
referral_headers_list_ =
g_browser_process->local_state()->GetList(kReferralHeaders);
}

int BraveNetworkDelegateBase::OnBeforeURLRequest(net::URLRequest* request,
net::CompletionOnceCallback callback,
GURL* new_url) {
Expand Down Expand Up @@ -49,6 +62,7 @@ int BraveNetworkDelegateBase::OnBeforeStartTransaction(net::URLRequest* request,
ctx->headers = headers;
ctx->request_identifier = request->identifier();
ctx->event_type = brave::kOnBeforeStartTransaction;
ctx->referral_headers_list = referral_headers_list_;
RunNextCallback(request, nullptr, ctx);
return net::ERR_IO_PENDING;
}
Expand Down
2 changes: 2 additions & 0 deletions browser/net/brave_network_delegate_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class BraveNetworkDelegateBase : public ChromeNetworkDelegate {
headers_received_callbacks_;

private:
void GetReferralHeaders();
const base::ListValue* referral_headers_list_;
std::map<uint64_t, net::CompletionOnceCallback> callbacks_;

DISALLOW_COPY_AND_ASSIGN(BraveNetworkDelegateBase);
Expand Down
14 changes: 9 additions & 5 deletions browser/net/brave_profile_network_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

#include "brave/browser/net/brave_common_static_redirect_network_delegate_helper.h"
#include "brave/browser/net/brave_httpse_network_delegate_helper.h"
#include "brave/browser/net/brave_referrals_network_delegate_helper.h"
#include "brave/browser/net/brave_site_hacks_network_delegate_helper.h"
#include "brave/browser/net/brave_tor_network_delegate_helper.h"
#include "brave/components/brave_rewards/browser/buildflags/buildflags.h"
#include "brave/components/brave_webtorrent/browser/net/brave_torrent_redirect_network_delegate_helper.h"

#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
#include "brave/components/brave_rewards/browser/net/network_delegate_helper.h"
#endif
#include "brave/browser/net/brave_tor_network_delegate_helper.h"
#include "brave/components/brave_webtorrent/browser/net/brave_torrent_redirect_network_delegate_helper.h"

BraveProfileNetworkDelegate::BraveProfileNetworkDelegate(
extensions::EventRouterForwarder* event_router) :
Expand All @@ -39,9 +39,13 @@ BraveProfileNetworkDelegate::BraveProfileNetworkDelegate(
callback = base::Bind(brave::OnBeforeURLRequest_TorWork);
before_url_request_callbacks_.push_back(callback);

brave::OnBeforeStartTransactionCallback start_transactions_callback =
brave::OnBeforeStartTransactionCallback start_transaction_callback =
base::Bind(brave::OnBeforeStartTransaction_SiteHacksWork);
before_start_transaction_callbacks_.push_back(start_transactions_callback);
before_start_transaction_callbacks_.push_back(start_transaction_callback);

start_transaction_callback =
base::Bind(brave::OnBeforeStartTransaction_ReferralsWork);
before_start_transaction_callbacks_.push_back(start_transaction_callback);

brave::OnHeadersReceivedCallback headers_received_callback =
base::Bind(
Expand Down
56 changes: 56 additions & 0 deletions browser/net/brave_referrals_network_delegate_helper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* 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/net/brave_referrals_network_delegate_helper.h"

#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/common/url_pattern.h"
#include "net/url_request/url_request.h"

namespace brave {

int OnBeforeStartTransaction_ReferralsWork(
net::URLRequest* request,
net::HttpRequestHeaders* headers,
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx) {
if (!ctx->referral_headers_list)
return net::OK;
// If the domain for this request matches one of our target domains,
// set the associated custom headers.
for (const auto& headers_value : *ctx->referral_headers_list) {
const base::Value* domains_list =
headers_value.FindKeyOfType("domains", base::Value::Type::LIST);
if (!domains_list) {
LOG(WARNING) << "Failed to retrieve 'domains' key from referral headers";
continue;
}
const base::Value* headers_dict =
headers_value.FindKeyOfType("headers", base::Value::Type::DICTIONARY);
if (!headers_dict) {
LOG(WARNING) << "Failed to retrieve 'headers' key from referral headers";
continue;
}
for (const auto& domain_value : domains_list->GetList()) {
URLPattern url_pattern(URLPattern::SCHEME_HTTPS |
URLPattern::SCHEME_HTTP);
url_pattern.SetScheme("*");
url_pattern.SetHost(domain_value.GetString());
url_pattern.SetPath("/*");
url_pattern.SetMatchSubdomains(true);
if (!url_pattern.MatchesURL(request->url()))
continue;
for (const auto& it : headers_dict->DictItems()) {
headers->SetHeader(it.first, it.second.GetString());
}
return net::OK;
}
}

return net::OK;
}

} // namespace brave
27 changes: 27 additions & 0 deletions browser/net/brave_referrals_network_delegate_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* 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_NET_BRAVE_REFERRALS_NETWORK_DELEGATE_H_
#define BRAVE_BROWSER_NET_BRAVE_REFERRALS_NETWORK_DELEGATE_H_

#include "brave/browser/net/url_context.h"

struct BraveRequestInfo;

namespace net {
class HttpRequestHeaders;
class URLRequest;
}

namespace brave {

int OnBeforeStartTransaction_ReferralsWork(
net::URLRequest* request,
net::HttpRequestHeaders* headers,
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx);

} // namespace brave

#endif // BRAVE_BROWSER_NET_BRAVE_REFERRALS_NETWORK_DELEGATE_H_
Loading