Skip to content

Commit

Permalink
Merge pull request #184 from brave/update-server-pings
Browse files Browse the repository at this point in the history
Add support for updating stats via server pings
  • Loading branch information
emerick authored Jun 26, 2018
2 parents aed38ed + 3bd7802 commit 1f440c9
Show file tree
Hide file tree
Showing 15 changed files with 662 additions and 3 deletions.
10 changes: 8 additions & 2 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ source_set("browser_process") {
"brave_browser_main_extra_parts.h",
"brave_browser_process_impl.cc",
"brave_browser_process_impl.h",
"brave_tab_helpers.cc",
"brave_tab_helpers.h",
"brave_local_state_prefs.cc",
"brave_local_state_prefs.h",
"brave_profile_prefs.cc",
"brave_profile_prefs.h",
"brave_stats_updater.cc",
"brave_stats_updater.h",
"brave_stats_updater_params.cc",
"brave_stats_updater_params.h",
"brave_tab_helpers.cc",
"brave_tab_helpers.h",
"component_updater/brave_component_installer.cc",
"component_updater/brave_component_installer.h",
"component_updater/brave_component_updater_configurator.cc",
Expand Down
13 changes: 13 additions & 0 deletions browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

#include "brave/browser/brave_browser_process_impl.h"

#include "base/bind.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "brave/browser/component_updater/brave_component_updater_configurator.h"
#include "brave/browser/brave_stats_updater.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 @@ -25,6 +29,15 @@ BraveBrowserProcessImpl::BraveBrowserProcessImpl(
: BrowserProcessImpl(local_state_task_runner) {
g_browser_process = this;
g_brave_browser_process = this;
brave_stats_updater_ = brave::BraveStatsUpdaterFactory(local_state());
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](brave::BraveStatsUpdater* stats_updater) {
stats_updater->Start();
},
base::Unretained(brave_stats_updater_.get())),
base::TimeDelta::FromMinutes(2));
}

component_updater::ComponentUpdateService*
Expand Down
5 changes: 5 additions & 0 deletions browser/brave_browser_process_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#include "chrome/browser/browser_process_impl.h"

namespace brave {
class BraveStatsUpdater;
}

namespace brave_shields {
class AdBlockService;
class AdBlockRegionalService;
Expand Down Expand Up @@ -35,6 +39,7 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
tracking_protection_service_;
std::unique_ptr<brave_shields::HTTPSEverywhereService>
https_everywhere_service_;
std::unique_ptr<brave::BraveStatsUpdater> brave_stats_updater_;

DISALLOW_COPY_AND_ASSIGN(BraveBrowserProcessImpl);
};
Expand Down
15 changes: 15 additions & 0 deletions browser/brave_local_state_prefs.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* 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/brave_local_state_prefs.h"

#include "brave/browser/brave_stats_updater.h"

namespace brave {

void RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
RegisterPrefsForBraveStatsUpdater(registry);
}

} // namespace brave
16 changes: 16 additions & 0 deletions browser/brave_local_state_prefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* 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_BRAVE_LOCAL_STATE_PREFS_H_
#define BRAVE_BROWSER_BRAVE_LOCAL_STATE_PREFS_H_

class PrefRegistrySimple;

namespace brave {

void RegisterLocalStatePrefs(PrefRegistrySimple* registry);

} // namespace brave

