From 4f9dcad8637bf0724554ea543eb843d31517d17b Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Mon, 25 Jan 2021 22:49:10 -0800 Subject: [PATCH 01/11] Add unstoppable domains support via DNS over HTTPS --- app/brave_generated_resources.grd | 3 ++ browser/BUILD.gn | 13 +++++ browser/brave_local_state_prefs.cc | 10 ++++ browser/extensions/BUILD.gn | 6 +++ .../api/settings_private/brave_prefs_util.cc | 11 ++++ browser/profiles/BUILD.gn | 2 +- browser/profiles/brave_profile_manager.cc | 8 +++ .../brave_default_extensions_page.html | 6 +++ .../brave_default_extensions_page.js | 13 ++++- browser/ui/BUILD.gn | 6 +++ ...stoppable_domains_service_delegate_impl.cc | 18 +++++++ ...nstoppable_domains_service_delegate_impl.h | 29 +++++++++++ .../unstoppable_domains_service_factory.cc | 45 +++++++++++++++++ .../unstoppable_domains_service_factory.h | 41 +++++++++++++++ .../net/stub_resolver_config_reader.cc | 47 +++++++++++++++++ .../settings_localized_strings_provider.cc | 2 + .../settings/settings_secure_dns_handler.cc | 50 +++++++++++++++++++ chromium_src/net/dns/dns_transaction.cc | 47 +++++++++++++++++ components/unstoppable_domains/BUILD.gn | 23 +++++++++ .../unstoppable_domains/buildflags/BUILD.gn | 7 +++ .../buildflags/buildflags.gni | 5 ++ components/unstoppable_domains/constants.cc | 14 ++++++ components/unstoppable_domains/constants.h | 22 ++++++++ components/unstoppable_domains/pref_names.cc | 16 ++++++ components/unstoppable_domains/pref_names.h | 15 ++++++ .../unstoppable_domains_service.cc | 45 +++++++++++++++++ .../unstoppable_domains_service.h | 49 ++++++++++++++++++ .../unstoppable_domains_service_delegate.h | 20 ++++++++ net/sources.gni | 5 ++ ...r-net-stub_resolver_config_reader.cc.patch | 12 +++++ ...tings-settings_secure_dns_handler.cc.patch | 13 +++++ patches/net-BUILD.gn.patch | 10 +++- patches/net-dns-dns_transaction.cc.patch | 12 +++++ 33 files changed, 622 insertions(+), 3 deletions(-) create mode 100644 browser/unstoppable_domains/unstoppable_domains_service_delegate_impl.cc create mode 100644 browser/unstoppable_domains/unstoppable_domains_service_delegate_impl.h create mode 100644 browser/unstoppable_domains/unstoppable_domains_service_factory.cc create mode 100644 browser/unstoppable_domains/unstoppable_domains_service_factory.h create mode 100644 chromium_src/chrome/browser/net/stub_resolver_config_reader.cc create mode 100644 chromium_src/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc create mode 100644 chromium_src/net/dns/dns_transaction.cc create mode 100644 components/unstoppable_domains/BUILD.gn create mode 100644 components/unstoppable_domains/buildflags/BUILD.gn create mode 100644 components/unstoppable_domains/buildflags/buildflags.gni create mode 100644 components/unstoppable_domains/constants.cc create mode 100644 components/unstoppable_domains/constants.h create mode 100644 components/unstoppable_domains/pref_names.cc create mode 100644 components/unstoppable_domains/pref_names.h create mode 100644 components/unstoppable_domains/unstoppable_domains_service.cc create mode 100644 components/unstoppable_domains/unstoppable_domains_service.h create mode 100644 components/unstoppable_domains/unstoppable_domains_service_delegate.h create mode 100644 patches/chrome-browser-net-stub_resolver_config_reader.cc.patch create mode 100644 patches/chrome-browser-ui-webui-settings-settings_secure_dns_handler.cc.patch create mode 100644 patches/net-dns-dns_transaction.cc.patch diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index b1fc3b0d743..10d6f8da5a3 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -694,6 +694,9 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U Allow Google login for extensions + + Method to resolve Unstoppable Domains + Method to resolve IPFS resources diff --git a/browser/BUILD.gn b/browser/BUILD.gn index 688213df4c4..b93014a5e13 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -16,6 +16,7 @@ import("//brave/components/ipfs/buildflags/buildflags.gni") import("//brave/components/sidebar/buildflags/buildflags.gni") import("//brave/components/speedreader/buildflags.gni") import("//brave/components/tor/buildflags/buildflags.gni") +import("//brave/components/unstoppable_domains/buildflags/buildflags.gni") import("//build/buildflag_header.gni") import("//build/config/features.gni") import("//chrome/common/features.gni") @@ -154,6 +155,7 @@ source_set("browser_process") { "//brave/components/sidebar/buildflags", "//brave/components/speedreader:buildflags", "//brave/components/tor/buildflags", + "//brave/components/unstoppable_domains/buildflags", "//brave/components/weekly_storage", "//brave/services/network/public/cpp", "//brave/ui/brave_ads", @@ -289,6 +291,17 @@ source_set("browser_process") { ] } + if (unstoppable_domains_enabled) { + sources += [ + "unstoppable_domains/unstoppable_domains_service_delegate_impl.cc", + "unstoppable_domains/unstoppable_domains_service_delegate_impl.h", + "unstoppable_domains/unstoppable_domains_service_factory.cc", + "unstoppable_domains/unstoppable_domains_service_factory.h", + ] + + deps += [ "//brave/components/unstoppable_domains" ] + } + if (ipfs_enabled) { sources += [ "ipfs/content_browser_client_helper.cc", diff --git a/browser/brave_local_state_prefs.cc b/browser/brave_local_state_prefs.cc index 42b92a71c26..667ee1120ca 100644 --- a/browser/brave_local_state_prefs.cc +++ b/browser/brave_local_state_prefs.cc @@ -18,6 +18,7 @@ #include "brave/components/p3a/brave_p3a_service.h" #include "brave/components/p3a/buildflags.h" #include "brave/components/tor/buildflags/buildflags.h" +#include "brave/components/unstoppable_domains/buildflags/buildflags.h" #include "chrome/common/pref_names.h" #include "components/metrics/metrics_pref_names.h" #include "components/prefs/pref_registry_simple.h" @@ -42,6 +43,10 @@ #include "brave/browser/widevine/widevine_utils.h" #endif +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) +#include "brave/components/unstoppable_domains/unstoppable_domains_service.h" +#endif + namespace brave { void RegisterLocalStatePrefsForMigration(PrefRegistrySimple* registry) { @@ -95,6 +100,11 @@ void RegisterLocalStatePrefs(PrefRegistrySimple* registry) { RegisterWidevineLocalstatePrefs(registry); #endif +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) + unstoppable_domains::UnstoppableDomainsService::RegisterLocalStatePrefs( + registry); +#endif + RegisterLocalStatePrefsForMigration(registry); } diff --git a/browser/extensions/BUILD.gn b/browser/extensions/BUILD.gn index 0a95f5484c9..fe2f08ba484 100644 --- a/browser/extensions/BUILD.gn +++ b/browser/extensions/BUILD.gn @@ -10,6 +10,7 @@ import("//brave/components/gemini/browser/buildflags/buildflags.gni") import("//brave/components/ipfs/buildflags/buildflags.gni") import("//brave/components/sidebar/buildflags/buildflags.gni") import("//brave/components/tor/buildflags/buildflags.gni") +import("//brave/components/unstoppable_domains/buildflags/buildflags.gni") import("//build/config/features.gni") import("//components/gcm_driver/config.gni") @@ -67,6 +68,7 @@ source_set("extensions") { "//brave/components/ipfs/buildflags", "//brave/components/sidebar/buildflags", "//brave/components/tor/buildflags", + "//brave/components/unstoppable_domains/buildflags", "//chrome/browser/extensions", "//chrome/common", "//components/gcm_driver:gcm_buildflags", @@ -111,6 +113,10 @@ source_set("extensions") { deps += [ "//brave/components/ipfs" ] } + if (unstoppable_domains_enabled) { + deps += [ "//brave/components/unstoppable_domains" ] + } + if (brave_rewards_enabled) { sources += [ "api/brave_rewards_api.cc", diff --git a/browser/extensions/api/settings_private/brave_prefs_util.cc b/browser/extensions/api/settings_private/brave_prefs_util.cc index a626ca1a3f6..682c4c2767b 100644 --- a/browser/extensions/api/settings_private/brave_prefs_util.cc +++ b/browser/extensions/api/settings_private/brave_prefs_util.cc @@ -14,6 +14,7 @@ #include "brave/components/ntp_background_images/common/pref_names.h" #include "brave/components/sidebar/buildflags/buildflags.h" #include "brave/components/tor/buildflags/buildflags.h" +#include "brave/components/unstoppable_domains/buildflags/buildflags.h" #include "chrome/browser/extensions/api/settings_private/prefs_util.h" #include "chrome/common/extensions/api/settings_private.h" #include "chrome/common/pref_names.h" @@ -45,6 +46,10 @@ #include "brave/components/tor/pref_names.h" #endif +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) +#include "brave/components/unstoppable_domains/pref_names.h" +#endif + namespace extensions { using ntp_background_images::prefs::kNewTabPageShowBackgroundImage; @@ -207,6 +212,12 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetAllowlistedKeys() { (*s_brave_allowlist)[prefs::kWebRTCIPHandlingPolicy] = settings_api::PrefType::PREF_TYPE_STRING; +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) + // Unstoppable Domains pref + (*s_brave_allowlist)[unstoppable_domains::kResolveMethod] = + settings_api::PrefType::PREF_TYPE_NUMBER; +#endif + return *s_brave_allowlist; } diff --git a/browser/profiles/BUILD.gn b/browser/profiles/BUILD.gn index b714402d246..43138a45ec7 100644 --- a/browser/profiles/BUILD.gn +++ b/browser/profiles/BUILD.gn @@ -33,11 +33,11 @@ source_set("profiles") { "//brave/components/brave_rewards/browser", "//brave/components/brave_sync:prefs", "//brave/components/brave_wallet/buildflags", - "//brave/components/brave_wallet/buildflags", "//brave/components/content_settings/core/browser", "//brave/components/ipfs/buildflags", "//brave/components/ntp_background_images/common", "//brave/components/tor", + "//brave/components/unstoppable_domains/buildflags", "//brave/content:browser", "//chrome/common", "//components/gcm_driver:gcm_buildflags", diff --git a/browser/profiles/brave_profile_manager.cc b/browser/profiles/brave_profile_manager.cc index 1c6d7a89524..64b7ddb880c 100644 --- a/browser/profiles/brave_profile_manager.cc +++ b/browser/profiles/brave_profile_manager.cc @@ -19,6 +19,7 @@ #include "brave/components/content_settings/core/browser/brave_content_settings_pref_provider.h" #include "brave/components/ipfs/buildflags/buildflags.h" #include "brave/components/tor/tor_constants.h" +#include "brave/components/unstoppable_domains/buildflags/buildflags.h" #include "brave/content/browser/webui/brave_shared_resources_data_source.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/chrome_notification_types.h" @@ -50,6 +51,10 @@ #include "brave/browser/ipfs/ipfs_service_factory.h" #endif +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) +#include "brave/browser/unstoppable_domains/unstoppable_domains_service_factory.h" +#endif + using content::BrowserThread; BraveProfileManager::BraveProfileManager(const base::FilePath& user_data_dir) @@ -108,6 +113,9 @@ void BraveProfileManager::DoFinalInitForServices(Profile* profile, #if BUILDFLAG(IPFS_ENABLED) ipfs::IpfsServiceFactory::GetForContext(profile); #endif +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) + unstoppable_domains::UnstoppableDomainsServiceFactory::GetForContext(profile); +#endif #if !BUILDFLAG(USE_GCM_FROM_PLATFORM) gcm::BraveGCMChannelStatus* status = gcm::BraveGCMChannelStatus::GetForProfile(profile); diff --git a/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html b/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html index c0d231e44e9..579fe7fbe7a 100644 --- a/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html +++ b/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html @@ -85,6 +85,12 @@ sub-label="$i18n{hangoutsEnabledDesc}" on-settings-boolean-control-change="onHangoutsEnabledChange_"> +
+
$i18n{resolveUnstoppableDomainsDesc}
+ +
UpdateNetworkService(false /* record_metrics */); +} + +} // namespace unstoppable_domains diff --git a/browser/unstoppable_domains/unstoppable_domains_service_delegate_impl.h b/browser/unstoppable_domains/unstoppable_domains_service_delegate_impl.h new file mode 100644 index 00000000000..aed7638b453 --- /dev/null +++ b/browser/unstoppable_domains/unstoppable_domains_service_delegate_impl.h @@ -0,0 +1,29 @@ +/* Copyright (c) 2021 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_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_SERVICE_DELEGATE_IMPL_H_ +#define BRAVE_BROWSER_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_SERVICE_DELEGATE_IMPL_H_ + +#include "brave/components/unstoppable_domains/unstoppable_domains_service_delegate.h" + +namespace unstoppable_domains { + +class UnstoppableDomainsServiceDelegateImpl + : public UnstoppableDomainsServiceDelegate { + public: + UnstoppableDomainsServiceDelegateImpl() = default; + ~UnstoppableDomainsServiceDelegateImpl() override = default; + + UnstoppableDomainsServiceDelegateImpl( + const UnstoppableDomainsServiceDelegateImpl&) = delete; + UnstoppableDomainsServiceDelegateImpl& operator=( + UnstoppableDomainsServiceDelegateImpl&) = delete; + + void UpdateNetworkService() override; +}; + +} // namespace unstoppable_domains + +#endif // BRAVE_BROWSER_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_SERVICE_DELEGATE_IMPL_H_ diff --git a/browser/unstoppable_domains/unstoppable_domains_service_factory.cc b/browser/unstoppable_domains/unstoppable_domains_service_factory.cc new file mode 100644 index 00000000000..909c1efc34f --- /dev/null +++ b/browser/unstoppable_domains/unstoppable_domains_service_factory.cc @@ -0,0 +1,45 @@ +/* Copyright (c) 2021 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/unstoppable_domains/unstoppable_domains_service_factory.h" + +#include + +#include "brave/browser/brave_browser_process_impl.h" +#include "brave/browser/unstoppable_domains/unstoppable_domains_service_delegate_impl.h" +#include "brave/components/unstoppable_domains/unstoppable_domains_service.h" +#include "components/keyed_service/content/browser_context_dependency_manager.h" + +namespace unstoppable_domains { + +UnstoppableDomainsServiceFactory::UnstoppableDomainsServiceFactory() + : BrowserContextKeyedServiceFactory( + "UnstoppableDomainsService", + BrowserContextDependencyManager::GetInstance()) {} + +UnstoppableDomainsServiceFactory::~UnstoppableDomainsServiceFactory() {} + +// static +UnstoppableDomainsServiceFactory* +UnstoppableDomainsServiceFactory::GetInstance() { + return base::Singleton::get(); +} + +// static +UnstoppableDomainsService* UnstoppableDomainsServiceFactory::GetForContext( + content::BrowserContext* context) { + return static_cast( + GetInstance()->GetServiceForBrowserContext(context, true)); +} + +KeyedService* UnstoppableDomainsServiceFactory::BuildServiceInstanceFor( + content::BrowserContext* context) const { + return new UnstoppableDomainsService( + std::make_unique(), context, + g_brave_browser_process ? g_brave_browser_process->local_state() + : nullptr); +} + +} // namespace unstoppable_domains diff --git a/browser/unstoppable_domains/unstoppable_domains_service_factory.h b/browser/unstoppable_domains/unstoppable_domains_service_factory.h new file mode 100644 index 00000000000..d64665f916e --- /dev/null +++ b/browser/unstoppable_domains/unstoppable_domains_service_factory.h @@ -0,0 +1,41 @@ +/* Copyright (c) 2021 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_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_SERVICE_FACTORY_H_ +#define BRAVE_BROWSER_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_SERVICE_FACTORY_H_ + +#include "base/memory/singleton.h" +#include "components/keyed_service/content/browser_context_keyed_service_factory.h" + +namespace unstoppable_domains { + +class UnstoppableDomainsService; + +class UnstoppableDomainsServiceFactory + : public BrowserContextKeyedServiceFactory { + public: + static UnstoppableDomainsService* GetForContext( + content::BrowserContext* context); + static UnstoppableDomainsServiceFactory* GetInstance(); + + private: + friend struct base::DefaultSingletonTraits; + + UnstoppableDomainsServiceFactory(); + ~UnstoppableDomainsServiceFactory() override; + + UnstoppableDomainsServiceFactory(const UnstoppableDomainsServiceFactory&) = + delete; + UnstoppableDomainsServiceFactory& operator=( + const UnstoppableDomainsServiceFactory&) = delete; + + // BrowserContextKeyedServiceFactory overrides: + KeyedService* BuildServiceInstanceFor( + content::BrowserContext* context) const override; +}; + +} // namespace unstoppable_domains + +#endif // BRAVE_BROWSER_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_SERVICE_FACTORY_H_ diff --git a/chromium_src/chrome/browser/net/stub_resolver_config_reader.cc b/chromium_src/chrome/browser/net/stub_resolver_config_reader.cc new file mode 100644 index 00000000000..916042f120e --- /dev/null +++ b/chromium_src/chrome/browser/net/stub_resolver_config_reader.cc @@ -0,0 +1,47 @@ +/* Copyright (c) 2021 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/strings/strcat.h" +#include "brave/components/unstoppable_domains/buildflags/buildflags.h" +#include "components/prefs/pref_service.h" + +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) +#include "brave/components/unstoppable_domains/constants.h" +#include "brave/components/unstoppable_domains/pref_names.h" +#endif + +namespace { + +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) +void AddUnstoppableDomainsResolver(std::string* doh_templates, + PrefService* local_state) { + bool resolve_ud_enabled = + local_state->FindPreference(unstoppable_domains::kResolveMethod) && + local_state->GetInteger(unstoppable_domains::kResolveMethod) == + static_cast( + unstoppable_domains::ResolveMethodTypes::DNS_OVER_HTTPS); + + if (resolve_ud_enabled && + doh_templates->find(unstoppable_domains::kDoHResolver) == + std::string::npos) { + *doh_templates = + base::StrCat({unstoppable_domains::kDoHResolver, " ", *doh_templates}); + } +} +#endif // BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) + +void AddDoHServers(std::string* doh_templates, PrefService* local_state) { +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) + AddUnstoppableDomainsResolver(doh_templates, local_state); +#endif +} + +} // namespace + +#define BRAVE_GET_AND_UPDATE_CONFIGURATION \ + AddDoHServers(&doh_templates, local_state_); + +#include "../../../../../chrome/browser/net/stub_resolver_config_reader.cc" +#undef BRAVE_GET_AND_UPDATE_CONFIGURATION diff --git a/chromium_src/chrome/browser/ui/webui/settings/settings_localized_strings_provider.cc b/chromium_src/chrome/browser/ui/webui/settings/settings_localized_strings_provider.cc index 4d38033d27d..4a0281ce541 100644 --- a/chromium_src/chrome/browser/ui/webui/settings/settings_localized_strings_provider.cc +++ b/chromium_src/chrome/browser/ui/webui/settings/settings_localized_strings_provider.cc @@ -219,6 +219,8 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source, IDS_SETTINGS_LOAD_CRYPTO_WALLETS_ON_STARTUP}, {"googleLoginForExtensionsDesc", IDS_SETTINGS_GOOGLE_LOGIN_FOR_EXTENSIONS}, {"hangoutsEnabledDesc", IDS_SETTINGS_HANGOUTS_ENABLED_DESC}, + {"resolveUnstoppableDomainsDesc", + IDS_SETTINGS_RESOLVE_UNSTOPPABLE_DOMAINS_DESC}, {"resolveIPFSURLDesc", IDS_SETTINGS_RESOLVE_IPFS_URLS_DESC}, {"ipfsPublicGatewayDesc", IDS_SETTINGS_IPFS_PUBLIC_GATEWAY_DESC}, {"ipfsChangeGatewayButtonLabel", diff --git a/chromium_src/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc b/chromium_src/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc new file mode 100644 index 00000000000..4b7687839f3 --- /dev/null +++ b/chromium_src/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc @@ -0,0 +1,50 @@ +/* Copyright (c) 2021 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/values.h" +#include "brave/components/unstoppable_domains/buildflags/buildflags.h" + +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) +#include "brave/components/unstoppable_domains/constants.h" +#endif + +namespace settings { +namespace { +std::unique_ptr CreateSecureDnsSettingDict(); +} // namespace +} // namespace settings + +#include "../../../../../../../chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc" + +namespace settings { + +namespace { + +// Hide Unstoppable Domains resolver in the custom provider list in settings +// because it will be used for name resolution only for TLDs from Unstoppable +// Domains, instaed of a global DoH settings. +std::unique_ptr CreateSecureDnsSettingDict() { + auto dict = CreateSecureDnsSettingDict_ChromiumImpl(); +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) + if (!dict->FindListPath("templates")) { + return dict; + } + + auto secure_dns_templates = std::make_unique(); + for (const auto& template_str : dict->FindListPath("templates")->GetList()) { + if (!template_str.is_string()) + return dict; + if (template_str.GetString() != unstoppable_domains::kDoHResolver) + secure_dns_templates->Append(template_str.GetString()); + } + + dict->SetList("templates", std::move(secure_dns_templates)); +#endif // BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) + return dict; +} + +} // namespace + +} // namespace settings diff --git a/chromium_src/net/dns/dns_transaction.cc b/chromium_src/net/dns/dns_transaction.cc new file mode 100644 index 00000000000..085f7326703 --- /dev/null +++ b/chromium_src/net/dns/dns_transaction.cc @@ -0,0 +1,47 @@ +/* Copyright (c) 2021 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/strings/string_util.h" +#include "brave/components/unstoppable_domains/buildflags/buildflags.h" +#include "net/dns/dns_config.h" +#include "net/dns/dns_server_iterator.h" + +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) +#include "brave/components/unstoppable_domains/constants.h" +#endif + +namespace { + +bool GetNextIndex(const std::string& hostname, + const net::DnsConfig& config, + net::DnsServerIterator* dns_server_iterator, + size_t* doh_server_index) { +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) + // Skip unstoppable domains resolver for non-crypto domains. + if (!base::EndsWith(hostname, unstoppable_domains::kCryptoDomain) && + config.dns_over_https_servers[*doh_server_index].server_template == + unstoppable_domains::kDoHResolver) { + // No next available index to attempt. + if (!dns_server_iterator->AttemptAvailable()) { + return false; + } + + *doh_server_index = dns_server_iterator->GetNextAttemptIndex(); + } +#endif + + return true; +} + +} // namespace + +#define BRAVE_MAKE_HTTP_ATTEMPT \ + if (!GetNextIndex(hostname_, session_.get()->config(), \ + dns_server_iterator_.get(), &doh_server_index)) { \ + return AttemptResult(ERR_BLOCKED_BY_CLIENT, nullptr); \ + } + +#include "../../../../net/dns/dns_transaction.cc" +#undef BRAVE_MAKE_HTTP_ATTEMPT diff --git a/components/unstoppable_domains/BUILD.gn b/components/unstoppable_domains/BUILD.gn new file mode 100644 index 00000000000..dd9e614ee5c --- /dev/null +++ b/components/unstoppable_domains/BUILD.gn @@ -0,0 +1,23 @@ +source_set("constants") { + sources = [ + "constants.cc", + "constants.h", + ] +} + +source_set("unstoppable_domains") { + sources = [ + "pref_names.cc", + "pref_names.h", + "unstoppable_domains_service.cc", + "unstoppable_domains_service.h", + "unstoppable_domains_service_delegate.h", + ] + + public_deps = [ ":constants" ] + + deps = [ + "//components/keyed_service/core", + "//components/prefs", + ] +} diff --git a/components/unstoppable_domains/buildflags/BUILD.gn b/components/unstoppable_domains/buildflags/BUILD.gn new file mode 100644 index 00000000000..9df0a982d31 --- /dev/null +++ b/components/unstoppable_domains/buildflags/BUILD.gn @@ -0,0 +1,7 @@ +import("//brave/components/unstoppable_domains/buildflags/buildflags.gni") +import("//build/buildflag_header.gni") + +buildflag_header("buildflags") { + header = "buildflags.h" + flags = [ "UNSTOPPABLE_DOMAINS_ENABLED=$unstoppable_domains_enabled" ] +} diff --git a/components/unstoppable_domains/buildflags/buildflags.gni b/components/unstoppable_domains/buildflags/buildflags.gni new file mode 100644 index 00000000000..dda7786ea2e --- /dev/null +++ b/components/unstoppable_domains/buildflags/buildflags.gni @@ -0,0 +1,5 @@ +import("//build/config/features.gni") + +declare_args() { + unstoppable_domains_enabled = is_mac || is_linux || is_win +} diff --git a/components/unstoppable_domains/constants.cc b/components/unstoppable_domains/constants.cc new file mode 100644 index 00000000000..a826878b5f5 --- /dev/null +++ b/components/unstoppable_domains/constants.cc @@ -0,0 +1,14 @@ +/* Copyright (c) 2021 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/components/unstoppable_domains/constants.h" + +namespace unstoppable_domains { + +const char kCryptoDomain[] = ".crypto"; +const char kDoHResolver[] = + "https://resolver.unstoppable.io/dns-query{?brave_UD}"; + +} // namespace unstoppable_domains diff --git a/components/unstoppable_domains/constants.h b/components/unstoppable_domains/constants.h new file mode 100644 index 00000000000..5f6042d1a5e --- /dev/null +++ b/components/unstoppable_domains/constants.h @@ -0,0 +1,22 @@ +/* Copyright (c) 2021 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_COMPONENTS_UNSTOPPABLE_DOMAINS_CONSTANTS_H_ +#define BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_CONSTANTS_H_ + +namespace unstoppable_domains { + +extern const char kCryptoDomain[]; +extern const char kDoHResolver[]; + +enum class ResolveMethodTypes { + ASK, + DISABLED, + DNS_OVER_HTTPS, +}; + +} // namespace unstoppable_domains + +#endif // BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_CONSTANTS_H_ diff --git a/components/unstoppable_domains/pref_names.cc b/components/unstoppable_domains/pref_names.cc new file mode 100644 index 00000000000..cfe58f0a36d --- /dev/null +++ b/components/unstoppable_domains/pref_names.cc @@ -0,0 +1,16 @@ +/* Copyright (c) 2021 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/components/unstoppable_domains/pref_names.h" + +namespace unstoppable_domains { + +// Used to determine which method should be used to resolve unstoppable +// domains, between: +// Disabled: Disable all unstoppable domains resolution. +// DNS Over HTTPS: Resolve domain name using a public DNS over HTTPS server. +const char kResolveMethod[] = "brave.unstoppable_domains.resolve_method"; + +} // namespace unstoppable_domains diff --git a/components/unstoppable_domains/pref_names.h b/components/unstoppable_domains/pref_names.h new file mode 100644 index 00000000000..31b1265805a --- /dev/null +++ b/components/unstoppable_domains/pref_names.h @@ -0,0 +1,15 @@ +/* Copyright (c) 2021 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_COMPONENTS_UNSTOPPABLE_DOMAINS_PREF_NAMES_H_ +#define BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_PREF_NAMES_H_ + +namespace unstoppable_domains { + +extern const char kResolveMethod[]; + +} // namespace unstoppable_domains + +#endif // BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_PREF_NAMES_H_ diff --git a/components/unstoppable_domains/unstoppable_domains_service.cc b/components/unstoppable_domains/unstoppable_domains_service.cc new file mode 100644 index 00000000000..06e80ba4a1f --- /dev/null +++ b/components/unstoppable_domains/unstoppable_domains_service.cc @@ -0,0 +1,45 @@ +/* Copyright (c) 2021 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/components/unstoppable_domains/unstoppable_domains_service.h" + +#include + +#include "brave/components/unstoppable_domains/constants.h" +#include "brave/components/unstoppable_domains/pref_names.h" +#include "brave/components/unstoppable_domains/unstoppable_domains_service_delegate.h" +#include "components/prefs/pref_change_registrar.h" +#include "components/prefs/pref_registry_simple.h" +#include "components/prefs/pref_service.h" + +namespace unstoppable_domains { + +UnstoppableDomainsService::UnstoppableDomainsService( + std::unique_ptr delegate, + content::BrowserContext* context, + PrefService* local_state) + : local_state_(local_state), delegate_(std::move(delegate)) { + pref_change_registrar_ = std::make_unique(); + pref_change_registrar_->Init(local_state_); + pref_change_registrar_->Add( + kResolveMethod, + base::Bind(&UnstoppableDomainsService::OnPreferenceChanged, + base::Unretained(this))); +} + +UnstoppableDomainsService::~UnstoppableDomainsService() = default; + +// static +void UnstoppableDomainsService::RegisterLocalStatePrefs( + PrefRegistrySimple* registry) { + registry->RegisterIntegerPref(kResolveMethod, + static_cast(ResolveMethodTypes::ASK)); +} + +void UnstoppableDomainsService::OnPreferenceChanged() { + delegate_->UpdateNetworkService(); +} + +} // namespace unstoppable_domains diff --git a/components/unstoppable_domains/unstoppable_domains_service.h b/components/unstoppable_domains/unstoppable_domains_service.h new file mode 100644 index 00000000000..e07d0ac086f --- /dev/null +++ b/components/unstoppable_domains/unstoppable_domains_service.h @@ -0,0 +1,49 @@ +/* Copyright (c) 2021 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_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_SERVICE_H_ +#define BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_SERVICE_H_ + +#include + +#include "components/keyed_service/core/keyed_service.h" + +namespace content { +class BrowserContext; +} // namespace content + +class PrefChangeRegistrar; +class PrefRegistrySimple; +class PrefService; + +namespace unstoppable_domains { + +class UnstoppableDomainsServiceDelegate; + +class UnstoppableDomainsService : public KeyedService { + public: + UnstoppableDomainsService( + std::unique_ptr delegate, + content::BrowserContext* context, + PrefService* local_state); + ~UnstoppableDomainsService() override; + + UnstoppableDomainsService(const UnstoppableDomainsService&) = delete; + UnstoppableDomainsService& operator=(const UnstoppableDomainsService&) = + delete; + + static void RegisterLocalStatePrefs(PrefRegistrySimple* registry); + + private: + void OnPreferenceChanged(); + + PrefService* local_state_; + std::unique_ptr pref_change_registrar_; + std::unique_ptr delegate_; +}; + +} // namespace unstoppable_domains + +#endif // BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_SERVICE_H_ diff --git a/components/unstoppable_domains/unstoppable_domains_service_delegate.h b/components/unstoppable_domains/unstoppable_domains_service_delegate.h new file mode 100644 index 00000000000..91f2726ffb9 --- /dev/null +++ b/components/unstoppable_domains/unstoppable_domains_service_delegate.h @@ -0,0 +1,20 @@ +/* Copyright (c) 2021 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_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_SERVICE_DELEGATE_H_ +#define BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_SERVICE_DELEGATE_H_ + +namespace unstoppable_domains { + +class UnstoppableDomainsServiceDelegate { + public: + UnstoppableDomainsServiceDelegate() = default; + virtual ~UnstoppableDomainsServiceDelegate() = default; + virtual void UpdateNetworkService() = 0; +}; + +} // namespace unstoppable_domains + +#endif // BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_SERVICE_DELEGATE_H_ diff --git a/net/sources.gni b/net/sources.gni index 784143f6efc..d027ca3edb3 100644 --- a/net/sources.gni +++ b/net/sources.gni @@ -7,3 +7,8 @@ brave_net_sources = [ "//brave/net/proxy_resolution/proxy_config_service_tor.cc", "//brave/net/proxy_resolution/proxy_config_service_tor.h", ] + +brave_net_deps = [ + "//brave/components/unstoppable_domains:constants", + "//brave/components/unstoppable_domains/buildflags", +] diff --git a/patches/chrome-browser-net-stub_resolver_config_reader.cc.patch b/patches/chrome-browser-net-stub_resolver_config_reader.cc.patch new file mode 100644 index 00000000000..5310997be15 --- /dev/null +++ b/patches/chrome-browser-net-stub_resolver_config_reader.cc.patch @@ -0,0 +1,12 @@ +diff --git a/chrome/browser/net/stub_resolver_config_reader.cc b/chrome/browser/net/stub_resolver_config_reader.cc +index 677f6c50fc602a1c5ba6ca52f4ea6496d7d60e7f..d736b6e06fcbee8fb17e3397cee86f3dbe09ad8e 100644 +--- a/chrome/browser/net/stub_resolver_config_reader.cc ++++ b/chrome/browser/net/stub_resolver_config_reader.cc +@@ -353,6 +353,7 @@ SecureDnsConfig StubResolverConfigReader::GetAndUpdateConfiguration( + + std::string doh_templates = + local_state_->GetString(prefs::kDnsOverHttpsTemplates); ++ BRAVE_GET_AND_UPDATE_CONFIGURATION + std::string server_method; + std::vector dns_over_https_servers; + base::Optional> diff --git a/patches/chrome-browser-ui-webui-settings-settings_secure_dns_handler.cc.patch b/patches/chrome-browser-ui-webui-settings-settings_secure_dns_handler.cc.patch new file mode 100644 index 00000000000..cec34e0b85f --- /dev/null +++ b/patches/chrome-browser-ui-webui-settings-settings_secure_dns_handler.cc.patch @@ -0,0 +1,13 @@ +diff --git a/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc b/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc +index 36ddfefcbc527e43d7efcb2210614b9df8c9919f..399f34639a082ea071801de7bc89ebbff275d652 100644 +--- a/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc ++++ b/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc +@@ -33,7 +33,7 @@ namespace settings { + + namespace { + +-std::unique_ptr CreateSecureDnsSettingDict() { ++std::unique_ptr CreateSecureDnsSettingDict_ChromiumImpl() { + // Fetch the current host resolver configuration. It is not sufficient to read + // the secure DNS prefs directly since the host resolver configuration takes + // other factors into account such as whether a managed environment or diff --git a/patches/net-BUILD.gn.patch b/patches/net-BUILD.gn.patch index da3168afa28..fd17d3048e6 100644 --- a/patches/net-BUILD.gn.patch +++ b/patches/net-BUILD.gn.patch @@ -1,5 +1,5 @@ diff --git a/net/BUILD.gn b/net/BUILD.gn -index d214ac4959ff76dd8e95d327c9839ee967d14444..292ce4d3c2c294be04cc8d7ba8ed5ac06e2d58f4 100644 +index d214ac4959ff76dd8e95d327c9839ee967d14444..709ca7acfa482e5d85f2e4207bbbe2d93dd61fee 100644 --- a/net/BUILD.gn +++ b/net/BUILD.gn @@ -1176,6 +1176,7 @@ component("net") { @@ -10,3 +10,11 @@ index d214ac4959ff76dd8e95d327c9839ee967d14444..292ce4d3c2c294be04cc8d7ba8ed5ac0 if (is_android) { sources += [ +@@ -1695,6 +1696,7 @@ source_set("net_deps") { + ] + + public_configs = net_configs ++ public_deps += brave_net_deps + + if (!is_nacl) { + public_deps += [ diff --git a/patches/net-dns-dns_transaction.cc.patch b/patches/net-dns-dns_transaction.cc.patch new file mode 100644 index 00000000000..db1e6f6f25a --- /dev/null +++ b/patches/net-dns-dns_transaction.cc.patch @@ -0,0 +1,12 @@ +diff --git a/net/dns/dns_transaction.cc b/net/dns/dns_transaction.cc +index 9bfd4cace384ca502716efa191eaa6ccfd6a54a8..9123e8cb94b566ef1a25cbc498dfc859e4165e73 100644 +--- a/net/dns/dns_transaction.cc ++++ b/net/dns/dns_transaction.cc +@@ -1299,6 +1299,7 @@ class DnsTransactionImpl : public DnsTransaction, + DCHECK(secure_); + + size_t doh_server_index = dns_server_iterator_->GetNextAttemptIndex(); ++ BRAVE_MAKE_HTTP_ATTEMPT + + unsigned attempt_number = attempts_.size(); + ConstructDnsHTTPAttempt( From edbf029b6e20080db22a58a7a7d3ab0861c4d37b Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Mon, 1 Feb 2021 22:17:59 -0800 Subject: [PATCH 02/11] Recognize .crypto as known TLD --- .../net/base/lookup_string_in_fixed_set.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/chromium_src/net/base/lookup_string_in_fixed_set.cc b/chromium_src/net/base/lookup_string_in_fixed_set.cc index 9327fb71d6e..a7df828e996 100644 --- a/chromium_src/net/base/lookup_string_in_fixed_set.cc +++ b/chromium_src/net/base/lookup_string_in_fixed_set.cc @@ -10,6 +10,11 @@ #undef LookupSuffixInReversedSet #include "base/strings/string_util.h" +#include "brave/components/unstoppable_domains/buildflags/buildflags.h" + +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) +#include "brave/components/unstoppable_domains/constants.h" +#endif namespace net { @@ -40,6 +45,17 @@ int LookupSuffixInReversedSet(const unsigned char* graph, return kDafsaFound; } + // Recognize .crypto as a known TLD for unstoppable domains support. With + // this, when users type *.crypto in omnibox, it will be parsed as + // OmniboxInputType::URL input type instead of OmniboxInputType::UNKNOWN, + // The first entry in the autocomplete list will be URL instead of search. +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) + if (base::EndsWith(host, unstoppable_domains::kCryptoDomain)) { + *suffix_length = strlen(unstoppable_domains::kCryptoDomain) - 1; + return kDafsaFound; + } +#endif + return LookupSuffixInReversedSet_ChromiumImpl(graph, length, include_private, host, suffix_length); } From f2721748bf1fc7e9fb223e01516e5d1cda617bc7 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Thu, 28 Jan 2021 16:18:08 -0800 Subject: [PATCH 03/11] Add unstoppable domains opt-in interstitial page --- browser/brave_content_browser_client.cc | 22 +++- components/resources/BUILD.gn | 3 + .../resources/brave_components_resources.grd | 1 + .../resources/brave_components_strings.grd | 1 + .../unstoppable_domains_resources.grdp | 8 ++ .../unstoppable_domains_strings.grdp | 20 ++++ components/unstoppable_domains/BUILD.gn | 22 ++++ .../unstoppable_domains_interstitial.css | 14 +++ .../unstoppable_domains_interstitial.html | 31 +++++ .../unstoppable_domains_interstitial.js | 21 ++++ ..._domains_interstitial_controller_client.cc | 59 ++++++++++ ...e_domains_interstitial_controller_client.h | 60 ++++++++++ ...unstoppable_domains_navigation_throttle.cc | 98 ++++++++++++++++ .../unstoppable_domains_navigation_throttle.h | 63 ++++++++++ .../unstoppable_domains_opt_in_page.cc | 111 ++++++++++++++++++ .../unstoppable_domains_opt_in_page.h | 58 +++++++++ .../unstoppable_domains_service.cc | 7 +- .../unstoppable_domains_service.h | 2 + components/unstoppable_domains/utils.cc | 18 +++ components/unstoppable_domains/utils.h | 17 +++ 20 files changed, 634 insertions(+), 2 deletions(-) create mode 100644 components/resources/unstoppable_domains_resources.grdp create mode 100644 components/resources/unstoppable_domains_strings.grdp create mode 100644 components/unstoppable_domains/resources/unstoppable_domains_interstitial.css create mode 100644 components/unstoppable_domains/resources/unstoppable_domains_interstitial.html create mode 100644 components/unstoppable_domains/resources/unstoppable_domains_interstitial.js create mode 100644 components/unstoppable_domains/unstoppable_domains_interstitial_controller_client.cc create mode 100644 components/unstoppable_domains/unstoppable_domains_interstitial_controller_client.h create mode 100644 components/unstoppable_domains/unstoppable_domains_navigation_throttle.cc create mode 100644 components/unstoppable_domains/unstoppable_domains_navigation_throttle.h create mode 100644 components/unstoppable_domains/unstoppable_domains_opt_in_page.cc create mode 100644 components/unstoppable_domains/unstoppable_domains_opt_in_page.h create mode 100644 components/unstoppable_domains/utils.cc create mode 100644 components/unstoppable_domains/utils.h diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 123c3d79ad6..355b31a01fa 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -35,6 +35,7 @@ #include "brave/components/ipfs/buildflags/buildflags.h" #include "brave/components/speedreader/buildflags.h" #include "brave/components/tor/buildflags/buildflags.h" +#include "brave/components/unstoppable_domains/buildflags/buildflags.h" #include "brave/grit/brave_generated_resources.h" #include "chrome/browser/chrome_content_browser_client.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h" @@ -88,6 +89,11 @@ using extensions::ChromeContentBrowserClientExtensionsPart; #include "brave/components/ipfs/ipfs_navigation_throttle.h" #endif +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) +#include "brave/browser/unstoppable_domains/unstoppable_domains_service_factory.h" +#include "brave/components/unstoppable_domains/unstoppable_domains_navigation_throttle.h" +#endif + #if BUILDFLAG(BRAVE_REWARDS_ENABLED) #include "brave/components/brave_rewards/browser/rewards_protocol_handler.h" #endif @@ -585,7 +591,8 @@ BraveContentBrowserClient::CreateThrottlesForNavigation( std::make_unique(handle)); #endif -#if BUILDFLAG(ENABLE_TOR) ||BUILDFLAG(IPFS_ENABLED) +#if BUILDFLAG(ENABLE_TOR) || BUILDFLAG(IPFS_ENABLED) || \ + BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) content::BrowserContext* context = handle->GetWebContents()->GetBrowserContext(); #endif @@ -618,5 +625,18 @@ BraveContentBrowserClient::CreateThrottlesForNavigation( throttles.push_back(std::move(ipfs_navigation_throttle)); #endif +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) + std::unique_ptr + unstoppable_domains_navigation_throttle = unstoppable_domains:: + UnstoppableDomainsNavigationThrottle::MaybeCreateThrottleFor( + handle, + unstoppable_domains::UnstoppableDomainsServiceFactory:: + GetForContext(context), + g_brave_browser_process->local_state(), + g_brave_browser_process->GetApplicationLocale()); + if (unstoppable_domains_navigation_throttle) + throttles.push_back(std::move(unstoppable_domains_navigation_throttle)); +#endif + return throttles; } diff --git a/components/resources/BUILD.gn b/components/resources/BUILD.gn index 810d3c6c738..ae513bcb7e2 100644 --- a/components/resources/BUILD.gn +++ b/components/resources/BUILD.gn @@ -7,6 +7,7 @@ import("//brave/components/ipfs/buildflags/buildflags.gni") import("//brave/components/sidebar/buildflags/buildflags.gni") import("//brave/components/speedreader/buildflags.gni") import("//brave/components/tor/buildflags/buildflags.gni") +import("//brave/components/unstoppable_domains/buildflags/buildflags.gni") import("//build/config/locales.gni") import("//extensions/buildflags/buildflags.gni") import("//tools/grit/grit_rule.gni") @@ -42,6 +43,7 @@ grit("static_resources") { "enable_speedreader=$enable_speedreader", "ipfs_enabled=$ipfs_enabled", "crypto_dot_com_enabled=$crypto_dot_com_enabled", + "unstoppable_domains_enabled=$unstoppable_domains_enabled", ] grit_flags = [ @@ -95,6 +97,7 @@ grit("strings") { defines = [ "enable_speedreader=$enable_speedreader", "ipfs_enabled=$ipfs_enabled", + "unstoppable_domains_enabled=$unstoppable_domains_enabled", "crypto_dot_com_enabled=$crypto_dot_com_enabled", "enable_tor=$enable_tor", "enable_sidebar=$enable_sidebar", diff --git a/components/resources/brave_components_resources.grd b/components/resources/brave_components_resources.grd index 89f2c529fa0..2924602c0d6 100644 --- a/components/resources/brave_components_resources.grd +++ b/components/resources/brave_components_resources.grd @@ -63,6 +63,7 @@ + diff --git a/components/resources/brave_components_strings.grd b/components/resources/brave_components_strings.grd index e40b491a635..36c711a6c9f 100644 --- a/components/resources/brave_components_strings.grd +++ b/components/resources/brave_components_strings.grd @@ -1090,6 +1090,7 @@ I understand + diff --git a/components/resources/unstoppable_domains_resources.grdp b/components/resources/unstoppable_domains_resources.grdp new file mode 100644 index 00000000000..79d5a2b704f --- /dev/null +++ b/components/resources/unstoppable_domains_resources.grdp @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/components/resources/unstoppable_domains_strings.grdp b/components/resources/unstoppable_domains_strings.grdp new file mode 100644 index 00000000000..8d1a3b30d14 --- /dev/null +++ b/components/resources/unstoppable_domains_strings.grdp @@ -0,0 +1,20 @@ + + + + + Unstoppable Domains + + + Enable support of Unstoppable Domains in Brave? + + + Brave can enable you to visit .crypto domains by using Cloudflare to resolve .crypto domain name lookup requests using DNS over HTTPS. If you enable this, Cloudflare will see the .crypto domain that you're trying to visit but they will not be able to see other domains. See Cloudflare's $1terms of use$2 and $3privacy policy$4. + + + Proceed using Cloudflare server + + + Disable + + + diff --git a/components/unstoppable_domains/BUILD.gn b/components/unstoppable_domains/BUILD.gn index dd9e614ee5c..a11babaea4f 100644 --- a/components/unstoppable_domains/BUILD.gn +++ b/components/unstoppable_domains/BUILD.gn @@ -1,3 +1,8 @@ +# Copyright (c) 2021 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/. */ + source_set("constants") { sources = [ "constants.cc", @@ -9,15 +14,32 @@ source_set("unstoppable_domains") { sources = [ "pref_names.cc", "pref_names.h", + "unstoppable_domains_interstitial_controller_client.cc", + "unstoppable_domains_interstitial_controller_client.h", + "unstoppable_domains_navigation_throttle.cc", + "unstoppable_domains_navigation_throttle.h", + "unstoppable_domains_opt_in_page.cc", + "unstoppable_domains_opt_in_page.h", "unstoppable_domains_service.cc", "unstoppable_domains_service.h", "unstoppable_domains_service_delegate.h", + "utils.cc", + "utils.h", ] public_deps = [ ":constants" ] deps = [ + "//base", + "//brave/components/resources:static_resources", + "//brave/components/resources:strings", "//components/keyed_service/core", "//components/prefs", + "//components/security_interstitials/content:security_interstitial_page", + "//components/security_interstitials/core", + "//components/user_prefs", + "//content/public/browser", + "//net", + "//url", ] } diff --git a/components/unstoppable_domains/resources/unstoppable_domains_interstitial.css b/components/unstoppable_domains/resources/unstoppable_domains_interstitial.css new file mode 100644 index 00000000000..0d0524870d7 --- /dev/null +++ b/components/unstoppable_domains/resources/unstoppable_domains_interstitial.css @@ -0,0 +1,14 @@ +.unstoppable_domains #main-content a { + color: var(--google-blue-700); + text-decoration: none; +} + +.unstoppable_domains button { + background: var(--primary-button-fill-color); +} + +.unstoppable_domains .icon { + background-image: -webkit-image-set( + url(../../../../components/security_interstitials/core/browser/resources/images/1x/info.png) 1x, + url(../../../../components/security_interstitials/core/browser/resources/images/2x/info.png) 2x); +} diff --git a/components/unstoppable_domains/resources/unstoppable_domains_interstitial.html b/components/unstoppable_domains/resources/unstoppable_domains_interstitial.html new file mode 100644 index 00000000000..7206962d899 --- /dev/null +++ b/components/unstoppable_domains/resources/unstoppable_domains_interstitial.html @@ -0,0 +1,31 @@ + + + + + $i18n{tabTitle} + + + + + + + + + +
+
+
+
+

