From 04c2403755148fb0218d67efb944ec60a38add79 Mon Sep 17 00:00:00 2001 From: brave-builds Date: Thu, 28 Jan 2021 01:34:38 +0000 Subject: [PATCH] Uplift of #6933 (squashed) to release --- app/brave_main_delegate.cc | 4 + app/brave_main_delegate_browsertest.cc | 4 + .../disable_client_hints_browsertest.cc | 29 ++- .../origin_trials/origin_trial_context.cc | 35 +++ .../core/origin_trials/origin_trial_context.h | 21 ++ .../core/origin_trials/origin_trials.cc | 60 +++++ .../core/origin_trials/origin_trials.h | 20 ++ renderer/brave_content_renderer_client.cc | 5 + renderer/test/BUILD.gn | 29 +++ .../test/digital_goods_api_browsertest.cc | 92 ++++++++ .../native_file_system_api_browsertest.cc | 162 +++++++++++++ .../subresource_web_bundles_browsertest.cc | 212 ++++++++++++++++++ script/redirect-cc.py | 11 +- test/BUILD.gn | 1 + 14 files changed, 678 insertions(+), 7 deletions(-) create mode 100644 chromium_src/third_party/blink/renderer/core/origin_trials/origin_trial_context.cc create mode 100644 chromium_src/third_party/blink/renderer/core/origin_trials/origin_trial_context.h create mode 100644 chromium_src/third_party/blink/renderer/core/origin_trials/origin_trials.cc create mode 100644 chromium_src/third_party/blink/renderer/core/origin_trials/origin_trials.h create mode 100644 renderer/test/BUILD.gn create mode 100644 renderer/test/digital_goods_api_browsertest.cc create mode 100644 renderer/test/native_file_system_api_browsertest.cc create mode 100644 renderer/test/subresource_web_bundles_browsertest.cc diff --git a/app/brave_main_delegate.cc b/app/brave_main_delegate.cc index f6024d24f678..bf160248b18f 100644 --- a/app/brave_main_delegate.cc +++ b/app/brave_main_delegate.cc @@ -224,10 +224,14 @@ bool BraveMainDelegate::BasicStartupComplete(int* exit_code) { autofill::features::kAutofillEnableAccountWalletStorage.name, autofill::features::kAutofillServerCommunication.name, blink::features::kTextFragmentAnchor.name, + features::kDirectSockets.name, features::kIdleDetection.name, + features::kLangClientHintHeader.name, features::kNotificationTriggers.name, features::kPrivacySettingsRedesign.name, + features::kSignedExchangePrefetchCacheForNavigations.name, features::kSignedExchangeSubresourcePrefetch.name, + features::kSubresourceWebBundles.name, features::kTabHoverCards.name, features::kWebOTP.name, network_time::kNetworkTimeServiceQuerying.name, diff --git a/app/brave_main_delegate_browsertest.cc b/app/brave_main_delegate_browsertest.cc index e12c44a0f805..fe1eaa778fdc 100644 --- a/app/brave_main_delegate_browsertest.cc +++ b/app/brave_main_delegate_browsertest.cc @@ -65,10 +65,14 @@ IN_PROC_BROWSER_TEST_F(BraveMainDelegateBrowserTest, DisabledFeatures) { &autofill::features::kAutofillEnableAccountWalletStorage, &autofill::features::kAutofillServerCommunication, &blink::features::kTextFragmentAnchor, + &features::kDirectSockets, &features::kIdleDetection, + &features::kLangClientHintHeader, &features::kNotificationTriggers, &features::kPrivacySettingsRedesign, + &features::kSignedExchangePrefetchCacheForNavigations, &features::kSignedExchangeSubresourcePrefetch, + &features::kSubresourceWebBundles, &features::kTabHoverCards, &features::kWebOTP, &network_time::kNetworkTimeServiceQuerying, diff --git a/chromium_src/third_party/blink/public/platform/disable_client_hints_browsertest.cc b/chromium_src/third_party/blink/public/platform/disable_client_hints_browsertest.cc index 855911cec066..e2f9502a4afb 100644 --- a/chromium_src/third_party/blink/public/platform/disable_client_hints_browsertest.cc +++ b/chromium_src/third_party/blink/public/platform/disable_client_hints_browsertest.cc @@ -4,12 +4,15 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "base/bind.h" +#include "base/feature_list.h" #include "base/path_service.h" #include "base/run_loop.h" #include "base/stl_util.h" +#include "base/test/scoped_feature_list.h" #include "brave/common/brave_paths.h" #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/ui_test_utils.h" +#include "content/public/common/content_features.h" #include "content/public/test/browser_test.h" #include "net/dns/mock_host_resolver.h" #include "net/http/http_request_headers.h" @@ -19,7 +22,8 @@ const char kClientHints[] = "/ch.html"; -class ClientHintsBrowserTest : public InProcessBrowserTest { +class ClientHintsBrowserTest : public InProcessBrowserTest, + public ::testing::WithParamInterface { public: ClientHintsBrowserTest() : https_server_(net::EmbeddedTestServer::TYPE_HTTPS), @@ -40,7 +44,19 @@ class ClientHintsBrowserTest : public InProcessBrowserTest { ~ClientHintsBrowserTest() override {} - void SetUp() override { InProcessBrowserTest::SetUp(); } + bool IsLangClientHintHeaderEnabled() { return GetParam(); } + + void SetUp() override { + if (IsLangClientHintHeaderEnabled()) { + // Test that even with Lang CH feature enabled, there is no header. + scoped_feature_list_.InitAndEnableFeature( + features::kLangClientHintHeader); + } else { + scoped_feature_list_.InitAndDisableFeature( + features::kLangClientHintHeader); + } + InProcessBrowserTest::SetUp(); + } void SetUpOnMainThread() override { host_resolver()->AddRule("*", "127.0.0.1"); @@ -68,11 +84,18 @@ class ClientHintsBrowserTest : public InProcessBrowserTest { net::EmbeddedTestServer https_server_; GURL client_hints_url_; size_t count_client_hints_headers_seen_; + base::test::ScopedFeatureList scoped_feature_list_; DISALLOW_COPY_AND_ASSIGN(ClientHintsBrowserTest); }; -IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, ClientHintsDisabled) { +IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, ClientHintsDisabled) { + EXPECT_EQ(IsLangClientHintHeaderEnabled(), + base::FeatureList::IsEnabled(features::kLangClientHintHeader)); ui_test_utils::NavigateToURL(browser(), client_hints_url()); EXPECT_EQ(0u, count_client_hints_headers_seen()); } + +INSTANTIATE_TEST_SUITE_P(ClientHintsBrowserTest, + ClientHintsBrowserTest, + ::testing::Bool()); diff --git a/chromium_src/third_party/blink/renderer/core/origin_trials/origin_trial_context.cc b/chromium_src/third_party/blink/renderer/core/origin_trials/origin_trial_context.cc new file mode 100644 index 000000000000..1c1645121f70 --- /dev/null +++ b/chromium_src/third_party/blink/renderer/core/origin_trials/origin_trial_context.cc @@ -0,0 +1,35 @@ +/* Copyright (c) 2020 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 "third_party/blink/renderer/core/origin_trials/origin_trial_context.h" + +#define AddFeature AddFeature_ChromiumImpl +#define AddForceEnabledTrials AddForceEnabledTrials_ChromiumImpl +#include "../../../../../../../third_party/blink/renderer/core/origin_trials/origin_trial_context.cc" +#undef AddForceEnabledTrials +#undef AddFeature + +namespace blink { + +// AddFeature doesn't check if origin_trials::IsTrialValid. +void OriginTrialContext::AddFeature(OriginTrialFeature feature) { + if (origin_trials::IsTrialDisabledInBrave(feature)) + return; + + AddFeature_ChromiumImpl(feature); +} + +// AddForceEnabledTrials only has a DCHECK with origin_trials::IsTrialValid. +void OriginTrialContext::AddForceEnabledTrials( + const Vector& trial_names) { + for (const String& trial_name : trial_names) { + if (origin_trials::IsTrialDisabledInBrave(trial_name)) + return; + } + + AddForceEnabledTrials_ChromiumImpl(trial_names); +} + +} // namespace blink diff --git a/chromium_src/third_party/blink/renderer/core/origin_trials/origin_trial_context.h b/chromium_src/third_party/blink/renderer/core/origin_trials/origin_trial_context.h new file mode 100644 index 000000000000..3ba357a836f2 --- /dev/null +++ b/chromium_src/third_party/blink/renderer/core/origin_trials/origin_trial_context.h @@ -0,0 +1,21 @@ +/* Copyright (c) 2020 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_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_ORIGIN_TRIALS_ORIGIN_TRIAL_CONTEXT_H_ +#define BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_ORIGIN_TRIALS_ORIGIN_TRIAL_CONTEXT_H_ + +#define AddFeature \ + AddFeature_ChromiumImpl(OriginTrialFeature feature); \ + void AddFeature + +#define AddForceEnabledTrials \ + AddForceEnabledTrials_ChromiumImpl(const Vector& trial_names); \ + void AddForceEnabledTrials + +#include "../../../../../../../third_party/blink/renderer/core/origin_trials/origin_trial_context.h" +#undef AddForceEnabledTrials +#undef AddFeature + +#endif // BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_ORIGIN_TRIALS_ORIGIN_TRIAL_CONTEXT_H_ diff --git a/chromium_src/third_party/blink/renderer/core/origin_trials/origin_trials.cc b/chromium_src/third_party/blink/renderer/core/origin_trials/origin_trials.cc new file mode 100644 index 000000000000..37cc5cdd541d --- /dev/null +++ b/chromium_src/third_party/blink/renderer/core/origin_trials/origin_trials.cc @@ -0,0 +1,60 @@ +/* Copyright (c) 2020 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 "third_party/blink/renderer/core/origin_trials/origin_trials.h" + +namespace blink { +namespace origin_trials { +bool IsTrialValid_ChromiumImpl(const StringView& trial_name); +} // namespace origin_trials +} // namespace blink + +#define IsTrialValid IsTrialValid_ChromiumImpl +#include "../gen/third_party/blink/renderer/core/origin_trials/origin_trials.cc" +#undef IsTrialValid + +namespace blink { +namespace origin_trials { + +bool IsTrialDisabledInBrave(const StringView& trial_name) { + // When updating also update the array in the overload below. + static const char* const kBraveDisabledTrialNames[] = { + "DigitalGoods", + "NativeFileSystem2", + "SignedExchangeSubresourcePrefetch", + "SubresourceWebBundles", + }; + + if (base::Contains(kBraveDisabledTrialNames, trial_name)) { + // Check if this is still a valid trial name in Chromium. If not, it needs + // to be changed as in Chromium or removed. + DCHECK(IsTrialValid_ChromiumImpl(trial_name)); + return true; + } + + return false; +} + +bool IsTrialDisabledInBrave(OriginTrialFeature feature) { + // When updating also update the array in the overload above. + static const std::array kBraveDisabledTrialFeatures = { + OriginTrialFeature::kDigitalGoods, + OriginTrialFeature::kNativeFileSystem, + OriginTrialFeature::kSignedExchangeSubresourcePrefetch, + OriginTrialFeature::kSubresourceWebBundles, + }; + + return base::Contains(kBraveDisabledTrialFeatures, feature); +} + +bool IsTrialValid(const StringView& trial_name) { + if (IsTrialDisabledInBrave(trial_name)) + return false; + + return IsTrialValid_ChromiumImpl(trial_name); +} + +} // namespace origin_trials +} // namespace blink diff --git a/chromium_src/third_party/blink/renderer/core/origin_trials/origin_trials.h b/chromium_src/third_party/blink/renderer/core/origin_trials/origin_trials.h new file mode 100644 index 000000000000..05c3c620c46d --- /dev/null +++ b/chromium_src/third_party/blink/renderer/core/origin_trials/origin_trials.h @@ -0,0 +1,20 @@ +/* Copyright (c) 2020 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_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_ORIGIN_TRIALS_ORIGIN_TRIALS_H_ +#define BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_ORIGIN_TRIALS_ORIGIN_TRIALS_H_ + +#include "../../../../../../../third_party/blink/renderer/core/origin_trials/origin_trials.h" + +namespace blink { +namespace origin_trials { + +bool IsTrialDisabledInBrave(const StringView& trial_name); +bool IsTrialDisabledInBrave(OriginTrialFeature feature); + +} // namespace origin_trials +} // namespace blink + +#endif // BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_ORIGIN_TRIALS_ORIGIN_TRIALS_H_ diff --git a/renderer/brave_content_renderer_client.cc b/renderer/brave_content_renderer_client.cc index 57d2952ebb02..6d0d46ff29c8 100644 --- a/renderer/brave_content_renderer_client.cc +++ b/renderer/brave_content_renderer_client.cc @@ -15,5 +15,10 @@ SetRuntimeFeaturesDefaultsBeforeBlinkInitialization() { SetRuntimeFeaturesDefaultsBeforeBlinkInitialization(); blink::WebRuntimeFeatures::EnableSharedArrayBuffer(false); + + // These features don't have dedicated WebRuntimeFeatures wrappers. + blink::WebRuntimeFeatures::EnableFeatureFromString("DigitalGoods", false); + blink::WebRuntimeFeatures::EnableFeatureFromString("NativeFileSystem", false); } + BraveContentRendererClient::~BraveContentRendererClient() = default; diff --git a/renderer/test/BUILD.gn b/renderer/test/BUILD.gn new file mode 100644 index 000000000000..89e44ef01d24 --- /dev/null +++ b/renderer/test/BUILD.gn @@ -0,0 +1,29 @@ +# Copyright (c) 2020 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("//testing/test.gni") + +source_set("browser_tests") { + testonly = true + + sources = [ + "digital_goods_api_browsertest.cc", + "native_file_system_api_browsertest.cc", + "subresource_web_bundles_browsertest.cc", + ] + + defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ] + + deps = [ + "//base/test:test_support", + "//brave/common", + "//chrome/browser/ui", + "//chrome/test:test_support_ui", + "//components/embedder_support", + "//components/web_package:test_support", + "//content/test:test_support", + "//net:test_support", + ] +} diff --git a/renderer/test/digital_goods_api_browsertest.cc b/renderer/test/digital_goods_api_browsertest.cc new file mode 100644 index 000000000000..aab59ebb0131 --- /dev/null +++ b/renderer/test/digital_goods_api_browsertest.cc @@ -0,0 +1,92 @@ +/* Copyright (c) 2020 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "base/path_service.h" +#include "brave/common/brave_paths.h" +#include "build/build_config.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 "content/public/browser/render_frame_host.h" +#include "content/public/browser/web_contents.h" +#include "content/public/common/content_switches.h" +#include "content/public/test/browser_test.h" +#include "content/public/test/browser_test_utils.h" +#include "net/dns/mock_host_resolver.h" +#include "net/test/embedded_test_server/embedded_test_server.h" +#include "url/gurl.h" + +class DigitalGoodsAPIBrowserTest : public InProcessBrowserTest, + public ::testing::WithParamInterface { + public: + DigitalGoodsAPIBrowserTest() + : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) { + brave::RegisterPathProvider(); + base::FilePath test_data_dir; + base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir); + https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_OK); + https_server_.ServeFilesFromDirectory(test_data_dir); + } + + ~DigitalGoodsAPIBrowserTest() override = default; + + bool IsDigitalGoodsAPIEnabled() { return GetParam(); } + + void SetUpCommandLine(base::CommandLine* command_line) override { + InProcessBrowserTest::SetUpCommandLine(command_line); + if (IsDigitalGoodsAPIEnabled()) { +#if defined(OS_ANDROID) + command_line->AppendSwitch( + switches::kEnableExperimentalWebPlatformFeatures); +#else + command_line->AppendSwitch(switches::kEnableBlinkTestFeatures); +#endif + } + } + + void SetUpOnMainThread() override { + InProcessBrowserTest::SetUpOnMainThread(); + + EXPECT_TRUE(https_server_.Start()); + // Map all hosts to localhost. + host_resolver()->AddRule("*", "127.0.0.1"); + } + + content::WebContents* web_contents() { + return browser()->tab_strip_model()->GetActiveWebContents(); + } + + content::RenderFrameHost* main_frame() { + return web_contents()->GetMainFrame(); + } + + protected: + net::EmbeddedTestServer https_server_; +}; + +IN_PROC_BROWSER_TEST_P(DigitalGoodsAPIBrowserTest, DigitalGoods) { + const GURL url = https_server_.GetURL("/simple.html"); + ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url)); + + auto result = + content::EvalJs(main_frame(), "window.getDigitalGoodsService()"); + if (IsDigitalGoodsAPIEnabled()) { + EXPECT_TRUE(result.error.find( + "Failed to execute 'getDigitalGoodsService' on " + "'Window': 1 argument required, but only 0 present.") != + std::string::npos) + << result.error; + } else { + EXPECT_TRUE( + result.error.find("window.getDigitalGoodsService is not a function") != + std::string::npos) + << result.error; + } +} + +INSTANTIATE_TEST_SUITE_P(DigitalGoodsAPIBrowserTest, + DigitalGoodsAPIBrowserTest, + ::testing::Bool()); diff --git a/renderer/test/native_file_system_api_browsertest.cc b/renderer/test/native_file_system_api_browsertest.cc new file mode 100644 index 000000000000..584f4a04d8f4 --- /dev/null +++ b/renderer/test/native_file_system_api_browsertest.cc @@ -0,0 +1,162 @@ +/* Copyright (c) 2020 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "base/path_service.h" +#include "base/strings/strcat.h" +#include "base/strings/string_util.h" +#include "brave/common/brave_paths.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/embedder_support/switches.h" +#include "content/public/browser/render_frame_host.h" +#include "content/public/browser/web_contents.h" +#include "content/public/test/browser_test.h" +#include "content/public/test/browser_test_utils.h" +#include "content/public/test/url_loader_interceptor.h" +#include "net/dns/mock_host_resolver.h" +#include "net/test/embedded_test_server/embedded_test_server.h" +#include "url/gurl.h" + +class NativeFileSystemAPIBrowserTest : public InProcessBrowserTest { + public: + NativeFileSystemAPIBrowserTest() + : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) { + brave::RegisterPathProvider(); + base::FilePath test_data_dir; + base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir); + https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_OK); + https_server_.ServeFilesFromDirectory(test_data_dir); + } + + ~NativeFileSystemAPIBrowserTest() override = default; + + void SetUpOnMainThread() override { + InProcessBrowserTest::SetUpOnMainThread(); + + EXPECT_TRUE(https_server_.Start()); + // Map all hosts to localhost. + host_resolver()->AddRule("*", "127.0.0.1"); + } + + content::WebContents* web_contents() { + return browser()->tab_strip_model()->GetActiveWebContents(); + } + + content::RenderFrameHost* main_frame() { + return web_contents()->GetMainFrame(); + } + + protected: + net::EmbeddedTestServer https_server_; +}; + +IN_PROC_BROWSER_TEST_F(NativeFileSystemAPIBrowserTest, FilePicker) { + const GURL url = https_server_.GetURL("/simple.html"); + ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url)); + + auto result = content::EvalJs(main_frame(), "self.showOpenFilePicker()"); + EXPECT_TRUE(result.error.find("self.showOpenFilePicker is not a function") != + std::string::npos) + << result.error; +} + +namespace { + +constexpr char kOriginTrialTestPublicKey[] = + "dRCs+TocuKkocNKa0AtZ4awrt9XKH2SQCI6o4FY6BNA="; + +const char kTestHeaders[] = "HTTP/1.1 200 OK\nContent-type: text/html\n\n"; + +constexpr char kOriginTrialTestHostname[] = "https://localhost"; +constexpr char kOriginTrialPage[] = "page.html"; + +// tools/origin_trials/generate_token.py \ +// --expire-days 3650 https://localhost NativeFileSystem2 +constexpr char kOriginTrialToken[] = + "AzOJFCOVN9n5+fKf7X2W8DpbQzs54hnLqPxDGPpm/XyfBZTgOybwDGNWhKMUVPf1qn3t7LTZA3" + "LlRBlFPbMn9AIAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly9sb2NhbGhvc3Q6NDQzIiwgImZlYXR1" + "cmUiOiAiTmF0aXZlRmlsZVN5c3RlbTIiLCAiZXhwaXJ5IjogMTkyMDkyMzIxOX0="; + +constexpr char kOriginTrialTestResponseTemplate[] = R"( + + + Native File System Origin Trial Test + META_TAG + + +)"; + +std::string GetContentForURL(const std::string& url) { + if (!base::EndsWith(url, kOriginTrialPage, base::CompareCase::SENSITIVE)) + return std::string(); + + std::string response = kOriginTrialTestResponseTemplate; + std::string meta_tag = + base::StrCat({R"()"}); + base::ReplaceFirstSubstringAfterOffset(&response, 0, "META_TAG", meta_tag); + return response; +} + +bool URLLoaderInterceptorCallback( + content::URLLoaderInterceptor::RequestParams* params) { + content::URLLoaderInterceptor::WriteResponse( + kTestHeaders, GetContentForURL(params->url_request.url.path()), + params->client.get()); + return true; +} + +} // namespace + +class NativeFileSystemOriginTrialBrowserTest : public InProcessBrowserTest { + public: + NativeFileSystemOriginTrialBrowserTest() = default; + ~NativeFileSystemOriginTrialBrowserTest() override = default; + + void SetUpDefaultCommandLine(base::CommandLine* command_line) override { + InProcessBrowserTest::SetUpDefaultCommandLine(command_line); + command_line->AppendSwitchASCII(embedder_support::kOriginTrialPublicKey, + kOriginTrialTestPublicKey); + } + + void SetUpOnMainThread() override { + InProcessBrowserTest::SetUpOnMainThread(); + + // We use a URLLoaderInterceptor, rather than the EmbeddedTestServer, since + // the origin trial token in the response is associated with a fixed + // origin, whereas EmbeddedTestServer serves content on a random port. + url_loader_interceptor_ = std::make_unique( + base::BindRepeating(&URLLoaderInterceptorCallback)); + } + + void TearDownOnMainThread() override { + url_loader_interceptor_.reset(); + InProcessBrowserTest::TearDownOnMainThread(); + } + + content::WebContents* web_contents() { + return browser()->tab_strip_model()->GetActiveWebContents(); + } + + content::RenderFrameHost* main_frame() { + return web_contents()->GetMainFrame(); + } + + protected: + std::unique_ptr url_loader_interceptor_; +}; + +IN_PROC_BROWSER_TEST_F(NativeFileSystemOriginTrialBrowserTest, OriginTrial) { + ASSERT_TRUE(ui_test_utils::NavigateToURL( + browser(), GURL(base::JoinString( + {kOriginTrialTestHostname, kOriginTrialPage}, "/")))); + + auto result = content::EvalJs(main_frame(), "self.showOpenFilePicker()"); + EXPECT_TRUE(result.error.find("self.showOpenFilePicker is not a function") != + std::string::npos) + << result.error; +} diff --git a/renderer/test/subresource_web_bundles_browsertest.cc b/renderer/test/subresource_web_bundles_browsertest.cc new file mode 100644 index 000000000000..ef28d3d34726 --- /dev/null +++ b/renderer/test/subresource_web_bundles_browsertest.cc @@ -0,0 +1,212 @@ +/* Copyright (c) 2020 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/feature_list.h" +#include "base/strings/strcat.h" +#include "base/strings/string_util.h" +#include "base/strings/utf_string_conversions.h" +#include "base/test/scoped_feature_list.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/embedder_support/switches.h" +#include "components/web_package/test_support/web_bundle_builder.h" +#include "content/public/browser/render_frame_host.h" +#include "content/public/browser/web_contents.h" +#include "content/public/common/content_features.h" +#include "content/public/test/browser_test.h" +#include "content/public/test/browser_test_utils.h" +#include "content/public/test/url_loader_interceptor.h" +#include "third_party/blink/public/common/features.h" +#include "url/gurl.h" + +namespace { + +constexpr char kOriginTrialTestPublicKey[] = + "dRCs+TocuKkocNKa0AtZ4awrt9XKH2SQCI6o4FY6BNA="; + +constexpr char kOriginTrialTestHostname[] = "https://localhost"; + +constexpr char kOriginTrialPage[] = "page.html"; +constexpr char kOriginTrialPageHeaders[] = + "HTTP/1.1 200 OK\nContent-type: text/html\n\n"; + +constexpr char kWebBundle[] = "web_bundle.wbn"; +constexpr char kWebBundleHeaders[] = + "HTTP/1.1 200 OK\nContent-type: application/webbundle\n\n"; + +constexpr char kPassJs[] = "pass.js"; +constexpr char kPassJsHeaders[] = "HTTP/1.1 404 Not Found\n\n"; + +// tools/origin_trials/generate_token.py \ +// --expire-days 3650 https://localhost SubresourceWebBundles +constexpr char kOriginTrialToken[] = + "A0bbldJxinw6xRKnkDrBLVob3U638q6NVqmE5nax5Bdu+hZVIgy1sXCM9ccc+5wvAZb+V48iSV" + "vGX8H6s+cbGgsAAABdeyJvcmlnaW4iOiAiaHR0cHM6Ly9sb2NhbGhvc3Q6NDQzIiwgImZlYXR1" + "cmUiOiAiU3VicmVzb3VyY2VXZWJCdW5kbGVzIiwgImV4cGlyeSI6IDE5MjEwNzcxMjB9"; + +constexpr char kPageHtml[] = R"( + + + Loaded + META_TAG + + + + + +)"; + +constexpr char kLoadPassJs[] = R"( + new Promise(function (resolve, reject) { + var s = document.createElement('script'); + s.onload = () => { resolve(true); }; + s.onerror = () => { resolve(false); }; + s.src = 'pass.js'; + document.head.appendChild(s); + }) +)"; + +std::string CreateWebBundle() { + std::string pass_js_url_str = + GURL(base::JoinString({kOriginTrialTestHostname, kPassJs}, "/")).spec(); + // Currently the web bundle format requires a valid GURL for the fallback + // URL of a web bundle. + std::string flbk_js_url_str = + GURL(base::JoinString({kOriginTrialTestHostname, "fallback.js"}, "/")) + .spec(); + web_package::test::WebBundleBuilder builder(flbk_js_url_str, ""); + auto pass_js_location = builder.AddResponse( + {{":status", "200"}, {"content-type", "application/javascript"}}, + "document.title = 'script loaded';"); + builder.AddIndexEntry(pass_js_url_str, "", {pass_js_location}); + std::vector bundle = builder.CreateBundle(); + return std::string(bundle.begin(), bundle.end()); +} + +std::string GetHeadersForURL(const std::string& url) { + if (base::EndsWith(url, kOriginTrialPage, base::CompareCase::SENSITIVE)) { + return kOriginTrialPageHeaders; + } else if (base::EndsWith(url, kWebBundle, base::CompareCase::SENSITIVE)) { + return kWebBundleHeaders; + } else if (base::EndsWith(url, kPassJs, base::CompareCase::SENSITIVE)) { + return kPassJsHeaders; + } else { + return std::string(); + } +} + +std::string GetContentForURL(const std::string& url) { + if (base::EndsWith(url, kOriginTrialPage, base::CompareCase::SENSITIVE)) { + std::string response = kPageHtml; + std::string meta_tag = + base::StrCat({R"()"}); + base::ReplaceFirstSubstringAfterOffset(&response, 0, "META_TAG", meta_tag); + return response; + } else if (base::EndsWith(url, kWebBundle, base::CompareCase::SENSITIVE)) { + return CreateWebBundle(); + } else { + return std::string(); + } +} + +bool URLLoaderInterceptorCallback( + content::URLLoaderInterceptor::RequestParams* params) { + content::URLLoaderInterceptor::WriteResponse( + GetHeadersForURL(params->url_request.url.path()), + GetContentForURL(params->url_request.url.path()), params->client.get()); + return true; +} + +} // namespace + +class SubresourceWebBundlesBrowserTest + : public InProcessBrowserTest, + public ::testing::WithParamInterface { + public: + SubresourceWebBundlesBrowserTest() = default; + ~SubresourceWebBundlesBrowserTest() override = default; + + bool IsSubresourceWebBundlesEnabled() { return GetParam(); } + + void SetUp() override { + if (IsSubresourceWebBundlesEnabled()) { + scoped_feature_list_.InitAndEnableFeature( + features::kSubresourceWebBundles); + } + InProcessBrowserTest::SetUp(); + } + + void SetUpDefaultCommandLine(base::CommandLine* command_line) override { + InProcessBrowserTest::SetUpDefaultCommandLine(command_line); + if (!IsSubresourceWebBundlesEnabled()) { + // With feature initially disabled, use origin trial. + command_line->AppendSwitchASCII(embedder_support::kOriginTrialPublicKey, + kOriginTrialTestPublicKey); + } + } + + void SetUpOnMainThread() override { + InProcessBrowserTest::SetUpOnMainThread(); + + // We use a URLLoaderInterceptor, rather than the EmbeddedTestServer, since + // the origin trial token in the response is associated with a fixed + // origin, whereas EmbeddedTestServer serves content on a random port. + url_loader_interceptor_ = std::make_unique( + base::BindRepeating(&URLLoaderInterceptorCallback)); + } + + void TearDownOnMainThread() override { + url_loader_interceptor_.reset(); + InProcessBrowserTest::TearDownOnMainThread(); + } + + content::WebContents* web_contents() { + return browser()->tab_strip_model()->GetActiveWebContents(); + } + + content::RenderFrameHost* main_frame() { + return web_contents()->GetMainFrame(); + } + + protected: + base::test::ScopedFeatureList scoped_feature_list_; + std::unique_ptr url_loader_interceptor_; +}; + +IN_PROC_BROWSER_TEST_P(SubresourceWebBundlesBrowserTest, + SubresourceWebBundles) { + EXPECT_EQ(IsSubresourceWebBundlesEnabled(), + base::FeatureList::IsEnabled(features::kSubresourceWebBundles)); + ASSERT_TRUE(ui_test_utils::NavigateToURL( + browser(), GURL(base::JoinString( + {kOriginTrialTestHostname, kOriginTrialPage}, "/")))); + + if (IsSubresourceWebBundlesEnabled()) { + base::string16 expected_title = base::ASCIIToUTF16("script loaded"); + content::TitleWatcher title_watcher(web_contents(), expected_title); + EXPECT_EQ(true, content::EvalJs(main_frame(), kLoadPassJs)); + EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); + } else { + EXPECT_EQ(false, content::EvalJs(main_frame(), kLoadPassJs)); + } +} + +INSTANTIATE_TEST_SUITE_P(SubresourceWebBundlesBrowserTest, + SubresourceWebBundlesBrowserTest, + ::testing::Bool()); diff --git a/script/redirect-cc.py b/script/redirect-cc.py index 3fcd7d3497e6..d9c9af31d43b 100755 --- a/script/redirect-cc.py +++ b/script/redirect-cc.py @@ -55,10 +55,13 @@ def replace_cc_arg(args): # Filter away out and out_x86 # Maybe this dir can be queried from env variable instead of hardcoding OUT_DIR_NAMES = ['out', 'out_x86'] - start_dir = rel_path.split(os.sep)[0] - if start_dir in OUT_DIR_NAMES: - # Don't even try to substitute path for auto-generated cc - return + rel_path_parts = rel_path.split(os.sep, 3) + if rel_path_parts[0] in OUT_DIR_NAMES: + if rel_path_parts[2] == 'gen': + rel_path = rel_path_parts[3] + else: + # Don't even try to substitute path for other auto-generated cc + return # Build possible brave/chromium_src_path brave_path = os.path.join(chromium_original_dir, 'brave', 'chromium_src', diff --git a/test/BUILD.gn b/test/BUILD.gn index 09760a44e897..c90b404b10a9 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -634,6 +634,7 @@ if (!is_android) { "//brave/browser/ui/tabs/test:browser_tests", "//brave/chromium_src/third_party/blink/renderer/modules:browser_tests", "//brave/components/ipfs/test:brave_ipfs_browser_tests", + "//brave/renderer/test:browser_tests", "//media:test_support", ]