diff --git a/browser/brave_vpn/android/brave_vpn_native_worker.cc b/browser/brave_vpn/android/brave_vpn_native_worker.cc index df0d471c5a23..3626fd584438 100644 --- a/browser/brave_vpn/android/brave_vpn_native_worker.cc +++ b/browser/brave_vpn/android/brave_vpn_native_worker.cc @@ -17,10 +17,12 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_manager.h" +using brave_vpn::BraveVpnService; + namespace { BraveVpnService* GetBraveVpnService() { - return BraveVpnServiceFactory::GetForProfile( + return brave_vpn::BraveVpnServiceFactory::GetForProfile( ProfileManager::GetActiveUserProfile()->GetOriginalProfile()); } diff --git a/browser/brave_vpn/brave_vpn_service_factory.cc b/browser/brave_vpn/brave_vpn_service_factory.cc index 6bf4b941a00f..6b8bba8d129d 100644 --- a/browser/brave_vpn/brave_vpn_service_factory.cc +++ b/browser/brave_vpn/brave_vpn_service_factory.cc @@ -22,6 +22,8 @@ #include "brave/components/brave_vpn/brave_vpn_utils.h" #endif +namespace brave_vpn { + // static BraveVpnServiceFactory* BraveVpnServiceFactory::GetInstance() { return base::Singleton::get(); @@ -48,7 +50,7 @@ KeyedService* BraveVpnServiceFactory::BuildServiceInstanceFor( // For now, vpn is disabled by default on desktop but not sure on // android. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) - if (!brave_vpn::IsBraveVPNEnabled()) + if (!IsBraveVPNEnabled()) return nullptr; #endif @@ -68,3 +70,5 @@ KeyedService* BraveVpnServiceFactory::BuildServiceInstanceFor( return new BraveVpnService(shared_url_loader_factory, callback); #endif } + +} // namespace brave_vpn diff --git a/browser/brave_vpn/brave_vpn_service_factory.h b/browser/brave_vpn/brave_vpn_service_factory.h index e2d13d0637a3..d71798a53efa 100644 --- a/browser/brave_vpn/brave_vpn_service_factory.h +++ b/browser/brave_vpn/brave_vpn_service_factory.h @@ -12,9 +12,12 @@ #include "build/build_config.h" #include "components/keyed_service/content/browser_context_keyed_service_factory.h" -class BraveVpnService; class Profile; +namespace brave_vpn { + +class BraveVpnService; + class BraveVpnServiceFactory : public BrowserContextKeyedServiceFactory { public: static BraveVpnService* GetForProfile(Profile* profile); @@ -34,4 +37,6 @@ class BraveVpnServiceFactory : public BrowserContextKeyedServiceFactory { content::BrowserContext* context) const override; }; +} // namespace brave_vpn + #endif // BRAVE_BROWSER_BRAVE_VPN_BRAVE_VPN_SERVICE_FACTORY_H_ diff --git a/browser/ui/brave_browser_command_controller.cc b/browser/ui/brave_browser_command_controller.cc index 06786744a18d..d31652bae036 100644 --- a/browser/ui/brave_browser_command_controller.cc +++ b/browser/ui/brave_browser_command_controller.cc @@ -64,8 +64,8 @@ BraveBrowserCommandController::BraveBrowserCommandController(Browser* browser) brave_command_updater_(nullptr) { InitBraveCommandState(); #if BUILDFLAG(ENABLE_BRAVE_VPN) - if (auto* vpn_service = - BraveVpnServiceFactory::GetForProfile(browser_->profile())) { + if (auto* vpn_service = brave_vpn::BraveVpnServiceFactory::GetForProfile( + browser_->profile())) { Observe(vpn_service); } #endif @@ -209,8 +209,8 @@ void BraveBrowserCommandController::UpdateCommandForBraveVPN() { UpdateCommandEnabled(IDC_ABOUT_BRAVE_VPN, true); UpdateCommandEnabled(IDC_MANAGE_BRAVE_VPN_PLAN, true); - if (auto* vpn_service = - BraveVpnServiceFactory::GetForProfile(browser_->profile())) { + if (auto* vpn_service = brave_vpn::BraveVpnServiceFactory::GetForProfile( + browser_->profile())) { // Only show vpn sub menu for purchased user. UpdateCommandEnabled(IDC_BRAVE_VPN_MENU, vpn_service->is_purchased_user()); UpdateCommandEnabled(IDC_TOGGLE_BRAVE_VPN, diff --git a/browser/ui/brave_browser_command_controller.h b/browser/ui/brave_browser_command_controller.h index c66cd5610514..3200e531c9dd 100644 --- a/browser/ui/brave_browser_command_controller.h +++ b/browser/ui/brave_browser_command_controller.h @@ -28,7 +28,7 @@ namespace chrome { class BraveBrowserCommandController : public chrome::BrowserCommandController #if BUILDFLAG(ENABLE_BRAVE_VPN) , - public BraveVPNServiceObserver + public brave_vpn::BraveVPNServiceObserver #endif { public: @@ -59,7 +59,7 @@ class BraveBrowserCommandController : public chrome::BrowserCommandController bool UpdateCommandEnabled(int id, bool state) override; #if BUILDFLAG(ENABLE_BRAVE_VPN) - // BraveVPNServiceObserver overrides: + // brave_vpn::BraveVPNServiceObserver overrides: void OnPurchasedStateChanged(brave_vpn::mojom::PurchasedState state) override; #endif diff --git a/browser/ui/brave_browser_command_controller_browsertest.cc b/browser/ui/brave_browser_command_controller_browsertest.cc index 23a340ff472f..ed3feb357304 100644 --- a/browser/ui/brave_browser_command_controller_browsertest.cc +++ b/browser/ui/brave_browser_command_controller_browsertest.cc @@ -47,7 +47,8 @@ class BraveBrowserCommandControllerTest : public InProcessBrowserTest { #if BUILDFLAG(ENABLE_BRAVE_VPN) void SetPurchasedUserForBraveVPN(Browser* browser, bool purchased) { - auto* service = BraveVpnServiceFactory::GetForProfile(browser->profile()); + auto* service = + brave_vpn::BraveVpnServiceFactory::GetForProfile(browser->profile()); ASSERT_TRUE(!!service); auto target_state = purchased ? brave_vpn::mojom::PurchasedState::PURCHASED diff --git a/browser/ui/toolbar/brave_app_menu_model_browsertest.cc b/browser/ui/toolbar/brave_app_menu_model_browsertest.cc index e042f39dadce..bf4d52247037 100644 --- a/browser/ui/toolbar/brave_app_menu_model_browsertest.cc +++ b/browser/ui/toolbar/brave_app_menu_model_browsertest.cc @@ -55,7 +55,8 @@ class BraveAppMenuBrowserTest : public InProcessBrowserTest { #if BUILDFLAG(ENABLE_BRAVE_VPN) void SetPurchasedUserForBraveVPN(Browser* browser, bool purchased) { - auto* service = BraveVpnServiceFactory::GetForProfile(browser->profile()); + auto* service = + brave_vpn::BraveVpnServiceFactory::GetForProfile(browser->profile()); ASSERT_TRUE(!!service); auto target_state = purchased ? brave_vpn::mojom::PurchasedState::PURCHASED diff --git a/browser/ui/views/toolbar/brave_vpn_button.cc b/browser/ui/views/toolbar/brave_vpn_button.cc index 9f3bbb7df750..9db6a52e95ac 100644 --- a/browser/ui/views/toolbar/brave_vpn_button.cc +++ b/browser/ui/views/toolbar/brave_vpn_button.cc @@ -57,12 +57,13 @@ class BraveVPNButtonHighlightPathGenerator class VPNButtonMenuModel : public ui::SimpleMenuModel, public ui::SimpleMenuModel::Delegate, - public BraveVPNServiceObserver { + public brave_vpn::BraveVPNServiceObserver { public: explicit VPNButtonMenuModel(Browser* browser) : SimpleMenuModel(this), browser_(browser), - service_(BraveVpnServiceFactory::GetForProfile(browser_->profile())) { + service_(brave_vpn::BraveVpnServiceFactory::GetForProfile( + browser_->profile())) { DCHECK(service_); Observe(service_); Build(service_->is_purchased_user()); @@ -102,7 +103,7 @@ class VPNButtonMenuModel : public ui::SimpleMenuModel, } raw_ptr browser_ = nullptr; - raw_ptr service_ = nullptr; + raw_ptr service_ = nullptr; }; } // namespace @@ -113,7 +114,8 @@ BraveVPNButton::BraveVPNButton(Browser* browser) std::make_unique(browser), nullptr), browser_(browser), - service_(BraveVpnServiceFactory::GetForProfile(browser_->profile())) { + service_(brave_vpn::BraveVpnServiceFactory::GetForProfile( + browser_->profile())) { DCHECK(service_); Observe(service_); diff --git a/browser/ui/views/toolbar/brave_vpn_button.h b/browser/ui/views/toolbar/brave_vpn_button.h index ffa2fa80907c..41c8ffb11cfb 100644 --- a/browser/ui/views/toolbar/brave_vpn_button.h +++ b/browser/ui/views/toolbar/brave_vpn_button.h @@ -11,10 +11,14 @@ #include "chrome/browser/ui/views/toolbar/toolbar_button.h" #include "ui/base/metadata/metadata_header_macros.h" +namespace brave_vpn { class BraveVpnService; +} // namespace brave_vpn + class Browser; -class BraveVPNButton : public ToolbarButton, public BraveVPNServiceObserver { +class BraveVPNButton : public ToolbarButton, + public brave_vpn::BraveVPNServiceObserver { public: METADATA_HEADER(BraveVPNButton); @@ -38,7 +42,7 @@ class BraveVPNButton : public ToolbarButton, public BraveVPNServiceObserver { void OnButtonPressed(const ui::Event& event); raw_ptr browser_ = nullptr; - raw_ptr service_ = nullptr; + raw_ptr service_ = nullptr; }; #endif // BRAVE_BROWSER_UI_VIEWS_TOOLBAR_BRAVE_VPN_BUTTON_H_ diff --git a/browser/ui/views/toolbar/brave_vpn_status_label.cc b/browser/ui/views/toolbar/brave_vpn_status_label.cc index 1cf49b608a9b..dc9cdcbb12ea 100644 --- a/browser/ui/views/toolbar/brave_vpn_status_label.cc +++ b/browser/ui/views/toolbar/brave_vpn_status_label.cc @@ -21,7 +21,8 @@ using PurchasedState = brave_vpn::mojom::PurchasedState; BraveVPNStatusLabel::BraveVPNStatusLabel(Browser* browser) : browser_(browser), - service_(BraveVpnServiceFactory::GetForProfile(browser_->profile())) { + service_(brave_vpn::BraveVpnServiceFactory::GetForProfile( + browser_->profile())) { DCHECK(service_); Observe(service_); diff --git a/browser/ui/views/toolbar/brave_vpn_status_label.h b/browser/ui/views/toolbar/brave_vpn_status_label.h index 9ee6e0b2aa17..1c7c45c4ad5f 100644 --- a/browser/ui/views/toolbar/brave_vpn_status_label.h +++ b/browser/ui/views/toolbar/brave_vpn_status_label.h @@ -10,11 +10,14 @@ #include "brave/components/brave_vpn/brave_vpn_service_observer.h" #include "ui/views/controls/label.h" +namespace brave_vpn { class BraveVpnService; +} // namespace brave_vpn + class Browser; class BraveVPNStatusLabel : public views::Label, - public BraveVPNServiceObserver { + public brave_vpn::BraveVPNServiceObserver { public: explicit BraveVPNStatusLabel(Browser* browser); ~BraveVPNStatusLabel() override; @@ -23,14 +26,14 @@ class BraveVPNStatusLabel : public views::Label, BraveVPNStatusLabel& operator=(const BraveVPNStatusLabel&) = delete; private: - // BraveVPNServiceObserver overrides: + // brave_vpn::BraveVPNServiceObserver overrides: void OnConnectionStateChanged( brave_vpn::mojom::ConnectionState state) override; void UpdateState(); raw_ptr browser_ = nullptr; - raw_ptr service_ = nullptr; + raw_ptr service_ = nullptr; }; #endif // BRAVE_BROWSER_UI_VIEWS_TOOLBAR_BRAVE_VPN_STATUS_LABEL_H_ diff --git a/browser/ui/views/toolbar/brave_vpn_toggle_button.cc b/browser/ui/views/toolbar/brave_vpn_toggle_button.cc index 0e5e6ac959b2..3814ee6e00f5 100644 --- a/browser/ui/views/toolbar/brave_vpn_toggle_button.cc +++ b/browser/ui/views/toolbar/brave_vpn_toggle_button.cc @@ -22,7 +22,8 @@ using PurchasedState = brave_vpn::mojom::PurchasedState; BraveVPNToggleButton::BraveVPNToggleButton(Browser* browser) : browser_(browser), - service_(BraveVpnServiceFactory::GetForProfile(browser_->profile())) { + service_(brave_vpn::BraveVpnServiceFactory::GetForProfile( + browser_->profile())) { DCHECK(service_); Observe(service_); diff --git a/browser/ui/views/toolbar/brave_vpn_toggle_button.h b/browser/ui/views/toolbar/brave_vpn_toggle_button.h index b6418e6dd8c3..31c444bea199 100644 --- a/browser/ui/views/toolbar/brave_vpn_toggle_button.h +++ b/browser/ui/views/toolbar/brave_vpn_toggle_button.h @@ -10,11 +10,14 @@ #include "brave/components/brave_vpn/brave_vpn_service_observer.h" #include "ui/views/controls/button/toggle_button.h" +namespace brave_vpn { class BraveVpnService; +} // namespace brave_vpn + class Browser; class BraveVPNToggleButton : public views::ToggleButton, - public BraveVPNServiceObserver { + public brave_vpn::BraveVPNServiceObserver { public: explicit BraveVPNToggleButton(Browser* browser); ~BraveVPNToggleButton() override; @@ -31,7 +34,7 @@ class BraveVPNToggleButton : public views::ToggleButton, void UpdateState(); raw_ptr browser_ = nullptr; - raw_ptr service_ = nullptr; + raw_ptr service_ = nullptr; }; #endif // BRAVE_BROWSER_UI_VIEWS_TOOLBAR_BRAVE_VPN_TOGGLE_BUTTON_H_ diff --git a/browser/ui/webui/brave_vpn/vpn_panel_handler.cc b/browser/ui/webui/brave_vpn/vpn_panel_handler.cc index 3f0d200e6b41..378cf6230918 100644 --- a/browser/ui/webui/brave_vpn/vpn_panel_handler.cc +++ b/browser/ui/webui/brave_vpn/vpn_panel_handler.cc @@ -22,8 +22,8 @@ VPNPanelHandler::~VPNPanelHandler() = default; void VPNPanelHandler::ShowUI() { auto embedder = webui_controller_->embedder(); - BraveVpnService* vpn_service = - BraveVpnServiceFactory::GetForProfile(profile_); + brave_vpn::BraveVpnService* vpn_service = + brave_vpn::BraveVpnServiceFactory::GetForProfile(profile_); DCHECK(vpn_service); if (embedder) { embedder->ShowUI(); diff --git a/browser/ui/webui/brave_vpn/vpn_panel_ui.cc b/browser/ui/webui/brave_vpn/vpn_panel_ui.cc index daa927714316..587ba90b58ad 100644 --- a/browser/ui/webui/brave_vpn/vpn_panel_ui.cc +++ b/browser/ui/webui/brave_vpn/vpn_panel_ui.cc @@ -67,9 +67,9 @@ void VPNPanelUI::CreatePanelHandler( panel_handler_ = std::make_unique(std::move(panel_receiver), this, profile); - BraveVpnService* vpn_service_desktop = - BraveVpnServiceFactory::GetForProfile(profile); - if (vpn_service_desktop) { - vpn_service_desktop->BindInterface(std::move(vpn_service_receiver)); + brave_vpn::BraveVpnService* vpn_service = + brave_vpn::BraveVpnServiceFactory::GetForProfile(profile); + if (vpn_service) { + vpn_service->BindInterface(std::move(vpn_service_receiver)); } } diff --git a/components/brave_vpn/brave_vpn_service.cc b/components/brave_vpn/brave_vpn_service.cc index 778222607eac..f4f302baca20 100644 --- a/components/brave_vpn/brave_vpn_service.cc +++ b/components/brave_vpn/brave_vpn_service.cc @@ -154,6 +154,8 @@ std::string GetSubscriberCredentialFromJson(const std::string& json) { } // namespace +namespace brave_vpn { + #if BUILDFLAG(IS_ANDROID) BraveVpnService::BraveVpnService( scoped_refptr url_loader_factory, @@ -172,10 +174,10 @@ BraveVpnService::BraveVpnService( skus_service_getter_(skus_service_getter), api_request_helper_(GetNetworkTrafficAnnotationTag(), url_loader_factory) { - DCHECK(brave_vpn::IsBraveVPNEnabled()); + DCHECK(IsBraveVPNEnabled()); auto* cmd = base::CommandLine::ForCurrentProcess(); - is_simulation_ = cmd->HasSwitch(brave_vpn::switches::kBraveVPNSimulation); + is_simulation_ = cmd->HasSwitch(switches::kBraveVPNSimulation); observed_.Observe(GetBraveVPNConnectionAPI()); GetBraveVPNConnectionAPI()->set_target_vpn_entry_name(kBraveVPNEntryName); @@ -413,13 +415,13 @@ void BraveVpnService::ToggleConnection() { can_disconnect ? Disconnect() : Connect(); } -const brave_vpn::BraveVPNConnectionInfo& BraveVpnService::GetConnectionInfo() { +const BraveVPNConnectionInfo& BraveVpnService::GetConnectionInfo() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); return connection_info_; } void BraveVpnService::BindInterface( - mojo::PendingReceiver receiver) { + mojo::PendingReceiver receiver) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); receivers_.Add(this, std::move(receiver)); } @@ -467,8 +469,7 @@ void BraveVpnService::LoadCachedRegionData() { if (GetDeviceRegion().empty()) return; - auto* preference = - prefs_->FindPreference(brave_vpn::prefs::kBraveVPNRegionList); + auto* preference = prefs_->FindPreference(prefs::kBraveVPNRegionList); DCHECK(preference); // Early return when we don't have any cached region data. if (preference->IsDefaultValue()) @@ -495,7 +496,7 @@ void BraveVpnService::SetRegionListToPrefs() { for (const auto& region : regions_) { regions_list.Append(GetValueFromRegion(region)); } - prefs_->Set(brave_vpn::prefs::kBraveVPNRegionList, std::move(regions_list)); + prefs_->Set(prefs::kBraveVPNRegionList, std::move(regions_list)); } void BraveVpnService::OnFetchRegionList(bool background_fetch, @@ -548,7 +549,7 @@ bool BraveVpnService::ValidateCachedRegionData( } base::Value BraveVpnService::GetValueFromRegion( - const brave_vpn::mojom::Region& region) const { + const mojom::Region& region) const { base::Value region_dict(base::Value::Type::DICTIONARY); region_dict.SetStringKey(kRegionContinentKey, region.continent); region_dict.SetStringKey(kRegionNameKey, region.name); @@ -556,9 +557,9 @@ base::Value BraveVpnService::GetValueFromRegion( return region_dict; } -brave_vpn::mojom::Region BraveVpnService::GetRegionFromValue( +mojom::Region BraveVpnService::GetRegionFromValue( const base::Value& value) const { - brave_vpn::mojom::Region region; + mojom::Region region; if (auto* continent = value.FindStringKey(kRegionContinentKey)) region.continent = *continent; if (auto* name = value.FindStringKey(kRegionNameKey)) @@ -585,7 +586,7 @@ bool BraveVpnService::ParseAndCacheRegionList(const base::Value& region_value, // Sort region list alphabetically std::sort(regions_.begin(), regions_.end(), - [](brave_vpn::mojom::Region& a, brave_vpn::mojom::Region& b) { + [](mojom::Region& a, mojom::Region& b) { return (a.name_pretty < b.name_pretty); }); @@ -653,22 +654,22 @@ void BraveVpnService::SetDeviceRegionWithTimezone( void BraveVpnService::SetDeviceRegion(const std::string& name) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - prefs_->SetString(brave_vpn::prefs::kBraveVPNDeviceRegion, name); + prefs_->SetString(prefs::kBraveVPNDeviceRegion, name); } void BraveVpnService::SetSelectedRegion(const std::string& name) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - prefs_->SetString(brave_vpn::prefs::kBraveVPNSelectedRegion, name); + prefs_->SetString(prefs::kBraveVPNSelectedRegion, name); } std::string BraveVpnService::GetDeviceRegion() const { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - return prefs_->GetString(brave_vpn::prefs::kBraveVPNDeviceRegion); + return prefs_->GetString(prefs::kBraveVPNDeviceRegion); } std::string BraveVpnService::GetSelectedRegion() const { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - return prefs_->GetString(brave_vpn::prefs::kBraveVPNSelectedRegion); + return prefs_->GetString(prefs::kBraveVPNSelectedRegion); } void BraveVpnService::SetFallbackDeviceRegion() { @@ -693,17 +694,17 @@ std::string BraveVpnService::GetCurrentTimeZone() { void BraveVpnService::GetAllRegions(GetAllRegionsCallback callback) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - std::vector regions; + std::vector regions; for (const auto& region : regions_) { regions.push_back(region.Clone()); } std::move(callback).Run(std::move(regions)); } -brave_vpn::mojom::Region BraveVpnService::GetRegionWithName( +mojom::Region BraveVpnService::GetRegionWithName( const std::string& name) const { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - brave_vpn::mojom::Region region; + mojom::Region region; auto it = std::find_if(regions_.begin(), regions_.end(), [&name](const auto& region) { return region.name == name; }); @@ -737,8 +738,7 @@ void BraveVpnService::GetSelectedRegion(GetSelectedRegionCallback callback) { std::move(callback).Run(region.Clone()); } -void BraveVpnService::SetSelectedRegion( - brave_vpn::mojom::RegionPtr region_ptr) { +void BraveVpnService::SetSelectedRegion(mojom::RegionPtr region_ptr) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); if (connection_state_ == ConnectionState::DISCONNECTING || connection_state_ == ConnectionState::CONNECTING) { @@ -758,9 +758,8 @@ void BraveVpnService::SetSelectedRegion( void BraveVpnService::GetProductUrls(GetProductUrlsCallback callback) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - std::move(callback).Run(brave_vpn::mojom::ProductUrls::New( - brave_vpn::kFeedbackUrl, brave_vpn::kAboutUrl, - brave_vpn::GetManageUrl())); + std::move(callback).Run( + mojom::ProductUrls::New(kFeedbackUrl, kAboutUrl, GetManageUrl())); } void BraveVpnService::CreateSupportTicket( @@ -859,7 +858,7 @@ void BraveVpnService::ParseAndCacheHostnames( constexpr char kOfflineKey[] = "offline"; constexpr char kCapacityScoreKey[] = "capacity-score"; - std::vector hostnames; + std::vector hostnames; for (const auto& value : hostnames_value.GetList()) { DCHECK(value.is_dict()); if (!value.is_dict()) @@ -873,8 +872,8 @@ void BraveVpnService::ParseAndCacheHostnames( if (!hostname_str || !display_name_str || !offline || !capacity_score) continue; - hostnames.push_back(brave_vpn::Hostname{*hostname_str, *display_name_str, - *offline, *capacity_score}); + hostnames.push_back( + Hostname{*hostname_str, *display_name_str, *offline, *capacity_score}); } VLOG(2) << __func__ << " : has hostname: " << !hostnames.empty(); @@ -978,32 +977,30 @@ void BraveVpnService::OnGetProfileCredentials( UpdateAndNotifyConnectionStateChange(ConnectionState::CONNECT_FAILED); } -std::unique_ptr BraveVpnService::PickBestHostname( - const std::vector& hostnames) { +std::unique_ptr BraveVpnService::PickBestHostname( + const std::vector& hostnames) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - std::vector filtered_hostnames; - std::copy_if( - hostnames.begin(), hostnames.end(), - std::back_inserter(filtered_hostnames), - [](const brave_vpn::Hostname& hostname) { return !hostname.is_offline; }); + std::vector filtered_hostnames; + std::copy_if(hostnames.begin(), hostnames.end(), + std::back_inserter(filtered_hostnames), + [](const Hostname& hostname) { return !hostname.is_offline; }); std::sort(filtered_hostnames.begin(), filtered_hostnames.end(), - [](const brave_vpn::Hostname& a, const brave_vpn::Hostname& b) { + [](const Hostname& a, const Hostname& b) { return a.capacity_score > b.capacity_score; }); if (filtered_hostnames.empty()) - return std::make_unique(); + return std::make_unique(); // Pick highest capacity score. - return std::make_unique(filtered_hostnames[0]); + return std::make_unique(filtered_hostnames[0]); } -brave_vpn::BraveVPNOSConnectionAPI* -BraveVpnService::GetBraveVPNConnectionAPI() { +BraveVPNOSConnectionAPI* BraveVpnService::GetBraveVPNConnectionAPI() { if (is_simulation_) - return brave_vpn::BraveVPNOSConnectionAPI::GetInstanceForTest(); - return brave_vpn::BraveVPNOSConnectionAPI::GetInstance(); + return BraveVPNOSConnectionAPI::GetInstanceForTest(); + return BraveVPNOSConnectionAPI::GetInstance(); } void BraveVpnService::OnCreateSupportTicket( @@ -1019,7 +1016,7 @@ void BraveVpnService::OnCreateSupportTicket( #endif // !BUILDFLAG(IS_ANDROID) void BraveVpnService::AddObserver( - mojo::PendingRemote observer) { + mojo::PendingRemote observer) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); observers_.Add(std::move(observer)); } @@ -1039,9 +1036,9 @@ void BraveVpnService::LoadPurchasedState() { #if !BUILDFLAG(IS_ANDROID) && !defined(OFFICIAL_BUILD) auto* cmd = base::CommandLine::ForCurrentProcess(); - if (cmd->HasSwitch(brave_vpn::switches::kBraveVPNTestMonthlyPass)) { + if (cmd->HasSwitch(switches::kBraveVPNTestMonthlyPass)) { skus_credential_ = - cmd->GetSwitchValueASCII(brave_vpn::switches::kBraveVPNTestMonthlyPass); + cmd->GetSwitchValueASCII(switches::kBraveVPNTestMonthlyPass); LoadCachedRegionData(); if (!regions_.empty()) { SetPurchasedState(PurchasedState::PURCHASED); @@ -1376,3 +1373,5 @@ void BraveVpnService::GetSubscriberCredentialV12( OAuthRequest(base_url, "POST", request_body, std::move(internal_callback), {{"Brave-Payments-Environment", payments_environment}}); } + +} // namespace brave_vpn diff --git a/components/brave_vpn/brave_vpn_service.h b/components/brave_vpn/brave_vpn_service.h index 88c39891622e..65289aac3fd3 100644 --- a/components/brave_vpn/brave_vpn_service.h +++ b/components/brave_vpn/brave_vpn_service.h @@ -46,16 +46,21 @@ class Value; } // namespace base class PrefService; +class BraveAppMenuBrowserTest; +class BraveBrowserCommandControllerTest; + #endif // !BUILDFLAG(IS_ANDROID) +namespace brave_vpn { + // This class is used by desktop and android. // However, it includes desktop specific impls and it's hidden // by IS_ANDROID ifdef. class BraveVpnService : #if !BUILDFLAG(IS_ANDROID) - public brave_vpn::BraveVPNOSConnectionAPI::Observer, + public BraveVPNOSConnectionAPI::Observer, #endif - public brave_vpn::mojom::ServiceHandler, + public mojom::ServiceHandler, public KeyedService { public: #if BUILDFLAG(IS_ANDROID) @@ -80,17 +85,14 @@ class BraveVpnService : void RemoveVPNConnnection(); bool is_connected() const { - return connection_state_ == brave_vpn::mojom::ConnectionState::CONNECTED; + return connection_state_ == mojom::ConnectionState::CONNECTED; } bool is_purchased_user() const { - return purchased_state_ == brave_vpn::mojom::PurchasedState::PURCHASED; - } - brave_vpn::mojom::ConnectionState connection_state() const { - return connection_state_; + return purchased_state_ == mojom::PurchasedState::PURCHASED; } + mojom::ConnectionState connection_state() const { return connection_state_; } - void BindInterface( - mojo::PendingReceiver receiver); + void BindInterface(mojo::PendingReceiver receiver); // mojom::vpn::ServiceHandler void GetConnectionState(GetConnectionStateCallback callback) override; @@ -101,7 +103,7 @@ class BraveVpnService : void GetAllRegions(GetAllRegionsCallback callback) override; void GetDeviceRegion(GetDeviceRegionCallback callback) override; void GetSelectedRegion(GetSelectedRegionCallback callback) override; - void SetSelectedRegion(brave_vpn::mojom::RegionPtr region) override; + void SetSelectedRegion(mojom::RegionPtr region) override; void GetProductUrls(GetProductUrlsCallback callback) override; void CreateSupportTicket(const std::string& email, const std::string& subject, @@ -117,7 +119,7 @@ class BraveVpnService : // mojom::vpn::ServiceHandler void AddObserver( - mojo::PendingRemote observer) override; + mojo::PendingRemote observer) override; void GetPurchasedState(GetPurchasedStateCallback callback) override; void GetAllServerRegions(ResponseCallback callback); @@ -144,11 +146,11 @@ class BraveVpnService : private: #if !BUILDFLAG(IS_ANDROID) - friend class BraveAppMenuBrowserTest; - friend class BraveBrowserCommandControllerTest; + friend class ::BraveAppMenuBrowserTest; + friend class ::BraveBrowserCommandControllerTest; friend class BraveVPNServiceTest; - // brave_vpn::BraveVPNOSConnectionAPI::Observer overrides: + // BraveVPNOSConnectionAPI::Observer overrides: void OnCreated() override; void OnCreateFailed() override; void OnRemoved() override; @@ -158,11 +160,10 @@ class BraveVpnService : void OnDisconnected() override; void OnIsDisconnecting() override; - const brave_vpn::BraveVPNConnectionInfo& GetConnectionInfo(); + const BraveVPNConnectionInfo& GetConnectionInfo(); void LoadCachedRegionData(); - void UpdateAndNotifyConnectionStateChange( - brave_vpn::mojom::ConnectionState state, - bool force = false); + void UpdateAndNotifyConnectionStateChange(mojom::ConnectionState state, + bool force = false); void FetchRegionData(bool background_fetch); void OnFetchRegionList(bool background_fetch, @@ -183,17 +184,17 @@ class BraveVpnService : void SetSelectedRegion(const std::string& name); std::string GetDeviceRegion() const; std::string GetSelectedRegion() const; - brave_vpn::mojom::Region GetRegionWithName(const std::string& name) const; + mojom::Region GetRegionWithName(const std::string& name) const; void SetFallbackDeviceRegion(); void SetRegionListToPrefs(); - brave_vpn::mojom::Region GetRegionFromValue(const base::Value& value) const; - base::Value GetValueFromRegion(const brave_vpn::mojom::Region& region) const; + mojom::Region GetRegionFromValue(const base::Value& value) const; + base::Value GetValueFromRegion(const mojom::Region& region) const; std::string GetCurrentTimeZone(); void ScheduleBackgroundRegionDataFetch(); void ScheduleFetchRegionDataIfNeeded(); - std::unique_ptr PickBestHostname( - const std::vector& hostnames); + std::unique_ptr PickBestHostname( + const std::vector& hostnames); void OnGetSubscriberCredentialV12(const std::string& subscriber_credential, bool success); @@ -205,7 +206,7 @@ class BraveVpnService : const std::string& body, const base::flat_map& headers); - brave_vpn::BraveVPNOSConnectionAPI* GetBraveVPNConnectionAPI(); + BraveVPNOSConnectionAPI* GetBraveVPNConnectionAPI(); #endif // !BUILDFLAG(IS_ANDROID) using URLRequestCallback = @@ -237,7 +238,7 @@ class BraveVpnService : const std::string& body, const base::flat_map& headers); - void SetPurchasedState(brave_vpn::mojom::PurchasedState state); + void SetPurchasedState(mojom::PurchasedState state); void EnsureMojoConnected(); void OnMojoConnectionError(); void OnCredentialSummary(const std::string& summary_string); @@ -246,17 +247,17 @@ class BraveVpnService : #if !BUILDFLAG(IS_ANDROID) raw_ptr prefs_ = nullptr; - std::vector regions_; - std::unique_ptr hostname_; - brave_vpn::BraveVPNConnectionInfo connection_info_; + std::vector regions_; + std::unique_ptr hostname_; + BraveVPNConnectionInfo connection_info_; bool cancel_connecting_ = false; - brave_vpn::mojom::ConnectionState connection_state_ = - brave_vpn::mojom::ConnectionState::DISCONNECTED; + mojom::ConnectionState connection_state_ = + mojom::ConnectionState::DISCONNECTED; bool needs_connect_ = false; - base::ScopedObservation + base::ScopedObservation observed_{this}; - mojo::ReceiverSet receivers_; + mojo::ReceiverSet receivers_; base::RepeatingTimer region_data_update_timer_; // Only for testing. @@ -269,12 +270,13 @@ class BraveVpnService : base::RepeatingCallback()> skus_service_getter_; mojo::Remote skus_service_; - brave_vpn::mojom::PurchasedState purchased_state_ = - brave_vpn::mojom::PurchasedState::NOT_PURCHASED; - mojo::RemoteSet observers_; + mojom::PurchasedState purchased_state_ = mojom::PurchasedState::NOT_PURCHASED; + mojo::RemoteSet observers_; api_request_helper::APIRequestHelper api_request_helper_; std::string skus_credential_; base::WeakPtrFactory weak_ptr_factory_{this}; }; +} // namespace brave_vpn + #endif // BRAVE_COMPONENTS_BRAVE_VPN_BRAVE_VPN_SERVICE_H_ diff --git a/components/brave_vpn/brave_vpn_service_observer.cc b/components/brave_vpn/brave_vpn_service_observer.cc index d7a926ae9f2d..0c30ee6692a3 100644 --- a/components/brave_vpn/brave_vpn_service_observer.cc +++ b/components/brave_vpn/brave_vpn_service_observer.cc @@ -10,6 +10,8 @@ #include "brave/components/brave_vpn/brave_vpn_service.h" #include "brave/components/brave_vpn/brave_vpn_utils.h" +namespace brave_vpn { + BraveVPNServiceObserver::BraveVPNServiceObserver() = default; BraveVPNServiceObserver::~BraveVPNServiceObserver() = default; @@ -18,9 +20,11 @@ void BraveVPNServiceObserver::Observe(BraveVpnService* service) { if (!service) return; - if (brave_vpn::IsBraveVPNEnabled()) { - mojo::PendingRemote listener; + if (IsBraveVPNEnabled()) { + mojo::PendingRemote listener; receiver_.Bind(listener.InitWithNewPipeAndPassReceiver()); service->AddObserver(std::move(listener)); } } + +} // namespace brave_vpn diff --git a/components/brave_vpn/brave_vpn_service_observer.h b/components/brave_vpn/brave_vpn_service_observer.h index fc548df09792..de4f7d31aa12 100644 --- a/components/brave_vpn/brave_vpn_service_observer.h +++ b/components/brave_vpn/brave_vpn_service_observer.h @@ -10,9 +10,11 @@ #include "build/build_config.h" #include "mojo/public/cpp/bindings/receiver.h" +namespace brave_vpn { + class BraveVpnService; -class BraveVPNServiceObserver : public brave_vpn::mojom::ServiceObserver { +class BraveVPNServiceObserver : public mojom::ServiceObserver { public: BraveVPNServiceObserver(); ~BraveVPNServiceObserver() override; @@ -21,18 +23,18 @@ class BraveVPNServiceObserver : public brave_vpn::mojom::ServiceObserver { void Observe(BraveVpnService* service); - // brave_vpn::mojom::ServiceObserver overrides: - void OnPurchasedStateChanged( - brave_vpn::mojom::PurchasedState state) override {} + // mojom::ServiceObserver overrides: + void OnPurchasedStateChanged(mojom::PurchasedState state) override {} #if !BUILDFLAG(IS_ANDROID) - void OnConnectionStateChanged( - brave_vpn::mojom::ConnectionState state) override {} + void OnConnectionStateChanged(mojom::ConnectionState state) override {} void OnConnectionCreated() override {} void OnConnectionRemoved() override {} #endif // !BUILDFLAG(IS_ANDROID) private: - mojo::Receiver receiver_{this}; + mojo::Receiver receiver_{this}; }; +} // namespace brave_vpn + #endif // BRAVE_COMPONENTS_BRAVE_VPN_BRAVE_VPN_SERVICE_OBSERVER_H_ diff --git a/components/brave_vpn/brave_vpn_unittest.cc b/components/brave_vpn/brave_vpn_unittest.cc index cedfa58814e7..45567fad0561 100644 --- a/components/brave_vpn/brave_vpn_unittest.cc +++ b/components/brave_vpn/brave_vpn_unittest.cc @@ -26,19 +26,21 @@ #include "services/network/test/test_shared_url_loader_factory.h" #include "testing/gtest/include/gtest/gtest.h" -using ConnectionState = brave_vpn::mojom::ConnectionState; -using PurchasedState = brave_vpn::mojom::PurchasedState; +namespace brave_vpn { + +using ConnectionState = mojom::ConnectionState; +using PurchasedState = mojom::PurchasedState; class BraveVPNServiceTest : public testing::Test { public: BraveVPNServiceTest() { scoped_feature_list_.InitWithFeatures( - {skus::features::kSkusFeature, brave_vpn::features::kBraveVPN}, {}); + {skus::features::kSkusFeature, features::kBraveVPN}, {}); } void SetUp() override { skus::RegisterProfilePrefs(pref_service_.registry()); - brave_vpn::prefs::RegisterProfilePrefs(pref_service_.registry()); + prefs::RegisterProfilePrefs(pref_service_.registry()); // Setup required for SKU (dependency of VPN) auto url_loader_factory = @@ -79,17 +81,13 @@ class BraveVPNServiceTest : public testing::Test { service_->OnCredentialSummary(summary); } - std::vector& regions() const { - return service_->regions_; - } + std::vector& regions() const { return service_->regions_; } - brave_vpn::mojom::Region device_region() const { + mojom::Region device_region() const { return service_->GetRegionWithName(service_->GetDeviceRegion()); } - std::unique_ptr& hostname() { - return service_->hostname_; - } + std::unique_ptr& hostname() { return service_->hostname_; } bool& cancel_connecting() { return service_->cancel_connecting_; } @@ -132,7 +130,7 @@ class BraveVPNServiceTest : public testing::Test { void OnDisconnected() { service_->OnDisconnected(); } - const brave_vpn::BraveVPNConnectionInfo& GetConnectionInfo() { + const BraveVPNConnectionInfo& GetConnectionInfo() { return service_->GetConnectionInfo(); } @@ -311,7 +309,7 @@ class BraveVPNServiceTest : public testing::Test { }; TEST(BraveVPNFeatureTest, FeatureTest) { - EXPECT_FALSE(brave_vpn::IsBraveVPNEnabled()); + EXPECT_FALSE(IsBraveVPNEnabled()); } TEST_F(BraveVPNServiceTest, RegionDataTest) { @@ -479,7 +477,7 @@ TEST_F(BraveVPNServiceTest, ConnectionInfoTest) { // Check cached connection info is cleared when user set new selected region. connection_state() = ConnectionState::DISCONNECTED; - service_->SetSelectedRegion(brave_vpn::mojom::Region().Clone()); + service_->SetSelectedRegion(mojom::Region().Clone()); EXPECT_FALSE(GetConnectionInfo().IsValid()); } @@ -507,7 +505,7 @@ TEST_F(BraveVPNServiceTest, NeedsConnectTest) { TEST_F(BraveVPNServiceTest, LoadRegionDataFromPrefsTest) { // Initially, prefs doesn't have region data. - EXPECT_EQ(brave_vpn::mojom::Region(), device_region()); + EXPECT_EQ(mojom::Region(), device_region()); EXPECT_TRUE(regions().empty()); // Set proper data to store them in prefs. @@ -516,7 +514,7 @@ TEST_F(BraveVPNServiceTest, LoadRegionDataFromPrefsTest) { OnFetchTimezones(GetTimeZonesData(), true); // Check region data is set with above data. - EXPECT_FALSE(brave_vpn::mojom::Region() == device_region()); + EXPECT_FALSE(mojom::Region() == device_region()); EXPECT_FALSE(regions().empty()); // Clear region data. @@ -528,3 +526,5 @@ TEST_F(BraveVPNServiceTest, LoadRegionDataFromPrefsTest) { LoadCachedRegionData(); EXPECT_FALSE(regions().empty()); } + +} // namespace brave_vpn