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

Rewards check includes #25685

Merged
merged 12 commits into from
Sep 23, 2024
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
62 changes: 62 additions & 0 deletions browser/brave_rewards/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,70 @@
# 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/. */

import("//brave/components/greaselion/browser/buildflags/buildflags.gni")
import("//extensions/buildflags/buildflags.gni")
import("//testing/test.gni")

source_set("brave_rewards") {
sources = [
"rewards_service_factory.h",
"rewards_tab_helper.h",
]

deps = [
"//base",
"//brave/components/brave_rewards/browser",
"//chrome/browser/profiles",
"//components/keyed_service/content",
"//components/sessions",
"//content/public/browser",
]
}

source_set("brave_rewards_impl") {
sources = [
"rewards_prefs_util.cc",
"rewards_prefs_util.h",
"rewards_service_factory.cc",
"rewards_tab_helper.cc",
]

public_deps = [ ":brave_rewards" ]

deps = [
":util",
"//base",
"//brave/browser/brave_wallet",
"//brave/browser/ui:brave_rewards_source",
"//brave/components/brave_rewards/browser",
"//brave/components/brave_rewards/common",
"//brave/components/greaselion/browser/buildflags",
"//chrome/browser/bitmap_fetcher",
"//chrome/browser/favicon",
"//chrome/browser/profiles",
"//chrome/browser/ui",
"//components/keyed_service/content",
"//components/prefs",
"//components/sessions",
"//extensions/buildflags",
]

if (enable_extensions) {
sources += [
"extension_rewards_notification_service_observer.cc",
"extension_rewards_notification_service_observer.h",
"extension_rewards_service_observer.cc",
"extension_rewards_service_observer.h",
]

deps += [ "//extensions/browser" ]
}

if (enable_greaselion) {
deps += [ "//brave/browser/greaselion" ]
}
}

