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

Avoid race condition in FarbleScreenPopupPosition #26308

Merged
merged 3 commits into from
Oct 31, 2024
Merged
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
101 changes: 50 additions & 51 deletions browser/farbling/brave_screen_farbling_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
* 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/. */

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and
// convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include <algorithm>

#include "base/files/file_path.h"
Expand All @@ -28,7 +22,6 @@
#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 "net/test/embedded_test_server/http_request.h"
#include "third_party/blink/public/common/features.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h"
Expand Down Expand Up @@ -150,8 +143,8 @@ class BraveScreenFarblingBrowserTest : public InProcessBrowserTest {
}

void FarbleScreenSize(const GURL& url, bool content_scheme) {
for (int j = 0; j < static_cast<int>(std::size(kTestWindowBounds)); ++j) {
SetBounds(kTestWindowBounds[j]);
for (const auto& test_bounds : kTestWindowBounds) {
SetBounds(test_bounds);
for (bool allow_fingerprinting : {false, true}) {
SetFingerprintingSetting(allow_fingerprinting);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
Expand Down Expand Up @@ -210,8 +203,8 @@ class BraveScreenFarblingBrowserTest : public InProcessBrowserTest {
void FarbleWindowPosition() {
for (bool allow_fingerprinting : {false, true}) {
SetFingerprintingSetting(allow_fingerprinting);
for (int i = 0; i < static_cast<int>(std::size(kTestWindowBounds)); ++i) {
SetBounds(kTestWindowBounds[i]);
for (const auto& bounds : kTestWindowBounds) {
SetBounds(bounds);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), parent_url()));
for (bool test_iframe : {false, true}) {
content::RenderFrameHost* host = test_iframe ? Parent() : IFrame();
Expand All @@ -227,10 +220,10 @@ class BraveScreenFarblingBrowserTest : public InProcessBrowserTest {
"testEvent.screenY - devicePixelRatio * "
"testEvent.clientY"));
} else {
if (kTestWindowBounds[i].x() > 8) {
if (bounds.x() > 8) {
EXPECT_LT(8, EvalJs(host, "window.screenX"));
}
if (kTestWindowBounds[i].y() > 8) {
if (bounds.y() > 8) {
EXPECT_LT(8, EvalJs(host, "window.screenY"));
}
}
Expand All @@ -240,79 +233,85 @@ class BraveScreenFarblingBrowserTest : public InProcessBrowserTest {
}

void FarbleScreenMediaQuery() {
for (int j = 0; j < static_cast<int>(std::size(kTestWindowBounds)); ++j) {
SetBounds(kTestWindowBounds[j]);
for (const auto& bounds : kTestWindowBounds) {
SetBounds(bounds);
for (bool allow_fingerprinting : {false, true}) {
SetFingerprintingSetting(allow_fingerprinting);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), parent_url()));
for (bool test_iframe : {false, true}) {
content::RenderFrameHost* host = test_iframe ? Parent() : IFrame();
// Allow for a 2px variance due to non-integer devicePixelRatio.
EXPECT_EQ(
!allow_fingerprinting && !IsFlagDisabled(),
EvalJs(host,
"matchMedia(`(device-width: ${outerWidth}px)`).matches"));
"matchMedia(`(min-device-width: ${outerWidth - 2}px) and "
"(max-device-width: ${outerWidth + 2}px)`).matches"));
EXPECT_EQ(
!allow_fingerprinting && !IsFlagDisabled(),
EvalJs(
host,
"matchMedia(`(device-height: ${outerHeight}px)`).matches"));
"matchMedia(`(min-device-height: ${outerHeight - 2}px) and "
"(max-device-height: ${outerHeight + 2}px)`).matches"));
}
}
}
}

void FarbleScreenPopupPosition(int j) {
gfx::Rect parent_bounds;
parent_bounds = SetBounds(kTestWindowBounds[j]);
enum class TestMode { kIframe, kWindowSize, kWindowPosition };

void FarbleScreenPopupPosition(const gfx::Rect& bounds) {
const gfx::Rect parent_bounds = SetBounds(bounds);
for (bool allow_fingerprinting : {false, true}) {
SetFingerprintingSetting(allow_fingerprinting);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), parent_url()));
for (bool test_iframe : {false, true}) {
for (TestMode test_mode : {TestMode::kIframe, TestMode::kWindowSize,
TestMode::kWindowPosition}) {
const char* script =
"open('/simple.html', '', `"
"left=30,"
"top=30,"
"width=${outerWidth + 20},"
"height=${outerHeight + 20}"
"`);";
Browser* popup = OpenPopup(script, test_iframe);
Browser* popup = OpenPopup(script, test_mode == TestMode::kIframe);
auto* popup_contents = popup->tab_strip_model()->GetActiveWebContents();
content::WaitForLoadStop(popup_contents);
gfx::Rect child_bounds = popup->window()->GetBounds();
if (!allow_fingerprinting && !IsFlagDisabled()) {
EXPECT_GE(child_bounds.x(), parent_bounds.x());
EXPECT_GE(child_bounds.y(), parent_bounds.y());
int maxWidth = 10 + std::max(450, parent_bounds.width());
int maxHeight = 10 + std::max(450, parent_bounds.width());
EXPECT_GE(maxWidth, child_bounds.width());
EXPECT_GE(maxHeight, child_bounds.height());
int max_width = 10 + std::max(450, parent_bounds.width());
int max_height = 10 + std::max(450, parent_bounds.width());
EXPECT_GE(max_width, child_bounds.width());
EXPECT_GE(max_height, child_bounds.height());
} else {
EXPECT_LE(child_bounds.x(), std::max(80, 10 + parent_bounds.x()));
EXPECT_LE(child_bounds.y(), std::max(80, 10 + parent_bounds.y()));
EXPECT_LE(parent_bounds.width(), child_bounds.width());
EXPECT_LE(parent_bounds.height(), child_bounds.height());
}
if (!test_iframe) {
if (test_mode != TestMode::kIframe) {
auto* widget = views::Widget::GetWidgetForNativeWindow(
popup->window()->GetNativeWindow());

auto bounds_before = popup->window()->GetBounds();
auto waiter1 = WidgetBoundsChangeWaiter(widget, 10);
ASSERT_TRUE(ExecJs(popup_contents,
"resizeTo(outerWidth - 13, outerHeight - 14)"));
waiter1.Wait();
auto waiter = WidgetBoundsChangeWaiter(widget, 10);
if (test_mode == TestMode::kWindowSize) {
ASSERT_TRUE(ExecJs(popup_contents,
"resizeTo(outerWidth - 13, outerHeight - 14)"));
} else { // test_mode == TestMode::kWindowPosition
ASSERT_TRUE(
ExecJs(popup_contents, "moveTo(screenX + 11, screenY + 12)"));
}
waiter.Wait();
auto bounds_after = popup->window()->GetBounds();
EXPECT_EQ(-13, bounds_after.width() - bounds_before.width());
EXPECT_EQ(-14, bounds_after.height() - bounds_before.height());

bounds_before = popup->window()->GetBounds();
auto waiter2 = WidgetBoundsChangeWaiter(widget, 10);
ASSERT_TRUE(
ExecJs(popup_contents, "moveTo(screenX + 11, screenY + 12)"));
waiter2.Wait();
bounds_after = popup->window()->GetBounds();
EXPECT_EQ(11, bounds_after.x() - bounds_before.x());
EXPECT_EQ(12, bounds_after.y() - bounds_before.y());
// Allow for a 2px variance due to non-integer devicePixelRatio.
if (test_mode == TestMode::kWindowSize) {
EXPECT_NEAR(bounds_after.width() - bounds_before.width(), -13, 2);
EXPECT_NEAR(bounds_after.height() - bounds_before.height(), -14, 2);
} else { // test_mode == TestMode::kWindowPosition
EXPECT_NEAR(bounds_after.x() - bounds_before.x(), 11, 2);
EXPECT_NEAR(bounds_after.y() - bounds_before.y(), 12, 2);
}
}
}
}
Expand Down Expand Up @@ -385,42 +384,42 @@ IN_PROC_BROWSER_TEST_F(BraveScreenFarblingBrowserTest_DisableFlag,

IN_PROC_BROWSER_TEST_F(BraveScreenFarblingBrowserTest_EnableFlag,
FarbleScreenPopupPosition_EnableFlag_0) {
FarbleScreenPopupPosition(0);
FarbleScreenPopupPosition(kTestWindowBounds[0]);
}

IN_PROC_BROWSER_TEST_F(BraveScreenFarblingBrowserTest_DisableFlag,
FarbleScreenPopupPosition_DisableFlag_0) {
FarbleScreenPopupPosition(0);
FarbleScreenPopupPosition(kTestWindowBounds[0]);
}

IN_PROC_BROWSER_TEST_F(BraveScreenFarblingBrowserTest_EnableFlag,
FarbleScreenPopupPosition_EnableFlag_1) {
FarbleScreenPopupPosition(1);
FarbleScreenPopupPosition(kTestWindowBounds[1]);
}

IN_PROC_BROWSER_TEST_F(BraveScreenFarblingBrowserTest_DisableFlag,
FarbleScreenPopupPosition_DisableFlag_1) {
FarbleScreenPopupPosition(1);
FarbleScreenPopupPosition(kTestWindowBounds[1]);
}

IN_PROC_BROWSER_TEST_F(BraveScreenFarblingBrowserTest_EnableFlag,
FarbleScreenPopupPosition_EnableFlag_2) {
FarbleScreenPopupPosition(2);
FarbleScreenPopupPosition(kTestWindowBounds[2]);
}

IN_PROC_BROWSER_TEST_F(BraveScreenFarblingBrowserTest_DisableFlag,
FarbleScreenPopupPosition_DisableFlag_2) {
FarbleScreenPopupPosition(2);
FarbleScreenPopupPosition(kTestWindowBounds[2]);
}

IN_PROC_BROWSER_TEST_F(BraveScreenFarblingBrowserTest_EnableFlag,
FarbleScreenPopupPosition_EnableFlag_3) {
FarbleScreenPopupPosition(3);
FarbleScreenPopupPosition(kTestWindowBounds[3]);
}

IN_PROC_BROWSER_TEST_F(BraveScreenFarblingBrowserTest_DisableFlag,
FarbleScreenPopupPosition_DisableFlag_3) {
FarbleScreenPopupPosition(3);
FarbleScreenPopupPosition(kTestWindowBounds[3]);
}

IN_PROC_BROWSER_TEST_F(BraveScreenFarblingBrowserTest_EnableFlag,
Expand Down
Loading