1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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 "components/permissions/permission_request_manager.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/default_handlers.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "ui/display/screen_base.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/shell.h"
#include "ui/display/test/display_manager_test_api.h" // nogncheck
#endif // BUILDFLAG(IS_CHROMEOS)
namespace {
constexpr char kGetScreensScript[] = R"(
(async () => {
try {
const screenDetails = await self.getScreenDetails();
} catch {
return 'error';
}
try {
return (await navigator.permissions.query({name:'window-management'}))
.state;
} catch {
return "permission_error";
}
})();
)";
constexpr char kCheckPermissionScript[] = R"(
(async () => {
try {
return (await navigator.permissions.query({name:'window-management'}))
.state;
} catch {
return 'permission_error';
} })();
)";
// Tests of WindowManagementPermissionContext behavior.
class WindowManagementPermissionContextTest : public InProcessBrowserTest {
public:
void SetUpOnMainThread() override {
// Support multiple sites on the test server.
host_resolver()->AddRule("*", "127.0.0.1");
// Window management features are only available on secure contexts, and so
// we need to create an HTTPS test server here to serve those pages rather
// than using the default embedded_test_server().
https_test_server_ = std::make_unique<net::EmbeddedTestServer>(
net::EmbeddedTestServer::TYPE_HTTPS);
// Support sites like a.test, b.test, c.test etc
https_test_server_->SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES);
https_test_server_->ServeFilesFromSourceDirectory("chrome/test/data");
net::test_server::RegisterDefaultHandlers(https_test_server_.get());
content::SetupCrossSiteRedirector(https_test_server_.get());
ASSERT_TRUE(https_test_server_->Start());
}
// Awaits expiry of the navigator.userActivation signal on the active tab.
void WaitForUserActivationExpiry() {
const std::string await_activation_expiry_script = R"(
(async () => {
while (navigator.userActivation.isActive)
await new Promise(resolve => setTimeout(resolve, 1000));
return navigator.userActivation.isActive;
})();
)";
auto* tab = browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(false, EvalJs(tab, await_activation_expiry_script,
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_FALSE(tab->HasRecentInteraction());
}
net::EmbeddedTestServer* https_test_server() {
return https_test_server_.get();
}
protected:
std::unique_ptr<net::EmbeddedTestServer> https_test_server_;
};
class MultiscreenWindowManagementPermissionContextTest
: public WindowManagementPermissionContextTest {
public:
#if !BUILDFLAG(IS_CHROMEOS)
~MultiscreenWindowManagementPermissionContextTest() override {
display::Screen::SetScreenInstance(nullptr);
}
#endif
void SetScreenInstance() override {
#if BUILDFLAG(IS_CHROMEOS)
// Use the default, see SetUpOnMainThread.
WindowManagementPermissionContextTest::SetScreenInstance();
#else
display::Screen::SetScreenInstance(&screen_);
screen_.display_list().AddDisplay({1, gfx::Rect(100, 100, 801, 802)},
display::DisplayList::Type::PRIMARY);
screen_.display_list().AddDisplay({2, gfx::Rect(901, 100, 802, 803)},
display::DisplayList::Type::NOT_PRIMARY);
ASSERT_EQ(2, display::Screen::GetScreen()->GetNumDisplays());
#endif // BUILDFLAG(IS_CHROMEOS)
}
void SetUpOnMainThread() override {
#if BUILDFLAG(IS_CHROMEOS)
// This has to happen later than SetScreenInstance as the ash shell
// does not exist yet.
display::test::DisplayManagerTestApi(ash::Shell::Get()->display_manager())
.UpdateDisplay("100+100-801x802,901+100-802x803");
ASSERT_EQ(2, display::Screen::GetScreen()->GetNumDisplays());
#endif
WindowManagementPermissionContextTest::SetUpOnMainThread();
}
private:
display::ScreenBase screen_;
};
// Tests gesture requirements (a gesture is only needed to prompt the user).
IN_PROC_BROWSER_TEST_F(WindowManagementPermissionContextTest, GestureToPrompt) {
const GURL url(https_test_server()->GetURL("a.test", "/empty.html"));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
auto* tab = browser()->tab_strip_model()->GetActiveWebContents();
// Auto-dismiss the permission request, iff the prompt is shown.
permissions::PermissionRequestManager* permission_request_manager =
permissions::PermissionRequestManager::FromWebContents(tab);
permission_request_manager->set_auto_response_for_test(
permissions::PermissionRequestManager::ACCEPT_ALL);
// Calling getScreenDetails() without a gesture or pre-existing permission
// will not prompt the user, and leaves the permission in the default "prompt"
// state.
EXPECT_FALSE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
EXPECT_EQ("error", EvalJs(tab, kGetScreensScript,
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_EQ("prompt", EvalJs(tab, kCheckPermissionScript,
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
// Calling getScreenDetails() with a gesture will show the prompt, and
// auto-accept.
EXPECT_FALSE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
EXPECT_EQ("granted", EvalJs(tab, kGetScreensScript));
EXPECT_TRUE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
// Calling getScreenDetails() without a gesture, but with pre-existing
// permission, will succeed, since it does not need to prompt the user.
WaitForUserActivationExpiry();
EXPECT_FALSE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
EXPECT_EQ("granted", EvalJs(tab, kGetScreensScript,
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_FALSE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
}
// TODO(crbug.com/40212482): Test failing on linux-chromeos-chrome.
// TODO(crbug.com/40212443): Test failing on linux.
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
#define MAYBE_DismissAndDeny DISABLED_DismissAndDeny
#else
#define MAYBE_DismissAndDeny DismissAndDeny
#endif
// Tests user activation after dimissing and denying the permission request.
IN_PROC_BROWSER_TEST_F(WindowManagementPermissionContextTest,
MAYBE_DismissAndDeny) {
const GURL url(https_test_server()->GetURL("a.test", "/empty.html"));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
auto* tab = browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_FALSE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
permissions::PermissionRequestManager* permission_request_manager =
permissions::PermissionRequestManager::FromWebContents(tab);
// Dismiss the prompt after activation expires, expect no activation.
ExecuteScriptAsync(tab, "getScreenDetails()");
WaitForUserActivationExpiry();
ASSERT_TRUE(permission_request_manager->IsRequestInProgress());
permission_request_manager->Dismiss();
EXPECT_EQ("prompt", EvalJs(tab, kCheckPermissionScript,
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_FALSE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
// Deny the prompt after activation expires, expect no activation.
ExecuteScriptAsync(tab, "getScreenDetails()");
WaitForUserActivationExpiry();
ASSERT_TRUE(permission_request_manager->IsRequestInProgress());
permission_request_manager->Deny();
EXPECT_EQ("denied", EvalJs(tab, kCheckPermissionScript,
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_FALSE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
}
// Tests user activation after accepting the permission request.
IN_PROC_BROWSER_TEST_F(WindowManagementPermissionContextTest, Accept) {
const GURL url(https_test_server()->GetURL("a.test", "/empty.html"));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
auto* tab = browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_FALSE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
permissions::PermissionRequestManager* permission_request_manager =
permissions::PermissionRequestManager::FromWebContents(tab);
// Accept the prompt after activation expires, expect an activation signal.
ExecuteScriptAsync(tab, "getScreenDetails()");
WaitForUserActivationExpiry();
ASSERT_TRUE(permission_request_manager->IsRequestInProgress());
permission_request_manager->Accept();
EXPECT_EQ("granted", EvalJs(tab, kCheckPermissionScript,
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_TRUE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
}
IN_PROC_BROWSER_TEST_F(WindowManagementPermissionContextTest,
IFrameSameOriginAllow) {
const GURL url(https_test_server()->GetURL("a.test", "/iframe.html"));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
auto* tab = browser()->tab_strip_model()->GetActiveWebContents();
content::RenderFrameHost* child = ChildFrameAt(tab->GetPrimaryMainFrame(), 0);
ASSERT_TRUE(child);
EXPECT_FALSE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
EXPECT_FALSE(child->GetMainFrame()->HasTransientUserActivation());
permissions::PermissionRequestManager* permission_request_manager =
permissions::PermissionRequestManager::FromWebContents(tab);
// Accept the prompt after activation expires, expect an activation signal.
ExecuteScriptAsync(child, "getScreenDetails()");
WaitForUserActivationExpiry();
ASSERT_TRUE(permission_request_manager->IsRequestInProgress());
permission_request_manager->Accept();
EXPECT_EQ("granted", EvalJs(child, kCheckPermissionScript,
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_TRUE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
EXPECT_TRUE(child->GetMainFrame()->HasTransientUserActivation());
}
IN_PROC_BROWSER_TEST_F(WindowManagementPermissionContextTest,
IFrameCrossOriginDeny) {
const GURL url(https_test_server()->GetURL("a.test", "/iframe.html"));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
auto* tab = browser()->tab_strip_model()->GetActiveWebContents();
GURL subframe_url(https_test_server()->GetURL("b.test", "/title1.html"));
content::NavigateIframeToURL(tab, /*iframe_id=*/"test", subframe_url);
content::RenderFrameHost* child = ChildFrameAt(tab->GetPrimaryMainFrame(), 0);
ASSERT_TRUE(child);
EXPECT_FALSE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
EXPECT_FALSE(child->GetMainFrame()->HasTransientUserActivation());
permissions::PermissionRequestManager* permission_request_manager =
permissions::PermissionRequestManager::FromWebContents(tab);
// PermissionRequestManager will accept any window management permission
// dialogs that appear. However, the window-management permission is not
// explicitly allowed on the iframe, so requests made by the child frame will
// be automatically denied before a prompt might be issued.
permission_request_manager->set_auto_response_for_test(
permissions::PermissionRequestManager::ACCEPT_ALL);
EXPECT_EQ("error", EvalJs(child, kGetScreensScript));
EXPECT_EQ("denied", EvalJs(child, kCheckPermissionScript,
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
}
IN_PROC_BROWSER_TEST_F(WindowManagementPermissionContextTest,
IFrameCrossOriginExplicitAllow) {
const GURL url(https_test_server()->GetURL("a.test", "/iframe.html"));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
auto* tab = browser()->tab_strip_model()->GetActiveWebContents();
// See https://w3c.github.io/webappsec-permissions-policy/ for more
// information on permissions policies and allowing cross-origin iframes
// to have particular permissions.
EXPECT_TRUE(ExecJs(tab,
R"(const frame = document.getElementById('test');
frame.setAttribute('allow', 'window-management');)",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
GURL subframe_url(https_test_server()->GetURL("b.test", "/title1.html"));
content::NavigateIframeToURL(tab, /*iframe_id=*/"test", subframe_url);
content::RenderFrameHost* child = ChildFrameAt(tab->GetPrimaryMainFrame(), 0);
ASSERT_TRUE(child);
EXPECT_FALSE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
EXPECT_FALSE(child->GetMainFrame()->HasTransientUserActivation());
permissions::PermissionRequestManager* permission_request_manager =
permissions::PermissionRequestManager::FromWebContents(tab);
// Accept the prompt after activation expires, expect an activation signal.
ExecuteScriptAsync(child, "getScreenDetails()");
WaitForUserActivationExpiry();
ASSERT_TRUE(permission_request_manager->IsRequestInProgress());
permission_request_manager->Accept();
EXPECT_EQ("granted", EvalJs(child, kCheckPermissionScript,
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_TRUE(tab->GetPrimaryMainFrame()->HasTransientUserActivation());
EXPECT_TRUE(child->GetMainFrame()->HasTransientUserActivation());
}
// TODO(enne): Windows assumes that display::GetScreen() is a ScreenWin
// which is not true here.
#if !BUILDFLAG(IS_WIN)
// Verify that window.screen.isExtended returns true in a same-origin
// iframe without the window management permission policy allowed.
IN_PROC_BROWSER_TEST_F(MultiscreenWindowManagementPermissionContextTest,
IsExtendedSameOriginAllow) {
const GURL url(https_test_server()->GetURL("a.test", "/iframe.html"));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
auto* tab = browser()->tab_strip_model()->GetActiveWebContents();
content::RenderFrameHost* child = ChildFrameAt(tab->GetPrimaryMainFrame(), 0);
ASSERT_TRUE(child);
EXPECT_EQ(true, EvalJs(tab, R"(window.screen.isExtended)",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_EQ(true, EvalJs(child, R"(window.screen.isExtended)",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
}
// Verify that window.screen.isExtended returns false in a cross-origin
// iframe without the window management permission policy allowed.
IN_PROC_BROWSER_TEST_F(MultiscreenWindowManagementPermissionContextTest,
IsExtendedCrossOriginDeny) {
const GURL url(https_test_server()->GetURL("a.test", "/iframe.html"));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
auto* tab = browser()->tab_strip_model()->GetActiveWebContents();
GURL subframe_url(https_test_server()->GetURL("b.test", "/title1.html"));
content::NavigateIframeToURL(tab, /*iframe_id=*/"test", subframe_url);
content::RenderFrameHost* child = ChildFrameAt(tab->GetPrimaryMainFrame(), 0);
ASSERT_TRUE(child);
EXPECT_EQ(true, EvalJs(tab, R"(window.screen.isExtended)",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_EQ(false, EvalJs(child, R"(window.screen.isExtended)",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
}
// Verify that window.screen.isExtended returns true in a cross-origin
// iframe with the window management permission policy allowed.
IN_PROC_BROWSER_TEST_F(MultiscreenWindowManagementPermissionContextTest,
IsExtendedCrossOriginAllow) {
const GURL url(https_test_server()->GetURL("a.test", "/iframe.html"));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
auto* tab = browser()->tab_strip_model()->GetActiveWebContents();
// See https://w3c.github.io/webappsec-permissions-policy/ for more
// information on permissions policies and allowing cross-origin iframes
// to have particular permissions.
EXPECT_TRUE(ExecJs(tab, R"(const frame = document.getElementById('test');
frame.setAttribute('allow', 'window-management');)",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
GURL subframe_url(https_test_server()->GetURL("b.test", "/title1.html"));
content::NavigateIframeToURL(tab, /*iframe_id=*/"test", subframe_url);
content::RenderFrameHost* child = ChildFrameAt(tab->GetPrimaryMainFrame(), 0);
ASSERT_TRUE(child);
EXPECT_EQ(true, EvalJs(tab, R"(window.screen.isExtended)",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_EQ(true, EvalJs(child, R"(window.screen.isExtended)",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
}
#endif // !BUILDFLAG(IS_WIN)
} // namespace
|