Skip to content

Commit

Permalink
Implement GPC opt-out flag (uplift to 1.59.x) (#20147)
Browse files Browse the repository at this point in the history
Uplift of #19953 (squashed) to beta
  • Loading branch information
brave-builds authored Sep 19, 2023
1 parent cf6e2f1 commit ce7fe9f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
8 changes: 8 additions & 0 deletions browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,14 @@
kOsWin | kOsLinux | kOsMac, \
FEATURE_VALUE_TYPE(features::kBraveCopyCleanLinkByDefault), \
}, \
{ \
"brave-global-privacy-control-enabled", \
"Enable Global Privacy Control", \
"Enable the Sec-GPC request header and the " \
"navigator.globalPrivacyControl JS API", \
kOsAll, \
FEATURE_VALUE_TYPE(blink::features::kBraveGlobalPrivacyControl), \
}, \
{ \
"https-by-default", \
"Use HTTPS by Default", \
Expand Down
10 changes: 7 additions & 3 deletions browser/net/brave_request_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "extensions/common/constants.h"
#include "net/base/features.h"
#include "net/base/net_errors.h"
#include "third_party/blink/public/common/features.h"

#if BUILDFLAG(ENABLE_BRAVE_WEBTORRENT)
#include "brave/browser/net/brave_torrent_redirect_network_delegate_helper.h"
Expand Down Expand Up @@ -100,9 +101,12 @@ void BraveRequestHandler::SetupCallbacks() {
base::BindRepeating(brave::OnBeforeStartTransaction_SiteHacksWork);
before_start_transaction_callbacks_.push_back(start_transaction_callback);

start_transaction_callback = base::BindRepeating(
brave::OnBeforeStartTransaction_GlobalPrivacyControlWork);
before_start_transaction_callbacks_.push_back(start_transaction_callback);
if (base::FeatureList::IsEnabled(
blink::features::kBraveGlobalPrivacyControl)) {
start_transaction_callback = base::BindRepeating(
brave::OnBeforeStartTransaction_GlobalPrivacyControlWork);
before_start_transaction_callbacks_.push_back(start_transaction_callback);
}

start_transaction_callback =
base::BindRepeating(brave::OnBeforeStartTransaction_BraveServiceKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "base/feature_list.h"
#include "base/path_service.h"
#include "base/thread_annotations.h"
#include "brave/components/constants/brave_paths.h"
Expand All @@ -15,6 +16,9 @@
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/http_request.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/blink/public/common/features.h"

using blink::features::kBraveGlobalPrivacyControl;

enum class GPCHeaderResult {
kOk,
Expand Down Expand Up @@ -149,3 +153,37 @@ IN_PROC_BROWSER_TEST_F(GlobalPrivacyControlNetworkDelegateBrowserTest,
EXPECT_EQ(MessageServiceWorker(rfh, "hasGpc"), true);
EXPECT_EQ(MessageServiceWorker(rfh, "checkGpc"), true);
}

class GlobalPrivacyControlFlagDisabledTest
: public GlobalPrivacyControlNetworkDelegateBrowserTest {
public:
GlobalPrivacyControlFlagDisabledTest() {
feature_list_.InitAndDisableFeature(kBraveGlobalPrivacyControl);
}

private:
base::test::ScopedFeatureList feature_list_;
};

// When kGlobalPrivacyControl is disabled, the Sec-GPC header shouldn't be sent.
IN_PROC_BROWSER_TEST_F(GlobalPrivacyControlFlagDisabledTest, SecGPCHeaderNot1) {
const GURL target = https_server().GetURL("a.test", "/simple.html");
StartTracking();
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), target));
EXPECT_EQ(header_result(), GPCHeaderResult::kNoHeader);
}

// When kGlobalPrivacyControl is disabled, the `navigator.globalPrivacyControl`
// should not return true.
IN_PROC_BROWSER_TEST_F(GlobalPrivacyControlFlagDisabledTest,
NavigatorGlobalPrivacyAPI) {
const GURL target = https_server().GetURL("a.test", "/simple.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), target));

auto* rfh = browser()
->tab_strip_model()
->GetActiveWebContents()
->GetPrimaryMainFrame();

EXPECT_EQ(false, content::EvalJs(rfh, "navigator.globalPrivacyControl"));
}
5 changes: 5 additions & 0 deletions chromium_src/third_party/blink/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ BASE_FEATURE(kBraveRoundTimeStamps,
"BraveRoundTimeStamps",
base::FEATURE_ENABLED_BY_DEFAULT);

// Enables the Global Privacy Control header and navigator APIs.
BASE_FEATURE(kBraveGlobalPrivacyControl,
"BraveGlobalPrivacyControl",
base::FEATURE_ENABLED_BY_DEFAULT);

// Enable EventSource connection pool limit per eTLD+1.
BASE_FEATURE(kRestrictEventSourcePool,
"RestrictEventSourcePool",
Expand Down
1 change: 1 addition & 0 deletions chromium_src/third_party/blink/public/common/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kNavigatorConnectionAttribute);
BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kPartitionBlinkMemoryCache);
BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kRestrictWebSocketsPool);
BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kBraveBlockScreenFingerprinting);
BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kBraveGlobalPrivacyControl);
BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kBraveRoundTimeStamps);
BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kRestrictEventSourcePool);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

#include "brave/third_party/blink/renderer/modules/global_privacy_control/global_privacy_control.h"

#include "base/feature_list.h"
#include "third_party/blink/public/common/features.h"

namespace blink {

bool GlobalPrivacyControl::globalPrivacyControl(NavigatorBase& navigator) {
return true;
return base::FeatureList::IsEnabled(features::kBraveGlobalPrivacyControl);
}

} // namespace blink

0 comments on commit ce7fe9f

Please sign in to comment.