Skip to content

Commit ea61ffa

Browse files
committed
Added chrome.braveRequestAdsEnabled() api backend.
fix brave/brave-browser#16964
1 parent c18061d commit ea61ffa

30 files changed

+750
-7
lines changed

browser/brave_ads/BUILD.gn

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ source_set("brave_ads") {
1010
"ads_service_factory.h",
1111
"ads_tab_helper.cc",
1212
"ads_tab_helper.h",
13+
"brave_ads_driver.cc",
14+
"brave_ads_driver.h",
15+
"brave_ads_driver_private.cc",
16+
"brave_ads_driver_private.h",
1317
]
1418

1519
deps = [

browser/brave_ads/brave_ads_driver.cc

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#include "brave/browser/brave_ads/brave_ads_driver.h"
7+
8+
#include <memory>
9+
#include <string>
10+
#include <utility>
11+
12+
#include "brave/browser/brave_ads/ads_service_factory.h"
13+
#include "brave/browser/brave_rewards/rewards_service_factory.h"
14+
#include "brave/browser/extensions/api/brave_action_api.h"
15+
#include "brave/browser/extensions/brave_component_loader.h"
16+
#include "brave/components/brave_ads/browser/ads_service.h"
17+
#include "brave/components/brave_rewards/browser/rewards_service.h"
18+
#include "chrome/browser/extensions/extension_service.h"
19+
#include "chrome/browser/profiles/profile.h"
20+
#include "chrome/browser/ui/browser.h"
21+
#include "chrome/browser/ui/browser_finder.h"
22+
#include "content/public/browser/web_contents.h"
23+
#include "extensions/browser/extension_system.h"
24+
#include "extensions/common/constants.h"
25+
26+
namespace brave_ads {
27+
28+
namespace {
29+
30+
constexpr char kAdsEnableRelativeUrl[] =
31+
"request_ads_enabled_panel.html#enable_ads";
32+
33+
} // namespace
34+
35+
BraveAdsDriver::BraveAdsDriver(Profile* profile,
36+
content::WebContents* web_contents)
37+
: WebContentsObserver(web_contents),
38+
profile_(profile),
39+
web_contents_(web_contents) {
40+
DCHECK(profile_);
41+
DCHECK(web_contents_);
42+
43+
browser_ = chrome::FindBrowserWithWebContents(web_contents_);
44+
DCHECK(browser_);
45+
}
46+
47+
BraveAdsDriver::~BraveAdsDriver() {
48+
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
49+
DCHECK(!callback_);
50+
}
51+
52+
void BraveAdsDriver::RequestAdsEnabled(RequestAdsEnabledCallback callback) {
53+
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
54+
if (callback_) {
55+
std::move(callback).Run(false);
56+
return;
57+
}
58+
59+
AdsService* ads_service_ = AdsServiceFactory::GetForProfile(profile_);
60+
brave_rewards::RewardsService* rewards_service_ =
61+
brave_rewards::RewardsServiceFactory::GetForProfile(profile_);
62+
63+
if (!ads_service_ || !rewards_service_) {
64+
std::move(callback_).Run(false);
65+
return;
66+
}
67+
68+
callback_ = std::move(callback);
69+
70+
const bool is_rewards_enabled = rewards_service_->IsRewardsEnabled();
71+
const bool is_ads_enabled = ads_service_->IsEnabled();
72+
if (is_rewards_enabled && is_ads_enabled) {
73+
std::move(callback_).Run(true);
74+
return;
75+
}
76+
77+
rewards_service_->StartProcess(base::DoNothing());
78+
rewards_service_observation_.Observe(rewards_service_);
79+
80+
if (!ShowRewardsPopup()) {
81+
Reset();
82+
std::move(callback_).Run(false);
83+
}
84+
}
85+
86+
void BraveAdsDriver::WebContentsDestroyed() {
87+
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
88+
89+
Reset();
90+
91+
if (callback_) {
92+
std::move(callback_).Run(false);
93+
}
94+
}
95+
96+
void BraveAdsDriver::OnProcessForEnableRewardsStarted() {
97+
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
98+
if (!callback_) {
99+
NOTREACHED();
100+
return;
101+
}
102+
103+
process_for_enable_rewards_started_ = true;
104+
}
105+
106+
void BraveAdsDriver::OnRewardsPanelClosed(Browser* browser) {
107+
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
108+
if (browser_ != browser) {
109+
return;
110+
}
111+
112+
if (process_for_enable_rewards_started_ || !callback_) {
113+
return;
114+
}
115+
116+
Reset();
117+
118+
std::move(callback_).Run(false);
119+
}
120+
121+
void BraveAdsDriver::OnAdsEnabled(
122+
brave_rewards::RewardsService* rewards_service,
123+
bool ads_enabled) {
124+
DCHECK(rewards_service);
125+
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
126+
127+
Reset();
128+
129+
if (!callback_) {
130+
NOTREACHED();
131+
return;
132+
}
133+
134+
std::move(callback_).Run(ads_enabled);
135+
}
136+
137+
bool BraveAdsDriver::ShowRewardsPopup() {
138+
auto* extension_service =
139+
extensions::ExtensionSystem::Get(profile_)->extension_service();
140+
if (!extension_service) {
141+
return false;
142+
}
143+
144+
static_cast<extensions::BraveComponentLoader*>(
145+
extension_service->component_loader())
146+
->AddRewardsExtension();
147+
148+
std::string error;
149+
return extensions::BraveActionAPI::ShowActionUI(
150+
browser_, brave_rewards_extension_id,
151+
std::make_unique<std::string>(kAdsEnableRelativeUrl), &error);
152+
}
153+
154+
void BraveAdsDriver::Reset() {
155+
rewards_service_observation_.Reset();
156+
process_for_enable_rewards_started_ = false;
157+
}
158+
159+
} // namespace brave_ads

browser/brave_ads/brave_ads_driver.h

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#ifndef BRAVE_BROWSER_BRAVE_ADS_BRAVE_ADS_DRIVER_H_
7+
#define BRAVE_BROWSER_BRAVE_ADS_BRAVE_ADS_DRIVER_H_
8+
9+
#include "base/scoped_observation.h"
10+
#include "base/sequence_checker.h"
11+
#include "brave/components/brave_ads/common/brave_ads_driver.mojom.h"
12+
#include "brave/components/brave_rewards/browser/rewards_service.h"
13+
#include "brave/components/brave_rewards/browser/rewards_service_observer.h"
14+
#include "content/public/browser/web_contents_observer.h"
15+
16+
class Browser;
17+
class Profile;
18+
19+
namespace content {
20+
class WebContents;
21+
}
22+
23+
namespace brave_ads {
24+
25+
class AdsService;
26+
27+
class BraveAdsDriver final : public brave_ads::mojom::BraveAdsDriver,
28+
public content::WebContentsObserver,
29+
public brave_rewards::RewardsServiceObserver {
30+
public:
31+
BraveAdsDriver(Profile* profile, content::WebContents* web_contents);
32+
BraveAdsDriver(const BraveAdsDriver&) = delete;
33+
BraveAdsDriver& operator=(const BraveAdsDriver&) = delete;
34+
~BraveAdsDriver() override;
35+
36+
// brave_ads::mojom::BraveAdsDriver
37+
void RequestAdsEnabled(RequestAdsEnabledCallback callback) override;
38+
39+
// content::WebContentsObserver
40+
void WebContentsDestroyed() override;
41+
42+
// brave_rewards::RewardsServiceObserver
43+
void OnProcessForEnableRewardsStarted() override;
44+
void OnAdsEnabled(brave_rewards::RewardsService* rewards_service,
45+
bool ads_enabled) override;
46+
void OnRewardsPanelClosed(Browser* browser) override;
47+
48+
private:
49+
bool ShowRewardsPopup();
50+
void Reset();
51+
52+
bool process_for_enable_rewards_started_ = false;
53+
54+
Profile* profile_;
55+
content::WebContents* web_contents_;
56+
Browser* browser_ = nullptr;
57+
RequestAdsEnabledCallback callback_;
58+
base::ScopedObservation<brave_rewards::RewardsService,
59+
brave_rewards::RewardsServiceObserver>
60+
rewards_service_observation_{this};
61+
62+
SEQUENCE_CHECKER(sequence_checker_);
63+
};
64+
65+
} // namespace brave_ads
66+
67+
#endif // BRAVE_BROWSER_BRAVE_ADS_BRAVE_ADS_DRIVER_H_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2021 The Brave Authors. All rights reserved.
2+
// This Source Code Form is subject to the terms of the Mozilla Public
3+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
// you can obtain one at http://mozilla.org/MPL/2.0/.
5+
6+
#include "brave/browser/brave_ads/brave_ads_driver_private.h"
7+
8+
#include <utility>
9+
10+
namespace brave_ads {
11+
12+
BraveAdsDriverPrivate::BraveAdsDriverPrivate() = default;
13+
14+
BraveAdsDriverPrivate::~BraveAdsDriverPrivate() = default;
15+
16+
void BraveAdsDriverPrivate::RequestAdsEnabled(
17+
RequestAdsEnabledCallback callback) {
18+
std::move(callback).Run(false);
19+
}
20+
21+
} // namespace brave_ads
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2021 The Brave Authors. All rights reserved.
2+
// This Source Code Form is subject to the terms of the Mozilla Public
3+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
// you can obtain one at http://mozilla.org/MPL/2.0/.
5+
6+
#ifndef BRAVE_BROWSER_BRAVE_ADS_BRAVE_ADS_DRIVER_PRIVATE_H_
7+
#define BRAVE_BROWSER_BRAVE_ADS_BRAVE_ADS_DRIVER_PRIVATE_H_
8+
9+
#include "brave/components/brave_ads/common/brave_ads_driver.mojom.h"
10+
11+
namespace brave_ads {
12+
13+
class BraveAdsDriverPrivate final : public brave_ads::mojom::BraveAdsDriver {
14+
public:
15+
BraveAdsDriverPrivate();
16+
BraveAdsDriverPrivate(const BraveAdsDriverPrivate&) = delete;
17+
BraveAdsDriverPrivate& operator=(const BraveAdsDriverPrivate&) = delete;
18+
~BraveAdsDriverPrivate() override;
19+
20+
// brave_ads::mojom::BraveAdsDriver
21+
void RequestAdsEnabled(RequestAdsEnabledCallback callback) override;
22+
};
23+
24+
} // namespace brave_ads
25+
26+
#endif // BRAVE_BROWSER_BRAVE_ADS_BRAVE_ADS_DRIVER_PRIVATE_H_

browser/brave_content_browser_client.cc

+33
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ using extensions::ChromeContentBrowserClientExtensionsPart;
138138
#include "brave/browser/ftx/ftx_protocol_handler.h"
139139
#endif
140140

141+
#if BUILDFLAG(BRAVE_ADS_ENABLED)
142+
#include "brave/browser/brave_ads/brave_ads_driver.h"
143+
#include "brave/browser/brave_ads/brave_ads_driver_private.h"
144+
#include "brave/components/brave_ads/common/features.h"
145+
#endif
146+
141147
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
142148
#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
143149
#include "brave/browser/brave_wallet/rpc_controller_factory.h"
@@ -198,6 +204,27 @@ void BindCosmeticFiltersResources(
198204
std::move(receiver));
199205
}
200206

