Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round off high-resolution timers #15309

Merged
merged 8 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ constexpr char kBraveTorWindowsHttpsOnlyDescription[] =
"Prevents Private Windows with Tor from making any insecure HTTP "
"connections without warning the user first.";

constexpr char kBraveRoundTimeStampsName[] = "Round time stamps";
constexpr char kBraveRoundTimeStampsDescription[] =
"Prevents JavaScript from getting access to high-resolution clocks by "
"rounding all DOMHighResTimeStamps to the nearest millisecond.";

constexpr char kBraveSpeedreaderName[] = "Enable SpeedReader";
constexpr char kBraveSpeedreaderDescription[] =
"Enables faster loading of simplified article-style web pages.";
Expand Down Expand Up @@ -717,6 +722,11 @@ constexpr char kBraveBackgroundVideoPlaybackDescription[] =
flag_descriptions::kBraveTorWindowsHttpsOnlyDescription, \
kOsAll, FEATURE_VALUE_TYPE( \
blink::features::kBraveTorWindowsHttpsOnly)}, \
{"brave-round-time-stamps", \
flag_descriptions::kBraveRoundTimeStampsName, \
flag_descriptions::kBraveRoundTimeStampsDescription, \
kOsAll, FEATURE_VALUE_TYPE( \
blink::features::kBraveRoundTimeStamps)}, \
BRAVE_IPFS_FEATURE_ENTRIES \
BRAVE_NATIVE_WALLET_FEATURE_ENTRIES \
BRAVE_NEWS_FEATURE_ENTRIES \
Expand Down
72 changes: 72 additions & 0 deletions browser/brave_shields/brave_timestamp_rounding_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/test/render_view_test.h"
#include "third_party/blink/public/common/features.h"

using blink::features::kBraveRoundTimeStamps;

class BraveTimeStampRoundingRenderViewTest
arthuredelstein marked this conversation as resolved.
Show resolved Hide resolved
: public content::RenderViewTest,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have a RenderViewTest in browser. This belongs in renderer

public ::testing::WithParamInterface<bool> {
public:
BraveTimeStampRoundingRenderViewTest() {}

~BraveTimeStampRoundingRenderViewTest() override = default;

bool IsBraveRoundTimeStampsEnabled() { return GetParam(); }

void SetUp() override {
if (IsBraveRoundTimeStampsEnabled()) {
scoped_feature_list_.InitAndEnableFeature(kBraveRoundTimeStamps);
} else {
scoped_feature_list_.InitAndDisableFeature(kBraveRoundTimeStamps);
}
RenderViewTest::SetUp();
}

double ExecuteJSAndReturnDouble(const std::u16string& script) {
double result;
EXPECT_TRUE(ExecuteJavaScriptAndReturnNumberValue(script, &result));
return result;
}

void CheckRounded(const std::u16string& script, bool expect_rounded) {
double result = ExecuteJSAndReturnDouble(script);
if (expect_rounded) {
EXPECT_DOUBLE_EQ(round(result) - result, 0);
} else {
EXPECT_NE(round(result) - result, 0);
}
}

void Advance100Microseconds() {
task_environment_.AdvanceClock(base::Microseconds(100));
}

protected:
base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_P(BraveTimeStampRoundingRenderViewTest, SynchronousApisRounded) {
bool expect_rounded = IsBraveRoundTimeStampsEnabled();
LoadHTML("<html><body>hi</body></html>");
Advance100Microseconds();
CheckRounded(u"performance.now()", expect_rounded);
Advance100Microseconds();
CheckRounded(u"performance.mark('test').startTime", expect_rounded);
Advance100Microseconds();
if (expect_rounded) {
CheckRounded(u"performance.timeOrigin", true);
}
}

INSTANTIATE_TEST_SUITE_P(BraveTimeStampRoundingRenderViewTest,
BraveTimeStampRoundingRenderViewTest,
::testing::Bool());
8 changes: 6 additions & 2 deletions chromium_src/third_party/blink/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ const base::Feature kPartitionBlinkMemoryCache{
const base::Feature kRestrictWebSocketsPool{"RestrictWebSocketsPool",
base::FEATURE_ENABLED_BY_DEFAULT};

// Disable protection against fingerprinting on screen dimensions by default.
// Enables protection against fingerprinting on screen dimensions.
const base::Feature kBraveBlockScreenFingerprinting{
"BraveBlockScreenFingerprinting", base::FEATURE_DISABLED_BY_DEFAULT};

// Enable HTTPS-Only Mode in Private Windows with Tor by default.
// Enables HTTPS-Only Mode in Private Windows with Tor by default.
const base::Feature kBraveTorWindowsHttpsOnly{"BraveTorWindowsHttpsOnly",
base::FEATURE_ENABLED_BY_DEFAULT};

// Enables protection against fingerprinting via high-resolution time stamps.
const base::Feature kBraveRoundTimeStamps{"BraveRoundTimeStamps",
base::FEATURE_ENABLED_BY_DEFAULT};

} // namespace features
arthuredelstein marked this conversation as resolved.
Show resolved Hide resolved
} // namespace blink
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 @@ -18,6 +18,7 @@ BLINK_COMMON_EXPORT extern const base::Feature kPartitionBlinkMemoryCache;
BLINK_COMMON_EXPORT extern const base::Feature kRestrictWebSocketsPool;
BLINK_COMMON_EXPORT extern const base::Feature kBraveBlockScreenFingerprinting;
BLINK_COMMON_EXPORT extern const base::Feature kBraveTorWindowsHttpsOnly;
BLINK_COMMON_EXPORT extern const base::Feature kBraveRoundTimeStamps;

} // namespace features
} // namespace blink
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "third_party/blink/renderer/core/loader/document_load_timing.h"

