Skip to content

Commit

Permalink
brave_referrals: Hardcode custom headers list
Browse files Browse the repository at this point in the history
Minimize network requests by hardcoding the referral headers as opposed
to fetching them from the network. These headers are deprecated and
won't change anymore.

Resolves brave/brave-browser#16455
  • Loading branch information
Kevin Kuehler committed Jun 17, 2021
1 parent ef80663 commit 3730843
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 110 deletions.
152 changes: 51 additions & 101 deletions components/brave_referrals/browser/brave_referrals_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <memory>
#include <utility>
#include <vector>

#include "base/callback_helpers.h"
#include "base/environment.h"
Expand Down Expand Up @@ -50,9 +51,6 @@
#include "chrome/browser/ui/browser.h"
#endif

// Fetch headers from the referral server once a day.
const int kFetchReferralHeadersFrequency = 60 * 60 * 24;

// Perform finalization checks once a day.
const int kFinalizationChecksFrequency = 60 * 60 * 24;

Expand All @@ -70,11 +68,45 @@ namespace brave {

namespace {

constexpr char kPartnerEaffName[] = "eaff";
constexpr char kPartnerUpholdName[] = "uphold";
constexpr char kPartnerGrammarlyName[] = "grammarly";

const std::vector<std::string> kPartnerEaffDomains = {"eaff.com",
"stg.eaff.com"};
const std::vector<std::string> kPartnerUpholdDomains = {
"sandbox.uphold.com", "api-sandbox.uphold.com", "uphold.com",
"api.uphold.com"};
const std::vector<std::string> kPartnerGrammarlyDomains = {
"www.grammarly.com", "grammarly.com", "static.grammarly.com",
"gnar.grammarly.com"};

BraveReferralsService::ReferralInitializedCallback*
g_testing_referral_initialized_callback = nullptr;

base::FilePath g_promo_file_path;

base::Value CreateReferralHeader(
const char partner_name[],
const std::vector<std::string>& partner_domains) {
base::Value headers_dict(base::Value::Type::DICTIONARY);
base::Value cookie_names(base::Value::Type::LIST);
headers_dict.SetKey("cookieNames", std::move(cookie_names));
base::Value domains(base::Value::Type::LIST);
for (const auto& header : partner_domains)
domains.Append(header);
headers_dict.SetKey("domains", std::move(domains));

constexpr double referral_header_expiration = 31536000000.0;
headers_dict.SetDoubleKey("expiration", referral_header_expiration);

base::Value headers_sub_dict(base::Value::Type::DICTIONARY);
headers_sub_dict.SetStringKey(kBravePartnerHeader, partner_name);
headers_dict.SetKey("headers", std::move(headers_sub_dict));

return headers_dict;
}

void DeletePromoCodeFile(const base::FilePath& promo_code_file) {
if (!base::DeleteFile(promo_code_file)) {
LOG(ERROR) << "Failed to delete referral promo code file "
Expand All @@ -97,8 +129,7 @@ std::string ReadPromoCode(const base::FilePath& promo_code_file) {

base::TrimWhitespaceASCII(promo_code, base::TRIM_ALL, &promo_code);
if (promo_code.empty()) {
LOG(ERROR) << "Promo code file " << promo_code_file.value()
<< " is empty";
LOG(ERROR) << "Promo code file " << promo_code_file.value() << " is empty";
return "";
}

Expand All @@ -115,8 +146,7 @@ std::string BuildReferralEndpoint(const std::string& path) {
if (env->HasVar("BRAVE_REFERRALS_LOCAL"))
proto = "http";

return base::StringPrintf("%s://%s%s", proto.c_str(),
referral_server.c_str(),
return base::StringPrintf("%s://%s%s", proto.c_str(), referral_server.c_str(),
path.c_str());
}

Expand All @@ -140,8 +170,7 @@ BraveReferralsService::BraveReferralsService(PrefService* pref_service,
}
}

BraveReferralsService::~BraveReferralsService() {
}
BraveReferralsService::~BraveReferralsService() {}

void BraveReferralsService::OnProfileAdded(Profile* profile) {
if (profile == ProfileManager::GetPrimaryUserProfile()) {
Expand Down Expand Up @@ -170,20 +199,9 @@ void BraveReferralsService::Start() {
// Fetch the referral headers on startup.
FetchReferralHeaders();

// Also, periodically fetch the referral headers.
DCHECK(!fetch_referral_headers_timer_);
fetch_referral_headers_timer_ = std::make_unique<base::RepeatingTimer>();
fetch_referral_headers_timer_->Start(
FROM_HERE,
base::TimeDelta::FromSeconds(
brave_base::random::Geometric(kFetchReferralHeadersFrequency)),
this, &BraveReferralsService::OnFetchReferralHeadersTimerFired);
DCHECK(fetch_referral_headers_timer_->IsRunning());

// Read the promo code from user-data-dir and initialize the referral,
// retrying if necessary.
bool has_initialized =
pref_service_->GetBoolean(kReferralInitialization);
bool has_initialized = pref_service_->GetBoolean(kReferralInitialization);
// TODO(keur): This can be removed eventually. This prevents existing
// users without download_ids from initializing.
bool checked_for_promo_code_file =
Expand All @@ -207,7 +225,6 @@ void BraveReferralsService::Start() {
void BraveReferralsService::Stop() {
initialization_timer_.reset();
finalization_checks_timer_.reset();
fetch_referral_headers_timer_.reset();
initialized_ = false;
}

Expand Down Expand Up @@ -260,39 +277,6 @@ void BraveReferralsService::OnFinalizationChecksTimerFired() {
PerformFinalizationChecks();
}

void BraveReferralsService::OnFetchReferralHeadersTimerFired() {
FetchReferralHeaders();
}

void BraveReferralsService::OnReferralHeadersLoadComplete(
std::unique_ptr<std::string> response_body) {
int response_code = -1;
if (referral_headers_loader_->ResponseInfo() &&
referral_headers_loader_->ResponseInfo()->headers)
response_code =
referral_headers_loader_->ResponseInfo()->headers->response_code();
if (referral_headers_loader_->NetError() != net::OK || response_code < 200 ||
response_code > 299) {
const std::string safe_response_body =
response_body ? *response_body : std::string();
LOG(ERROR) << "Failed to fetch headers from referral server"
<< ", error: " << referral_headers_loader_->NetError()
<< ", response code: " << response_code
<< ", payload: " << safe_response_body
<< ", url: " << referral_headers_loader_->GetFinalURL().spec();
return;
}

base::JSONReader::ValueWithError root =
base::JSONReader::ReadAndReturnValueWithError(*response_body);
if (!root.value || !root.value->is_list()) {
LOG(ERROR) << "Failed to parse referral headers response: "
<< (!root.value ? root.error_message : "not a list");
return;
}
pref_service_->Set(kReferralHeaders, root.value.value());
}

void BraveReferralsService::OnReferralInitLoadComplete(
std::unique_ptr<std::string> response_body) {
int response_code = -1;
Expand All @@ -311,11 +295,10 @@ void BraveReferralsService::OnReferralInitLoadComplete(
<< ", url: " << referral_init_loader_->GetFinalURL().spec();
initialization_timer_ = std::make_unique<base::OneShotTimer>();
initialization_timer_->Start(
FROM_HERE,
base::TimeDelta::FromSeconds(
brave_base::random::Geometric(
kReportInitializationFrequency)),
this, &BraveReferralsService::InitReferral);
FROM_HERE,
base::TimeDelta::FromSeconds(
brave_base::random::Geometric(kReportInitializationFrequency)),
this, &BraveReferralsService::InitReferral);
DCHECK(initialization_timer_->IsRunning());
return;
}
Expand Down Expand Up @@ -574,46 +557,13 @@ std::string BraveReferralsService::BuildReferralFinalizationCheckPayload()
}

void BraveReferralsService::FetchReferralHeaders() {
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation(
"brave_referral_headers_fetcher", R"(
semantics {
sender:
"Brave Referrals Service"
description:
"Fetches referral headers from Brave."
trigger:
"An update timer indicates that it's time to fetch referral headers."
data: "Brave referral headers."
destination: WEBSITE
}
policy {
cookies_allowed: NO
setting:
"This feature cannot be disabled by settings."
policy_exception_justification:
"Not implemented."
})");
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url =
GURL(BuildReferralEndpoint(kBraveReferralsHeadersPath));
resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
resource_request->load_flags = net::LOAD_DO_NOT_SAVE_COOKIES |
net::LOAD_BYPASS_CACHE |
net::LOAD_DISABLE_CACHE;
network::mojom::URLLoaderFactory* loader_factory =
g_browser_process->system_network_context_manager()
->GetURLLoaderFactory();
referral_headers_loader_ = network::SimpleURLLoader::Create(
std::move(resource_request), traffic_annotation);
referral_headers_loader_->SetAllowHttpErrorResults(true);
referral_headers_loader_->SetRetryOptions(
1, network::SimpleURLLoader::RetryMode::RETRY_ON_NETWORK_CHANGE);
referral_headers_loader_->DownloadToString(
loader_factory,
base::BindOnce(&BraveReferralsService::OnReferralHeadersLoadComplete,
base::Unretained(this)),
kMaxReferralServerResponseSizeBytes);
base::Value headers(base::Value::Type::LIST);
headers.Append(CreateReferralHeader(kPartnerEaffName, kPartnerEaffDomains));
headers.Append(
CreateReferralHeader(kPartnerUpholdName, kPartnerUpholdDomains));
headers.Append(
CreateReferralHeader(kPartnerGrammarlyName, kPartnerGrammarlyDomains));
pref_service_->Set(kReferralHeaders, headers);
}

void BraveReferralsService::InitReferral() {
Expand Down Expand Up @@ -688,7 +638,7 @@ void BraveReferralsService::CheckForReferralFinalization() {
#endif
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("brave_referral_finalization_checker",
R"(
R"(
semantics {
sender:
"Brave Referrals Service"
Expand Down
9 changes: 0 additions & 9 deletions components/brave_referrals/browser/brave_referrals_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ class BraveReferralsService : public ProfileManagerObserver {
// fires.
void OnFinalizationChecksTimerFired();

// Invoked from RepeatingTimer when referral headers timer fires.
void OnFetchReferralHeadersTimerFired();

// Invoked from SimpleURLLoader after download of referral headers
// is complete.
void OnReferralHeadersLoadComplete(
std::unique_ptr<std::string> response_body);

// Invoked from SimpleURLLoader after referral init load
// completes.
void OnReferralInitLoadComplete(std::unique_ptr<std::string> response_body);
Expand Down Expand Up @@ -120,7 +112,6 @@ class BraveReferralsService : public ProfileManagerObserver {
std::unique_ptr<network::SimpleURLLoader> referral_init_loader_;
std::unique_ptr<network::SimpleURLLoader> referral_finalization_check_loader_;
std::unique_ptr<base::OneShotTimer> initialization_timer_;
std::unique_ptr<base::RepeatingTimer> fetch_referral_headers_timer_;
std::unique_ptr<base::RepeatingTimer> finalization_checks_timer_;
ReferralInitializedCallback referral_initialized_callback_;
PrefService* pref_service_;
Expand Down

0 comments on commit 3730843

Please sign in to comment.