Skip to content

Commit

Permalink
Disable serveral blink runtime features.
Browse files Browse the repository at this point in the history
Fixes brave/brave-browser#11133
Fixes brave/brave-browser#11407
Fixes brave/brave-browser#11546
Fixes brave/brave-browser#11547

- Disables features via Chromium features:
  * Direct Sockets
  * Lang Client Hint Header
  * Signed Exchange Prefetch Cache For Navigations
  * Subresource Web Bundles

- Disabled the following blink features in
  BraveContentRendererClient::SetRuntimeFeaturesDefaultsBeforeBlinkInitialization
  since they don't have Chromium features:
  * Digital Goods
  * Native File System (File System Access)
- Added browser tests for these features

- Modified redirect-cc.py to allow overriding files under out/XXX/gen

- Added an override for blink::origin_trials::IsTrialValid and overrides
  for blink::OriginTrialContext methods AddFeature and AddForceEnabledTrials
  for trials:
  * NativeFileSystem2
  * SignedExchangeSubresourcePrefetch
  * SubresourceWebBundles
- Added browser tests for trials disablement.
  • Loading branch information
mkarolin committed Dec 14, 2020
1 parent d48424e commit efbbadb
Show file tree
Hide file tree
Showing 14 changed files with 693 additions and 63 deletions.
4 changes: 4 additions & 0 deletions app/brave_main_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,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::kVideoPlaybackQuality.name,
features::kWebOTP.name,
Expand Down
4 changes: 4 additions & 0 deletions app/brave_main_delegate_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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::kVideoPlaybackQuality,
&features::kWebOTP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
* 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 "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"
Expand Down Expand Up @@ -40,7 +42,11 @@ class ClientHintsBrowserTest : public InProcessBrowserTest {

~ClientHintsBrowserTest() override {}

void SetUp() override { InProcessBrowserTest::SetUp(); }
void SetUp() override {
// Test that even with Lang CH feature enabled, there is no header.
scoped_feature_list_.InitAndEnableFeature(features::kLangClientHintHeader);
InProcessBrowserTest::SetUp();
}

void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
Expand Down Expand Up @@ -68,6 +74,7 @@ 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);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>& trial_names) {
for (const String& trial_name : trial_names) {
if (origin_trials::IsTrialDisabledInBrave(trial_name))
return;
}

AddForceEnabledTrials_ChromiumImpl(trial_names);
}

} // namespace blink
Original file line number Diff line number Diff line change
@@ -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<String>& 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_
Original file line number Diff line number Diff line change
@@ -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<OriginTrialFeature, 4> 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
Original file line number Diff line number Diff line change
@@ -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_
5 changes: 5 additions & 0 deletions renderer/brave_content_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
29 changes: 29 additions & 0 deletions renderer/test/BUILD.gn
Original file line number Diff line number Diff line change
@@ -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",
]
}
87 changes: 87 additions & 0 deletions renderer/test/digital_goods_api_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* 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 "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<bool> {
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()) {
command_line->AppendSwitch(
switches::kEnableExperimentalWebPlatformFeatures);
}
}

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());
Loading

0 comments on commit efbbadb

Please sign in to comment.