$i18nRaw{heading}

+

$i18nRaw{primaryParagraph}

+
+
+ +
+ diff --git a/components/unstoppable_domains/resources/unstoppable_domains_interstitial.js b/components/unstoppable_domains/resources/unstoppable_domains_interstitial.js new file mode 100644 index 00000000000..08dc2bdd3db --- /dev/null +++ b/components/unstoppable_domains/resources/unstoppable_domains_interstitial.js @@ -0,0 +1,21 @@ +// Copyright 2021 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/. + +function setupEvents() { + $('body').classList.add('unstoppable_domains'); + $('icon').classList.add('icon'); + + $('primary-button').addEventListener('click', function() { + sendCommand(SecurityInterstitialCommandId.CMD_PROCEED); + }); + + $('main-content').classList.remove(HIDDEN_CLASS); + + $('dont-proceed-button').addEventListener('click', function(event) { + sendCommand(SecurityInterstitialCommandId.CMD_DONT_PROCEED); + }); +} + +document.addEventListener('DOMContentLoaded', setupEvents); diff --git a/components/unstoppable_domains/unstoppable_domains_interstitial_controller_client.cc b/components/unstoppable_domains/unstoppable_domains_interstitial_controller_client.cc new file mode 100644 index 00000000000..4ad97f42235 --- /dev/null +++ b/components/unstoppable_domains/unstoppable_domains_interstitial_controller_client.cc @@ -0,0 +1,59 @@ +/* Copyright (c) 2021 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/components/unstoppable_domains/unstoppable_domains_interstitial_controller_client.h" + +#include "brave/components/unstoppable_domains/constants.h" +#include "brave/components/unstoppable_domains/pref_names.h" +#include "components/prefs/pref_service.h" +#include "components/security_interstitials/content/settings_page_helper.h" +#include "components/security_interstitials/core/metrics_helper.h" + +namespace unstoppable_domains { + +// static +std::unique_ptr +UnstoppableDomainsInterstitialControllerClient::GetMetricsHelper( + const GURL& url) { + security_interstitials::MetricsHelper::ReportDetails report_details; + report_details.metric_prefix = "UnstoppableDomains"; + + return std::make_unique( + url, report_details, nullptr); +} + +UnstoppableDomainsInterstitialControllerClient:: + UnstoppableDomainsInterstitialControllerClient( + content::WebContents* web_contents, + const GURL& request_url, + PrefService* user_prefs, + PrefService* local_state, + const std::string& locale) + : security_interstitials::SecurityInterstitialControllerClient( + web_contents, + GetMetricsHelper(request_url), + user_prefs, + locale, + GURL("about:blank") /* default_safe_page */, + nullptr /* settings_page_helper */), + request_url_(request_url), + local_state_(local_state) {} + +void UnstoppableDomainsInterstitialControllerClient::Proceed() { + SetResolveMethodAndReload(ResolveMethodTypes::DNS_OVER_HTTPS); +} + +void UnstoppableDomainsInterstitialControllerClient::DontProceed() { + SetResolveMethodAndReload(ResolveMethodTypes::DISABLED); +} + +void UnstoppableDomainsInterstitialControllerClient::SetResolveMethodAndReload( + ResolveMethodTypes type) { + DCHECK(local_state_); + local_state_->SetInteger(kResolveMethod, static_cast(type)); + Reload(); +} + +} // namespace unstoppable_domains diff --git a/components/unstoppable_domains/unstoppable_domains_interstitial_controller_client.h b/components/unstoppable_domains/unstoppable_domains_interstitial_controller_client.h new file mode 100644 index 00000000000..93947abbdb2 --- /dev/null +++ b/components/unstoppable_domains/unstoppable_domains_interstitial_controller_client.h @@ -0,0 +1,60 @@ +/* Copyright (c) 2021 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_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_INTERSTITIAL_CONTROLLER_CLIENT_H_ +#define BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_INTERSTITIAL_CONTROLLER_CLIENT_H_ + +#include +#include + +#include "components/security_interstitials/content/security_interstitial_controller_client.h" +#include "url/gurl.h" + +namespace content { +class WebContents; +} // namespace content + +namespace security_interstitials { +class MetricsHelper; +} // namespace security_interstitials + +class PrefService; + +namespace unstoppable_domains { +enum class ResolveMethodTypes; + +class UnstoppableDomainsInterstitialControllerClient + : public security_interstitials::SecurityInterstitialControllerClient { + public: + static std::unique_ptr + GetMetricsHelper(const GURL& url); + + UnstoppableDomainsInterstitialControllerClient( + content::WebContents* web_contents, + const GURL& request_url, + PrefService* user_prefs, + PrefService* local_state, + const std::string& locale); + ~UnstoppableDomainsInterstitialControllerClient() override = default; + + UnstoppableDomainsInterstitialControllerClient( + const UnstoppableDomainsInterstitialControllerClient&) = delete; + UnstoppableDomainsInterstitialControllerClient& operator=( + const UnstoppableDomainsInterstitialControllerClient&) = delete; + + // security_interstitials::SecurityInterstitialControllerClient: + void Proceed() override; + void DontProceed(); + + private: + void SetResolveMethodAndReload(ResolveMethodTypes type); + + const GURL request_url_; + PrefService* local_state_; +}; + +} // namespace unstoppable_domains + +#endif // BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_INTERSTITIAL_CONTROLLER_CLIENT_H_ diff --git a/components/unstoppable_domains/unstoppable_domains_navigation_throttle.cc b/components/unstoppable_domains/unstoppable_domains_navigation_throttle.cc new file mode 100644 index 00000000000..e5cafc5c46b --- /dev/null +++ b/components/unstoppable_domains/unstoppable_domains_navigation_throttle.cc @@ -0,0 +1,98 @@ +/* Copyright (c) 2021 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/components/unstoppable_domains/unstoppable_domains_navigation_throttle.h" + +#include + +#include "base/bind.h" +#include "base/threading/sequenced_task_runner_handle.h" +#include "brave/components/unstoppable_domains/unstoppable_domains_interstitial_controller_client.h" +#include "brave/components/unstoppable_domains/unstoppable_domains_opt_in_page.h" +#include "brave/components/unstoppable_domains/unstoppable_domains_service.h" +#include "brave/components/unstoppable_domains/utils.h" +#include "components/prefs/pref_service.h" +#include "components/security_interstitials/content/security_interstitial_tab_helper.h" +#include "components/user_prefs/user_prefs.h" +#include "content/public/browser/browser_context.h" +#include "content/public/browser/navigation_handle.h" +#include "content/public/browser/web_contents.h" +#include "net/base/net_errors.h" + +namespace unstoppable_domains { + +// static +std::unique_ptr +UnstoppableDomainsNavigationThrottle::MaybeCreateThrottleFor( + content::NavigationHandle* navigation_handle, + UnstoppableDomainsService* unstoppable_domains_service, + PrefService* local_state, + const std::string& locale) { + if (!unstoppable_domains_service) + return nullptr; + + return std::make_unique( + navigation_handle, unstoppable_domains_service, local_state, locale); +} + +UnstoppableDomainsNavigationThrottle::UnstoppableDomainsNavigationThrottle( + content::NavigationHandle* navigation_handle, + UnstoppableDomainsService* unstoppable_domains_service, + PrefService* local_state, + const std::string& locale) + : content::NavigationThrottle(navigation_handle), + unstoppable_domains_service_(unstoppable_domains_service), + local_state_(local_state), + locale_(locale) { + content::BrowserContext* context = + navigation_handle->GetWebContents()->GetBrowserContext(); + user_prefs_ = user_prefs::UserPrefs::Get(context); +} + +UnstoppableDomainsNavigationThrottle::~UnstoppableDomainsNavigationThrottle() = + default; + +content::NavigationThrottle::ThrottleCheckResult +UnstoppableDomainsNavigationThrottle::WillStartRequest() { + GURL url = navigation_handle()->GetURL(); + if (!IsUnstoppableDomainsTLD(url) || + !unstoppable_domains_service_->IsResolveMethodAsk()) { + return content::NavigationThrottle::PROCEED; + } + + base::SequencedTaskRunnerHandle::Get()->PostTask( + FROM_HERE, + base::BindOnce(&UnstoppableDomainsNavigationThrottle::ShowInterstitial, + weak_ptr_factory_.GetWeakPtr())); + return content::NavigationThrottle::DEFER; +} + +void UnstoppableDomainsNavigationThrottle::ShowInterstitial() { + content::NavigationHandle* handle = navigation_handle(); + content::WebContents* web_contents = handle->GetWebContents(); + const GURL& request_url = handle->GetURL(); + + auto controller_client = + std::make_unique( + web_contents, request_url, user_prefs_, local_state_, locale_); + auto page = std::make_unique( + web_contents, handle->GetURL(), std::move(controller_client)); + + // Get the page content before giving up ownership of |page|. + std::string page_content = page->GetHTMLContents(); + + security_interstitials::SecurityInterstitialTabHelper::AssociateBlockingPage( + web_contents, handle->GetNavigationId(), std::move(page)); + + CancelDeferredNavigation(content::NavigationThrottle::ThrottleCheckResult( + content::NavigationThrottle::CANCEL, net::ERR_BLOCKED_BY_CLIENT, + page_content)); +} + +const char* UnstoppableDomainsNavigationThrottle::GetNameForLogging() { + return "UnstoppableDomainsNavigationThrottle"; +} + +} // namespace unstoppable_domains diff --git a/components/unstoppable_domains/unstoppable_domains_navigation_throttle.h b/components/unstoppable_domains/unstoppable_domains_navigation_throttle.h new file mode 100644 index 00000000000..77115ad87e5 --- /dev/null +++ b/components/unstoppable_domains/unstoppable_domains_navigation_throttle.h @@ -0,0 +1,63 @@ +/* Copyright (c) 2021 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_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_NAVIGATION_THROTTLE_H_ +#define BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_NAVIGATION_THROTTLE_H_ + +#include +#include + +#include "base/memory/weak_ptr.h" +#include "content/public/browser/navigation_throttle.h" + +namespace content { +class NavigationHandle; +} // namespace content + +class PrefService; + +namespace unstoppable_domains { + +class UnstoppableDomainsService; + +class UnstoppableDomainsNavigationThrottle + : public content::NavigationThrottle { + public: + explicit UnstoppableDomainsNavigationThrottle( + content::NavigationHandle* navigation_handle, + UnstoppableDomainsService* unstoppable_domains_service, + PrefService* local_state, + const std::string& locale); + ~UnstoppableDomainsNavigationThrottle() override; + + UnstoppableDomainsNavigationThrottle( + const UnstoppableDomainsNavigationThrottle&) = delete; + UnstoppableDomainsNavigationThrottle& operator=( + const UnstoppableDomainsNavigationThrottle&) = delete; + + static std::unique_ptr + MaybeCreateThrottleFor(content::NavigationHandle* navigation_handle, + UnstoppableDomainsService* unstoppable_domains_service, + PrefService* local_state, + const std::string& locale); + + // content::NavigationThrottle implementation: + ThrottleCheckResult WillStartRequest() override; + const char* GetNameForLogging() override; + + private: + void ShowInterstitial(); + + UnstoppableDomainsService* unstoppable_domains_service_ = nullptr; + PrefService* user_prefs_ = nullptr; + PrefService* local_state_ = nullptr; + std::string locale_; + base::WeakPtrFactory weak_ptr_factory_{ + this}; +}; + +} // namespace unstoppable_domains + +#endif // BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_NAVIGATION_THROTTLE_H_ diff --git a/components/unstoppable_domains/unstoppable_domains_opt_in_page.cc b/components/unstoppable_domains/unstoppable_domains_opt_in_page.cc new file mode 100644 index 00000000000..69940d54c56 --- /dev/null +++ b/components/unstoppable_domains/unstoppable_domains_opt_in_page.cc @@ -0,0 +1,111 @@ +/* Copyright (c) 2021 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/components/unstoppable_domains/unstoppable_domains_opt_in_page.h" + +#include +#include + +#include "base/notreached.h" +#include "base/strings/string_number_conversions.h" +#include "base/strings/string_util.h" +#include "base/strings/utf_string_conversions.h" +#include "base/values.h" +#include "brave/components/unstoppable_domains/unstoppable_domains_interstitial_controller_client.h" +#include "components/grit/brave_components_resources.h" +#include "components/grit/brave_components_strings.h" +#include "components/security_interstitials/content/security_interstitial_controller_client.h" +#include "ui/base/l10n/l10n_util.h" + +namespace unstoppable_domains { + +// static +const security_interstitials::SecurityInterstitialPage::TypeID + UnstoppableDomainsOptInPage::kTypeForTesting = + &UnstoppableDomainsOptInPage::kTypeForTesting; + +UnstoppableDomainsOptInPage::UnstoppableDomainsOptInPage( + content::WebContents* web_contents, + const GURL& request_url, + std::unique_ptr< + security_interstitials::SecurityInterstitialControllerClient> + controller) + : security_interstitials::SecurityInterstitialPage(web_contents, + request_url, + std::move(controller)) {} + +UnstoppableDomainsOptInPage::~UnstoppableDomainsOptInPage() = default; + +void UnstoppableDomainsOptInPage::CommandReceived(const std::string& command) { + if (command == "\"pageLoadComplete\"") { + // content::WaitForRenderFrameReady sends this message when the page + // load completes. Ignore it. + return; + } + + int cmd = 0; + bool retval = base::StringToInt(command, &cmd); + DCHECK(retval); + + switch (cmd) { + case security_interstitials::CMD_DONT_PROCEED: + static_cast(controller()) + ->DontProceed(); + break; + case security_interstitials::CMD_PROCEED: + controller()->Proceed(); + break; + default: + NOTREACHED() << "Unsupported command: " << command; + } +} + +void UnstoppableDomainsOptInPage::PopulateInterstitialStrings( + base::DictionaryValue* load_time_data) { + load_time_data->SetString( + "tabTitle", + l10n_util::GetStringUTF16(IDS_UNSTOPPABLE_DOMAINS_OPT_IN_TITLE)); + load_time_data->SetString( + "heading", + l10n_util::GetStringUTF16(IDS_UNSTOPPABLE_DOMAINS_OPT_IN_HEADING)); + + const std::vector message_params = { + base::ASCIIToUTF16(""), + base::ASCIIToUTF16(""), + base::ASCIIToUTF16( + ""), + base::ASCIIToUTF16(""), + }; + load_time_data->SetString( + "primaryParagraph", + base::ReplaceStringPlaceholders( + l10n_util::GetStringUTF16( + IDS_UNSTOPPABLE_DOMAINS_OPT_IN_PRIMARY_PARAGRAPH), + message_params, nullptr)); + + load_time_data->SetString( + "primaryButtonText", + l10n_util::GetStringUTF16(IDS_UNSTOPPABLE_DOMAINS_OPT_IN_PRIMARY_BUTTON)); + load_time_data->SetString( + "dontProceedButtonText", + l10n_util::GetStringUTF16( + IDS_UNSTOPPABLE_DOMAINS_OPT_IN_DONT_PROCEED_BUTTON)); + load_time_data->SetString("finalParagraph", base::string16()); +} + +int UnstoppableDomainsOptInPage::GetHTMLTemplateId() { + return IDR_UNSTOPPABLE_DOMAINS_INTERSTITIAL_HTML; +} + +security_interstitials::SecurityInterstitialPage::TypeID +UnstoppableDomainsOptInPage::GetTypeForTesting() { + return UnstoppableDomainsOptInPage::kTypeForTesting; +} + +} // namespace unstoppable_domains diff --git a/components/unstoppable_domains/unstoppable_domains_opt_in_page.h b/components/unstoppable_domains/unstoppable_domains_opt_in_page.h new file mode 100644 index 00000000000..9f427b5a088 --- /dev/null +++ b/components/unstoppable_domains/unstoppable_domains_opt_in_page.h @@ -0,0 +1,58 @@ +/* Copyright (c) 2021 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_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_OPT_IN_PAGE_H_ +#define BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_OPT_IN_PAGE_H_ + +#include +#include + +#include "components/security_interstitials/content/security_interstitial_page.h" + +namespace content { +class WebContents; +} // namespace content + +namespace security_interstitials { +class SecurityInterstitialControllerClient; +} // namespace security_interstitials + +namespace unstoppable_domains { + +class UnstoppableDomainsOptInPage + : public security_interstitials::SecurityInterstitialPage { + public: + // Interstitial type, used in tests. + static const security_interstitials::SecurityInterstitialPage::TypeID + kTypeForTesting; + + UnstoppableDomainsOptInPage( + content::WebContents* web_contents, + const GURL& request_url, + std::unique_ptr< + security_interstitials::SecurityInterstitialControllerClient> + controller); + ~UnstoppableDomainsOptInPage() override; + + UnstoppableDomainsOptInPage(const UnstoppableDomainsOptInPage&) = delete; + UnstoppableDomainsOptInPage& operator=(const UnstoppableDomainsOptInPage&) = + delete; + + // SecurityInterstitialPage:: + void OnInterstitialClosing() override {} + void CommandReceived(const std::string& command) override; + security_interstitials::SecurityInterstitialPage::TypeID GetTypeForTesting() + override; + + protected: + // SecurityInterstitialPage:: + void PopulateInterstitialStrings( + base::DictionaryValue* load_time_data) override; + int GetHTMLTemplateId() override; +}; + +} // namespace unstoppable_domains + +#endif // BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UNSTOPPABLE_DOMAINS_OPT_IN_PAGE_H_ diff --git a/components/unstoppable_domains/unstoppable_domains_service.cc b/components/unstoppable_domains/unstoppable_domains_service.cc index 06e80ba4a1f..ba70060957e 100644 --- a/components/unstoppable_domains/unstoppable_domains_service.cc +++ b/components/unstoppable_domains/unstoppable_domains_service.cc @@ -22,7 +22,7 @@ UnstoppableDomainsService::UnstoppableDomainsService( PrefService* local_state) : local_state_(local_state), delegate_(std::move(delegate)) { pref_change_registrar_ = std::make_unique(); - pref_change_registrar_->Init(local_state_); + pref_change_registrar_->Init(local_state); pref_change_registrar_->Add( kResolveMethod, base::Bind(&UnstoppableDomainsService::OnPreferenceChanged, @@ -42,4 +42,9 @@ void UnstoppableDomainsService::OnPreferenceChanged() { delegate_->UpdateNetworkService(); } +bool UnstoppableDomainsService::IsResolveMethodAsk() { + return local_state_->GetInteger(kResolveMethod) == + static_cast(ResolveMethodTypes::ASK); +} + } // namespace unstoppable_domains diff --git a/components/unstoppable_domains/unstoppable_domains_service.h b/components/unstoppable_domains/unstoppable_domains_service.h index e07d0ac086f..ddb4f4016fb 100644 --- a/components/unstoppable_domains/unstoppable_domains_service.h +++ b/components/unstoppable_domains/unstoppable_domains_service.h @@ -36,6 +36,8 @@ class UnstoppableDomainsService : public KeyedService { static void RegisterLocalStatePrefs(PrefRegistrySimple* registry); + bool IsResolveMethodAsk(); + private: void OnPreferenceChanged(); diff --git a/components/unstoppable_domains/utils.cc b/components/unstoppable_domains/utils.cc new file mode 100644 index 00000000000..f41efa39259 --- /dev/null +++ b/components/unstoppable_domains/utils.cc @@ -0,0 +1,18 @@ +/* Copyright (c) 2021 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/components/unstoppable_domains/utils.h" + +#include "base/strings/string_util.h" +#include "brave/components/unstoppable_domains/constants.h" +#include "url/gurl.h" + +namespace unstoppable_domains { + +bool IsUnstoppableDomainsTLD(const GURL& url) { + return base::EndsWith(url.host_piece(), kCryptoDomain); +} + +} // namespace unstoppable_domains diff --git a/components/unstoppable_domains/utils.h b/components/unstoppable_domains/utils.h new file mode 100644 index 00000000000..983a7e8b838 --- /dev/null +++ b/components/unstoppable_domains/utils.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2021 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_COMPONENTS_UNSTOPPABLE_DOMAINS_UTILS_H_ +#define BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UTILS_H_ + +class GURL; + +namespace unstoppable_domains { + +bool IsUnstoppableDomainsTLD(const GURL& url); + +} // namespace unstoppable_domains + +#endif // BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UTILS_H_ From c3196d1e411b7097218230cd64e65148e5599549 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Wed, 3 Feb 2021 16:38:50 -0800 Subject: [PATCH 04/11] Add a feature flag for unstoppable domains support --- browser/brave_content_browser_client.cc | 5 +-- .../brave_default_extensions_browser_proxy.js | 4 +++ .../brave_default_extensions_page.html | 14 ++++---- .../brave_default_extensions_page.js | 4 +++ .../brave_default_extensions_handler.cc | 23 ++++++++++++ .../brave_default_extensions_handler.h | 1 + .../unstoppable_domains_service_factory.cc | 4 +++ chromium_src/chrome/browser/about_flags.cc | 14 ++++++++ .../chrome/browser/flag_descriptions.cc | 3 ++ .../chrome/browser/flag_descriptions.h | 2 ++ .../net/stub_resolver_config_reader.cc | 10 ++---- chromium_src/net/dns/dns_transaction.cc | 4 ++- components/unstoppable_domains/BUILD.gn | 35 ++++++++++++++++--- components/unstoppable_domains/features.cc | 17 +++++++++ components/unstoppable_domains/features.h | 21 +++++++++++ ...unstoppable_domains_navigation_throttle.cc | 11 ++---- .../unstoppable_domains_navigation_throttle.h | 5 --- .../unstoppable_domains_service.cc | 7 +--- .../unstoppable_domains_service.h | 3 -- components/unstoppable_domains/utils.cc | 26 ++++++++++++++ components/unstoppable_domains/utils.h | 4 +++ net/sources.gni | 1 + 22 files changed, 172 insertions(+), 46 deletions(-) create mode 100644 components/unstoppable_domains/features.cc create mode 100644 components/unstoppable_domains/features.h diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 355b31a01fa..987ad1cc57e 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -629,10 +629,7 @@ BraveContentBrowserClient::CreateThrottlesForNavigation( std::unique_ptr unstoppable_domains_navigation_throttle = unstoppable_domains:: UnstoppableDomainsNavigationThrottle::MaybeCreateThrottleFor( - handle, - unstoppable_domains::UnstoppableDomainsServiceFactory:: - GetForContext(context), - g_brave_browser_process->local_state(), + handle, g_brave_browser_process->local_state(), g_brave_browser_process->GetApplicationLocale()); if (unstoppable_domains_navigation_throttle) throttles.push_back(std::move(unstoppable_domains_navigation_throttle)); diff --git a/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_browser_proxy.js b/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_browser_proxy.js index c897a37c092..279ff0189f6 100644 --- a/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_browser_proxy.js +++ b/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_browser_proxy.js @@ -67,6 +67,10 @@ cr.define('settings', function () { wasSignInEnabledAtStartup () { return loadTimeData.getBoolean('signInAllowedOnNextStartupInitialValue') } + + isUnstoppableDomainsEnabled () { + return cr.sendWithPromise('isUnstoppableDomainsEnabled') + } } cr.addSingletonGetter(BraveDefaultExtensionsBrowserProxyImpl) diff --git a/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html b/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html index 579fe7fbe7a..e4dbb21ea6d 100644 --- a/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html +++ b/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html @@ -85,12 +85,14 @@ sub-label="$i18n{hangoutsEnabledDesc}" on-settings-boolean-control-change="onHangoutsEnabledChange_">
-
-
$i18n{resolveUnstoppableDomainsDesc}
- -
+ { this.widevineEnabled_ = enabled }) + this.browserProxy_.isUnstoppableDomainsEnabled().then(enabled => { + this.udEnabled_ = enabled + }); }, onWebTorrentEnabledChange_: function() { diff --git a/browser/ui/webui/settings/brave_default_extensions_handler.cc b/browser/ui/webui/settings/brave_default_extensions_handler.cc index c13845f3935..2719c668686 100644 --- a/browser/ui/webui/settings/brave_default_extensions_handler.cc +++ b/browser/ui/webui/settings/brave_default_extensions_handler.cc @@ -13,6 +13,7 @@ #include "brave/browser/extensions/brave_component_loader.h" #include "brave/common/pref_names.h" #include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources.h" +#include "brave/components/unstoppable_domains/buildflags/buildflags.h" #include "chrome/browser/about_flags.h" #include "chrome/browser/extensions/component_loader.h" #include "chrome/browser/extensions/extension_service.h" @@ -47,6 +48,10 @@ #include "brave/browser/widevine/widevine_utils.h" #endif +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) +#include "brave/components/unstoppable_domains/utils.h" +#endif + BraveDefaultExtensionsHandler::BraveDefaultExtensionsHandler() : weak_ptr_factory_(this) { #if BUILDFLAG(ENABLE_WIDEVINE) @@ -108,6 +113,11 @@ void BraveDefaultExtensionsHandler::RegisterMessages() { "isWidevineEnabled", base::BindRepeating(&BraveDefaultExtensionsHandler::IsWidevineEnabled, base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "isUnstoppableDomainsEnabled", + base::BindRepeating( + &BraveDefaultExtensionsHandler::IsUnstoppableDomainsEnabled, + base::Unretained(this))); // Can't call this in ctor because it needs to access web_ui(). InitializePrefCallbacks(); @@ -394,3 +404,16 @@ void BraveDefaultExtensionsHandler::SetBraveWalletEnabled( } } #endif + +void BraveDefaultExtensionsHandler::IsUnstoppableDomainsEnabled( + const base::ListValue* args) { + CHECK_EQ(args->GetSize(), 1U); + AllowJavascript(); + ResolveJavascriptCallback( + args->GetList()[0], +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) + base::Value(unstoppable_domains::IsUnstoppableDomainsEnabled())); +#else + base::Value(false)); +#endif +} diff --git a/browser/ui/webui/settings/brave_default_extensions_handler.h b/browser/ui/webui/settings/brave_default_extensions_handler.h index e2507d66202..9118497ea51 100644 --- a/browser/ui/webui/settings/brave_default_extensions_handler.h +++ b/browser/ui/webui/settings/brave_default_extensions_handler.h @@ -44,6 +44,7 @@ class BraveDefaultExtensionsHandler : public settings::SettingsPageUIHandler { void SetWidevineEnabled(const base::ListValue* args); void IsWidevineEnabled(const base::ListValue* args); void OnWidevineEnabledChanged(); + void IsUnstoppableDomainsEnabled(const base::ListValue* args); void InitializePrefCallbacks(); diff --git a/browser/unstoppable_domains/unstoppable_domains_service_factory.cc b/browser/unstoppable_domains/unstoppable_domains_service_factory.cc index 909c1efc34f..efe353478c9 100644 --- a/browser/unstoppable_domains/unstoppable_domains_service_factory.cc +++ b/browser/unstoppable_domains/unstoppable_domains_service_factory.cc @@ -10,6 +10,7 @@ #include "brave/browser/brave_browser_process_impl.h" #include "brave/browser/unstoppable_domains/unstoppable_domains_service_delegate_impl.h" #include "brave/components/unstoppable_domains/unstoppable_domains_service.h" +#include "brave/components/unstoppable_domains/utils.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" namespace unstoppable_domains { @@ -30,6 +31,9 @@ UnstoppableDomainsServiceFactory::GetInstance() { // static UnstoppableDomainsService* UnstoppableDomainsServiceFactory::GetForContext( content::BrowserContext* context) { + if (!IsUnstoppableDomainsEnabled()) + return nullptr; + return static_cast( GetInstance()->GetServiceForBrowserContext(context, true)); } diff --git a/chromium_src/chrome/browser/about_flags.cc b/chromium_src/chrome/browser/about_flags.cc index 6cece20930c..2ff3dcc098a 100644 --- a/chromium_src/chrome/browser/about_flags.cc +++ b/chromium_src/chrome/browser/about_flags.cc @@ -16,6 +16,7 @@ #include "brave/components/ntp_background_images/browser/features.h" #include "brave/components/sidebar/buildflags/buildflags.h" #include "brave/components/speedreader/buildflags.h" +#include "brave/components/unstoppable_domains/buildflags/buildflags.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_manager.h" #include "components/prefs/pref_service.h" @@ -92,6 +93,18 @@ using ntp_background_images::features::kBraveNTPSuperReferralWallpaper; #define BRAVE_NATIVE_WALLET_FEATURE_ENTRIES #endif +#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) +#include "brave/components/unstoppable_domains/features.h" + +#define BRAVE_UNSTOPPABLE_DOMAINS_FEATURE_ENTRIES \ + {"brave-unstoppable-domains", \ + flag_descriptions::kBraveUnstoppableDomainsName, \ + flag_descriptions::kBraveUnstoppableDomainsDescription, kOsDesktop, \ + FEATURE_VALUE_TYPE(unstoppable_domains::features::kUnstoppableDomains)}, +#else +#define BRAVE_UNSTOPPABLE_DOMAINS_FEATURE_ENTRIES +#endif + #define BRAVE_FEATURE_ENTRIES \ {"use-dev-updater-url", \ flag_descriptions::kUseDevUpdaterUrlName, \ @@ -114,6 +127,7 @@ using ntp_background_images::features::kBraveNTPSuperReferralWallpaper; BRAVE_IPFS_FEATURE_ENTRIES \ BRAVE_NATIVE_WALLET_FEATURE_ENTRIES \ SIDEBAR_FEATURE_ENTRIES \ + BRAVE_UNSTOPPABLE_DOMAINS_FEATURE_ENTRIES \ {"brave-super-referral", \ flag_descriptions::kBraveSuperReferralName, \ flag_descriptions::kBraveSuperReferralDescription, \ diff --git a/chromium_src/chrome/browser/flag_descriptions.cc b/chromium_src/chrome/browser/flag_descriptions.cc index c66a13653e4..47ad4056e5c 100644 --- a/chromium_src/chrome/browser/flag_descriptions.cc +++ b/chromium_src/chrome/browser/flag_descriptions.cc @@ -41,6 +41,9 @@ const char kNativeBraveWalletName[] = "Enable experimental Brave native wallet"; const char kNativeBraveWalletDescription[] = "Experimental native cryptocurrency wallet support without the use of " "extensions"; +const char kBraveUnstoppableDomainsName[] = "Enable unstoppable domains"; +const char kBraveUnstoppableDomainsDescription[] = + "Enable resolution support of unstoppable domains."; const char kBraveSuperReferralName[] = "Enable Brave Super Referral"; const char kBraveSuperReferralDescription[] = "Use custom theme for Brave Super Referral"; diff --git a/chromium_src/chrome/browser/flag_descriptions.h b/chromium_src/chrome/browser/flag_descriptions.h index d1d99499866..fcd12c94b45 100644 --- a/chromium_src/chrome/browser/flag_descriptions.h +++ b/chromium_src/chrome/browser/flag_descriptions.h @@ -27,6 +27,8 @@ extern const char kBraveIpfsName[]; extern const char kBraveIpfsDescription[]; extern const char kNativeBraveWalletName[]; extern const char kNativeBraveWalletDescription[]; +extern const char kBraveUnstoppableDomainsName[]; +extern const char kBraveUnstoppableDomainsDescription[]; extern const char kBraveSuperReferralName[]; extern const char kBraveSuperReferralDescription[]; extern const char kBraveEphemeralStorageName[]; diff --git a/chromium_src/chrome/browser/net/stub_resolver_config_reader.cc b/chromium_src/chrome/browser/net/stub_resolver_config_reader.cc index 916042f120e..eb01d336a3b 100644 --- a/chromium_src/chrome/browser/net/stub_resolver_config_reader.cc +++ b/chromium_src/chrome/browser/net/stub_resolver_config_reader.cc @@ -9,7 +9,7 @@ #if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) #include "brave/components/unstoppable_domains/constants.h" -#include "brave/components/unstoppable_domains/pref_names.h" +#include "brave/components/unstoppable_domains/utils.h" #endif namespace { @@ -17,13 +17,7 @@ namespace { #if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) void AddUnstoppableDomainsResolver(std::string* doh_templates, PrefService* local_state) { - bool resolve_ud_enabled = - local_state->FindPreference(unstoppable_domains::kResolveMethod) && - local_state->GetInteger(unstoppable_domains::kResolveMethod) == - static_cast( - unstoppable_domains::ResolveMethodTypes::DNS_OVER_HTTPS); - - if (resolve_ud_enabled && + if (unstoppable_domains::IsResolveMethodDoH(local_state) && doh_templates->find(unstoppable_domains::kDoHResolver) == std::string::npos) { *doh_templates = diff --git a/chromium_src/net/dns/dns_transaction.cc b/chromium_src/net/dns/dns_transaction.cc index 085f7326703..5dbdc659941 100644 --- a/chromium_src/net/dns/dns_transaction.cc +++ b/chromium_src/net/dns/dns_transaction.cc @@ -10,6 +10,7 @@ #if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) #include "brave/components/unstoppable_domains/constants.h" +#include "brave/components/unstoppable_domains/utils.h" #endif namespace { @@ -20,7 +21,8 @@ bool GetNextIndex(const std::string& hostname, size_t* doh_server_index) { #if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) // Skip unstoppable domains resolver for non-crypto domains. - if (!base::EndsWith(hostname, unstoppable_domains::kCryptoDomain) && + if (unstoppable_domains::IsUnstoppableDomainsEnabled() && + !base::EndsWith(hostname, unstoppable_domains::kCryptoDomain) && config.dns_over_https_servers[*doh_server_index].server_template == unstoppable_domains::kDoHResolver) { // No next available index to attempt. diff --git a/components/unstoppable_domains/BUILD.gn b/components/unstoppable_domains/BUILD.gn index a11babaea4f..740e796b61c 100644 --- a/components/unstoppable_domains/BUILD.gn +++ b/components/unstoppable_domains/BUILD.gn @@ -7,13 +7,36 @@ source_set("constants") { sources = [ "constants.cc", "constants.h", + "pref_names.cc", + "pref_names.h", + ] +} + +source_set("features") { + sources = [ + "features.cc", + "features.h", + ] + + deps = [ "//base" ] +} + +source_set("utils") { + sources = [ + "utils.cc", + "utils.h", + ] + + deps = [ + ":constants", + ":features", + "//components/prefs", + "//url", ] } source_set("unstoppable_domains") { sources = [ - "pref_names.cc", - "pref_names.h", "unstoppable_domains_interstitial_controller_client.cc", "unstoppable_domains_interstitial_controller_client.h", "unstoppable_domains_navigation_throttle.cc", @@ -23,11 +46,13 @@ source_set("unstoppable_domains") { "unstoppable_domains_service.cc", "unstoppable_domains_service.h", "unstoppable_domains_service_delegate.h", - "utils.cc", - "utils.h", ] - public_deps = [ ":constants" ] + public_deps = [ + ":constants", + ":features", + ":utils", + ] deps = [ "//base", diff --git a/components/unstoppable_domains/features.cc b/components/unstoppable_domains/features.cc new file mode 100644 index 00000000000..b5fa4695b4b --- /dev/null +++ b/components/unstoppable_domains/features.cc @@ -0,0 +1,17 @@ +/* Copyright (c) 2021 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/components/unstoppable_domains/features.h" + +#include "base/feature_list.h" + +namespace unstoppable_domains { +namespace features { + +const base::Feature kUnstoppableDomains{"UnstoppableDomains", + base::FEATURE_DISABLED_BY_DEFAULT}; + +} // namespace features +} // namespace unstoppable_domains diff --git a/components/unstoppable_domains/features.h b/components/unstoppable_domains/features.h new file mode 100644 index 00000000000..688169f5713 --- /dev/null +++ b/components/unstoppable_domains/features.h @@ -0,0 +1,21 @@ +/* Copyright (c) 2021 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_COMPONENTS_UNSTOPPABLE_DOMAINS_FEATURES_H_ +#define BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_FEATURES_H_ + +namespace base { +struct Feature; +} // namespace base + +namespace unstoppable_domains { +namespace features { + +extern const base::Feature kUnstoppableDomains; + +} // namespace features +} // namespace unstoppable_domains + +#endif // BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_FEATURES_H_ diff --git a/components/unstoppable_domains/unstoppable_domains_navigation_throttle.cc b/components/unstoppable_domains/unstoppable_domains_navigation_throttle.cc index e5cafc5c46b..59a2ddb18c6 100644 --- a/components/unstoppable_domains/unstoppable_domains_navigation_throttle.cc +++ b/components/unstoppable_domains/unstoppable_domains_navigation_throttle.cc @@ -11,7 +11,6 @@ #include "base/threading/sequenced_task_runner_handle.h" #include "brave/components/unstoppable_domains/unstoppable_domains_interstitial_controller_client.h" #include "brave/components/unstoppable_domains/unstoppable_domains_opt_in_page.h" -#include "brave/components/unstoppable_domains/unstoppable_domains_service.h" #include "brave/components/unstoppable_domains/utils.h" #include "components/prefs/pref_service.h" #include "components/security_interstitials/content/security_interstitial_tab_helper.h" @@ -27,23 +26,20 @@ namespace unstoppable_domains { std::unique_ptr UnstoppableDomainsNavigationThrottle::MaybeCreateThrottleFor( content::NavigationHandle* navigation_handle, - UnstoppableDomainsService* unstoppable_domains_service, PrefService* local_state, const std::string& locale) { - if (!unstoppable_domains_service) + if (!IsUnstoppableDomainsEnabled()) return nullptr; return std::make_unique( - navigation_handle, unstoppable_domains_service, local_state, locale); + navigation_handle, local_state, locale); } UnstoppableDomainsNavigationThrottle::UnstoppableDomainsNavigationThrottle( content::NavigationHandle* navigation_handle, - UnstoppableDomainsService* unstoppable_domains_service, PrefService* local_state, const std::string& locale) : content::NavigationThrottle(navigation_handle), - unstoppable_domains_service_(unstoppable_domains_service), local_state_(local_state), locale_(locale) { content::BrowserContext* context = @@ -57,8 +53,7 @@ UnstoppableDomainsNavigationThrottle::~UnstoppableDomainsNavigationThrottle() = content::NavigationThrottle::ThrottleCheckResult UnstoppableDomainsNavigationThrottle::WillStartRequest() { GURL url = navigation_handle()->GetURL(); - if (!IsUnstoppableDomainsTLD(url) || - !unstoppable_domains_service_->IsResolveMethodAsk()) { + if (!IsUnstoppableDomainsTLD(url) || !IsResolveMethodAsk(local_state_)) { return content::NavigationThrottle::PROCEED; } diff --git a/components/unstoppable_domains/unstoppable_domains_navigation_throttle.h b/components/unstoppable_domains/unstoppable_domains_navigation_throttle.h index 77115ad87e5..42b6c754277 100644 --- a/components/unstoppable_domains/unstoppable_domains_navigation_throttle.h +++ b/components/unstoppable_domains/unstoppable_domains_navigation_throttle.h @@ -20,14 +20,11 @@ class PrefService; namespace unstoppable_domains { -class UnstoppableDomainsService; - class UnstoppableDomainsNavigationThrottle : public content::NavigationThrottle { public: explicit UnstoppableDomainsNavigationThrottle( content::NavigationHandle* navigation_handle, - UnstoppableDomainsService* unstoppable_domains_service, PrefService* local_state, const std::string& locale); ~UnstoppableDomainsNavigationThrottle() override; @@ -39,7 +36,6 @@ class UnstoppableDomainsNavigationThrottle static std::unique_ptr MaybeCreateThrottleFor(content::NavigationHandle* navigation_handle, - UnstoppableDomainsService* unstoppable_domains_service, PrefService* local_state, const std::string& locale); @@ -50,7 +46,6 @@ class UnstoppableDomainsNavigationThrottle private: void ShowInterstitial(); - UnstoppableDomainsService* unstoppable_domains_service_ = nullptr; PrefService* user_prefs_ = nullptr; PrefService* local_state_ = nullptr; std::string locale_; diff --git a/components/unstoppable_domains/unstoppable_domains_service.cc b/components/unstoppable_domains/unstoppable_domains_service.cc index ba70060957e..71f3c93c5e3 100644 --- a/components/unstoppable_domains/unstoppable_domains_service.cc +++ b/components/unstoppable_domains/unstoppable_domains_service.cc @@ -20,7 +20,7 @@ UnstoppableDomainsService::UnstoppableDomainsService( std::unique_ptr delegate, content::BrowserContext* context, PrefService* local_state) - : local_state_(local_state), delegate_(std::move(delegate)) { + : delegate_(std::move(delegate)) { pref_change_registrar_ = std::make_unique(); pref_change_registrar_->Init(local_state); pref_change_registrar_->Add( @@ -42,9 +42,4 @@ void UnstoppableDomainsService::OnPreferenceChanged() { delegate_->UpdateNetworkService(); } -bool UnstoppableDomainsService::IsResolveMethodAsk() { - return local_state_->GetInteger(kResolveMethod) == - static_cast(ResolveMethodTypes::ASK); -} - } // namespace unstoppable_domains diff --git a/components/unstoppable_domains/unstoppable_domains_service.h b/components/unstoppable_domains/unstoppable_domains_service.h index ddb4f4016fb..7a6e29fca9b 100644 --- a/components/unstoppable_domains/unstoppable_domains_service.h +++ b/components/unstoppable_domains/unstoppable_domains_service.h @@ -36,12 +36,9 @@ class UnstoppableDomainsService : public KeyedService { static void RegisterLocalStatePrefs(PrefRegistrySimple* registry); - bool IsResolveMethodAsk(); - private: void OnPreferenceChanged(); - PrefService* local_state_; std::unique_ptr pref_change_registrar_; std::unique_ptr delegate_; }; diff --git a/components/unstoppable_domains/utils.cc b/components/unstoppable_domains/utils.cc index f41efa39259..4957cc16339 100644 --- a/components/unstoppable_domains/utils.cc +++ b/components/unstoppable_domains/utils.cc @@ -5,8 +5,12 @@ #include "brave/components/unstoppable_domains/utils.h" +#include "base/feature_list.h" #include "base/strings/string_util.h" #include "brave/components/unstoppable_domains/constants.h" +#include "brave/components/unstoppable_domains/features.h" +#include "brave/components/unstoppable_domains/pref_names.h" +#include "components/prefs/pref_service.h" #include "url/gurl.h" namespace unstoppable_domains { @@ -15,4 +19,26 @@ bool IsUnstoppableDomainsTLD(const GURL& url) { return base::EndsWith(url.host_piece(), kCryptoDomain); } +bool IsUnstoppableDomainsEnabled() { + return base::FeatureList::IsEnabled(features::kUnstoppableDomains); +} + +bool IsResolveMethodAsk(PrefService* local_state) { + if (!local_state || !IsUnstoppableDomainsEnabled()) { + return false; // Treat it as disabled. + } + + return local_state->GetInteger(kResolveMethod) == + static_cast(ResolveMethodTypes::ASK); +} + +bool IsResolveMethodDoH(PrefService* local_state) { + if (!local_state || !IsUnstoppableDomainsEnabled()) { + return false; // Treat it as disabled. + } + + return local_state->GetInteger(kResolveMethod) == + static_cast(ResolveMethodTypes::DNS_OVER_HTTPS); +} + } // namespace unstoppable_domains diff --git a/components/unstoppable_domains/utils.h b/components/unstoppable_domains/utils.h index 983a7e8b838..724bc573e99 100644 --- a/components/unstoppable_domains/utils.h +++ b/components/unstoppable_domains/utils.h @@ -7,10 +7,14 @@ #define BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_UTILS_H_ class GURL; +class PrefService; namespace unstoppable_domains { bool IsUnstoppableDomainsTLD(const GURL& url); +bool IsUnstoppableDomainsEnabled(); +bool IsResolveMethodAsk(PrefService* local_state); +bool IsResolveMethodDoH(PrefService* local_state); } // namespace unstoppable_domains diff --git a/net/sources.gni b/net/sources.gni index d027ca3edb3..ce78c472b24 100644 --- a/net/sources.gni +++ b/net/sources.gni @@ -10,5 +10,6 @@ brave_net_sources = [ brave_net_deps = [ "//brave/components/unstoppable_domains:constants", + "//brave/components/unstoppable_domains:utils", "//brave/components/unstoppable_domains/buildflags", ] From 61ab97f20effb5cf9a242bfdbeb397f70d725bfb Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Fri, 5 Feb 2021 14:34:06 -0800 Subject: [PATCH 05/11] Add tests --- browser/brave_content_browser_client.cc | 1 - browser/unstoppable_domains/test/BUILD.gn | 43 ++++++ ...domains_navigation_throttle_browsertest.cc | 134 ++++++++++++++++++ ...unstoppable_domains_service_browsertest.cc | 95 +++++++++++++ .../test/utils_unittest.cc | 73 ++++++++++ build/config/brave_build.gni | 5 +- .../net/dns/dns_transaction_unittest.cc | 116 +++++++++++++++ net/dns/sources.gni | 6 + patches/net-dns-BUILD.gn.patch | 28 ++++ test/BUILD.gn | 9 ++ 10 files changed, 507 insertions(+), 3 deletions(-) create mode 100644 browser/unstoppable_domains/test/BUILD.gn create mode 100644 browser/unstoppable_domains/test/unstoppable_domains_navigation_throttle_browsertest.cc create mode 100644 browser/unstoppable_domains/test/unstoppable_domains_service_browsertest.cc create mode 100644 browser/unstoppable_domains/test/utils_unittest.cc create mode 100644 chromium_src/net/dns/dns_transaction_unittest.cc create mode 100644 net/dns/sources.gni create mode 100644 patches/net-dns-BUILD.gn.patch diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 987ad1cc57e..8fec950fb4f 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -90,7 +90,6 @@ using extensions::ChromeContentBrowserClientExtensionsPart; #endif #if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) -#include "brave/browser/unstoppable_domains/unstoppable_domains_service_factory.h" #include "brave/components/unstoppable_domains/unstoppable_domains_navigation_throttle.h" #endif diff --git a/browser/unstoppable_domains/test/BUILD.gn b/browser/unstoppable_domains/test/BUILD.gn new file mode 100644 index 00000000000..29a4d9eba97 --- /dev/null +++ b/browser/unstoppable_domains/test/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2021 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/. */ + +import("//brave/build/config.gni") +import("//testing/test.gni") + +source_set("browser_tests") { + testonly = true + defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ] + sources = [ + "//brave/browser/unstoppable_domains/test/unstoppable_domains_navigation_throttle_browsertest.cc", + "//brave/browser/unstoppable_domains/test/unstoppable_domains_service_browsertest.cc", + ] + deps = [ + "//base/test:test_support", + "//brave/components/unstoppable_domains", + "//chrome/browser:browser_process", + "//chrome/test:test_support", + "//chrome/test:test_support_ui", + "//components/prefs", + "//components/security_interstitials/content:security_interstitial_page", + "//content/test:test_support", + "//testing/gtest", + ] +} # source_set("browser_tests") { + +source_set("unit_tests") { + testonly = true + sources = [ + "//brave/browser/unstoppable_domains/test/utils_unittest.cc", + "//net/dns/dns_transaction_unittest.cc", + ] + + deps = [ + "//base/test:test_support", + "//brave/components/unstoppable_domains", + "//chrome/test:test_support", + "//components/prefs", + "//testing/gtest", + ] +} # source_set("unit_tests") diff --git a/browser/unstoppable_domains/test/unstoppable_domains_navigation_throttle_browsertest.cc b/browser/unstoppable_domains/test/unstoppable_domains_navigation_throttle_browsertest.cc new file mode 100644 index 00000000000..e08a415aebe --- /dev/null +++ b/browser/unstoppable_domains/test/unstoppable_domains_navigation_throttle_browsertest.cc @@ -0,0 +1,134 @@ +/* Copyright (c) 2021 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/test/scoped_feature_list.h" +#include "brave/components/unstoppable_domains/constants.h" +#include "brave/components/unstoppable_domains/features.h" +#include "brave/components/unstoppable_domains/pref_names.h" +#include "brave/components/unstoppable_domains/unstoppable_domains_opt_in_page.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/tabs/tab_strip_model.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "chrome/test/base/ui_test_utils.h" +#include "components/prefs/pref_service.h" +#include "components/security_interstitials/content/security_interstitial_page.h" +#include "components/security_interstitials/content/security_interstitial_tab_helper.h" +#include "content/public/test/browser_test.h" +#include "content/public/test/browser_test_utils.h" +#include "content/public/test/test_navigation_observer.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "url/gurl.h" + +namespace { + +security_interstitials::SecurityInterstitialPage* GetCurrentInterstitial( + content::WebContents* web_contents) { + security_interstitials::SecurityInterstitialTabHelper* helper = + security_interstitials::SecurityInterstitialTabHelper::FromWebContents( + web_contents); + if (!helper) { + return nullptr; + } + return helper->GetBlockingPageForCurrentlyCommittedNavigationForTesting(); +} + +security_interstitials::SecurityInterstitialPage::TypeID GetInterstitialType( + content::WebContents* web_contents) { + security_interstitials::SecurityInterstitialPage* page = + GetCurrentInterstitial(web_contents); + if (!page) { + return nullptr; + } + return page->GetTypeForTesting(); +} + +void SendInterstitialCommand( + content::WebContents* web_contents, + security_interstitials::SecurityInterstitialCommand command) { + GetCurrentInterstitial(web_contents) + ->CommandReceived(base::NumberToString(command)); +} + +void SendInterstitialCommandSync( + Browser* browser, + security_interstitials::SecurityInterstitialCommand command) { + content::WebContents* web_contents = + browser->tab_strip_model()->GetActiveWebContents(); + + EXPECT_EQ(unstoppable_domains::UnstoppableDomainsOptInPage::kTypeForTesting, + GetInterstitialType(web_contents)); + + content::TestNavigationObserver navigation_observer(web_contents, 1); + SendInterstitialCommand(web_contents, command); + navigation_observer.Wait(); + + EXPECT_EQ(nullptr, GetCurrentInterstitial(web_contents)); +} + +} // namespace + +namespace unstoppable_domains { + +class UnstoppableDomainsNavigationThrottleBrowserTest + : public InProcessBrowserTest { + public: + UnstoppableDomainsNavigationThrottleBrowserTest() { + feature_list_.InitAndEnableFeature(features::kUnstoppableDomains); + } + + ~UnstoppableDomainsNavigationThrottleBrowserTest() override = default; + + void SetUpOnMainThread() override { + InProcessBrowserTest::SetUpOnMainThread(); + } + + PrefService* local_state() { return g_browser_process->local_state(); } + + private: + base::test::ScopedFeatureList feature_list_; +}; + +IN_PROC_BROWSER_TEST_F(UnstoppableDomainsNavigationThrottleBrowserTest, + ShowInterstitialAndProceed) { + ui_test_utils::NavigateToURL(browser(), GURL("http://test.crypto")); + + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + + EXPECT_TRUE(WaitForRenderFrameReady(web_contents->GetMainFrame())); + EXPECT_EQ(UnstoppableDomainsOptInPage::kTypeForTesting, + GetInterstitialType(web_contents)); + + EXPECT_EQ(static_cast(ResolveMethodTypes::ASK), + local_state()->GetInteger(kResolveMethod)); + SendInterstitialCommandSync( + browser(), + security_interstitials::SecurityInterstitialCommand::CMD_PROCEED); + EXPECT_EQ(static_cast(ResolveMethodTypes::DNS_OVER_HTTPS), + local_state()->GetInteger(kResolveMethod)); +} + +IN_PROC_BROWSER_TEST_F(UnstoppableDomainsNavigationThrottleBrowserTest, + ShowInterstitialAndReject) { + ui_test_utils::NavigateToURL(browser(), GURL("http://test.crypto")); + + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + + EXPECT_TRUE(WaitForRenderFrameReady(web_contents->GetMainFrame())); + EXPECT_EQ(UnstoppableDomainsOptInPage::kTypeForTesting, + GetInterstitialType(web_contents)); + + EXPECT_EQ(static_cast(ResolveMethodTypes::ASK), + local_state()->GetInteger(kResolveMethod)); + SendInterstitialCommandSync( + browser(), + security_interstitials::SecurityInterstitialCommand::CMD_DONT_PROCEED); + EXPECT_EQ(static_cast(ResolveMethodTypes::DISABLED), + local_state()->GetInteger(kResolveMethod)); +} + +} // namespace unstoppable_domains diff --git a/browser/unstoppable_domains/test/unstoppable_domains_service_browsertest.cc b/browser/unstoppable_domains/test/unstoppable_domains_service_browsertest.cc new file mode 100644 index 00000000000..b20b9a5ff64 --- /dev/null +++ b/browser/unstoppable_domains/test/unstoppable_domains_service_browsertest.cc @@ -0,0 +1,95 @@ +/* Copyright (c) 2021 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/test/scoped_feature_list.h" +#include "brave/components/unstoppable_domains/constants.h" +#include "brave/components/unstoppable_domains/features.h" +#include "brave/components/unstoppable_domains/pref_names.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/net/secure_dns_config.h" +#include "chrome/browser/net/stub_resolver_config_reader.h" +#include "chrome/browser/net/system_network_context_manager.h" +#include "chrome/common/pref_names.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "components/prefs/pref_service.h" +#include "content/public/test/browser_test.h" +#include "net/dns/public/secure_dns_mode.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace unstoppable_domains { + +class UnstoppableDomainsServiceBrowserTest : public InProcessBrowserTest { + public: + UnstoppableDomainsServiceBrowserTest() { + feature_list_.InitAndEnableFeature(features::kUnstoppableDomains); + } + + void SetUpOnMainThread() override { + InProcessBrowserTest::SetUpOnMainThread(); + stub_config_reader_ = + SystemNetworkContextManager::GetStubResolverConfigReader(); + ASSERT_TRUE(stub_config_reader_); + } + + ~UnstoppableDomainsServiceBrowserTest() override = default; + + PrefService* local_state() { return g_browser_process->local_state(); } + + SecureDnsConfig GetSecureDnsConfiguration() { + return stub_config_reader_->GetSecureDnsConfiguration( + false /*force_check_parental_controls_for_automatic_mode */); + } + + private: + base::test::ScopedFeatureList feature_list_; + StubResolverConfigReader* stub_config_reader_; +}; + +IN_PROC_BROWSER_TEST_F(UnstoppableDomainsServiceBrowserTest, + UpdateConfigWhenPrefChanged) { + // Initial state. + EXPECT_EQ(local_state()->GetInteger(kResolveMethod), + static_cast(ResolveMethodTypes::ASK)); + SecureDnsConfig config = GetSecureDnsConfiguration(); + EXPECT_EQ(config.mode(), net::SecureDnsMode::kAutomatic); + EXPECT_EQ(config.servers().size(), 0u); + + // Set resolve method to DoH should update the config. + local_state()->SetInteger( + kResolveMethod, static_cast(ResolveMethodTypes::DNS_OVER_HTTPS)); + config = GetSecureDnsConfiguration(); + std::vector expected_doh_servers = { + {kDoHResolver, true}}; + EXPECT_EQ(config.servers(), expected_doh_servers); + + // Set custom DoH provider should still keep the resolver for UD. + local_state()->SetString(prefs::kDnsOverHttpsTemplates, "https://test.com"); + config = GetSecureDnsConfiguration(); + expected_doh_servers = {{kDoHResolver, true}, {"https://test.com", true}}; + EXPECT_EQ(config.servers(), expected_doh_servers); + + // Set secure mode to off should return empty DoH servers. + local_state()->SetString( + prefs::kDnsOverHttpsMode, + SecureDnsConfig::ModeToString(net::SecureDnsMode::kOff)); + config = GetSecureDnsConfiguration(); + EXPECT_EQ(config.servers().size(), 0u); + + // Turn on secure mode again should get the same result as before. + local_state()->SetString( + prefs::kDnsOverHttpsMode, + SecureDnsConfig::ModeToString(net::SecureDnsMode::kSecure)); + config = GetSecureDnsConfiguration(); + EXPECT_EQ(config.servers(), expected_doh_servers); + + // Set resolve method to disabled should keep user's DoH setting. + local_state()->SetInteger(kResolveMethod, + static_cast(ResolveMethodTypes::DISABLED)); + config = GetSecureDnsConfiguration(); + expected_doh_servers = {{"https://test.com", true}}; + EXPECT_EQ(config.servers(), expected_doh_servers); +} + +} // namespace unstoppable_domains diff --git a/browser/unstoppable_domains/test/utils_unittest.cc b/browser/unstoppable_domains/test/utils_unittest.cc new file mode 100644 index 00000000000..c8655df3205 --- /dev/null +++ b/browser/unstoppable_domains/test/utils_unittest.cc @@ -0,0 +1,73 @@ +/* Copyright (c) 2021 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/components/unstoppable_domains/utils.h" + +#include "base/test/scoped_feature_list.h" +#include "base/test/task_environment.h" +#include "brave/components/unstoppable_domains/constants.h" +#include "brave/components/unstoppable_domains/features.h" +#include "brave/components/unstoppable_domains/pref_names.h" +#include "chrome/test/base/scoped_testing_local_state.h" +#include "chrome/test/base/testing_browser_process.h" +#include "components/prefs/testing_pref_service.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace unstoppable_domains { + +class UtilsUnitTest : public testing::TestWithParam { + public: + UtilsUnitTest() + : local_state_(TestingBrowserProcess::GetGlobal()), + feature_enabled_(GetParam()) {} + ~UtilsUnitTest() override = default; + + void SetUp() override { + if (feature_enabled_) { + feature_list_.InitAndEnableFeature(features::kUnstoppableDomains); + } else { + feature_list_.InitAndDisableFeature(features::kUnstoppableDomains); + } + } + + PrefService* local_state() { return local_state_.Get(); } + bool feature_enabled() { return feature_enabled_; } + + private: + base::test::TaskEnvironment task_environment_; + base::test::ScopedFeatureList feature_list_; + ScopedTestingLocalState local_state_; + bool feature_enabled_; +}; + +TEST_P(UtilsUnitTest, IsUnstoppableDomainsTLD) { + EXPECT_TRUE(IsUnstoppableDomainsTLD(GURL("http://test.crypto"))); + EXPECT_FALSE(IsUnstoppableDomainsTLD(GURL("http://test.com"))); + EXPECT_FALSE(IsUnstoppableDomainsTLD(GURL("http://crypto"))); +} + +TEST_P(UtilsUnitTest, IsUnstoppableDomainsEnabled) { + EXPECT_EQ(feature_enabled(), IsUnstoppableDomainsEnabled()); +} + +TEST_P(UtilsUnitTest, IsResolveMethodAsk) { + EXPECT_EQ(feature_enabled(), IsResolveMethodAsk(local_state())); + + local_state()->SetInteger( + kResolveMethod, static_cast(ResolveMethodTypes::DNS_OVER_HTTPS)); + EXPECT_FALSE(IsResolveMethodAsk(local_state())); +} + +TEST_P(UtilsUnitTest, IsResolveMethodDoH) { + EXPECT_FALSE(IsResolveMethodDoH(local_state())); + + local_state()->SetInteger( + kResolveMethod, static_cast(ResolveMethodTypes::DNS_OVER_HTTPS)); + EXPECT_EQ(feature_enabled(), IsResolveMethodDoH(local_state())); +} + +INSTANTIATE_TEST_SUITE_P(/* no prefix */, UtilsUnitTest, testing::Bool()); + +} // namespace unstoppable_domains diff --git a/build/config/brave_build.gni b/build/config/brave_build.gni index c3afdcc4bbc..b2814a0e27a 100644 --- a/build/config/brave_build.gni +++ b/build/config/brave_build.gni @@ -3,6 +3,8 @@ # import("//build/config/chrome_build.gni") in compiler.gni import("//brave/brave_repack_locales.gni") import("//brave/browser/extensions/resources.gni") +import("//brave/browser/resources/extensions/sources.gni") +import("//brave/browser/resources/settings/sources.gni") import("//brave/browser/sources.gni") import("//brave/build/config/compiler.gni") import("//brave/build/features.gni") @@ -13,8 +15,7 @@ import("//brave/components/sync/driver/sources.gni") import("//brave/components/sync/sources.gni") import("//brave/components/sync_device_info/sources.gni") import("//brave/installer/linux/sources.gni") +import("//brave/net/dns/sources.gni") import("//brave/net/sources.gni") import("//brave/renderer/sources.gni") import("//brave/third_party/blink/renderer/includes.gni") -import("//brave/browser/resources/settings/sources.gni") -import("//brave/browser/resources/extensions/sources.gni") diff --git a/chromium_src/net/dns/dns_transaction_unittest.cc b/chromium_src/net/dns/dns_transaction_unittest.cc new file mode 100644 index 00000000000..fb8110010e7 --- /dev/null +++ b/chromium_src/net/dns/dns_transaction_unittest.cc @@ -0,0 +1,116 @@ +/* Copyright (c) 2021 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/test/scoped_feature_list.h" +#include "brave/components/unstoppable_domains/constants.h" +#include "brave/components/unstoppable_domains/features.h" + +#include "../../../../net/dns/dns_transaction_unittest.cc" + +namespace { + +static const char kTestCryptoHostName[] = "test.crypto"; + +// Response contains IP address: 142.250.72.196 for test.crypto. +static const uint8_t kTestCryptoResponseDatagram[] = { + 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x74, 0x65, 0x73, 0x74, 0x06, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, + 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, + 0x00, 0x00, 0xa2, 0x00, 0x04, 0x8e, 0xfa, 0x48, 0xc4}; + +} // namespace + +namespace net { + +class BraveDnsTransactionTest : public DnsTransactionTestBase, + public WithTaskEnvironment { + public: + BraveDnsTransactionTest() { + feature_list_.InitAndEnableFeature( + unstoppable_domains::features::kUnstoppableDomains); + } + + ~BraveDnsTransactionTest() override = default; + + void BraveConfigureDohServers(bool multiple_server) { + GURL url(unstoppable_domains::kDoHResolver); + URLRequestFilter* filter = URLRequestFilter::GetInstance(); + filter->AddHostnameInterceptor(url.scheme(), url.host(), + std::make_unique(this)); + config_.dns_over_https_servers.push_back( + {unstoppable_domains::kDoHResolver, true}); + + if (multiple_server) { + GURL url2("https://test.com/dns-query"); + filter->AddHostnameInterceptor(url2.scheme(), url2.host(), + std::make_unique(this)); + config_.dns_over_https_servers.push_back({url2.spec(), true}); + } + + ConfigureFactory(); + for (size_t server_index = 0; + server_index < config_.dns_over_https_servers.size(); ++server_index) { + resolve_context_->RecordServerSuccess( + server_index, true /* is_doh_server */, session_.get()); + } + } + + private: + base::test::ScopedFeatureList feature_list_; +}; + +TEST_F(BraveDnsTransactionTest, SkipUDResolverForNonCryptoDomainsSingleServer) { + BraveConfigureDohServers(false); + EXPECT_TRUE(resolve_context_->GetDohServerAvailability( + 0u /* doh_server_index */, session_.get())); + TransactionHelper helper0(ERR_BLOCKED_BY_CLIENT); + helper0.StartTransaction(transaction_factory_.get(), kT0HostName, kT0Qtype, + true /* secure */, resolve_context_.get()); + helper0.RunUntilComplete(); +} + +TEST_F(BraveDnsTransactionTest, + SkipUDResolverForNonCryptoDomainsMultipleServers) { + BraveConfigureDohServers(true); + AddQueryAndResponse(0, kT0HostName, kT0Qtype, kT0ResponseDatagram, + base::size(kT0ResponseDatagram), SYNCHRONOUS, + Transport::HTTPS, nullptr /* opt_rdata */, + DnsQuery::PaddingStrategy::BLOCK_LENGTH_128, + false /* enqueue_transaction_id */); + TransactionHelper helper0(kT0RecordCount); + helper0.StartTransaction(transaction_factory_.get(), kT0HostName, kT0Qtype, + true /* secure */, resolve_context_.get()); + helper0.RunUntilComplete(); +} + +TEST_F(BraveDnsTransactionTest, UseUDResolverForCryptoDomainsSingleServer) { + BraveConfigureDohServers(false); + AddQueryAndResponse( + 0, kTestCryptoHostName, dns_protocol::kTypeA, kTestCryptoResponseDatagram, + base::size(kTestCryptoResponseDatagram), SYNCHRONOUS, Transport::HTTPS, + nullptr /* opt_rdata */, DnsQuery::PaddingStrategy::BLOCK_LENGTH_128, + false /* enqueue_transaction_id */); + TransactionHelper helper0(1); + helper0.StartTransaction(transaction_factory_.get(), kTestCryptoHostName, + dns_protocol::kTypeA, true /* secure */, + resolve_context_.get()); + helper0.RunUntilComplete(); +} + +TEST_F(BraveDnsTransactionTest, UseUDResolverForCryptoDomainsMultipleServer) { + BraveConfigureDohServers(true); + AddQueryAndResponse( + 0, kTestCryptoHostName, dns_protocol::kTypeA, kTestCryptoResponseDatagram, + base::size(kTestCryptoResponseDatagram), SYNCHRONOUS, Transport::HTTPS, + nullptr /* opt_rdata */, DnsQuery::PaddingStrategy::BLOCK_LENGTH_128, + false /* enqueue_transaction_id */); + TransactionHelper helper0(1); + helper0.StartTransaction(transaction_factory_.get(), kTestCryptoHostName, + dns_protocol::kTypeA, true /* secure */, + resolve_context_.get()); + helper0.RunUntilComplete(); +} + +} // namespace net diff --git a/net/dns/sources.gni b/net/dns/sources.gni new file mode 100644 index 00000000000..8d6c6c53bbf --- /dev/null +++ b/net/dns/sources.gni @@ -0,0 +1,6 @@ +# Copyright (c) 2021 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/. + +brave_dns_friend = [ "//brave/browser/unstoppable_domains/test:unit_tests" ] diff --git a/patches/net-dns-BUILD.gn.patch b/patches/net-dns-BUILD.gn.patch new file mode 100644 index 00000000000..96ddf756886 --- /dev/null +++ b/patches/net-dns-BUILD.gn.patch @@ -0,0 +1,28 @@ +diff --git a/net/dns/BUILD.gn b/net/dns/BUILD.gn +index ea7c29d285af754ee3c1ca0537645b05602d55f4..90a40ae96ca2de9048eab86fd2f45e3e3555b6b4 100644 +--- a/net/dns/BUILD.gn ++++ b/net/dns/BUILD.gn +@@ -23,6 +23,7 @@ source_set("dns") { + "//net/*", + "//services/network/*", + ] ++ friend += brave_dns_friend + + public = [] + sources = [ +@@ -205,6 +206,7 @@ source_set("host_resolver") { + # wrapper. + "//services/proxy_resolver/*", + ] ++ friend += brave_dns_friend + + sources = [] + public = [] +@@ -309,6 +311,7 @@ source_set("dns_client") { + "//net/*", + "//services/network/*", + ] ++ friend += brave_dns_friend + + sources = [] + public = [] diff --git a/test/BUILD.gn b/test/BUILD.gn index ac7a8c1f693..c0031464648 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -17,6 +17,7 @@ import("//brave/components/greaselion/browser/buildflags/buildflags.gni") import("//brave/components/ipfs/buildflags/buildflags.gni") import("//brave/components/speedreader/buildflags.gni") import("//brave/components/tor/buildflags/buildflags.gni") +import("//brave/components/unstoppable_domains/buildflags/buildflags.gni") import("//brave/test/testing.gni") import("//chrome/common/features.gni") import("//components/gcm_driver/config.gni") @@ -200,6 +201,10 @@ test("brave_unit_tests") { "//services/preferences/public/cpp", ] + if (unstoppable_domains_enabled) { + deps += [ "//brave/browser/unstoppable_domains/test:unit_tests" ] + } + if (toolkit_views) { deps += [ "//chrome/browser/ui/views" ] } @@ -629,6 +634,10 @@ if (!is_android) { "//testing/gmock", ] + if (unstoppable_domains_enabled) { + deps += [ "//brave/browser/unstoppable_domains/test:browser_tests" ] + } + if (enable_brave_perf_predictor) { sources += [ "//brave/components/brave_perf_predictor/browser/perf_predictor_tab_helper_browsertest.cc" ] From 69a67d16a9d7e88d62f79813724440575b7031ba Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 9 Feb 2021 19:12:57 -0800 Subject: [PATCH 06/11] fix DEPS --- chromium_src/net/DEPS | 1 + components/unstoppable_domains/DEPS | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 components/unstoppable_domains/DEPS diff --git a/chromium_src/net/DEPS b/chromium_src/net/DEPS index 518b5f55c96..ac9b5ec33aa 100644 --- a/chromium_src/net/DEPS +++ b/chromium_src/net/DEPS @@ -1,6 +1,7 @@ include_rules = [ "+../../../../net/base", "+../../../../net/cookies", + "+../../../../net/dns", "+../../../../net/log", "+../../../../net/proxy_resolution", "+../../../../net/socket", diff --git a/components/unstoppable_domains/DEPS b/components/unstoppable_domains/DEPS new file mode 100644 index 00000000000..778791c5d95 --- /dev/null +++ b/components/unstoppable_domains/DEPS @@ -0,0 +1,14 @@ +include_rules = [ + "+base", + "+components/keyed_service/core", + "+components/grit", + "+components/prefs", + "+components/security_interstitials/content", + "+components/security_interstitials/core", + "+components/user_prefs", + "+content/public/browser", + "+content/public/common", + "+net", + "+ui/base", + "+url", +] From a042e230828e6c1033a9debf0f79d0a77d05d3c1 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Thu, 11 Feb 2021 13:40:27 -0800 Subject: [PATCH 07/11] Use constexpr to defined constants in header file directly --- components/unstoppable_domains/BUILD.gn | 7 +------ components/unstoppable_domains/constants.cc | 14 -------------- components/unstoppable_domains/constants.h | 5 +++-- components/unstoppable_domains/features.cc | 17 ----------------- components/unstoppable_domains/features.h | 7 +++---- components/unstoppable_domains/pref_names.cc | 16 ---------------- components/unstoppable_domains/pref_names.h | 7 ++++++- 7 files changed, 13 insertions(+), 60 deletions(-) delete mode 100644 components/unstoppable_domains/constants.cc delete mode 100644 components/unstoppable_domains/features.cc delete mode 100644 components/unstoppable_domains/pref_names.cc diff --git a/components/unstoppable_domains/BUILD.gn b/components/unstoppable_domains/BUILD.gn index 740e796b61c..e469314b1d9 100644 --- a/components/unstoppable_domains/BUILD.gn +++ b/components/unstoppable_domains/BUILD.gn @@ -5,18 +5,13 @@ source_set("constants") { sources = [ - "constants.cc", "constants.h", - "pref_names.cc", "pref_names.h", ] } source_set("features") { - sources = [ - "features.cc", - "features.h", - ] + sources = [ "features.h" ] deps = [ "//base" ] } diff --git a/components/unstoppable_domains/constants.cc b/components/unstoppable_domains/constants.cc deleted file mode 100644 index a826878b5f5..00000000000 --- a/components/unstoppable_domains/constants.cc +++ /dev/null @@ -1,14 +0,0 @@ -/* Copyright (c) 2021 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/components/unstoppable_domains/constants.h" - -namespace unstoppable_domains { - -const char kCryptoDomain[] = ".crypto"; -const char kDoHResolver[] = - "https://resolver.unstoppable.io/dns-query{?brave_UD}"; - -} // namespace unstoppable_domains diff --git a/components/unstoppable_domains/constants.h b/components/unstoppable_domains/constants.h index 5f6042d1a5e..702a2b49015 100644 --- a/components/unstoppable_domains/constants.h +++ b/components/unstoppable_domains/constants.h @@ -8,8 +8,9 @@ namespace unstoppable_domains { -extern const char kCryptoDomain[]; -extern const char kDoHResolver[]; +constexpr char kCryptoDomain[] = ".crypto"; +constexpr char kDoHResolver[] = + "https://resolver.unstoppable.io/dns-query{?brave_UD}"; enum class ResolveMethodTypes { ASK, diff --git a/components/unstoppable_domains/features.cc b/components/unstoppable_domains/features.cc deleted file mode 100644 index b5fa4695b4b..00000000000 --- a/components/unstoppable_domains/features.cc +++ /dev/null @@ -1,17 +0,0 @@ -/* Copyright (c) 2021 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/components/unstoppable_domains/features.h" - -#include "base/feature_list.h" - -namespace unstoppable_domains { -namespace features { - -const base::Feature kUnstoppableDomains{"UnstoppableDomains", - base::FEATURE_DISABLED_BY_DEFAULT}; - -} // namespace features -} // namespace unstoppable_domains diff --git a/components/unstoppable_domains/features.h b/components/unstoppable_domains/features.h index 688169f5713..61be4b02372 100644 --- a/components/unstoppable_domains/features.h +++ b/components/unstoppable_domains/features.h @@ -6,14 +6,13 @@ #ifndef BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_FEATURES_H_ #define BRAVE_COMPONENTS_UNSTOPPABLE_DOMAINS_FEATURES_H_ -namespace base { -struct Feature; -} // namespace base +#include "base/feature_list.h" namespace unstoppable_domains { namespace features { -extern const base::Feature kUnstoppableDomains; +constexpr base::Feature kUnstoppableDomains{"UnstoppableDomains", + base::FEATURE_DISABLED_BY_DEFAULT}; } // namespace features } // namespace unstoppable_domains diff --git a/components/unstoppable_domains/pref_names.cc b/components/unstoppable_domains/pref_names.cc deleted file mode 100644 index cfe58f0a36d..00000000000 --- a/components/unstoppable_domains/pref_names.cc +++ /dev/null @@ -1,16 +0,0 @@ -/* Copyright (c) 2021 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/components/unstoppable_domains/pref_names.h" - -namespace unstoppable_domains { - -// Used to determine which method should be used to resolve unstoppable -// domains, between: -// Disabled: Disable all unstoppable domains resolution. -// DNS Over HTTPS: Resolve domain name using a public DNS over HTTPS server. -const char kResolveMethod[] = "brave.unstoppable_domains.resolve_method"; - -} // namespace unstoppable_domains diff --git a/components/unstoppable_domains/pref_names.h b/components/unstoppable_domains/pref_names.h index 31b1265805a..23416806e91 100644 --- a/components/unstoppable_domains/pref_names.h +++ b/components/unstoppable_domains/pref_names.h @@ -8,7 +8,12 @@ namespace unstoppable_domains { -extern const char kResolveMethod[]; +// Used to determine which method should be used to resolve unstoppable +// domains, between: +// Disabled: Disable all unstoppable domains resolution. +// Ask: Ask users if they want to enable support of unstoppable domains. +// DNS Over HTTPS: Resolve domain name using a public DNS over HTTPS server. +constexpr char kResolveMethod[] = "brave.unstoppable_domains.resolve_method"; } // namespace unstoppable_domains From 63727607fba6af415547dd94b541fcfa4414c681 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Wed, 10 Feb 2021 15:58:50 -0800 Subject: [PATCH 08/11] Move needed constants from component layer to net layer --- ...unstoppable_domains_service_browsertest.cc | 1 + ...stoppable_domains_service_delegate_impl.cc | 1 + .../net/stub_resolver_config_reader.cc | 2 +- .../settings/settings_secure_dns_handler.cc | 4 +- .../net/base/lookup_string_in_fixed_set.cc | 8 +--- chromium_src/net/dns/dns_transaction.cc | 16 ++------ .../net/dns/dns_transaction_unittest.cc | 12 +----- components/unstoppable_domains/BUILD.gn | 38 +++---------------- .../buildflags/buildflags.gni | 2 - components/unstoppable_domains/constants.h | 4 -- components/unstoppable_domains/utils.cc | 1 + net/sources.gni | 9 ++--- net/unstoppable_domains/constants.h | 17 +++++++++ patches/net-BUILD.gn.patch | 10 +---- 14 files changed, 39 insertions(+), 86 deletions(-) create mode 100644 net/unstoppable_domains/constants.h diff --git a/browser/unstoppable_domains/test/unstoppable_domains_service_browsertest.cc b/browser/unstoppable_domains/test/unstoppable_domains_service_browsertest.cc index b20b9a5ff64..bd12a1b6583 100644 --- a/browser/unstoppable_domains/test/unstoppable_domains_service_browsertest.cc +++ b/browser/unstoppable_domains/test/unstoppable_domains_service_browsertest.cc @@ -7,6 +7,7 @@ #include "brave/components/unstoppable_domains/constants.h" #include "brave/components/unstoppable_domains/features.h" #include "brave/components/unstoppable_domains/pref_names.h" +#include "brave/net/unstoppable_domains/constants.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/net/secure_dns_config.h" #include "chrome/browser/net/stub_resolver_config_reader.h" diff --git a/browser/unstoppable_domains/unstoppable_domains_service_delegate_impl.cc b/browser/unstoppable_domains/unstoppable_domains_service_delegate_impl.cc index e97cd641169..48081e70157 100644 --- a/browser/unstoppable_domains/unstoppable_domains_service_delegate_impl.cc +++ b/browser/unstoppable_domains/unstoppable_domains_service_delegate_impl.cc @@ -11,6 +11,7 @@ namespace unstoppable_domains { void UnstoppableDomainsServiceDelegateImpl::UpdateNetworkService() { + // Trigger a DoH config update in network service. SystemNetworkContextManager::GetStubResolverConfigReader() ->UpdateNetworkService(false /* record_metrics */); } diff --git a/chromium_src/chrome/browser/net/stub_resolver_config_reader.cc b/chromium_src/chrome/browser/net/stub_resolver_config_reader.cc index eb01d336a3b..c89634cd6e5 100644 --- a/chromium_src/chrome/browser/net/stub_resolver_config_reader.cc +++ b/chromium_src/chrome/browser/net/stub_resolver_config_reader.cc @@ -8,8 +8,8 @@ #include "components/prefs/pref_service.h" #if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) -#include "brave/components/unstoppable_domains/constants.h" #include "brave/components/unstoppable_domains/utils.h" +#include "brave/net/unstoppable_domains/constants.h" #endif namespace { diff --git a/chromium_src/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc b/chromium_src/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc index 4b7687839f3..2f1001aa1fb 100644 --- a/chromium_src/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc +++ b/chromium_src/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc @@ -7,7 +7,7 @@ #include "brave/components/unstoppable_domains/buildflags/buildflags.h" #if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) -#include "brave/components/unstoppable_domains/constants.h" +#include "brave/net/unstoppable_domains/constants.h" #endif namespace settings { @@ -24,7 +24,7 @@ namespace { // Hide Unstoppable Domains resolver in the custom provider list in settings // because it will be used for name resolution only for TLDs from Unstoppable -// Domains, instaed of a global DoH settings. +// Domains, instead of a global DoH setting. std::unique_ptr CreateSecureDnsSettingDict() { auto dict = CreateSecureDnsSettingDict_ChromiumImpl(); #if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) diff --git a/chromium_src/net/base/lookup_string_in_fixed_set.cc b/chromium_src/net/base/lookup_string_in_fixed_set.cc index a7df828e996..9f6c3e5a9d4 100644 --- a/chromium_src/net/base/lookup_string_in_fixed_set.cc +++ b/chromium_src/net/base/lookup_string_in_fixed_set.cc @@ -10,11 +10,7 @@ #undef LookupSuffixInReversedSet #include "base/strings/string_util.h" -#include "brave/components/unstoppable_domains/buildflags/buildflags.h" - -#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) -#include "brave/components/unstoppable_domains/constants.h" -#endif +#include "brave/net/unstoppable_domains/constants.h" namespace net { @@ -49,12 +45,10 @@ int LookupSuffixInReversedSet(const unsigned char* graph, // this, when users type *.crypto in omnibox, it will be parsed as // OmniboxInputType::URL input type instead of OmniboxInputType::UNKNOWN, // The first entry in the autocomplete list will be URL instead of search. -#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) if (base::EndsWith(host, unstoppable_domains::kCryptoDomain)) { *suffix_length = strlen(unstoppable_domains::kCryptoDomain) - 1; return kDafsaFound; } -#endif return LookupSuffixInReversedSet_ChromiumImpl(graph, length, include_private, host, suffix_length); diff --git a/chromium_src/net/dns/dns_transaction.cc b/chromium_src/net/dns/dns_transaction.cc index 5dbdc659941..2bc5e20c487 100644 --- a/chromium_src/net/dns/dns_transaction.cc +++ b/chromium_src/net/dns/dns_transaction.cc @@ -4,27 +4,20 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "base/strings/string_util.h" -#include "brave/components/unstoppable_domains/buildflags/buildflags.h" +#include "brave/net/unstoppable_domains/constants.h" #include "net/dns/dns_config.h" #include "net/dns/dns_server_iterator.h" -#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) -#include "brave/components/unstoppable_domains/constants.h" -#include "brave/components/unstoppable_domains/utils.h" -#endif - namespace { bool GetNextIndex(const std::string& hostname, const net::DnsConfig& config, net::DnsServerIterator* dns_server_iterator, size_t* doh_server_index) { -#if BUILDFLAG(UNSTOPPABLE_DOMAINS_ENABLED) // Skip unstoppable domains resolver for non-crypto domains. - if (unstoppable_domains::IsUnstoppableDomainsEnabled() && - !base::EndsWith(hostname, unstoppable_domains::kCryptoDomain) && - config.dns_over_https_servers[*doh_server_index].server_template == - unstoppable_domains::kDoHResolver) { + if (config.dns_over_https_servers[*doh_server_index].server_template == + unstoppable_domains::kDoHResolver && + !base::EndsWith(hostname, unstoppable_domains::kCryptoDomain)) { // No next available index to attempt. if (!dns_server_iterator->AttemptAvailable()) { return false; @@ -32,7 +25,6 @@ bool GetNextIndex(const std::string& hostname, *doh_server_index = dns_server_iterator->GetNextAttemptIndex(); } -#endif return true; } diff --git a/chromium_src/net/dns/dns_transaction_unittest.cc b/chromium_src/net/dns/dns_transaction_unittest.cc index fb8110010e7..7360a4e3477 100644 --- a/chromium_src/net/dns/dns_transaction_unittest.cc +++ b/chromium_src/net/dns/dns_transaction_unittest.cc @@ -3,9 +3,7 @@ * 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/test/scoped_feature_list.h" -#include "brave/components/unstoppable_domains/constants.h" -#include "brave/components/unstoppable_domains/features.h" +#include "brave/net/unstoppable_domains/constants.h" #include "../../../../net/dns/dns_transaction_unittest.cc" @@ -27,10 +25,7 @@ namespace net { class BraveDnsTransactionTest : public DnsTransactionTestBase, public WithTaskEnvironment { public: - BraveDnsTransactionTest() { - feature_list_.InitAndEnableFeature( - unstoppable_domains::features::kUnstoppableDomains); - } + BraveDnsTransactionTest() = default; ~BraveDnsTransactionTest() override = default; @@ -56,9 +51,6 @@ class BraveDnsTransactionTest : public DnsTransactionTestBase, server_index, true /* is_doh_server */, session_.get()); } } - - private: - base::test::ScopedFeatureList feature_list_; }; TEST_F(BraveDnsTransactionTest, SkipUDResolverForNonCryptoDomainsSingleServer) { diff --git a/components/unstoppable_domains/BUILD.gn b/components/unstoppable_domains/BUILD.gn index e469314b1d9..439f42687de 100644 --- a/components/unstoppable_domains/BUILD.gn +++ b/components/unstoppable_domains/BUILD.gn @@ -3,35 +3,11 @@ # 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/. */ -source_set("constants") { - sources = [ - "constants.h", - "pref_names.h", - ] -} - -source_set("features") { - sources = [ "features.h" ] - - deps = [ "//base" ] -} - -source_set("utils") { - sources = [ - "utils.cc", - "utils.h", - ] - - deps = [ - ":constants", - ":features", - "//components/prefs", - "//url", - ] -} - source_set("unstoppable_domains") { sources = [ + "constants.h", + "features.h", + "pref_names.h", "unstoppable_domains_interstitial_controller_client.cc", "unstoppable_domains_interstitial_controller_client.h", "unstoppable_domains_navigation_throttle.cc", @@ -41,12 +17,8 @@ source_set("unstoppable_domains") { "unstoppable_domains_service.cc", "unstoppable_domains_service.h", "unstoppable_domains_service_delegate.h", - ] - - public_deps = [ - ":constants", - ":features", - ":utils", + "utils.cc", + "utils.h", ] deps = [ diff --git a/components/unstoppable_domains/buildflags/buildflags.gni b/components/unstoppable_domains/buildflags/buildflags.gni index dda7786ea2e..d86b13dcaa5 100644 --- a/components/unstoppable_domains/buildflags/buildflags.gni +++ b/components/unstoppable_domains/buildflags/buildflags.gni @@ -1,5 +1,3 @@ -import("//build/config/features.gni") - declare_args() { unstoppable_domains_enabled = is_mac || is_linux || is_win } diff --git a/components/unstoppable_domains/constants.h b/components/unstoppable_domains/constants.h index 702a2b49015..53b19b782b4 100644 --- a/components/unstoppable_domains/constants.h +++ b/components/unstoppable_domains/constants.h @@ -8,10 +8,6 @@ namespace unstoppable_domains { -constexpr char kCryptoDomain[] = ".crypto"; -constexpr char kDoHResolver[] = - "https://resolver.unstoppable.io/dns-query{?brave_UD}"; - enum class ResolveMethodTypes { ASK, DISABLED, diff --git a/components/unstoppable_domains/utils.cc b/components/unstoppable_domains/utils.cc index 4957cc16339..84e2cc4b173 100644 --- a/components/unstoppable_domains/utils.cc +++ b/components/unstoppable_domains/utils.cc @@ -10,6 +10,7 @@ #include "brave/components/unstoppable_domains/constants.h" #include "brave/components/unstoppable_domains/features.h" #include "brave/components/unstoppable_domains/pref_names.h" +#include "brave/net/unstoppable_domains/constants.h" #include "components/prefs/pref_service.h" #include "url/gurl.h" diff --git a/net/sources.gni b/net/sources.gni index ce78c472b24..13dd035d6a1 100644 --- a/net/sources.gni +++ b/net/sources.gni @@ -3,13 +3,10 @@ # 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/. +import("//brave/components/unstoppable_domains/buildflags/buildflags.gni") + brave_net_sources = [ "//brave/net/proxy_resolution/proxy_config_service_tor.cc", "//brave/net/proxy_resolution/proxy_config_service_tor.h", -] - -brave_net_deps = [ - "//brave/components/unstoppable_domains:constants", - "//brave/components/unstoppable_domains:utils", - "//brave/components/unstoppable_domains/buildflags", + "//brave/net/unstoppable_domains/constants.h", ] diff --git a/net/unstoppable_domains/constants.h b/net/unstoppable_domains/constants.h new file mode 100644 index 00000000000..f4ab9c7d6e5 --- /dev/null +++ b/net/unstoppable_domains/constants.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2021 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_NET_UNSTOPPABLE_DOMAINS_CONSTANTS_H_ +#define BRAVE_NET_UNSTOPPABLE_DOMAINS_CONSTANTS_H_ + +namespace unstoppable_domains { + +constexpr char kCryptoDomain[] = ".crypto"; +constexpr char kDoHResolver[] = + "https://resolver.unstoppable.io/dns-query{?brave_UD}"; + +} // namespace unstoppable_domains + +#endif // BRAVE_NET_UNSTOPPABLE_DOMAINS_CONSTANTS_H_ diff --git a/patches/net-BUILD.gn.patch b/patches/net-BUILD.gn.patch index fd17d3048e6..da3168afa28 100644 --- a/patches/net-BUILD.gn.patch +++ b/patches/net-BUILD.gn.patch @@ -1,5 +1,5 @@ diff --git a/net/BUILD.gn b/net/BUILD.gn -index d214ac4959ff76dd8e95d327c9839ee967d14444..709ca7acfa482e5d85f2e4207bbbe2d93dd61fee 100644 +index d214ac4959ff76dd8e95d327c9839ee967d14444..292ce4d3c2c294be04cc8d7ba8ed5ac06e2d58f4 100644 --- a/net/BUILD.gn +++ b/net/BUILD.gn @@ -1176,6 +1176,7 @@ component("net") { @@ -10,11 +10,3 @@ index d214ac4959ff76dd8e95d327c9839ee967d14444..709ca7acfa482e5d85f2e4207bbbe2d9 if (is_android) { sources += [ -@@ -1695,6 +1696,7 @@ source_set("net_deps") { - ] - - public_configs = net_configs -+ public_deps += brave_net_deps - - if (!is_nacl) { - public_deps += [ From 3197149cff7a3095590be345617b6ce271b4a6e5 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Tue, 23 Feb 2021 10:47:33 -0800 Subject: [PATCH 09/11] Address review comments - Do not abbreviate unstoppable domains in brave_default_extensions_page - Add comment to stub_resolver_config_reader override - Separate Brave's own DnsTransaction unit tests - Remove patch of stub_resolver_config_reader.cc and settings_secure_dns_handler.cc - Update test - Move browser/unstoppable_domains into its own target --- browser/BUILD.gn | 9 +- .../brave_default_extensions_page.html | 6 +- .../brave_default_extensions_page.js | 6 +- browser/unstoppable_domains/BUILD.gn | 28 + browser/unstoppable_domains/test/BUILD.gn | 6 +- ...unstoppable_domains_service_browsertest.cc | 36 +- .../net/stub_resolver_config_reader.cc | 47 +- .../settings/settings_secure_dns_handler.cc | 50 - .../net/dns/dns_transaction_unittest.cc | 108 -- net/dns/dns_transaction_unittest.cc | 1023 +++++++++++++++++ ...r-net-stub_resolver_config_reader.cc.patch | 12 - ...tings-settings_secure_dns_handler.cc.patch | 13 - 12 files changed, 1124 insertions(+), 220 deletions(-) create mode 100644 browser/unstoppable_domains/BUILD.gn delete mode 100644 chromium_src/chrome/browser/ui/webui/settings/settings_secure_dns_handler.cc delete mode 100644 chromium_src/net/dns/dns_transaction_unittest.cc create mode 100644 net/dns/dns_transaction_unittest.cc delete mode 100644 patches/chrome-browser-net-stub_resolver_config_reader.cc.patch delete mode 100644 patches/chrome-browser-ui-webui-settings-settings_secure_dns_handler.cc.patch diff --git a/browser/BUILD.gn b/browser/BUILD.gn index b93014a5e13..edc79926ebe 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -292,14 +292,7 @@ source_set("browser_process") { } if (unstoppable_domains_enabled) { - sources += [ - "unstoppable_domains/unstoppable_domains_service_delegate_impl.cc", - "unstoppable_domains/unstoppable_domains_service_delegate_impl.h", - "unstoppable_domains/unstoppable_domains_service_factory.cc", - "unstoppable_domains/unstoppable_domains_service_factory.h", - ] - - deps += [ "//brave/components/unstoppable_domains" ] + deps += [ "//brave/browser/unstoppable_domains" ] } if (ipfs_enabled) { diff --git a/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html b/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html index e4dbb21ea6d..c9bb46c6e39 100644 --- a/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html +++ b/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html @@ -85,12 +85,12 @@ sub-label="$i18n{hangoutsEnabledDesc}" on-settings-boolean-control-change="onHangoutsEnabledChange_"> -