Skip to content

Commit

Permalink
Make purchased state available on Android
Browse files Browse the repository at this point in the history
On Android, LoadPurchasedState() is also called during the vpn service
initialization. And client can get purchased state by using
GetPurchasedState().
Also, client can get notified by overriding
ServiceObserver::OnPurchasedStateChanged().
  • Loading branch information
simonhong authored and bsclifton committed Feb 11, 2022
1 parent d119491 commit 8777523
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 218 deletions.
9 changes: 5 additions & 4 deletions browser/brave_vpn/brave_vpn_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ BraveVpnServiceFactory::BraveVpnServiceFactory()
: BrowserContextKeyedServiceFactory(
"BraveVpnService",
BrowserContextDependencyManager::GetInstance()) {
#if defined(OS_WIN) || defined(OS_MAC)
DependsOn(skus::SkusServiceFactory::GetInstance());
#endif
}

BraveVpnServiceFactory::~BraveVpnServiceFactory() = default;

KeyedService* BraveVpnServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
// TODO(simonhong): Can we use this check for android?
// For now, vpn is disabled by default on desktop but not sure on
// android.
#if defined(OS_WIN) || defined(OS_MAC)
if (!brave_vpn::IsBraveVPNEnabled())
return nullptr;
Expand All @@ -53,16 +54,16 @@ KeyedService* BraveVpnServiceFactory::BuildServiceInstanceFor(
auto shared_url_loader_factory =
default_storage_partition->GetURLLoaderFactoryForBrowserProcess();

#if defined(OS_WIN) || defined(OS_MAC)
auto callback = base::BindRepeating(
[](content::BrowserContext* context) {
return skus::SkusServiceFactory::GetForContext(context);
},
context);
#if defined(OS_WIN) || defined(OS_MAC)
return new BraveVpnService(
shared_url_loader_factory, user_prefs::UserPrefs::Get(context), callback);
#elif defined(OS_ANDROID)
return new BraveVpnService(shared_url_loader_factory);
return new BraveVpnService(shared_url_loader_factory, callback);
#endif
}

Expand Down
26 changes: 13 additions & 13 deletions components/brave_vpn/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,29 @@ preprocess_mojo_manifest = "preprocessed_mojo_manifest.json"

static_library("brave_vpn") {
sources = [
"brave_vpn_constants.h",
"brave_vpn_service.cc",
"brave_vpn_service.h",
"brave_vpn_utils.cc",
"brave_vpn_utils.h",
"brave_vpn_service_observer.cc",
"brave_vpn_service_observer.h",
"features.cc",
"features.h",
]

deps = [
":mojom",
"//base",
"//brave/components/api_request_helper:api_request_helper",
"//brave/components/resources:strings",
"//brave/components/skus/browser",
"//brave/components/skus/common",
"//brave/components/skus/common:mojom",
"//components/keyed_service/core",
"//services/network/public/cpp",
"//third_party/abseil-cpp:absl",
"//ui/base",
"//url",
]

Expand All @@ -31,18 +44,11 @@ static_library("brave_vpn") {
sources += [
"brave_vpn_connection_info.cc",
"brave_vpn_connection_info.h",
"brave_vpn_constants.h",
"brave_vpn_data_types.h",
"brave_vpn_os_connection_api.cc",
"brave_vpn_os_connection_api.h",
"brave_vpn_os_connection_api_sim.cc",
"brave_vpn_os_connection_api_sim.h",
"brave_vpn_service_observer.cc",
"brave_vpn_service_observer.h",
"brave_vpn_utils.cc",
"brave_vpn_utils.h",
"features.cc",
"features.h",
"pref_names.cc",
"pref_names.h",
"switches.h",
Expand All @@ -69,14 +75,8 @@ static_library("brave_vpn") {

deps += [
":brave_vpn_internal",
":mojom",
"//brave/components/resources:strings",
"//brave/components/skus/browser",
"//brave/components/skus/common",
"//brave/components/skus/common:mojom",
"//components/prefs",
"//third_party/icu",
"//ui/base",
]
}
}
Expand Down
28 changes: 26 additions & 2 deletions components/brave_vpn/brave_vpn.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,50 @@ interface PanelHandler {
};

interface ServiceObserver {
[EnableIfNot=is_android]
OnConnectionCreated();

[EnableIfNot=is_android]
OnConnectionRemoved();

[EnableIfNot=is_android]
OnConnectionStateChanged(ConnectionState state);

OnPurchasedStateChanged(PurchasedState state);
};

// Browser-side handler for vpn service things
interface ServiceHandler {
GetConnectionState() => (ConnectionState state);
GetPurchasedState() => (PurchasedState state);
AddObserver(pending_remote<ServiceObserver> observer);
GetPurchasedState() => (PurchasedState state);

[EnableIfNot=is_android]
GetConnectionState() => (ConnectionState state);

[EnableIfNot=is_android]
CreateVPNConnection();

[EnableIfNot=is_android]
Connect();

[EnableIfNot=is_android]
Disconnect();

// Gets all region from internal cache which is fetched from Guardian API
[EnableIfNot=is_android]
GetAllRegions() => (array<Region> regions);

// Gets region based on user's device timezone
[EnableIfNot=is_android]
GetDeviceRegion() => (Region device_region);

[EnableIfNot=is_android]
GetSelectedRegion() => (Region current_region);

[EnableIfNot=is_android]
SetSelectedRegion(Region region);

[EnableIfNot=is_android]
GetProductUrls() => (ProductUrls urls);
};

Expand Down
Loading

0 comments on commit 8777523

Please sign in to comment.