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

Adding Bitcoin.com widget to NTP [1.15] #6764

Merged
merged 3 commits into from
Oct 5, 2020
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
16 changes: 9 additions & 7 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import("//brave/build/config.gni")
import("//brave/components/binance/browser/buildflags/buildflags.gni")
import("//brave/components/brave_together/buildflags/buildflags.gni")
import("//brave/components/ntp_widget_utils/browser/buildflags/buildflags.gni")
import("//brave/components/moonpay/browser/buildflags/buildflags.gni")
import("//brave/components/gemini/browser/buildflags/buildflags.gni")
import("//brave/browser/tor/buildflags/buildflags.gni")
import("//brave/components/brave_ads/browser/buildflags/buildflags.gni")
Expand Down Expand Up @@ -139,6 +139,7 @@ source_set("browser_process") {
"//brave/components/greaselion/browser/buildflags",
"//brave/components/ipfs/browser/buildflags",
"//brave/components/l10n/browser",
"//brave/components/moonpay/browser/buildflags",
"//brave/components/ntp_background_images/browser",
"//brave/components/ntp_tiles",
"//brave/components/p3a",
Expand Down Expand Up @@ -305,12 +306,6 @@ source_set("browser_process") {
]
}

if (ntp_widget_utils_enabled) {
deps += [
"//brave/components/ntp_widget_utils/browser",
]
}

if (ipfs_enabled) {
sources += [
"ipfs/content_browser_client_helper.cc",
Expand Down Expand Up @@ -342,6 +337,13 @@ source_set("browser_process") {
]
}

if (moonpay_enabled) {
deps += [
"//brave/components/moonpay/browser",
"//brave/components/moonpay/common",
]
}

if (enable_tor) {
deps += [
"//brave/browser/tor",
Expand Down
10 changes: 10 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "brave/components/brave_wallet/browser/buildflags/buildflags.h"
#include "brave/components/brave_wayback_machine/buildflags.h"
#include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h"
#include "brave/components/moonpay/browser/buildflags/buildflags.h"
#include "brave/components/ipfs/browser/buildflags/buildflags.h"
#include "brave/components/speedreader/buildflags.h"
#include "chrome/browser/net/prediction_options.h"
Expand Down Expand Up @@ -61,6 +62,11 @@
#include "brave/components/gemini/browser/pref_names.h"
#endif

#if BUILDFLAG(MOONPAY_ENABLED)
#include "brave/components/moonpay/browser/moonpay_pref_utils.h"
#include "brave/components/moonpay/common/pref_names.h"
#endif

#if BUILDFLAG(ENABLE_BRAVE_PERF_PREDICTOR)
#include "brave/components/brave_perf_predictor/browser/perf_predictor_tab_helper.h"
#include "brave/components/brave_perf_predictor/browser/p3a_bandwidth_savings_tracker.h"
Expand Down Expand Up @@ -274,6 +280,10 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
speedreader::SpeedreaderService::RegisterPrefs(registry);
#endif

#if BUILDFLAG(MOONPAY_ENABLED)
moonpay::MoonpayPrefUtils::RegisterPrefs(registry);
#endif

#if !defined(OS_ANDROID)
BraveOmniboxClientImpl::RegisterPrefs(registry);
#endif
Expand Down
52 changes: 34 additions & 18 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import("//brave/components/brave_wayback_machine/buildflags/buildflags.gni")
import("//brave/components/brave_webtorrent/browser/buildflags/buildflags.gni")
import("//brave/components/brave_together/buildflags/buildflags.gni")
import("//brave/components/ipfs/browser/buildflags/buildflags.gni")
import("//brave/components/ntp_widget_utils/browser/buildflags/buildflags.gni")
import("//brave/components/moonpay/browser/buildflags/buildflags.gni")
import("//build/config/features.gni")
import("//components/gcm_driver/config.gni")

Expand Down Expand Up @@ -59,20 +59,6 @@ source_set("extensions") {
configs += [ ":infura_config" ]
}

if (binance_enabled) {
sources += [
"api/binance_api.cc",
"api/binance_api.h",
]
}

if (brave_together_enabled) {
sources += [
"api/brave_together_api.cc",
"api/brave_together_api.h",
]
}

deps = [
":resources",
"//base",
Expand Down Expand Up @@ -106,8 +92,15 @@ source_set("extensions") {
"//url",
]

if (ntp_widget_utils_enabled) {
if (moonpay_enabled) {
sources += [
"api/moonpay_api.cc",
"api/moonpay_api.h",
]

deps += [
"//brave/components/moonpay/browser/",
"//brave/components/moonpay/common/",
"//brave/components/ntp_widget_utils/browser",
]
}
Expand Down Expand Up @@ -156,9 +149,32 @@ source_set("extensions") {
"api/gemini_api.h",
]
deps += [
"//brave/common",
"//brave/components/gemini/browser",
"//extensions/browser",
"//brave/components/ntp_widget_utils/browser",
]
}

if (binance_enabled) {
sources += [
"api/binance_api.cc",
"api/binance_api.h",
]

deps += [
"//brave/components/binance/browser",
"//brave/components/ntp_widget_utils/browser",
]
}

if (brave_together_enabled) {
sources += [
"api/brave_together_api.cc",
"api/brave_together_api.h",
]

deps += [
"//brave/components/brave_together/browser",
"//brave/components/ntp_widget_utils/browser",
]
}
}
Expand Down
93 changes: 93 additions & 0 deletions browser/extensions/api/moonpay_api.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* 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/. */

#include "brave/browser/extensions/api/moonpay_api.h"

#include <string>
#include <memory>
#include <utility>

#include "brave/browser/profiles/profile_util.h"
#include "brave/components/moonpay/browser/regions.h"
#include "brave/components/moonpay/common/pref_names.h"
#include "brave/components/ntp_widget_utils/browser/ntp_widget_utils_region.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"

namespace {
bool IsMoonpayAPIAvailable(content::BrowserContext* context) {
Profile* profile = Profile::FromBrowserContext(context);
return !brave::IsTorProfile(profile) &&
!profile->IsIncognitoProfile() &&
!profile->IsGuestSession();
}
}

namespace extensions {
namespace api {

ExtensionFunction::ResponseAction
MoonpayIsBitcoinDotComSupportedFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());

if (!IsMoonpayAPIAvailable(browser_context())) {
return RespondNow(Error("Not available in Tor/incognito/guest profile"));
}

bool is_supported = ntp_widget_utils::IsRegionSupported(
profile->GetPrefs(), moonpay::bitcoin_dot_com_supported_regions, true);
return RespondNow(OneArgument(
std::make_unique<base::Value>(is_supported)));
}

ExtensionFunction::ResponseAction
MoonpayOnBuyBitcoinDotComCryptoFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());

if (!IsMoonpayAPIAvailable(browser_context())) {
return RespondNow(Error("Not available in Tor/incognito/guest profile"));
}

profile->GetPrefs()->SetBoolean(kMoonpayHasBoughtBitcoinDotComCrypto, true);
profile->GetPrefs()->SetBoolean(kMoonpayHasInteractedBitcoinDotCom, true);

return RespondNow(NoArguments());
}

ExtensionFunction::ResponseAction
MoonpayOnInteractionBitcoinDotComFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());