#define MonotonicTimeToZeroBasedDocumentTime \
MonotonicTimeToZeroBasedDocumentTime_ChromiumImpl

#include "src/third_party/blink/renderer/core/loader/document_load_timing.cc"

#undef MonotonicTimeToZeroBasedDocumentTime

#include "third_party/blink/renderer/core/timing/time_clamper.h"

namespace blink {

base::TimeDelta DocumentLoadTiming::MonotonicTimeToZeroBasedDocumentTime(
base::TimeTicks monotonic_time) const {
return TimeClamper::MaybeRoundTimeDelta(
MonotonicTimeToZeroBasedDocumentTime_ChromiumImpl(monotonic_time));
}

} // namespace blink
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_DOCUMENT_LOAD_TIMING_H_
#define BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_DOCUMENT_LOAD_TIMING_H_

#define MonotonicTimeToZeroBasedDocumentTime \
MonotonicTimeToZeroBasedDocumentTime(base::TimeTicks) const; \
base::TimeDelta MonotonicTimeToZeroBasedDocumentTime_ChromiumImpl

#include "src/third_party/blink/renderer/core/loader/document_load_timing.h"

#undef MonotonicTimeToZeroBasedDocumentTime

#endif // BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_DOCUMENT_LOAD_TIMING_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "third_party/blink/renderer/core/timing/time_clamper.h"

#define kFineResolutionMicroseconds FineResolutionMicroseconds()
#define kCoarseResolutionMicroseconds CoarseResolutionMicroseconds()

#include "src/third_party/blink/renderer/core/timing/time_clamper.cc"

#undef FineResolutionMicroseconds
#undef CoarseResolutionMicroseconds

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