#endif // BRAVE_BROWSER_BRAVE_LOCAL_STATE_PREFS_H_
2 changes: 1 addition & 1 deletion browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace brave {
void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
brave_shields::BraveShieldsWebContentsObserver::RegisterProfilePrefs(registry);

// No sign into Bravea functionality
// No sign into Brave functionality
registry->SetDefaultPrefValue(prefs::kSigninAllowed, base::Value(false));

// Restore last profile on restart
Expand Down
178 changes: 178 additions & 0 deletions browser/brave_stats_updater.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/* 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/brave_stats_updater.h"

#include "base/sys_info.h"
#include "brave/browser/brave_stats_updater_params.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/common/channel_info.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/version_info/version_info.h"
#include "net/base/load_flags.h"
#include "net/base/url_util.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"

const char kBaseUpdateURL[] = "https://laptop-updates.brave.com/1/usage/brave-core";

const char kPlatformIdentifierLinux[] = "linux-bc";
const char kPlatformIdentifierMac[] = "osx-bc";
const char kPlatformIdentifierWinIA32[] = "winia32-bc";
const char kPlatformIdentifierWinx64[] = "winx64-bc";

// Ping the update server once an hour.
const int kUpdateServerPingFrequency = 60 * 60;

// Maximum size of the server ping response in bytes.
const int kMaxUpdateServerPingResponseSizeBytes = 1024 * 1024;

namespace {

std::string GetChannelName() {
std::string channel = chrome::GetChannelName();
if (channel.empty())
channel = "release";
return channel;
}

std::string GetPlatformIdentifier() {
std::string os_type = version_info::GetOSType();
if (os_type == "Linux")
return kPlatformIdentifierLinux;
else if (os_type == "Mac")
return kPlatformIdentifierMac;
else if (os_type == "Windows") {
if (base::SysInfo::OperatingSystemArchitecture() == "x86")
return kPlatformIdentifierWinIA32;
else
return kPlatformIdentifierWinx64;
} else {
NOTREACHED();
return std::string();
}
}

GURL GetUpdateURL(const brave::BraveStatsUpdaterParams& stats_updater_params) {
GURL update_url(kBaseUpdateURL);
update_url = net::AppendQueryParameter(update_url, "platform",
GetPlatformIdentifier());
update_url =
net::AppendQueryParameter(update_url, "channel", GetChannelName());
update_url = net::AppendQueryParameter(update_url, "version",
version_info::GetVersionNumber());
update_url = net::AppendQueryParameter(update_url, "daily",
stats_updater_params.GetDailyParam());
update_url = net::AppendQueryParameter(update_url, "weekly",
stats_updater_params.GetWeeklyParam());
update_url = net::AppendQueryParameter(
update_url, "monthly", stats_updater_params.GetMonthlyParam());
update_url = net::AppendQueryParameter(
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");
return update_url;
}

}

namespace brave {

BraveStatsUpdater::BraveStatsUpdater(PrefService* pref_service)
: pref_service_(pref_service) {
}

BraveStatsUpdater::~BraveStatsUpdater() {
}

void BraveStatsUpdater::Start() {
DCHECK(!server_ping_timer_);
server_ping_timer_ = std::make_unique<base::RepeatingTimer>();
server_ping_timer_->Start(
FROM_HERE, base::TimeDelta::FromSeconds(kUpdateServerPingFrequency), this,
&BraveStatsUpdater::OnServerPingTimerFired);
DCHECK(server_ping_timer_->IsRunning());
}

void BraveStatsUpdater::Stop() {
return server_ping_timer_.reset();
}

void BraveStatsUpdater::OnSimpleLoaderComplete(
std::unique_ptr<std::string> response_body) {
int response_code = -1;
if (simple_url_loader_->ResponseInfo() &&
simple_url_loader_->ResponseInfo()->headers)
response_code =
simple_url_loader_->ResponseInfo()->headers->response_code();
if (simple_url_loader_->NetError() != net::OK || response_code != 200) {
LOG(ERROR) << "Failed to send usage stats to update server"
<< ", error: " << simple_url_loader_->NetError()
<< ", response code: " << response_code
<< ", payload: " << *response_body
<< ", url: " << simple_url_loader_->GetFinalURL().spec();
return;
}
}

void BraveStatsUpdater::OnServerPingTimerFired() {
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("brave_stats_updater", R"(
semantics {
sender:
"Brave Stats Updater"
description:
"This service sends anonymous usage statistics to Brave."
trigger:
"Stats are automatically sent at intervals while Brave "
"is running."
data: "Anonymous usage statistics."
destination: WEBSITE
}
policy {
cookies_allowed: NO
setting:
"This feature cannot be disabled by settings."
policy_exception_justification:
"Not implemented."
})");
brave::BraveStatsUpdaterParams stats_updater_params(pref_service_);
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = GetUpdateURL(stats_updater_params);
resource_request->load_flags =
net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES |
net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE |
net::LOAD_DO_NOT_SEND_AUTH_DATA;
network::mojom::URLLoaderFactory* loader_factory =
g_browser_process->system_network_context_manager()
->GetURLLoaderFactory();
simple_url_loader_ = network::SimpleURLLoader::Create(
std::move(resource_request), traffic_annotation);
simple_url_loader_->SetAllowHttpErrorResults(true);
simple_url_loader_->DownloadToString(
loader_factory,
base::BindOnce(&BraveStatsUpdater::OnSimpleLoaderComplete,
base::Unretained(this)),
kMaxUpdateServerPingResponseSizeBytes);
}

///////////////////////////////////////////////////////////////////////////////

std::unique_ptr<BraveStatsUpdater> BraveStatsUpdaterFactory(PrefService* pref_service) {
return std::make_unique<BraveStatsUpdater>(pref_service);
}

void RegisterPrefsForBraveStatsUpdater(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(kFirstCheckMade, false);
registry->RegisterIntegerPref(kLastCheckWOY, 0);
registry->RegisterIntegerPref(kLastCheckMonth, 0);
registry->RegisterStringPref(kLastCheckYMD, std::string());
registry->RegisterStringPref(kWeekOfInstallation, std::string());
}

} // namespace brave
53 changes: 53 additions & 0 deletions browser/brave_stats_updater.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_BRAVE_STATS_UPDATER_H_
#define BRAVE_BROWSER_BRAVE_STATS_UPDATER_H_

#include <memory>

#include "base/macros.h"
#include "base/timer/timer.h"

class PrefRegistrySimple;
class PrefService;

namespace network {
class SimpleURLLoader;
}

namespace brave {

class BraveStatsUpdater {
public:
BraveStatsUpdater(PrefService* pref_service);
~BraveStatsUpdater();

void Start();
void Stop();

private:
// Invoked from SimpleURLLoader after download is complete.
void OnSimpleLoaderComplete(std::unique_ptr<std::string> response_body);

// Invoked from RepeatingTimer when server ping timer fires.
void OnServerPingTimerFired();

PrefService* pref_service_;
std::unique_ptr<network::SimpleURLLoader> simple_url_loader_;
std::unique_ptr<base::RepeatingTimer> server_ping_timer_;

DISALLOW_COPY_AND_ASSIGN(BraveStatsUpdater);
};

// Creates the BraveStatsUpdater
std::unique_ptr<BraveStatsUpdater> BraveStatsUpdaterFactory(
PrefService* pref_service);

// Registers the preferences used by BraveStatsUpdater
void RegisterPrefsForBraveStatsUpdater(PrefRegistrySimple* registry);

} // namespace brave

#endif // BRAVE_BROWSER_BRAVE_STATS_UPDATER_H_
Loading

0 comments on commit 1f440c9

Please sign in to comment.