source_set("util") {
sources = [
"rewards_util.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

#include "brave/browser/brave_rewards/android/brave_rewards_native_worker.h"

#include <iomanip>
#include <string>
#include <vector>
#include <utility>

#include "base/android/jni_android.h"
Expand Down
9 changes: 9 additions & 0 deletions browser/brave_rewards/android/sources.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/.

brave_browser_brave_rewards_android_sources = [
"//brave/browser/brave_rewards/android/brave_rewards_native_worker.cc",
"//brave/browser/brave_rewards/android/brave_rewards_native_worker.h",
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "base/memory/raw_ptr.h"
#include "brave/components/brave_rewards/browser/rewards_notification_service_observer.h"
#include "brave/components/brave_rewards/common/mojom/rewards.mojom.h"

class Profile;

Expand Down
37 changes: 19 additions & 18 deletions browser/brave_rewards/rewards_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
#include "base/no_destructor.h"
#include "brave/browser/brave_rewards/rewards_util.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/browser/ui/webui/brave_rewards_source.h"
#include "brave/components/brave_rewards/browser/rewards_notification_service_observer.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/components/brave_rewards/browser/rewards_service_impl.h"
#include "brave/components/brave_rewards/browser/rewards_service_observer.h"
#include "brave/components/greaselion/browser/buildflags/buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/buildflags/buildflags.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
Expand All @@ -32,7 +33,6 @@

#if BUILDFLAG(ENABLE_GREASELION)
#include "brave/browser/greaselion/greaselion_service_factory.h"
#include "brave/components/greaselion/browser/greaselion_service.h"
#endif

namespace brave_rewards {
Expand Down Expand Up @@ -77,25 +77,26 @@ KeyedService* RewardsServiceFactory::BuildServiceInstanceFor(
std::unique_ptr<RewardsServiceObserver> extension_observer = nullptr;
std::unique_ptr<RewardsNotificationServiceObserver> notification_observer =
nullptr;
auto* profile = Profile::FromBrowserContext(context);
#if BUILDFLAG(ENABLE_EXTENSIONS)
extension_observer = std::make_unique<ExtensionRewardsServiceObserver>(
Profile::FromBrowserContext(context));
extension_observer =
std::make_unique<ExtensionRewardsServiceObserver>(profile);
notification_observer =
std::make_unique<ExtensionRewardsNotificationServiceObserver>(
Profile::FromBrowserContext(context));
std::make_unique<ExtensionRewardsNotificationServiceObserver>(profile);
#endif
auto* brave_wallet_service =
brave_wallet::BraveWalletServiceFactory::GetServiceForContext(context);
#if BUILDFLAG(ENABLE_GREASELION)
greaselion::GreaselionService* greaselion_service =
greaselion::GreaselionServiceFactory::GetForBrowserContext(context);
std::unique_ptr<RewardsServiceImpl> rewards_service(
new RewardsServiceImpl(Profile::FromBrowserContext(context),
greaselion_service, brave_wallet_service));
#else
// Set up the rewards data source
content::URLDataSource::Add(profile,
std::make_unique<BraveRewardsSource>(profile));
std::unique_ptr<RewardsServiceImpl> rewards_service(new RewardsServiceImpl(
Profile::FromBrowserContext(context), brave_wallet_service));
profile->GetPrefs(), profile->GetPath(),
FaviconServiceFactory::GetForProfile(profile,
ServiceAccessType::EXPLICIT_ACCESS),
BitmapFetcherServiceFactory::GetForBrowserContext(profile),
profile->GetDefaultStoragePartition(),
#if BUILDFLAG(ENABLE_GREASELION)
greaselion::GreaselionServiceFactory::GetForBrowserContext(context),
#endif
brave_wallet::BraveWalletServiceFactory::GetServiceForContext(context)));
rewards_service->Init(std::move(extension_observer),
std::move(notification_observer));
return rewards_service.release();
Expand Down
26 changes: 24 additions & 2 deletions browser/brave_rewards/rewards_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,36 @@
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#endif

namespace brave_rewards {

#if !BUILDFLAG(IS_ANDROID)
class BraveBrowserListObserver : public BrowserListObserver {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you, but since this is only used within the tab helper, it could be a nested class or be given a more specific name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't really sure what to call it, but I'm open to suggestions

public:
explicit BraveBrowserListObserver(RewardsTabHelper* tab_helper)
: tab_helper_(tab_helper) {}
~BraveBrowserListObserver() override {}
void OnBrowserSetLastActive(Browser* browser) override {
tab_helper_->OnBrowserSetLastActive(browser);
}
void OnBrowserNoLongerActive(Browser* browser) override {
tab_helper_->OnBrowserNoLongerActive(browser);
}

private:
const raw_ptr<RewardsTabHelper> tab_helper_ = nullptr; // Not owned.
};
#endif

RewardsTabHelper::RewardsTabHelper(content::WebContents* web_contents)
: content::WebContentsUserData<RewardsTabHelper>(*web_contents),
WebContentsObserver(web_contents),
#if !BUILDFLAG(IS_ANDROID)
browser_list_observer_(new BraveBrowserListObserver(this)),
#endif
tab_id_(sessions::SessionTabHelper::IdForTab(web_contents)) {
if (tab_id_.is_valid()) {
rewards_service_ = RewardsServiceFactory::GetForProfile(
Expand All @@ -40,7 +62,7 @@ RewardsTabHelper::RewardsTabHelper(content::WebContents* web_contents)
}

#if !BUILDFLAG(IS_ANDROID)
BrowserList::AddObserver(this);
BrowserList::AddObserver(browser_list_observer_.get());
#endif
}

Expand All @@ -49,7 +71,7 @@ RewardsTabHelper::~RewardsTabHelper() {
rewards_service_->RemoveObserver(this);
}
#if !BUILDFLAG(IS_ANDROID)
BrowserList::RemoveObserver(this);
BrowserList::RemoveObserver(browser_list_observer_.get());
#endif
}

Expand Down
36 changes: 17 additions & 19 deletions browser/brave_rewards/rewards_tab_helper.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* Copyright (c) 2019 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/. */
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_BRAVE_REWARDS_REWARDS_TAB_HELPER_H_
#define BRAVE_BROWSER_BRAVE_REWARDS_REWARDS_TAB_HELPER_H_

#include <memory>
#include <string>

#include "base/memory/raw_ptr.h"
Expand All @@ -18,24 +19,18 @@
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/browser_list_observer.h"
#endif

class Browser;
class BrowserListObserver;

namespace brave_rewards {

class RewardsService;
class BraveBrowserListObserver;

// A tab helper responsible for sending user-activity events to the Rewards
// engine in order to support the Auto Contribute feature, and for storing the
// publisher ID corresponding to a given tab.
// engine in order to support the Auto Contribute feature, and for storing
// the publisher ID corresponding to a given tab.
class RewardsTabHelper : public content::WebContentsUserData<RewardsTabHelper>,
public content::WebContentsObserver,
#if !BUILDFLAG(IS_ANDROID)
public BrowserListObserver,
#endif
public RewardsServiceObserver {
public:
RewardsTabHelper(const RewardsTabHelper&) = delete;
Expand All @@ -52,16 +47,18 @@ class RewardsTabHelper : public content::WebContentsUserData<RewardsTabHelper>,
using Observation = base::ScopedObservation<RewardsTabHelper, Observer>;

// Returns the publisher ID associated with the web content loaded into this
// tab. The publisher ID does not necessarily refer to a registered publisher.
// tab. The publisher ID does not necessarily refer to a registered
// publisher.
std::string GetPublisherIdForTab() { return publisher_id_; }

// Sets the publisher ID associated with the web content loaded into this tab.
// This method can be used to override the default publisher ID as determined
// by the current domain.
// Sets the publisher ID associated with the web content loaded into this
// tab. This method can be used to override the default publisher ID as
// determined by the current domain.
void SetPublisherIdForTab(const std::string& publisher_id);

private:
friend class content::WebContentsUserData<RewardsTabHelper>;
friend class brave_rewards::BraveBrowserListObserver;

explicit RewardsTabHelper(content::WebContents* web_contents);

Expand All @@ -78,10 +75,8 @@ class RewardsTabHelper : public content::WebContentsUserData<RewardsTabHelper>,
void WebContentsDestroyed() override;

#if !BUILDFLAG(IS_ANDROID)
// BrowserListObserver:
void OnBrowserSetLastActive(Browser* browser) override;
void OnBrowserNoLongerActive(Browser* browser) override;

void OnBrowserSetLastActive(Browser* browser);
void OnBrowserNoLongerActive(Browser* browser);
bool BrowserHasWebContents(Browser* browser);
#endif

Expand All @@ -90,6 +85,9 @@ class RewardsTabHelper : public content::WebContentsUserData<RewardsTabHelper>,

void MaybeSavePublisherInfo();

#if !BUILDFLAG(IS_ANDROID)
std::unique_ptr<BrowserListObserver> browser_list_observer_;
#endif
SessionID tab_id_;
raw_ptr<RewardsService> rewards_service_ = nullptr; // NOT OWNED
base::ObserverList<Observer> observer_list_;
Expand Down
68 changes: 0 additions & 68 deletions browser/brave_rewards/sources.gni

This file was deleted.

1 change: 1 addition & 0 deletions browser/brave_rewards/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ source_set("browser_tests") {
"//base/test:test_support",
"//brave/app:command_ids",
"//brave/browser",
"//brave/browser/brave_rewards",
"//brave/browser/brave_rewards:util",
"//brave/components/brave_rewards/browser",
"//brave/components/brave_rewards/common",
Expand Down
Loading
Loading