namespace blink {

namespace {

constexpr int kBraveTimerResolutionMicroseconds = 1000;

bool ShouldRound() {
return base::FeatureList::IsEnabled(features::kBraveRoundTimeStamps);
}

} // namespace

// static
double TimeClamper::MaybeRoundMilliseconds(double value) {
return ShouldRound() ? round(value) : value;
}

// static
base::TimeDelta TimeClamper::MaybeRoundTimeDelta(base::TimeDelta value) {
return ShouldRound() ? value.RoundToMultiple(base::Microseconds(
kBraveTimerResolutionMicroseconds))
: value;
}

// static
int TimeClamper::FineResolutionMicroseconds() {
return ShouldRound() ? kBraveTimerResolutionMicroseconds
: kFineResolutionMicroseconds_ChromiumImpl;
}

// static
int TimeClamper::CoarseResolutionMicroseconds() {
return ShouldRound() ? kBraveTimerResolutionMicroseconds
: kCoarseResolutionMicroseconds_ChromiumImpl;
}

} // namespace blink
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_TIMING_TIME_CLAMPER_H_
#define BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_TIMING_TIME_CLAMPER_H_

#include "base/time/time.h"

// Any future usages of kCoarseResolutionMicroseconds or
// kFineResolutionMicroseconds won't compile and will need to be fixed.
#define kCoarseResolutionMicroseconds kCoarseResolutionMicroseconds_ChromiumImpl

#define kFineResolutionMicroseconds \
kFineResolutionMicroseconds_ChromiumImpl = 5; \
static int CoarseResolutionMicroseconds(); \
static int FineResolutionMicroseconds(); \
static double MaybeRoundMilliseconds(double value); \
static base::TimeDelta MaybeRoundTimeDelta(base::TimeDelta value); \
int dummy

#include "src/third_party/blink/renderer/core/timing/time_clamper.h"

#undef kCoarseResolutionMicroseconds
#undef kFineResolutionMicroseconds

#endif // BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_TIMING_TIME_CLAMPER_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "third_party/blink/renderer/modules/video_rvfc/video_frame_callback_requester_impl.h"

#define kCoarseResolution kCoarseResolution [[maybe_unused]]

#define FloorToMultiple(X) \
FloorToMultiple( \
base::Microseconds(TimeClamper::CoarseResolutionMicroseconds()))

#define static_assert(...) static_assert(true)
arthuredelstein marked this conversation as resolved.
Show resolved Hide resolved

#include "src/third_party/blink/renderer/modules/video_rvfc/video_frame_callback_requester_impl.cc"

#undef kCoarseResolution
#undef FloorToMultiple
#undef static_assert
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ test("brave_browser_tests") {
"//brave/browser/brave_shields/ad_block_service_browsertest.cc",
"//brave/browser/brave_shields/ad_block_service_browsertest.h",
"//brave/browser/brave_shields/brave_shields_web_contents_observer_browsertest.cc",
"//brave/browser/brave_shields/brave_timestamp_rounding_browsertest.cc",
"//brave/browser/brave_shields/cookie_expiry_browsertest.cc",
"//brave/browser/brave_shields/cookie_pref_service_browsertest.cc",
"//brave/browser/brave_shields/domain_block_page_browsertest.cc",
Expand Down
2 changes: 2 additions & 0 deletions test/filters/browser_tests-linux.filter
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
-ExternalProtocolHandlerSandboxBrowserTest.*
-IndividualNetworkContextsPrefetchProxyBrowserTest.*
-LayoutInstabilityTest.*
-MetricIntegrationTest.FirstInputDelay
-MultiscreenWindowPlacementPermissionContextTest.IsExtendedCrossOriginAllow
-MultiscreenWindowPlacementPermissionContextTest.IsExtendedCrossOriginDeny
-MultiscreenWindowPlacementPermissionContextTest.IsExtendedSameOriginAllow
-NewTabUIBrowserTest.*
-NewTabUIProcessPerTabTest.*
-OzonePlatformTest.*
-PolicyTestPrefetchProxyBrowserTest.*
-PolicyTestSetTimeoutWithout1MsClamp.DisablePolicy
-PPAPINaClPNaClNonSfiTest.*
-PrefetchProxyBrowserTest.*
-PrefetchProxyWithDecoyRequestsBrowserTest.*
Expand Down