207+
#if BUILDFLAG(BRAVE_ADS_ENABLED)
208+
void BindBraveAdsDriver(
209+
content::RenderFrameHost* const frame_host,
210+
mojo::PendingReceiver<brave_ads::mojom::BraveAdsDriver> receiver) {
211+
auto* context = frame_host->GetBrowserContext();
212+
auto* profile = Profile::FromBrowserContext(context);
213+
content::WebContents* web_contents =
214+
content::WebContents::FromRenderFrameHost(frame_host);
215+
if (brave::IsRegularProfile(profile)) {
216+
mojo::MakeSelfOwnedReceiver(
217+
std::make_unique<brave_ads::BraveAdsDriver>(profile, web_contents),
218+
std::move(receiver));
219+
} else {
220+
// Dummy API which always returns false for private contexts.
221+
mojo::MakeSelfOwnedReceiver(
222+
std::make_unique<brave_ads::BraveAdsDriverPrivate>(),
223+
std::move(receiver));
224+
}
225+
}
226+
#endif // BUILDFLAG(BRAVE_ADS_ENABLED)
227+
201228
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
202229
void MaybeBindBraveWalletProvider(
203230
content::RenderFrameHost* const frame_host,
@@ -334,6 +361,12 @@ void BraveContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
334361
map->Add<brave_search::mojom::BraveSearchDefault>(
335362
base::BindRepeating(&BindBraveSearchDefaultHost));
336363
}
364+
365+
#if BUILDFLAG(BRAVE_ADS_ENABLED)
366+
map->Add<brave_ads::mojom::BraveAdsDriver>(
367+
base::BindRepeating(&BindBraveAdsDriver));
368+
#endif // BUILDFLAG(BRAVE_ADS_ENABLED)
369+
337370
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
338371
if (brave_wallet::IsNativeWalletEnabled()) {
339372
map->Add<brave_wallet::mojom::BraveWalletProvider>(

browser/ui/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ source_set("ui") {
405405
"views/brave_actions/brave_action_view.h",
406406
"views/brave_actions/brave_actions_container.cc",
407407
"views/brave_actions/brave_actions_container.h",
408+
"views/brave_actions/brave_actions_container_delegate.h",
408409
"views/brave_actions/brave_rewards_action_stub_view.cc",
409410
"views/brave_actions/brave_rewards_action_stub_view.h",
410411
"views/location_bar/brave_location_bar_view.cc",

0 commit comments

Comments
 (0)