From a4a8bb44f50e59775c2c75237d8018074f69490f Mon Sep 17 00:00:00 2001 From: boocmp Date: Tue, 8 Nov 2022 15:25:11 +0700 Subject: [PATCH 01/10] Added support of Snowflake extension in the privacy settings. --- app/brave_settings_strings.grdp | 6 + .../brave_tor_page/brave_tor_browser_proxy.ts | 15 ++ .../brave_tor_page/brave_tor_subpage.html | 7 + .../brave_tor_page/brave_tor_subpage.ts | 53 +++++ browser/ui/BUILD.gn | 6 + browser/ui/webui/brave_settings_ui.cc | 11 + ...ave_settings_localized_strings_provider.cc | 2 + browser/ui/webui/settings/brave_tor_handler.h | 1 - .../brave_tor_snowflake_extension_handler.cc | 193 ++++++++++++++++++ .../brave_tor_snowflake_extension_handler.h | 66 ++++++ 10 files changed, 359 insertions(+), 1 deletion(-) create mode 100644 browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc create mode 100644 browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h diff --git a/app/brave_settings_strings.grdp b/app/brave_settings_strings.grdp index 87e65a7a292a..88484d95e2af 100644 --- a/app/brave_settings_strings.grdp +++ b/app/brave_settings_strings.grdp @@ -726,6 +726,12 @@ Tor hides your IP address from the sites you visit. + + Volunteer to help others connect to the Tor network + + + Enable the Snowflake extension to allow users in censored countries to connect to the Tor network via your network connection. + Use Bridges diff --git a/browser/resources/settings/brave_tor_page/brave_tor_browser_proxy.ts b/browser/resources/settings/brave_tor_page/brave_tor_browser_proxy.ts index c250408f011d..ad873aedc150 100644 --- a/browser/resources/settings/brave_tor_page/brave_tor_browser_proxy.ts +++ b/browser/resources/settings/brave_tor_page/brave_tor_browser_proxy.ts @@ -15,6 +15,9 @@ export interface BraveTorBrowserProxy { setTorEnabled(value: boolean) isTorEnabled(): Promise isTorManaged(): Promise + isSnowflakeExtensionAllowed(): Promise + isSnowflakeExtensionEnabled(): Promise + enableSnowflakeExtension(enable: boolean): Promise } export class BraveTorBrowserProxyImpl implements BraveTorBrowserProxy { @@ -49,6 +52,18 @@ export class BraveTorBrowserProxyImpl implements BraveTorBrowserProxy { isTorManaged() { return sendWithPromise('brave_tor.isTorManaged') } + + isSnowflakeExtensionAllowed() { + return sendWithPromise('brave_tor.isSnowflakeExtensionAllowed') + } + + isSnowflakeExtensionEnabled(): Promise { + return sendWithPromise('brave_tor.isSnowflakeExtensionEnabled') + } + + enableSnowflakeExtension(enable): Promise { + return sendWithPromise('brave_tor.enableSnowflakeExtension', enable) + } } let instance: BraveTorBrowserProxy|null = null diff --git a/browser/resources/settings/brave_tor_page/brave_tor_subpage.html b/browser/resources/settings/brave_tor_page/brave_tor_subpage.html index 0aef158a2686..c6cde9c61b9f 100644 --- a/browser/resources/settings/brave_tor_page/brave_tor_subpage.html +++ b/browser/resources/settings/brave_tor_page/brave_tor_subpage.html @@ -40,6 +40,13 @@ + + + + { this.loadedConfig_ = config this.isUsingBridgesPref_ = { @@ -160,6 +183,18 @@ class SettingsBraveTorPageElement extends SettingBraveTorPageElementBase { this.browserProxy_.isTorManaged().then(managed => { this.disableTorOption_ = managed }) + + if (loadTimeData.getBoolean('enable_extensions')) { + this.browserProxy_.isSnowflakeExtensionAllowed().then(allowed => { + this.torSnowflakeExtensionAllowed_ = allowed + }) + this.addWebUIListener('tor-snowflake-extension-enabled', enabled => { + this.setTorSnowflakeExtensionEnabledPref_(enabled) + }) + this.browserProxy_.isSnowflakeExtensionEnabled().then(enabled => { + this.setTorSnowflakeExtensionEnabledPref_(enabled) + }) + } } onSlotClick_(e) { @@ -321,6 +356,24 @@ class SettingsBraveTorPageElement extends SettingBraveTorPageElementBase { } } + setTorSnowflakeExtensionEnabledPref_(enabled: boolean) { + const pref = { + key: '', + type: chrome.settingsPrivate.PrefType.BOOLEAN, + value: enabled, + } + this.torSnowflakeExtensionEnabledPref_ = pref + } + + onTorSnowflakeExtensionChange_(e: Event) { + e.stopPropagation() + this.browserProxy_.enableSnowflakeExtension(e.target.checked). + catch((reason: String) => { + console.log(reason) + this.setTorSnowflakeExtensionEnabledPref_(false) + }) + } + currentRouteChanged() { // This is intentional. currentRouteChanged() should be overridden. } diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index 91a52a6dea56..2c01bff68ea5 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -192,6 +192,12 @@ source_set("ui") { "webui/settings/brave_tor_handler.h", ] } + if (enable_extensions) { + sources += [ + "webui/settings/brave_tor_snowflake_extension_handler.cc", + "webui/settings/brave_tor_snowflake_extension_handler.h", + ] + } public_deps += [ "//brave/vendor/bat-native-ads" ] } # !is_android diff --git a/browser/ui/webui/brave_settings_ui.cc b/browser/ui/webui/brave_settings_ui.cc index 4ee1ec82f58f..63d2e8ae9831 100644 --- a/browser/ui/webui/brave_settings_ui.cc +++ b/browser/ui/webui/brave_settings_ui.cc @@ -37,6 +37,7 @@ #include "components/sync/base/command_line_switches.h" #include "content/public/browser/web_ui_data_source.h" #include "content/public/common/content_features.h" +#include "extensions/buildflags/buildflags.h" #if BUILDFLAG(ENABLE_PIN_SHORTCUT) #include "brave/browser/ui/webui/settings/pin_shortcut_handler.h" @@ -54,6 +55,10 @@ #include "brave/browser/ui/webui/settings/brave_tor_handler.h" #endif +#if BUILDFLAG(ENABLE_EXTENSIONS) +#include "brave/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h" +#endif + using ntp_background_images::ViewCounterServiceFactory; BraveSettingsUI::BraveSettingsUI(content::WebUI* web_ui, @@ -71,6 +76,10 @@ BraveSettingsUI::BraveSettingsUI(content::WebUI* web_ui, #if BUILDFLAG(ENABLE_TOR) web_ui->AddMessageHandler(std::make_unique()); #endif +#if BUILDFLAG(ENABLE_EXTENSIONS) + web_ui->AddMessageHandler( + std::make_unique()); +#endif #if BUILDFLAG(ENABLE_PIN_SHORTCUT) web_ui->AddMessageHandler(std::make_unique()); #endif @@ -123,6 +132,8 @@ void BraveSettingsUI::AddResources(content::WebUIDataSource* html_source, html_source->AddBoolean("shouldExposeElementsForTesting", ShouldExposeElementsForTesting()); + + html_source->AddBoolean("enable_extensions", BUILDFLAG(ENABLE_EXTENSIONS)); } // static diff --git a/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc b/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc index ce017aa6c6ac..b1f320094c00 100644 --- a/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc +++ b/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc @@ -394,6 +394,8 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source, {"torEnabledDesc", IDS_SETTINGS_ENABLE_TOR_DESC}, {"torConnectionSettingsTitle", IDS_SETTINGS_TOR_CONNECTION_SETTINGS_TITLE}, {"torConnectionSettingsDesc", IDS_SETTINGS_TOR_CONNECTION_SETTINGS_DESC}, + {"torSnoflakeExtensionLabel", IDS_SETTINGS_TOR_SNOWFLAKE_EXTENSION_TITLE}, + {"torSnoflakeExtensionDesc", IDS_SETTINGS_TOR_SNOWFLAKE_EXTENSION_DESC}, {"torUseBridgesTitle", IDS_SETTINGS_TOR_USE_BRIDGES_TITLE}, {"torUseBridgesDesc", IDS_SETTINGS_TOR_USE_BRIDGES_DESC}, {"torSelectBuiltInRadio", IDS_SETTINGS_TOR_SELECT_BUILT_IN_RADIO}, diff --git a/browser/ui/webui/settings/brave_tor_handler.h b/browser/ui/webui/settings/brave_tor_handler.h index 50d35471f911..b45d8e4d1c65 100644 --- a/browser/ui/webui/settings/brave_tor_handler.h +++ b/browser/ui/webui/settings/brave_tor_handler.h @@ -8,7 +8,6 @@ #include -#include "base/memory/weak_ptr.h" #include "base/values.h" #include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h" #include "components/prefs/pref_change_registrar.h" diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc new file mode 100644 index 000000000000..ec078a2ce0a3 --- /dev/null +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc @@ -0,0 +1,193 @@ +// Copyright (c) 2022 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/ui/webui/settings/brave_tor_snowflake_extension_handler.h" + +#include "base/memory/scoped_refptr.h" +#include "brave/components/tor/pref_names.h" +#include "chrome/browser/extensions/extension_allowlist.h" +#include "chrome/browser/extensions/extension_service.h" +#include "chrome/browser/extensions/webstore_install_with_prompt.h" +#include "chrome/browser/profiles/profile.h" +#include "components/prefs/pref_service.h" +#include "content/public/browser/web_contents.h" +#include "extensions/browser/extension_registry.h" +#include "extensions/browser/extension_system.h" + +namespace { +// https://chrome.google.com/webstore/detail/snowflake/mafpmfcccpbjnhfhjnllmmalhifmlcie +constexpr const char kSnowflakeExtensionId[] = + "mafpmfcccpbjnhfhjnllmmalhifmlcie"; +} // namespace + +class SnowflakeWebstoreInstaller final + : public extensions::WebstoreInstallWithPrompt { + public: + using WebstoreInstallWithPrompt::WebstoreInstallWithPrompt; + + private: + ~SnowflakeWebstoreInstaller() final = default; + + std::unique_ptr CreateInstallPrompt() + const final { + return nullptr; + } + bool ShouldShowAppInstalledBubble() const final { return false; } + bool ShouldShowPostInstallUI() const final { return true; } +}; + +BraveTorSnowflakeExtensionHandler::BraveTorSnowflakeExtensionHandler() = + default; +BraveTorSnowflakeExtensionHandler::~BraveTorSnowflakeExtensionHandler() = + default; + +void BraveTorSnowflakeExtensionHandler::RegisterMessages() { + web_ui()->RegisterMessageCallback( + "brave_tor.isSnowflakeExtensionAllowed", + base::BindRepeating( + &BraveTorSnowflakeExtensionHandler::IsSnowflakeExtensionAllowed, + base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "brave_tor.isSnowflakeExtensionEnabled", + base::BindRepeating( + &BraveTorSnowflakeExtensionHandler::IsSnowflakeExtensionEnabled, + base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "brave_tor.enableSnowflakeExtension", + base::BindRepeating( + &BraveTorSnowflakeExtensionHandler::EnableSnowflakeExtension, + base::Unretained(this))); + + observation_.Observe(GetExtensionRegistry()); +} + +void BraveTorSnowflakeExtensionHandler::OnExtensionLoaded( + content::BrowserContext* browser_context, + const extensions::Extension* extension) { + DCHECK(extension); + if (extension->id() != kSnowflakeExtensionId || + browser_context != web_ui()->GetWebContents()->GetBrowserContext()) { + return; + } + if (IsJavascriptAllowed()) { + FireWebUIListener("tor-snowflake-extension-enabled", base::Value(true)); + } +} + +void BraveTorSnowflakeExtensionHandler::OnExtensionUnloaded( + content::BrowserContext* browser_context, + const extensions::Extension* extension, + extensions::UnloadedExtensionReason reason) { + DCHECK(extension); + if (extension->id() != kSnowflakeExtensionId || + browser_context != web_ui()->GetWebContents()->GetBrowserContext()) { + return; + } + if (IsJavascriptAllowed()) { + FireWebUIListener("tor-snowflake-extension-enabled", base::Value(false)); + } +} + +void BraveTorSnowflakeExtensionHandler::OnExtensionInstalled( + content::BrowserContext* browser_context, + const extensions::Extension* extension, + bool is_update) { + OnExtensionLoaded(browser_context, extension); +} + +void BraveTorSnowflakeExtensionHandler::OnExtensionUninstalled( + content::BrowserContext* browser_context, + const extensions::Extension* extension, + extensions::UninstallReason reason) { + OnExtensionUnloaded(browser_context, extension, + extensions::UnloadedExtensionReason::UNINSTALL); +} + +extensions::ExtensionRegistry* +BraveTorSnowflakeExtensionHandler::GetExtensionRegistry() { + return extensions::ExtensionRegistry::Get( + web_ui()->GetWebContents()->GetBrowserContext()); +} + +void BraveTorSnowflakeExtensionHandler::IsSnowflakeExtensionAllowed( + const base::Value::List& args) { + CHECK_EQ(args.size(), 1U); + + Profile* profile = Profile::FromBrowserContext( + web_ui()->GetWebContents()->GetBrowserContext()); + + const bool is_allowed = + !profile->GetPrefs()->IsManagedPreference(tor::prefs::kTorDisabled) || + profile->GetPrefs()->GetBoolean(tor::prefs::kTorDisabled); + + AllowJavascript(); + ResolveJavascriptCallback(args[0], base::Value(is_allowed)); +} + +void BraveTorSnowflakeExtensionHandler::IsSnowflakeExtensionEnabled( + const base::Value::List& args) { + CHECK_EQ(args.size(), 1U); + + const bool is_enabled = GetExtensionRegistry()->enabled_extensions().Contains( + kSnowflakeExtensionId); + + AllowJavascript(); + ResolveJavascriptCallback(args[0], base::Value(is_enabled)); +} + +void BraveTorSnowflakeExtensionHandler::EnableSnowflakeExtension( + const base::Value::List& args) { + CHECK_EQ(args.size(), 2U); + + const bool enable = args[1].GetBool(); + const bool installed = GetExtensionRegistry()->GetInstalledExtension( + kSnowflakeExtensionId) != nullptr; + const bool enabled = GetExtensionRegistry()->enabled_extensions().Contains( + kSnowflakeExtensionId); + + AllowJavascript(); + + if (enable == enabled) { + ResolveJavascriptCallback(args[0], base::Value(true)); + return; + } + + auto* profile = Profile::FromBrowserContext( + web_ui()->GetWebContents()->GetBrowserContext()); + auto* extension_service = + extensions::ExtensionSystem::Get(profile)->extension_service(); + + if (enable) { + if (!installed) { + installer_ = base::MakeRefCounted( + kSnowflakeExtensionId, profile, + base::BindOnce( + &BraveTorSnowflakeExtensionHandler::OnSnowflakeExtensionInstalled, + weak_factory_.GetWeakPtr(), args[0].Clone())); + installer_->BeginInstall(); + } else { + extension_service->EnableExtension(kSnowflakeExtensionId); + ResolveJavascriptCallback(args[0], base::Value(true)); + } + } else { + installer_.reset(); + extension_service->UninstallExtension( + kSnowflakeExtensionId, extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT, + nullptr); + ResolveJavascriptCallback(args[0], base::Value(true)); + } +} + +void BraveTorSnowflakeExtensionHandler::OnSnowflakeExtensionInstalled( + base::Value js_callback, + bool success, + const std::string& error, + extensions::webstore_install::Result result) { + if (!success) { + RejectJavascriptCallback(js_callback, base::Value(error)); + } else { + ResolveJavascriptCallback(js_callback, base::Value(true)); + } +} \ No newline at end of file diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h new file mode 100644 index 000000000000..d59f73e9a487 --- /dev/null +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h @@ -0,0 +1,66 @@ +// Copyright (c) 2022 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_UI_WEBUI_SETTINGS_BRAVE_TOR_SNOWFLAKE_EXTENSION_HANDLER_H_ +#define BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_TOR_SNOWFLAKE_EXTENSION_HANDLER_H_ + +#include "base/memory/scoped_refptr.h" +#include "base/memory/weak_ptr.h" +#include "base/scoped_observation.h" +#include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h" +#include "chrome/common/extensions/webstore_install_result.h" +#include "extensions/browser/extension_registry.h" +#include "extensions/browser/extension_registry_observer.h" + +class BraveTorSnowflakeExtensionHandler + : public settings::SettingsPageUIHandler, + public extensions::ExtensionRegistryObserver { + public: + BraveTorSnowflakeExtensionHandler(); + BraveTorSnowflakeExtensionHandler(const BraveTorSnowflakeExtensionHandler&) = + delete; + BraveTorSnowflakeExtensionHandler& operator=( + const BraveTorSnowflakeExtensionHandler&) = delete; + ~BraveTorSnowflakeExtensionHandler() override; + + private: + // SettingsPageUIHandler: + void RegisterMessages() override; + void OnJavascriptAllowed() override {} + void OnJavascriptDisallowed() override {} + + // extensions::ExtensionRegistryObserver: + void OnExtensionLoaded(content::BrowserContext* browser_context, + const extensions::Extension* extension) override; + void OnExtensionUnloaded(content::BrowserContext* browser_context, + const extensions::Extension* extension, + extensions::UnloadedExtensionReason reason) override; + void OnExtensionInstalled(content::BrowserContext* browser_context, + const extensions::Extension* extension, + bool is_update) override; + void OnExtensionUninstalled(content::BrowserContext* browser_context, + const extensions::Extension* extension, + extensions::UninstallReason reason) override; + + extensions::ExtensionRegistry* GetExtensionRegistry(); + + void IsSnowflakeExtensionAllowed(const base::Value::List& args); + void IsSnowflakeExtensionEnabled(const base::Value::List& args); + void EnableSnowflakeExtension(const base::Value::List& args); + void OnSnowflakeExtensionInstalled( + base::Value js_callback, + bool success, + const std::string& error, + extensions::webstore_install::Result result); + + base::ScopedObservation + observation_{this}; + scoped_refptr installer_; + + base::WeakPtrFactory weak_factory_{this}; +}; + +#endif // BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_TOR_SNOWFLAKE_EXTENSION_HANDLER_H_ From e5edea892859c4b194e48dca09898d2c4039947a Mon Sep 17 00:00:00 2001 From: boocmp Date: Fri, 11 Nov 2022 17:51:12 +0700 Subject: [PATCH 02/10] Added tests. --- ...ave_tor_snowflake_extension_browsertest.cc | 120 ++++++++++++++++++ test/BUILD.gn | 1 + 2 files changed, 121 insertions(+) create mode 100644 browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc b/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc new file mode 100644 index 000000000000..417f51e5a476 --- /dev/null +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc @@ -0,0 +1,120 @@ +// Copyright (c) 2022 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 "base/path_service.h" +#include "brave/browser/ui/webui/brave_settings_ui.h" +#include "chrome/browser/extensions/extension_service.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "chrome/test/base/ui_test_utils.h" +#include "content/public/test/browser_test.h" +#include "extensions/browser/disable_reason.h" +#include "extensions/browser/extension_system.h" +#include "extensions/common/extension.h" +#include "extensions/common/extension_builder.h" + +namespace { + +constexpr const char kSnowflakeExtensionId[] = + "mafpmfcccpbjnhfhjnllmmalhifmlcie"; + +bool ClickSnowflakeToggle(content::WebContents* web_contents) { + return EvalJs( + web_contents, + "window.testing.torSubpage.getElementById('torSnowflake').click()") + .value.is_none(); +} + +bool IsSnowflakeToggled(content::WebContents* web_contents) { + return EvalJs( + web_contents, + "window.testing.torSubpage.getElementById('torSnowflake').checked") + .value.GetBool(); +} + +} // namespace + +class TorSnowflakeExtensionBrowserTest : public InProcessBrowserTest { + public: + TorSnowflakeExtensionBrowserTest() { + // Disabling CSP on webui pages so EvalJS could be run in main world. + BraveSettingsUI::ShouldDisableCSPForTesting() = true; + BraveSettingsUI::ShouldExposeElementsForTesting() = true; + } + + ~TorSnowflakeExtensionBrowserTest() override { + BraveSettingsUI::ShouldDisableCSPForTesting() = false; + BraveSettingsUI::ShouldExposeElementsForTesting() = false; + } + + void SimulateSnowflakeInstall() { + scoped_refptr extension( + extensions::ExtensionBuilder("Snowflake") + .SetID(kSnowflakeExtensionId) + .Build()); + extensions::ExtensionSystem::Get(browser()->profile()) + ->extension_service() + ->AddExtension(extension.get()); + } + + void EnableSnowflake(bool enable) { + if (enable) { + extensions::ExtensionSystem::Get(browser()->profile()) + ->extension_service() + ->EnableExtension(kSnowflakeExtensionId); + } else { + extensions::ExtensionSystem::Get(browser()->profile()) + ->extension_service() + ->DisableExtension(kSnowflakeExtensionId, + extensions::disable_reason::DISABLE_USER_ACTION); + } + } + + bool IsSnowflakeInstalled() { + return extensions::ExtensionRegistry::Get(browser()->profile()) + ->GetInstalledExtension(kSnowflakeExtensionId); + } +}; + +IN_PROC_BROWSER_TEST_F(TorSnowflakeExtensionBrowserTest, InstallFail) { + ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), + GURL("brave://settings/privacy"))); + auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); + content::WebContentsConsoleObserver console_observer(web_contents); + console_observer.SetPattern("Could not fetch data from the Chrome Web Store"); + ClickSnowflakeToggle(web_contents); + console_observer.Wait(); +} + +IN_PROC_BROWSER_TEST_F(TorSnowflakeExtensionBrowserTest, + InstallDisableEnableUninstall) { + ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), + GURL("brave://settings/privacy"))); + auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); + EXPECT_FALSE(IsSnowflakeToggled(web_contents)); + + SimulateSnowflakeInstall(); + EXPECT_TRUE(IsSnowflakeToggled(web_contents)); + { + // toggle extension disabled->enabled->disabled + EnableSnowflake(false); + EXPECT_FALSE(IsSnowflakeToggled(web_contents)); + EXPECT_TRUE(IsSnowflakeInstalled()); + + EnableSnowflake(true); + EXPECT_TRUE(IsSnowflakeToggled(web_contents)); + EXPECT_TRUE(IsSnowflakeInstalled()); + + EnableSnowflake(false); + } + + ClickSnowflakeToggle(web_contents); + EXPECT_TRUE(IsSnowflakeToggled(web_contents)); + EXPECT_TRUE(IsSnowflakeInstalled()); + + ClickSnowflakeToggle(web_contents); + EXPECT_FALSE(IsSnowflakeToggled(web_contents)); + EXPECT_FALSE(IsSnowflakeInstalled()); +} diff --git a/test/BUILD.gn b/test/BUILD.gn index c1a298f6f20d..850290c7ae50 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -762,6 +762,7 @@ test("brave_browser_tests") { "//brave/browser/ui/webui/brave_welcome_ui_browsertest.cc", "//brave/browser/ui/webui/new_tab_page/brave_new_tab_ui_browsertest.cc", "//brave/browser/ui/webui/private_new_tab_page/brave_private_new_tab_page_browsertest.cc", + "//brave/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc", "//brave/browser/web_package/signed_exchange_request_browsertest.cc", "//brave/chromium_src/chrome/browser/safe_browsing/download_protection/check_client_download_request_base_browsertest.cc", "//brave/chromium_src/chrome/browser/ui/hats/hats_service_browsertest.cc", From c85bdf9941ea3508c418e50e0deb2ebe9494a496 Mon Sep 17 00:00:00 2001 From: boocmp Date: Fri, 11 Nov 2022 21:31:28 +0700 Subject: [PATCH 03/10] Added install failed dialog. --- app/brave_settings_strings.grdp | 3 ++ ...e_tor_snowflake_install_failed_dialog.html | 11 ++++++ ...ave_tor_snowflake_install_failed_dialog.ts | 38 +++++++++++++++++++ .../brave_tor_page/brave_tor_subpage.html | 10 ++++- .../brave_tor_page/brave_tor_subpage.ts | 12 +++++- browser/resources/settings/sources.gni | 1 + ...ave_settings_localized_strings_provider.cc | 1 + ...ave_tor_snowflake_extension_browsertest.cc | 3 ++ 8 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.html create mode 100644 browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts diff --git a/app/brave_settings_strings.grdp b/app/brave_settings_strings.grdp index 88484d95e2af..c5c94fc6add5 100644 --- a/app/brave_settings_strings.grdp +++ b/app/brave_settings_strings.grdp @@ -744,6 +744,9 @@ Controls how Private Windows with Tor connect to the Tor network + + The Snowflake extension necessary for this functionality could not be downloaded. Please check your Internet connection and try again. + Select a built-in bridge diff --git a/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.html b/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.html new file mode 100644 index 000000000000..cebde2f5911f --- /dev/null +++ b/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.html @@ -0,0 +1,11 @@ + + +
+
+
$i18n{torSnowflakeInstallFailed}
+
+
+ OK +
+
diff --git a/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts b/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts new file mode 100644 index 000000000000..ca8fa5717426 --- /dev/null +++ b/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts @@ -0,0 +1,38 @@ +// Copyright (c) 2022 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/. + +// @ts-nocheck TODO(petemill): Define types and remove ts-nocheck + +import 'chrome://resources/cr_elements/cr_button/cr_button.js'; +import 'chrome://resources/cr_elements/cr_dialog/cr_dialog.js'; +import '../settings_shared.css.js'; + +import { PolymerElement } from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; +import { I18nMixin } from 'chrome://resources/js/i18n_mixin.js'; +import { PrefsMixin } from '../prefs/prefs_mixin.js'; +import { getTemplate } from './brave_tor_snowflake_install_failed_dialog.html.js' + +const DialogBase = I18nMixin(PolymerElement) + +class TorSnowflakeInstallFailedDialog extends DialogBase { + static get is() { + return 'tor-snowflake-install-failed-dialog' + } + + static get template() { + return getTemplate() + } + + static get properties() { + return {} + } + + cancelClicked_() { + this.$.dialog.close() + } +} + +customElements.define( + TorSnowflakeInstallFailedDialog.is, TorSnowflakeInstallFailedDialog) diff --git a/browser/resources/settings/brave_tor_page/brave_tor_subpage.html b/browser/resources/settings/brave_tor_page/brave_tor_subpage.html index c6cde9c61b9f..a41441e30f11 100644 --- a/browser/resources/settings/brave_tor_page/brave_tor_subpage.html +++ b/browser/resources/settings/brave_tor_page/brave_tor_subpage.html @@ -96,4 +96,12 @@ \ No newline at end of file + + + + + diff --git a/browser/resources/settings/brave_tor_page/brave_tor_subpage.ts b/browser/resources/settings/brave_tor_page/brave_tor_subpage.ts index 55d652c209c7..003b7dbc5a9e 100644 --- a/browser/resources/settings/brave_tor_page/brave_tor_subpage.ts +++ b/browser/resources/settings/brave_tor_page/brave_tor_subpage.ts @@ -12,6 +12,7 @@ import { RouteObserverMixin } from '../router.js' import { PrefsMixin } from '../prefs/prefs_mixin.js' import { BraveTorBrowserProxyImpl } from './brave_tor_browser_proxy.js' import './brave_tor_bridges_dialog.js' +import './brave_tor_snowflake_install_failed_dialog.js' import {getTemplate} from './brave_tor_subpage.html.js' import { loadTimeData } from '../i18n_setup.js' @@ -139,7 +140,9 @@ class SettingsBraveTorPageElement extends SettingBraveTorPageElementBase { computed: 'computeIsConfigChanged_(useBridges_, builtinBridges_, requestedBridges_, providedBridges_, loadedConfig_, shouldShowBridgesGroup_, torEnabledPref_.value)', value: false, notify: true - } + }, + + showTorSnowflakeInstallFailed_: Boolean, } } @@ -158,7 +161,7 @@ class SettingsBraveTorPageElement extends SettingBraveTorPageElementBase { window.testing = window.testing || {} window.testing[`torSubpage`] = this.shadowRoot } - + this.browserProxy_.getBridgesConfig().then((config) => { this.loadedConfig_ = config this.isUsingBridgesPref_ = { @@ -371,9 +374,14 @@ class SettingsBraveTorPageElement extends SettingBraveTorPageElementBase { catch((reason: String) => { console.log(reason) this.setTorSnowflakeExtensionEnabledPref_(false) + this.showTorSnowflakeInstallFailed_ = true }) } + torSnowflakeInstallFailedDialogClosed_() { + this.showTorSnowflakeInstallFailed_ = false + } + currentRouteChanged() { // This is intentional. currentRouteChanged() should be overridden. } diff --git a/browser/resources/settings/sources.gni b/browser/resources/settings/sources.gni index d5e713eafe4c..b1d74eddcc97 100644 --- a/browser/resources/settings/sources.gni +++ b/browser/resources/settings/sources.gni @@ -37,6 +37,7 @@ brave_settings_web_component_files = [ "brave_sync_page/brave_sync_subpage.ts", "brave_tor_page/brave_tor_bridges_dialog.ts", "brave_tor_page/brave_tor_subpage.ts", + "brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts", "brave_wallet_page/add_wallet_network_dialog.ts", "brave_wallet_page/brave_wallet_page.ts", "brave_wallet_page/wallet_networks_list.ts", diff --git a/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc b/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc index b1f320094c00..d94d2aaf5eb0 100644 --- a/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc +++ b/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc @@ -396,6 +396,7 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source, {"torConnectionSettingsDesc", IDS_SETTINGS_TOR_CONNECTION_SETTINGS_DESC}, {"torSnoflakeExtensionLabel", IDS_SETTINGS_TOR_SNOWFLAKE_EXTENSION_TITLE}, {"torSnoflakeExtensionDesc", IDS_SETTINGS_TOR_SNOWFLAKE_EXTENSION_DESC}, + {"torSnowflakeInstallFailed", IDS_SETTINGS_TOR_SNOWFLAKE_INSTALL_FAILED}, {"torUseBridgesTitle", IDS_SETTINGS_TOR_USE_BRIDGES_TITLE}, {"torUseBridgesDesc", IDS_SETTINGS_TOR_USE_BRIDGES_DESC}, {"torSelectBuiltInRadio", IDS_SETTINGS_TOR_SELECT_BUILT_IN_RADIO}, diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc b/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc index 417f51e5a476..b861d8c22593 100644 --- a/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc @@ -86,6 +86,7 @@ IN_PROC_BROWSER_TEST_F(TorSnowflakeExtensionBrowserTest, InstallFail) { console_observer.SetPattern("Could not fetch data from the Chrome Web Store"); ClickSnowflakeToggle(web_contents); console_observer.Wait(); + EXPECT_FALSE(IsSnowflakeToggled(web_contents)); } IN_PROC_BROWSER_TEST_F(TorSnowflakeExtensionBrowserTest, @@ -110,10 +111,12 @@ IN_PROC_BROWSER_TEST_F(TorSnowflakeExtensionBrowserTest, EnableSnowflake(false); } + // enable from settings. ClickSnowflakeToggle(web_contents); EXPECT_TRUE(IsSnowflakeToggled(web_contents)); EXPECT_TRUE(IsSnowflakeInstalled()); + // disabled from settings -> uninstalled. ClickSnowflakeToggle(web_contents); EXPECT_FALSE(IsSnowflakeToggled(web_contents)); EXPECT_FALSE(IsSnowflakeInstalled()); From 1590e04d237594842a22a225d3572d591bfb8ef4 Mon Sep 17 00:00:00 2001 From: boocmp Date: Fri, 11 Nov 2022 21:34:31 +0700 Subject: [PATCH 04/10] Lint. --- browser/ui/BUILD.gn | 2 +- .../webui/settings/brave_tor_snowflake_extension_handler.cc | 5 ++++- .../webui/settings/brave_tor_snowflake_extension_handler.h | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index 2c01bff68ea5..04b07ea2cc73 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -196,7 +196,7 @@ source_set("ui") { sources += [ "webui/settings/brave_tor_snowflake_extension_handler.cc", "webui/settings/brave_tor_snowflake_extension_handler.h", - ] + ] } public_deps += [ "//brave/vendor/bat-native-ads" ] diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc index ec078a2ce0a3..e8260d2b871d 100644 --- a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc @@ -5,6 +5,9 @@ #include "brave/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h" +#include +#include + #include "base/memory/scoped_refptr.h" #include "brave/components/tor/pref_names.h" #include "chrome/browser/extensions/extension_allowlist.h" @@ -190,4 +193,4 @@ void BraveTorSnowflakeExtensionHandler::OnSnowflakeExtensionInstalled( } else { ResolveJavascriptCallback(js_callback, base::Value(true)); } -} \ No newline at end of file +} diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h index d59f73e9a487..a5be4ed4d332 100644 --- a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h @@ -6,6 +6,8 @@ #ifndef BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_TOR_SNOWFLAKE_EXTENSION_HANDLER_H_ #define BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_TOR_SNOWFLAKE_EXTENSION_HANDLER_H_ +#include + #include "base/memory/scoped_refptr.h" #include "base/memory/weak_ptr.h" #include "base/scoped_observation.h" From 7b7dd8980178766f5ec737ac87e16fd8ad8e761c Mon Sep 17 00:00:00 2001 From: boocmp Date: Sat, 12 Nov 2022 14:45:45 +0700 Subject: [PATCH 05/10] AllowJavascript call. --- .../ui/webui/settings/brave_tor_snowflake_extension_handler.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc index e8260d2b871d..36525f7200de 100644 --- a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc @@ -188,6 +188,7 @@ void BraveTorSnowflakeExtensionHandler::OnSnowflakeExtensionInstalled( bool success, const std::string& error, extensions::webstore_install::Result result) { + AllowJavascript(); if (!success) { RejectJavascriptCallback(js_callback, base::Value(error)); } else { From 4f73719b74898a72256b62d77a2583c55d6d481d Mon Sep 17 00:00:00 2001 From: boocmp Date: Sat, 12 Nov 2022 14:49:39 +0700 Subject: [PATCH 06/10] Cleanup. --- .../brave_tor_snowflake_install_failed_dialog.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts b/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts index ca8fa5717426..f2157bbfe57f 100644 --- a/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts +++ b/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts @@ -5,13 +5,12 @@ // @ts-nocheck TODO(petemill): Define types and remove ts-nocheck -import 'chrome://resources/cr_elements/cr_button/cr_button.js'; -import 'chrome://resources/cr_elements/cr_dialog/cr_dialog.js'; -import '../settings_shared.css.js'; +import 'chrome://resources/cr_elements/cr_button/cr_button.js' +import 'chrome://resources/cr_elements/cr_dialog/cr_dialog.js' +import '../settings_shared.css.js' -import { PolymerElement } from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; -import { I18nMixin } from 'chrome://resources/js/i18n_mixin.js'; -import { PrefsMixin } from '../prefs/prefs_mixin.js'; +import { PolymerElement } from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js' +import { I18nMixin } from 'chrome://resources/js/i18n_mixin.js' import { getTemplate } from './brave_tor_snowflake_install_failed_dialog.html.js' const DialogBase = I18nMixin(PolymerElement) From 32f85c0d3fc14a8bcc310666bce26b86039e42db Mon Sep 17 00:00:00 2001 From: boocmp Date: Mon, 14 Nov 2022 12:24:56 +0700 Subject: [PATCH 07/10] Policy check and test. --- ...ave_tor_snowflake_extension_browsertest.cc | 39 +++++++++++++++++++ .../brave_tor_snowflake_extension_handler.cc | 8 ++-- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc b/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc index b861d8c22593..e6f40ba125b2 100644 --- a/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc @@ -9,6 +9,9 @@ #include "chrome/browser/ui/browser.h" #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/ui_test_utils.h" +#include "components/policy/core/browser/browser_policy_connector.h" +#include "components/policy/core/common/mock_configuration_policy_provider.h" +#include "components/policy/policy_constants.h" #include "content/public/test/browser_test.h" #include "extensions/browser/disable_reason.h" #include "extensions/browser/extension_system.h" @@ -34,6 +37,13 @@ bool IsSnowflakeToggled(content::WebContents* web_contents) { .value.GetBool(); } +bool IsSnowflakeToggleEnabled(content::WebContents* web_contents) { + return EvalJs(web_contents, + "!window.testing.torSubpage.getElementById('torSnowflake')." + "disabled") + .value.GetBool(); +} + } // namespace class TorSnowflakeExtensionBrowserTest : public InProcessBrowserTest { @@ -76,6 +86,23 @@ class TorSnowflakeExtensionBrowserTest : public InProcessBrowserTest { return extensions::ExtensionRegistry::Get(browser()->profile()) ->GetInstalledExtension(kSnowflakeExtensionId); } + + void SetTorDisabledPolicy(bool value) { + policy::PolicyMap policies; + policies.Set(policy::key::kTorDisabled, policy::POLICY_LEVEL_MANDATORY, + policy::POLICY_SCOPE_MACHINE, policy::POLICY_SOURCE_PLATFORM, + base::Value(value), nullptr); + provider_.UpdateChromePolicy(policies); + } + + private: + void SetUpInProcessBrowserTestFixture() override { + EXPECT_CALL(provider_, IsInitializationComplete(testing::_)) + .WillRepeatedly(testing::Return(true)); + policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); + } + + testing::NiceMock provider_; }; IN_PROC_BROWSER_TEST_F(TorSnowflakeExtensionBrowserTest, InstallFail) { @@ -87,6 +114,7 @@ IN_PROC_BROWSER_TEST_F(TorSnowflakeExtensionBrowserTest, InstallFail) { ClickSnowflakeToggle(web_contents); console_observer.Wait(); EXPECT_FALSE(IsSnowflakeToggled(web_contents)); + EXPECT_TRUE(IsSnowflakeToggleEnabled(web_contents)); } IN_PROC_BROWSER_TEST_F(TorSnowflakeExtensionBrowserTest, @@ -95,6 +123,7 @@ IN_PROC_BROWSER_TEST_F(TorSnowflakeExtensionBrowserTest, GURL("brave://settings/privacy"))); auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); EXPECT_FALSE(IsSnowflakeToggled(web_contents)); + EXPECT_TRUE(IsSnowflakeToggleEnabled(web_contents)); SimulateSnowflakeInstall(); EXPECT_TRUE(IsSnowflakeToggled(web_contents)); @@ -121,3 +150,13 @@ IN_PROC_BROWSER_TEST_F(TorSnowflakeExtensionBrowserTest, EXPECT_FALSE(IsSnowflakeToggled(web_contents)); EXPECT_FALSE(IsSnowflakeInstalled()); } + +IN_PROC_BROWSER_TEST_F(TorSnowflakeExtensionBrowserTest, CheckPolicy) { + SetTorDisabledPolicy(true); + + ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), + GURL("brave://settings/privacy"))); + auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); + EXPECT_FALSE(IsSnowflakeToggled(web_contents)); + EXPECT_FALSE(IsSnowflakeToggleEnabled(web_contents)); +} diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc index 36525f7200de..084b51d5d4e3 100644 --- a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc @@ -10,6 +10,7 @@ #include "base/memory/scoped_refptr.h" #include "brave/components/tor/pref_names.h" +#include "chrome/browser/browser_process.h" #include "chrome/browser/extensions/extension_allowlist.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/webstore_install_with_prompt.h" @@ -118,12 +119,9 @@ void BraveTorSnowflakeExtensionHandler::IsSnowflakeExtensionAllowed( const base::Value::List& args) { CHECK_EQ(args.size(), 1U); - Profile* profile = Profile::FromBrowserContext( - web_ui()->GetWebContents()->GetBrowserContext()); - const bool is_allowed = - !profile->GetPrefs()->IsManagedPreference(tor::prefs::kTorDisabled) || - profile->GetPrefs()->GetBoolean(tor::prefs::kTorDisabled); + !g_browser_process->local_state()->IsManagedPreference( + tor::prefs::kTorDisabled); AllowJavascript(); ResolveJavascriptCallback(args[0], base::Value(is_allowed)); From b467f50c818ce3e972036fec71826b8ededc75a6 Mon Sep 17 00:00:00 2001 From: boocmp Date: Mon, 14 Nov 2022 15:47:43 +0700 Subject: [PATCH 08/10] Cleanup. --- .../resources/settings/brave_tor_page/brave_tor_subpage.html | 2 +- browser/resources/settings/sources.gni | 2 +- .../settings/brave_settings_localized_strings_provider.cc | 4 ++-- test/BUILD.gn | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/browser/resources/settings/brave_tor_page/brave_tor_subpage.html b/browser/resources/settings/brave_tor_page/brave_tor_subpage.html index a41441e30f11..737b6b4e8e24 100644 --- a/browser/resources/settings/brave_tor_page/brave_tor_subpage.html +++ b/browser/resources/settings/brave_tor_page/brave_tor_subpage.html @@ -42,7 +42,7 @@
diff --git a/browser/resources/settings/sources.gni b/browser/resources/settings/sources.gni index b1d74eddcc97..ad8177324fbc 100644 --- a/browser/resources/settings/sources.gni +++ b/browser/resources/settings/sources.gni @@ -36,8 +36,8 @@ brave_settings_web_component_files = [ "brave_sync_page/brave_sync_setup.ts", "brave_sync_page/brave_sync_subpage.ts", "brave_tor_page/brave_tor_bridges_dialog.ts", - "brave_tor_page/brave_tor_subpage.ts", "brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts", + "brave_tor_page/brave_tor_subpage.ts", "brave_wallet_page/add_wallet_network_dialog.ts", "brave_wallet_page/brave_wallet_page.ts", "brave_wallet_page/wallet_networks_list.ts", diff --git a/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc b/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc index d94d2aaf5eb0..c4170362bb0f 100644 --- a/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc +++ b/browser/ui/webui/settings/brave_settings_localized_strings_provider.cc @@ -394,8 +394,8 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source, {"torEnabledDesc", IDS_SETTINGS_ENABLE_TOR_DESC}, {"torConnectionSettingsTitle", IDS_SETTINGS_TOR_CONNECTION_SETTINGS_TITLE}, {"torConnectionSettingsDesc", IDS_SETTINGS_TOR_CONNECTION_SETTINGS_DESC}, - {"torSnoflakeExtensionLabel", IDS_SETTINGS_TOR_SNOWFLAKE_EXTENSION_TITLE}, - {"torSnoflakeExtensionDesc", IDS_SETTINGS_TOR_SNOWFLAKE_EXTENSION_DESC}, + {"torSnowflakeExtensionLabel", IDS_SETTINGS_TOR_SNOWFLAKE_EXTENSION_TITLE}, + {"torSnowflakeExtensionDesc", IDS_SETTINGS_TOR_SNOWFLAKE_EXTENSION_DESC}, {"torSnowflakeInstallFailed", IDS_SETTINGS_TOR_SNOWFLAKE_INSTALL_FAILED}, {"torUseBridgesTitle", IDS_SETTINGS_TOR_USE_BRIDGES_TITLE}, {"torUseBridgesDesc", IDS_SETTINGS_TOR_USE_BRIDGES_DESC}, diff --git a/test/BUILD.gn b/test/BUILD.gn index 850290c7ae50..3b745b53328c 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -762,7 +762,6 @@ test("brave_browser_tests") { "//brave/browser/ui/webui/brave_welcome_ui_browsertest.cc", "//brave/browser/ui/webui/new_tab_page/brave_new_tab_ui_browsertest.cc", "//brave/browser/ui/webui/private_new_tab_page/brave_private_new_tab_page_browsertest.cc", - "//brave/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc", "//brave/browser/web_package/signed_exchange_request_browsertest.cc", "//brave/chromium_src/chrome/browser/safe_browsing/download_protection/check_client_download_request_base_browsertest.cc", "//brave/chromium_src/chrome/browser/ui/hats/hats_service_browsertest.cc", @@ -1124,6 +1123,7 @@ test("brave_browser_tests") { if (enable_extensions) { sources += [ "//brave/browser/extensions/brave_shields_apitest.cc", + "//brave/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc", "//chrome/browser/extensions/extension_apitest.cc", "//chrome/browser/extensions/extension_apitest.h", ] From 960b28cc62d7168417b9c25f3a971eb7cecb16ee Mon Sep 17 00:00:00 2001 From: boocmp Date: Mon, 14 Nov 2022 19:06:18 +0700 Subject: [PATCH 09/10] Do not show enabled when not allowed. --- .../brave_tor_snowflake_extension_handler.cc | 16 ++++++++++------ .../brave_tor_snowflake_extension_handler.h | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc index 084b51d5d4e3..9a6f764edcb0 100644 --- a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc @@ -115,27 +115,31 @@ BraveTorSnowflakeExtensionHandler::GetExtensionRegistry() { web_ui()->GetWebContents()->GetBrowserContext()); } -void BraveTorSnowflakeExtensionHandler::IsSnowflakeExtensionAllowed( - const base::Value::List& args) { - CHECK_EQ(args.size(), 1U); - +bool BraveTorSnowflakeExtensionHandler::IsTorAllowedByPolicy() { const bool is_allowed = !g_browser_process->local_state()->IsManagedPreference( tor::prefs::kTorDisabled); + return is_allowed; +} + +void BraveTorSnowflakeExtensionHandler::IsSnowflakeExtensionAllowed( + const base::Value::List& args) { + CHECK_EQ(args.size(), 1U); AllowJavascript(); - ResolveJavascriptCallback(args[0], base::Value(is_allowed)); + ResolveJavascriptCallback(args[0], base::Value(IsTorAllowedByPolicy())); } void BraveTorSnowflakeExtensionHandler::IsSnowflakeExtensionEnabled( const base::Value::List& args) { CHECK_EQ(args.size(), 1U); + const bool is_allowed = IsTorAllowedByPolicy(); const bool is_enabled = GetExtensionRegistry()->enabled_extensions().Contains( kSnowflakeExtensionId); AllowJavascript(); - ResolveJavascriptCallback(args[0], base::Value(is_enabled)); + ResolveJavascriptCallback(args[0], base::Value(is_allowed && is_enabled)); } void BraveTorSnowflakeExtensionHandler::EnableSnowflakeExtension( diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h index a5be4ed4d332..448d859512be 100644 --- a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h @@ -48,6 +48,7 @@ class BraveTorSnowflakeExtensionHandler extensions::ExtensionRegistry* GetExtensionRegistry(); + bool IsTorAllowedByPolicy(); void IsSnowflakeExtensionAllowed(const base::Value::List& args); void IsSnowflakeExtensionEnabled(const base::Value::List& args); void EnableSnowflakeExtension(const base::Value::List& args); From 6e63e6e4e1433a3a184cbb2ce84a1c523fd10e7c Mon Sep 17 00:00:00 2001 From: boocmp Date: Tue, 15 Nov 2022 17:14:56 +0700 Subject: [PATCH 10/10] Rebase & review. --- .../brave_tor_snowflake_install_failed_dialog.ts | 4 ++-- .../settings/brave_tor_snowflake_extension_browsertest.cc | 2 +- .../webui/settings/brave_tor_snowflake_extension_handler.cc | 2 +- .../ui/webui/settings/brave_tor_snowflake_extension_handler.h | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts b/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts index f2157bbfe57f..c624b133374f 100644 --- a/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts +++ b/browser/resources/settings/brave_tor_page/brave_tor_snowflake_install_failed_dialog.ts @@ -1,7 +1,7 @@ // Copyright (c) 2022 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/. +// you can obtain one at https://mozilla.org/MPL/2.0/. // @ts-nocheck TODO(petemill): Define types and remove ts-nocheck @@ -10,7 +10,7 @@ import 'chrome://resources/cr_elements/cr_dialog/cr_dialog.js' import '../settings_shared.css.js' import { PolymerElement } from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js' -import { I18nMixin } from 'chrome://resources/js/i18n_mixin.js' +import { I18nMixin } from 'chrome://resources/cr_elements/i18n_mixin.js' import { getTemplate } from './brave_tor_snowflake_install_failed_dialog.html.js' const DialogBase = I18nMixin(PolymerElement) diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc b/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc index e6f40ba125b2..e294238890b8 100644 --- a/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_browsertest.cc @@ -1,7 +1,7 @@ // Copyright (c) 2022 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/. +// you can obtain one at https://mozilla.org/MPL/2.0/. #include "base/path_service.h" #include "brave/browser/ui/webui/brave_settings_ui.h" diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc index 9a6f764edcb0..067a9311cd27 100644 --- a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.cc @@ -1,7 +1,7 @@ // Copyright (c) 2022 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/. +// you can obtain one at https://mozilla.org/MPL/2.0/. #include "brave/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h" diff --git a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h index 448d859512be..25809a6e106a 100644 --- a/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h +++ b/browser/ui/webui/settings/brave_tor_snowflake_extension_handler.h @@ -1,7 +1,7 @@ // Copyright (c) 2022 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/. +// you can obtain one at https://mozilla.org/MPL/2.0/. #ifndef BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_TOR_SNOWFLAKE_EXTENSION_HANDLER_H_ #define BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_TOR_SNOWFLAKE_EXTENSION_HANDLER_H_