Skip to content

Commit

Permalink
Moved BraveVpnService/Factory into brave_vpn ns
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhong committed Apr 11, 2022
1 parent e913c1e commit 6647609
Show file tree
Hide file tree
Showing 20 changed files with 168 additions and 134 deletions.
4 changes: 3 additions & 1 deletion browser/brave_vpn/android/brave_vpn_native_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
6 changes: 5 additions & 1 deletion browser/brave_vpn/brave_vpn_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "brave/components/brave_vpn/brave_vpn_utils.h"
#endif

namespace brave_vpn {

// static
BraveVpnServiceFactory* BraveVpnServiceFactory::GetInstance() {
return base::Singleton<BraveVpnServiceFactory>::get();
Expand All @@ -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

Expand All @@ -68,3 +70,5 @@ KeyedService* BraveVpnServiceFactory::BuildServiceInstanceFor(
return new BraveVpnService(shared_url_loader_factory, callback);
#endif
}

} // namespace brave_vpn
7 changes: 6 additions & 1 deletion browser/brave_vpn/brave_vpn_service_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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_
8 changes: 4 additions & 4 deletions browser/ui/brave_browser_command_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions browser/ui/brave_browser_command_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace chrome {
class BraveBrowserCommandController : public chrome::BrowserCommandController
#if BUILDFLAG(ENABLE_BRAVE_VPN)
,
public BraveVPNServiceObserver
public brave_vpn::BraveVPNServiceObserver
#endif
{
public:
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion browser/ui/brave_browser_command_controller_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion browser/ui/toolbar/brave_app_menu_model_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions browser/ui/views/toolbar/brave_vpn_button.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -102,7 +103,7 @@ class VPNButtonMenuModel : public ui::SimpleMenuModel,
}

raw_ptr<Browser> browser_ = nullptr;
raw_ptr<BraveVpnService> service_ = nullptr;
raw_ptr<brave_vpn::BraveVpnService> service_ = nullptr;
};

} // namespace
Expand All @@ -113,7 +114,8 @@ BraveVPNButton::BraveVPNButton(Browser* browser)
std::make_unique<VPNButtonMenuModel>(browser),
nullptr),
browser_(browser),
service_(BraveVpnServiceFactory::GetForProfile(browser_->profile())) {
service_(brave_vpn::BraveVpnServiceFactory::GetForProfile(
browser_->profile())) {
DCHECK(service_);
Observe(service_);

Expand Down
8 changes: 6 additions & 2 deletions browser/ui/views/toolbar/brave_vpn_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -38,7 +42,7 @@ class BraveVPNButton : public ToolbarButton, public BraveVPNServiceObserver {
void OnButtonPressed(const ui::Event& event);

raw_ptr<Browser> browser_ = nullptr;
raw_ptr<BraveVpnService> service_ = nullptr;
raw_ptr<brave_vpn::BraveVpnService> service_ = nullptr;
};

#endif // BRAVE_BROWSER_UI_VIEWS_TOOLBAR_BRAVE_VPN_BUTTON_H_
3 changes: 2 additions & 1 deletion browser/ui/views/toolbar/brave_vpn_status_label.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand Down
9 changes: 6 additions & 3 deletions browser/ui/views/toolbar/brave_vpn_status_label.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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> browser_ = nullptr;
raw_ptr<BraveVpnService> service_ = nullptr;
raw_ptr<brave_vpn::BraveVpnService> service_ = nullptr;
};

#endif // BRAVE_BROWSER_UI_VIEWS_TOOLBAR_BRAVE_VPN_STATUS_LABEL_H_
3 changes: 2 additions & 1 deletion browser/ui/views/toolbar/brave_vpn_toggle_button.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand Down
7 changes: 5 additions & 2 deletions browser/ui/views/toolbar/brave_vpn_toggle_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +34,7 @@ class BraveVPNToggleButton : public views::ToggleButton,
void UpdateState();

raw_ptr<Browser> browser_ = nullptr;
raw_ptr<BraveVpnService> service_ = nullptr;
raw_ptr<brave_vpn::BraveVpnService> service_ = nullptr;
};

#endif // BRAVE_BROWSER_UI_VIEWS_TOOLBAR_BRAVE_VPN_TOGGLE_BUTTON_H_
4 changes: 2 additions & 2 deletions browser/ui/webui/brave_vpn/vpn_panel_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions browser/ui/webui/brave_vpn/vpn_panel_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ void VPNPanelUI::CreatePanelHandler(
panel_handler_ = std::make_unique<VPNPanelHandler>(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));
}
}
Loading

0 comments on commit 6647609

Please sign in to comment.