if (!IsMoonpayAPIAvailable(browser_context())) {
return RespondNow(Error("Not available in Tor/incognito/guest profile"));
}

profile->GetPrefs()->SetBoolean(kMoonpayHasInteractedBitcoinDotCom, true);

return RespondNow(NoArguments());
}

ExtensionFunction::ResponseAction
MoonpayGetBitcoinDotComInteractionsFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());

if (!IsMoonpayAPIAvailable(browser_context())) {
return RespondNow(Error("Not available in Tor/incognito/guest profile"));
}

bool has_bought = profile->GetPrefs()->GetBoolean(
kMoonpayHasBoughtBitcoinDotComCrypto);
bool has_interacted = profile->GetPrefs()->GetBoolean(
kMoonpayHasInteractedBitcoinDotCom);

auto interactions = std::make_unique<base::DictionaryValue>();
interactions->SetBoolean("boughtCrypto", has_bought);
interactions->SetBoolean("interacted", has_interacted);

return RespondNow(OneArgument(std::move(interactions)));
}

} // namespace api
} // namespace extensions
63 changes: 63 additions & 0 deletions browser/extensions/api/moonpay_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* 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_BROWSER_EXTENSIONS_API_MOONPAY_API_H_
#define BRAVE_BROWSER_EXTENSIONS_API_MOONPAY_API_H_

#include <map>
#include <string>

#include "extensions/browser/extension_function.h"

class Profile;

namespace extensions {
namespace api {

class MoonpayIsBitcoinDotComSupportedFunction :
public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("moonpay.isBitcoinDotComSupported", UNKNOWN)

protected:
~MoonpayIsBitcoinDotComSupportedFunction() override {}
ResponseAction Run() override;
};

class MoonpayOnBuyBitcoinDotComCryptoFunction :
public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("moonpay.onBuyBitcoinDotComCrypto", UNKNOWN)

protected:
~MoonpayOnBuyBitcoinDotComCryptoFunction() override {}
ResponseAction Run() override;
};

class MoonpayOnInteractionBitcoinDotComFunction :
public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("moonpay.onInteractionBitcoinDotCom", UNKNOWN)

protected:
~MoonpayOnInteractionBitcoinDotComFunction() override {}
ResponseAction Run() override;
};

class MoonpayGetBitcoinDotComInteractionsFunction :
public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("moonpay.getBitcoinDotComInteractions", UNKNOWN)

protected:
~MoonpayGetBitcoinDotComInteractionsFunction() override {}
ResponseAction Run() override;
};


} // namespace api
} // namespace extensions

#endif // BRAVE_BROWSER_EXTENSIONS_API_MOONPAY_API_H_
Loading