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

Enhance and refactor the Brave Ads component updater #8502

Merged
merged 2 commits into from
Apr 22, 2021
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
1 change: 0 additions & 1 deletion browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ source_set("browser_process") {
if (brave_ads_enabled) {
deps += [
"//brave/components/brave_ads/browser",
"//brave/components/brave_user_model/browser",
"//components/dom_distiller/core",
]
}
Expand Down
16 changes: 7 additions & 9 deletions browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
#endif

#if BUILDFLAG(BRAVE_ADS_ENABLED)
#include "brave/components/brave_user_model/browser/user_model_file_service.h"
#include "brave/components/brave_ads/browser/component_updater/resource_component.h"
#endif

using brave_component_updater::BraveComponent;
Expand Down Expand Up @@ -192,7 +192,7 @@ void BraveBrowserProcessImpl::StartBraveServices() {
speedreader_rewriter_service();
#endif
#if BUILDFLAG(BRAVE_ADS_ENABLED)
user_model_file_service();
resource_component();
#endif
// Now start the local data files service, which calls all observers.
local_data_files_service()->Start();
Expand Down Expand Up @@ -392,14 +392,12 @@ BraveBrowserProcessImpl::speedreader_rewriter_service() {
#endif // BUILDFLAG(ENABLE_SPEEDREADER)

#if BUILDFLAG(BRAVE_ADS_ENABLED)
brave_user_model::UserModelFileService*
BraveBrowserProcessImpl::user_model_file_service() {
if (!user_model_file_service_) {
user_model_file_service_.reset(
new brave_user_model::UserModelFileService(
brave_component_updater_delegate()));
brave_ads::ResourceComponent* BraveBrowserProcessImpl::resource_component() {
if (!resource_component_) {
resource_component_.reset(
new brave_ads::ResourceComponent(brave_component_updater_delegate()));
}
return user_model_file_service_.get();
return resource_component_.get();
}

#endif // BUILDFLAG(BRAVE_ADS_ENABLED)
Expand Down
9 changes: 4 additions & 5 deletions browser/brave_browser_process_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ namespace speedreader {
class SpeedreaderRewriterService;
}

namespace brave_user_model {
class UserModelFileService;
namespace brave_ads {
class ResourceComponent;
}

class BraveBrowserProcessImpl : public BrowserProcessImpl {
Expand Down Expand Up @@ -109,7 +109,7 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
speedreader::SpeedreaderRewriterService* speedreader_rewriter_service();
#endif
#if BUILDFLAG(BRAVE_ADS_ENABLED)
brave_user_model::UserModelFileService* user_model_file_service();
brave_ads::ResourceComponent* resource_component();
#endif

private:
Expand Down Expand Up @@ -168,8 +168,7 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
#endif

#if BUILDFLAG(BRAVE_ADS_ENABLED)
std::unique_ptr<brave_user_model::UserModelFileService>
user_model_file_service_;
std::unique_ptr<brave_ads::ResourceComponent> resource_component_;
#endif

SEQUENCE_CHECKER(sequence_checker_);
Expand Down
13 changes: 13 additions & 0 deletions components/brave_ads/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ source_set("browser") {
"ads_service_impl.h",
"background_helper.cc",
"background_helper.h",
"component_updater/component_info.cc",
"component_updater/component_info.h",
"component_updater/component_util.cc",
"component_updater/component_util.h",
"component_updater/components.h",
"component_updater/resource_component.cc",
"component_updater/resource_component.h",
"component_updater/resource_component_observer.h",
"component_updater/resource_info.h",
"frequency_capping_helper.cc",
"frequency_capping_helper.h",
"notification_helper.cc",
Expand All @@ -57,9 +66,13 @@ source_set("browser") {
public_deps += [ "//brave/vendor/bat-native-ads" ]

deps += [
"//base",
"//brave/app:brave_generated_resources_grit",
"//brave/browser/notifications",
"//brave/components/brave_ads/resources",
"//brave/components/brave_component_updater/browser",
"//brave/components/l10n/browser",
"//brave/components/l10n/common",
"//brave/components/services/bat_ads/public/cpp",
"//components/history/core/browser",
"//components/history/core/common",
Expand Down
2 changes: 1 addition & 1 deletion components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class AdsService : public KeyedService {

virtual void OnTabClosed(const SessionID& tab_id) = 0;

virtual void OnUserModelUpdated(const std::string& id) = 0;
virtual void OnResourceComponentUpdated(const std::string& id) = 0;

virtual void OnNewTabPageAdEvent(
const std::string& uuid,
Expand Down
27 changes: 14 additions & 13 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ AdsServiceImpl::AdsServiceImpl(Profile* profile,

AdsServiceImpl::~AdsServiceImpl() = default;

void AdsServiceImpl::OnUserModelUpdated(const std::string& id) {
void AdsServiceImpl::OnResourceComponentUpdated(const std::string& id) {
if (!connected()) {
return;
}

bat_ads_->OnUserModelUpdated(id);
bat_ads_->OnResourceComponentUpdated(id);
}

bool AdsServiceImpl::IsSupportedLocale() const {
Expand Down Expand Up @@ -304,7 +304,7 @@ void AdsServiceImpl::ChangeLocale(const std::string& locale) {
return;
}

RegisterUserModelComponentsForLocale(locale);
RegisterResourceComponentsForLocale(locale);

bat_ads_->ChangeLocale(locale);
}
Expand Down Expand Up @@ -555,7 +555,7 @@ void AdsServiceImpl::Shutdown() {

BackgroundHelper::GetInstance()->RemoveObserver(this);

g_brave_browser_process->user_model_file_service()->RemoveObserver(this);
g_brave_browser_process->resource_component()->RemoveObserver(this);

url_loaders_.clear();

Expand Down Expand Up @@ -843,7 +843,7 @@ void AdsServiceImpl::OnEnsureBaseDirectoryExists(const bool success) {
return;
}

g_brave_browser_process->user_model_file_service()->AddObserver(this);
g_brave_browser_process->resource_component()->AddObserver(this);

BackgroundHelper::GetInstance()->AddObserver(this);

Expand All @@ -858,7 +858,7 @@ void AdsServiceImpl::OnEnsureBaseDirectoryExists(const bool success) {
OnWalletUpdated();

const std::string locale = GetLocale();
RegisterUserModelComponentsForLocale(locale);
RegisterResourceComponentsForLocale(locale);

MaybeShowMyFirstAdNotification();
}
Expand Down Expand Up @@ -1115,10 +1115,10 @@ void AdsServiceImpl::NotificationTimedOut(const std::string& uuid) {
CloseNotification(uuid);
}

void AdsServiceImpl::RegisterUserModelComponentsForLocale(
void AdsServiceImpl::RegisterResourceComponentsForLocale(
const std::string& locale) {
g_brave_browser_process->user_model_file_service()
->RegisterComponentsForLocale(locale);
g_brave_browser_process->resource_component()->RegisterComponentsForLocale(
locale);
}

void AdsServiceImpl::OnURLRequestStarted(
Expand Down Expand Up @@ -1896,17 +1896,18 @@ void AdsServiceImpl::Save(const std::string& name,
std::move(callback)));
}

void AdsServiceImpl::LoadUserModelForId(const std::string& id,
ads::LoadCallback callback) {
void AdsServiceImpl::LoadAdsResource(const std::string& id,
const int version,
ads::LoadCallback callback) {
const base::Optional<base::FilePath> path =
g_brave_browser_process->user_model_file_service()->GetPathForId(id);
g_brave_browser_process->resource_component()->GetPath(id, version);

if (!path) {
callback(ads::Result::FAILED, "");
return;
}

VLOG(1) << "Loading user model from " << path.value();
VLOG(1) << "Loading ads resource from " << path.value();

base::PostTaskAndReplyWithResult(
file_task_runner_.get(), FROM_HERE,
Expand Down
15 changes: 8 additions & 7 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
#include "bat/ledger/mojom_structs.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/browser/background_helper.h"
#include "brave/components/brave_ads/browser/component_updater/resource_component.h"
#include "brave/components/brave_ads/browser/notification_helper.h"
#include "brave/components/brave_rewards/browser/rewards_notification_service_observer.h"
#include "brave/components/brave_user_model/browser/user_model_file_service.h"
#include "brave/components/services/bat_ads/public/interfaces/bat_ads.mojom.h"
#include "chrome/browser/notifications/notification_handler.h"
#include "components/history/core/browser/history_service_observer.h"
Expand All @@ -40,8 +40,8 @@

#include "base/task/cancelable_task_tracker.h"

using brave_ads::ResourceComponent;
using brave_rewards::RewardsNotificationService;
using brave_user_model::UserModelFileService;

class NotificationDisplayService;
class Profile;
Expand Down Expand Up @@ -70,7 +70,7 @@ class AdsServiceImpl : public AdsService,
public ads::AdsClient,
public history::HistoryServiceObserver,
BackgroundHelper::Observer,
public brave_user_model::Observer,
public brave_ads::Observer,
public base::SupportsWeakPtr<AdsServiceImpl> {
public:
void OnWalletUpdated();
Expand Down Expand Up @@ -124,7 +124,7 @@ class AdsServiceImpl : public AdsService,

void OnTabClosed(const SessionID& tab_id) override;

void OnUserModelUpdated(const std::string& id) override;
void OnResourceComponentUpdated(const std::string& id) override;

void OnNewTabPageAdEvent(
const std::string& uuid,
Expand Down Expand Up @@ -236,7 +236,7 @@ class AdsServiceImpl : public AdsService,

void NotificationTimedOut(const std::string& uuid);

void RegisterUserModelComponentsForLocale(const std::string& locale);
void RegisterResourceComponentsForLocale(const std::string& locale);

void OnURLRequestStarted(
const GURL& final_url,
Expand Down Expand Up @@ -352,8 +352,9 @@ class AdsServiceImpl : public AdsService,

void Load(const std::string& name, ads::LoadCallback callback) override;

void LoadUserModelForId(const std::string& id,
ads::LoadCallback callback) override;
void LoadAdsResource(const std::string& id,
const int version,
ads::LoadCallback callback) override;

void GetBrowsingHistory(const int max_count,
const int days_ago,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
* 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/components/brave_user_model/browser/component_info.h"
#include "brave/components/brave_ads/browser/component_updater/component_info.h"

namespace brave_user_model {
namespace brave_ads {

ComponentInfo::ComponentInfo() = default;

ComponentInfo::ComponentInfo(
const std::string& id,
const std::string& public_key)
: id(id),
public_key(public_key) {}
ComponentInfo::ComponentInfo(const std::string& id,
const std::string& public_key)
: id(id), public_key(public_key) {}

ComponentInfo::~ComponentInfo() = default;

} // namespace brave_user_model
} // namespace brave_ads
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@
* 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_COMPONENTS_BRAVE_USER_MODEL_BROWSER_COMPONENT_INFO_H_
#define BRAVE_COMPONENTS_BRAVE_USER_MODEL_BROWSER_COMPONENT_INFO_H_
#ifndef BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_COMPONENT_UPDATER_COMPONENT_INFO_H_
#define BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_COMPONENT_UPDATER_COMPONENT_INFO_H_

#include <string>

namespace brave_user_model {
namespace brave_ads {

struct ComponentInfo {
ComponentInfo();
ComponentInfo(
const std::string& id,
const std::string& public_key);
ComponentInfo(const std::string& id, const std::string& public_key);
~ComponentInfo();

std::string id;
std::string public_key;
};

} // namespace brave_user_model
} // namespace brave_ads

#endif // BRAVE_COMPONENTS_BRAVE_USER_MODEL_BROWSER_COMPONENT_INFO_H_
#endif // BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_COMPONENT_UPDATER_COMPONENT_INFO_H_
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
* 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/components/brave_user_model/browser/component_util.h"
#include "brave/components/brave_ads/browser/component_updater/component_util.h"

#include "brave/components/brave_user_model/browser/components.h"
#include "brave/components/brave_ads/browser/component_updater/components.h"

namespace brave_user_model {
namespace brave_ads {

base::Optional<ComponentInfo> GetComponentInfo(
const std::string& id) {
base::Optional<ComponentInfo> GetComponentInfo(const std::string& id) {
const auto iter = components.find(id);
if (iter == components.end()) {
return base::nullopt;
Expand All @@ -19,4 +18,4 @@ base::Optional<ComponentInfo> GetComponentInfo(
return iter->second;
}

} // namespace brave_user_model
} // namespace brave_ads
20 changes: 20 additions & 0 deletions components/brave_ads/browser/component_updater/component_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* 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_COMPONENTS_BRAVE_ADS_BROWSER_COMPONENT_UPDATER_COMPONENT_UTIL_H_
#define BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_COMPONENT_UPDATER_COMPONENT_UTIL_H_

#include <string>

#include "base/optional.h"
#include "brave/components/brave_ads/browser/component_updater/component_info.h"

namespace brave_ads {

base::Optional<ComponentInfo> GetComponentInfo(const std::string& id);

} // namespace brave_ads

#endif // BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_COMPONENT_UPDATER_COMPONENT_UTIL_H_
Loading