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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/simple_test_clock.h"
#include "base/time/default_clock.h"
#include "base/time/time.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/content_settings/one_time_permission_provider.h"
#include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
#include "chrome/browser/media/webrtc/media_stream_capture_indicator.h"
#include "chrome/browser/media/webrtc/webrtc_browsertest_base.h"
#include "chrome/browser/permissions/one_time_permissions_tracker.h"
#include "chrome/browser/permissions/one_time_permissions_tracker_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/resource_coordinator/tab_lifecycle_unit_external.h"
#include "chrome/browser/resource_coordinator/tab_lifecycle_unit_source.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model_delegate.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/test_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/content_settings_uma_util.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/features.h"
#include "components/performance_manager/public/performance_manager.h"
#include "components/permissions/content_setting_permission_context_base.h"
#include "components/permissions/features.h"
#include "components/permissions/permission_decision_auto_blocker.h"
#include "components/permissions/permission_request_manager.h"
#include "components/permissions/permission_uma_util.h"
#include "components/permissions/permission_util.h"
#include "components/permissions/permissions_client.h"
#include "components/permissions/request_type.h"
#include "components/permissions/test/permission_request_observer.h"
#include "components/strings/grit/components_strings.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 "content/public/test/permissions_test_utils.h"
#include "media/base/media_switches.h"
#include "net/dns/mock_host_resolver.h"
#include "services/device/public/cpp/test/scoped_geolocation_overrider.h"
namespace {
std::string RunScript(content::RenderFrameHost* render_frame_host,
const std::string& script) {
return content::EvalJs(render_frame_host, script).ExtractString();
}
class IndicatorObserver : public MediaStreamCaptureIndicator::Observer {
public:
IndicatorObserver() {
MediaCaptureDevicesDispatcher::GetInstance()
->GetMediaStreamCaptureIndicator()
->AddObserver(this);
}
~IndicatorObserver() override {
MediaCaptureDevicesDispatcher::GetInstance()
->GetMediaStreamCaptureIndicator()
->RemoveObserver(this);
}
void Wait() { loop_.Run(); }
void OnIsCapturingVideoChanged(content::WebContents* web_contents,
bool is_capturing_video) override {
loop_.Quit();
}
base::RunLoop loop_;
};
} // namespace
class OneTimePermissionInteractiveUiTest : public WebRtcTestBase {
public:
OneTimePermissionInteractiveUiTest()
: geolocation_overrider_(
std::make_unique<device::ScopedGeolocationOverrider>(6.66, 9.99)) {
}
OneTimePermissionInteractiveUiTest(
const OneTimePermissionInteractiveUiTest&) = delete;
OneTimePermissionInteractiveUiTest& operator=(
const OneTimePermissionInteractiveUiTest&) = delete;
~OneTimePermissionInteractiveUiTest() override = default;
enum InitializationOptions {
// The default profile and browser window will be used.
INITIALIZATION_DEFAULT,
// A new tab will be created using the default profile and browser window.
INITIALIZATION_NEWTAB,
// A new tab will be created using the default profile and browser window,
// then the tab at position 0 will be closed.
INITIALIZATION_CLOSETAB_NEWTAB
};
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream);
}
// InProcessBrowserTest:
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->Start());
}
Browser* current_browser() { return current_browser_; }
GURL GetDifferentOriginUrl() const { return GURL("https://test.com"); }
GURL GetGeolocationGurl() const {
return embedded_test_server()->GetURL("/geolocation/simple.html");
}
GURL GetWebrtcGurl() const {
return embedded_test_server()->GetURL("/webrtc/webrtc_jsep01_test.html");
}
// Initializes the test server and navigates to the initial url.
void Initialize(InitializationOptions options, const GURL& url) {
current_browser_ = browser();
if (options == INITIALIZATION_NEWTAB) {
chrome::NewTab(current_browser_);
} else if (options == INITIALIZATION_CLOSETAB_NEWTAB) {
chrome::NewTabToRight(current_browser_);
current_browser_->tab_strip_model()->CloseWebContentsAt(
0, TabCloseTypes::CLOSE_CREATE_HISTORICAL_TAB);
}
ASSERT_TRUE(current_browser_);
ASSERT_TRUE(ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
current_browser_, url, 1));
SetFrameForScriptExecutionToCurrent(
current_browser_->tab_strip_model()->GetActiveWebContents());
}
void SetFrameForScriptExecutionToCurrent(content::WebContents* contents) {
render_frame_host_ = contents->GetPrimaryMainFrame();
}
void CloseLastLocalStreamAt(int index) {
IndicatorObserver observer;
WebRtcTestBase::CloseLastLocalStream(
current_browser()->tab_strip_model()->GetWebContentsAt(index));
observer.Wait();
}
void FireRunningExpirationTimers() {
OneTimePermissionsTrackerFactory::GetForBrowserContext(
current_browser()
->tab_strip_model()
->GetActiveWebContents()
->GetBrowserContext())
->FireRunningTimersForTesting();
}
void WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::AutoResponseType auto_response,
bool expect_prompt) {
content::WebContents* contents =
current_browser()->tab_strip_model()->GetActiveWebContents();
SetFrameForScriptExecutionToCurrent(contents);
permissions::PermissionRequestManager::FromWebContents(contents)
->set_auto_response_for_test(auto_response);
permissions::PermissionRequestObserver observer(contents);
if (expect_prompt) {
// Control will return as soon as the API call is made, and then the
// observer will wait for the request to display.
std::string result =
content::EvalJs(contents, "geoStartWithAsyncResponse();")
.ExtractString();
if (auto_response == permissions::PermissionRequestManager::DISMISS ||
auto_response == permissions::PermissionRequestManager::DENY_ALL) {
EXPECT_TRUE(result == "request-callback-error");
} else {
EXPECT_TRUE(
result == "request-callback-success" || // First request.
result ==
"geoposition-updated"); // May occur when page is not reloaded,
// lost permission and successfully
// prompted for it again.
}
observer.Wait();
} else {
// Control will return once one of the callbacks fires.
RunScript(render_frame_host_, "geoStartWithSyncResponse()");
}
EXPECT_EQ(expect_prompt, observer.request_shown());
}
void GetUserMediaAndExpectGrantedPermission(
permissions::PermissionRequestManager::AutoResponseType auto_response,
bool expect_prompt,
int tab_index = 0) {
content::WebContents* contents =
current_browser()->tab_strip_model()->GetWebContentsAt(tab_index);
SetFrameForScriptExecutionToCurrent(contents);
permissions::PermissionRequestManager::FromWebContents(contents)
->set_auto_response_for_test(auto_response);
permissions::PermissionRequestObserver observer(contents);
GetUserMedia(contents, kAudioVideoCallConstraints);
EXPECT_EQ(expect_prompt, observer.request_shown());
EXPECT_EQ(
content::EvalJs(render_frame_host_, "obtainGetUserMediaResult();"),
kOkGotStream);
}
void DiscardTabAt(int index) {
resource_coordinator::TabLifecycleUnitSource::GetTabLifecycleUnitExternal(
browser()->tab_strip_model()->GetWebContentsAt(index))
->DiscardTab(mojom::LifecycleUnitDiscardReason::URGENT);
}
protected:
void OtpEventExpectUniqueSample(ContentSettingsType content_setting_type,
permissions::OneTimePermissionEvent event,
int occ) {
histograms_.ExpectUniqueSample(
permissions::PermissionUmaUtil::GetOneTimePermissionEventHistogram(
content_setting_type),
static_cast<base::HistogramBase::Sample32>(event), occ);
}
void OtpEventExpectBucketCount(ContentSettingsType content_setting_type,
permissions::OneTimePermissionEvent event,
int occ) {
histograms_.ExpectBucketCount(
permissions::PermissionUmaUtil::GetOneTimePermissionEventHistogram(
content_setting_type),
static_cast<base::HistogramBase::Sample32>(event), occ);
}
std::unique_ptr<device::ScopedGeolocationOverrider> geolocation_overrider_;
raw_ptr<Browser, AcrossTasksDanglingUntriaged> current_browser_ = nullptr;
base::HistogramTester histograms_;
private:
// The render frame host where JS calls will be executed.
raw_ptr<content::RenderFrameHost, AcrossTasksDanglingUntriaged>
render_frame_host_ = nullptr;
};
IN_PROC_BROWSER_TEST_F(OneTimePermissionInteractiveUiTest,
OneTimeGrantResetsEmbargoCounter) {
auto* autoblocker =
permissions::PermissionsClient::Get()->GetPermissionDecisionAutoBlocker(
browser()
->tab_strip_model()
->GetWebContentsAt(0)
->GetBrowserContext());
EXPECT_EQ(0, autoblocker->GetDismissCount(GetGeolocationGurl(),
ContentSettingsType::GEOLOCATION));
// Navigate to geolocation page, request geolocation permission, expect prompt
// and dismiss it.
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_DEFAULT, GetGeolocationGurl()));
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::DISMISS, true);
// Expect that embargo counter has been incremented.
EXPECT_EQ(1, autoblocker->GetDismissCount(GetGeolocationGurl(),
ContentSettingsType::GEOLOCATION));
// Refresh.
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_DEFAULT, GetGeolocationGurl()));
// Request geolocation permission, expect prompt and grant it once.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true);
// Expect the embargo counter has been reset to 0.
EXPECT_EQ(0, autoblocker->GetDismissCount(GetGeolocationGurl(),
ContentSettingsType::GEOLOCATION));
}
IN_PROC_BROWSER_TEST_F(OneTimePermissionInteractiveUiTest,
SameTabForegroundBehaviour) {
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_DEFAULT, GetGeolocationGurl()));
// Request geolocation permission, expect prompt and grant it.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true);
// Navigate to the same site in the same tab.
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_DEFAULT, GetGeolocationGurl()));
// Ensure position is accessible in new tab without prompt.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, false);
// Ensure grant event is recorded
OtpEventExpectUniqueSample(
ContentSettingsType::GEOLOCATION,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 1);
}
IN_PROC_BROWSER_TEST_F(OneTimePermissionInteractiveUiTest,
VerifyDifferentTabBehaviour) {
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_DEFAULT, GetGeolocationGurl()));
// Request geolocation permission, expect prompt and grant it.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true);
// Open new tab to the right.
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_NEWTAB, GetGeolocationGurl()));
// Ensure position is accessible in new tab without prompt.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, false);
// Ensure grant event is only recorded once
OtpEventExpectUniqueSample(
ContentSettingsType::GEOLOCATION,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 1);
}
IN_PROC_BROWSER_TEST_F(OneTimePermissionInteractiveUiTest,
VerifyPermissionPromptAfterClosingAllTabsToOrigin) {
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_DEFAULT, GetGeolocationGurl()));
// Request geolocation permission, expect prompt and grant it.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true);
// Ensure no content setting is persisted.
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_CLOSETAB_NEWTAB, GetGeolocationGurl()));
// Ensure that a prompt is triggered.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ALL, true);
// Since the second request was resolved with a persistent accept, only one
// otp-grant event should be recorded
OtpEventExpectBucketCount(
ContentSettingsType::GEOLOCATION,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 1);
OtpEventExpectBucketCount(
ContentSettingsType::GEOLOCATION,
permissions::OneTimePermissionEvent::ALL_TABS_CLOSED_OR_DISCARDED, 1);
}
IN_PROC_BROWSER_TEST_F(OneTimePermissionInteractiveUiTest,
DiscardingTabToOriginRevokesPermission) {
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_DEFAULT, GetGeolocationGurl()));
// Request geolocation permission, expect prompt and grant it.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true);
// Open new tab to the right.
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_NEWTAB, GetDifferentOriginUrl()));
// Discard previous tab
DiscardTabAt(0);
// Open new tab to the right.
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_DEFAULT, GetGeolocationGurl()));
// Ensure that a prompt is triggered again when requesting geolocation
// permission.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ALL, true);
// Since the second request was resolved with a persistent accept, only one
// otp-grant event should be recorded
OtpEventExpectBucketCount(
ContentSettingsType::GEOLOCATION,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 1);
OtpEventExpectBucketCount(
ContentSettingsType::GEOLOCATION,
permissions::OneTimePermissionEvent::ALL_TABS_CLOSED_OR_DISCARDED, 1);
}
IN_PROC_BROWSER_TEST_F(
OneTimePermissionInteractiveUiTest,
DiscardingTabToOriginDoesNotRevokePermissionIfDifferentTabToOriginIsUsingIt) {
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_DEFAULT, GetGeolocationGurl()));
// Request geolocation permission, expect prompt and grant it.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true);
// Open new tab to the right.
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_NEWTAB, GetGeolocationGurl()));
// Request geolocation permission in new tab, expect no prompt.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, false);
// Discard previous tab
DiscardTabAt(0);
// Reload
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_DEFAULT, GetGeolocationGurl()));
// Request geolocation permission and ensure that no prompt is triggered.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, false);
// Ensure that only a single GRANTED_ONE_TIME event is recorded
OtpEventExpectUniqueSample(
ContentSettingsType::GEOLOCATION,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 1);
}
IN_PROC_BROWSER_TEST_F(OneTimePermissionInteractiveUiTest,
GeolocationIsRevokedAfterFiveMinutesInBackground) {
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_DEFAULT, GetGeolocationGurl()));
// Request geolocation permission, expect prompt and grant it.
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true);
// Open new tab, this puts the first tab in the background
ASSERT_NO_FATAL_FAILURE(
Initialize(INITIALIZATION_NEWTAB, GetDifferentOriginUrl()));
// Fire running timers. This means, that all one time permission expiration
// timers that are running at this point in time will fire their callbacks and
// are stopped.
OneTimePermissionsTrackerFactory::GetForBrowserContext(
browser()->tab_strip_model()->GetWebContentsAt(0)->GetBrowserContext())
->FireRunningTimersForTesting();
// Go to previous tab.
browser()->tab_strip_model()->ActivateTabAt(0);
// Ensure that a prompt is triggered again when requesting permission
WatchPositionAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true);
OtpEventExpectBucketCount(
ContentSettingsType::GEOLOCATION,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 2);
OtpEventExpectBucketCount(
ContentSettingsType::GEOLOCATION,
permissions::OneTimePermissionEvent::EXPIRED_IN_BACKGROUND, 1);
}
IN_PROC_BROWSER_TEST_F(OneTimePermissionInteractiveUiTest,
CamMicNotRevokedWhenPausedInForeground) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT, GetWebrtcGurl()));
// Request cam/mic permission, expect prompt and grant it.
GetUserMediaAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true, 0);
// Stop local streams (i.e. stop all cam/mic permission usage within specified
// web contents).
CloseLastLocalStreamAt(0);
// Fire running timers. This means, that all one time permission
// expiration timers that are running at this point in time will fire
// their callbacks and are stopped.
FireRunningExpirationTimers();
// Request cam/mic permission, expect no prompt is triggered.
GetUserMediaAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, false, 0);
// Ensure that only a single GRANTED_ONE_TIME event is recorded per permission
OtpEventExpectUniqueSample(
ContentSettingsType::MEDIASTREAM_MIC,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 1);
OtpEventExpectUniqueSample(
ContentSettingsType::MEDIASTREAM_CAMERA,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 1);
}
IN_PROC_BROWSER_TEST_F(OneTimePermissionInteractiveUiTest,
CamMicRevokedWhenPausedInBackground) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT, GetWebrtcGurl()));
// Request cam/mic permission, expect prompt and grant it.
GetUserMediaAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true, 0);
// Open new tab, this puts the first tab in the background.
Initialize(INITIALIZATION_NEWTAB, GetDifferentOriginUrl());
// Stop local streams in previous tab (i.e. stop all cam/mic permission usage
// within specified web contents).
CloseLastLocalStreamAt(0);
// Fire running timers. This means, that all one time permission
// expiration timers that are running at this point in time will fire
// their callbacks and are stopped.
FireRunningExpirationTimers();
// Switch back to previous tab
browser()->tab_strip_model()->ActivateTabAt(0);
// Request cam/mic permission, expect a prompt is triggered.
GetUserMediaAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true, 0);
OtpEventExpectBucketCount(
ContentSettingsType::MEDIASTREAM_MIC,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 2);
OtpEventExpectBucketCount(
ContentSettingsType::MEDIASTREAM_CAMERA,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 2);
OtpEventExpectBucketCount(
ContentSettingsType::MEDIASTREAM_MIC,
permissions::OneTimePermissionEvent::EXPIRED_IN_BACKGROUND, 1);
OtpEventExpectBucketCount(
ContentSettingsType::MEDIASTREAM_CAMERA,
permissions::OneTimePermissionEvent::EXPIRED_IN_BACKGROUND, 1);
}
IN_PROC_BROWSER_TEST_F(OneTimePermissionInteractiveUiTest,
CamMicDoesNotExpireWhenNotPausedInBackground) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT, GetWebrtcGurl()));
// Request cam/mic permission, expect prompt and grant it.
GetUserMediaAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true, 0);
// Open new tab, this puts the first tab in the background.
Initialize(INITIALIZATION_NEWTAB, GetDifferentOriginUrl());
// Fire running timers. This means, that all one time permission expiration
// timers that are running at this point in time will fire their callbacks and
// are stopped.
FireRunningExpirationTimers();
// Switch back to previous tab
browser()->tab_strip_model()->ActivateTabAt(0);
// Request cam/mic permission, expect no prompt is triggered.
GetUserMediaAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, false, 0);
// Ensure that only a single GRANTED_ONE_TIME event is recorded per permission
OtpEventExpectUniqueSample(
ContentSettingsType::MEDIASTREAM_MIC,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 1);
OtpEventExpectUniqueSample(
ContentSettingsType::MEDIASTREAM_CAMERA,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 1);
}
IN_PROC_BROWSER_TEST_F(OneTimePermissionInteractiveUiTest,
CamMicDoesNotExpireWhenPausedInForeground) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT, GetWebrtcGurl()));
// Request cam/mic permission, expect prompt and grant it.
GetUserMediaAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true, 0);
// Stop local streams (i.e. stop all cam/mic permission usage within specified
// web contents).
CloseLastLocalStreamAt(0);
// Fire running timers. This means, that all one time permission
// expiration timers that are running at this point in time will fire
// their callbacks and are stopped.
FireRunningExpirationTimers();
// Request cam/mic permission, expect no prompt is triggered.
GetUserMediaAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, false, 0);
// Ensure that only a single GRANTED_ONE_TIME event is recorded per permission
OtpEventExpectUniqueSample(
ContentSettingsType::MEDIASTREAM_MIC,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 1);
OtpEventExpectUniqueSample(
ContentSettingsType::MEDIASTREAM_CAMERA,
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME, 1);
}
class OneTimePermissionExpiryEnforcementUmaInteractiveUiTest
: public OneTimePermissionInteractiveUiTest,
public ::testing::WithParamInterface<bool> {
public:
OneTimePermissionExpiryEnforcementUmaInteractiveUiTest() {
if (GetParam()) {
feature_list_.InitWithFeatures(
{content_settings::features::kActiveContentSettingExpiry}, {});
} else {
feature_list_.InitWithFeatures(
{}, {content_settings::features::kActiveContentSettingExpiry});
}
}
OneTimePermissionExpiryEnforcementUmaInteractiveUiTest(
const OneTimePermissionExpiryEnforcementUmaInteractiveUiTest&) = delete;
OneTimePermissionExpiryEnforcementUmaInteractiveUiTest& operator=(
const OneTimePermissionExpiryEnforcementUmaInteractiveUiTest&) = delete;
private:
base::test::ScopedFeatureList feature_list_;
};
INSTANTIATE_TEST_SUITE_P(All,
OneTimePermissionExpiryEnforcementUmaInteractiveUiTest,
testing::Bool());
IN_PROC_BROWSER_TEST_P(OneTimePermissionExpiryEnforcementUmaInteractiveUiTest,
TestExpiryEnforcement) {
base::HistogramTester histograms;
const std::string kActiveExpiryHistogram =
"ContentSettings.ActiveExpiry.OneTimePermissionProvider."
"ContentSettingsType";
bool active_expiry_is_active = GetParam();
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT, GetWebrtcGurl()));
auto* hcsm =
HostContentSettingsMapFactory::GetForProfile(browser()->profile());
// Setup to request content setting in the past (maximum one time
// permission grant lifetime)
base::Time now(base::Time::Now());
base::Time past(now - permissions::kOneTimePermissionMaximumLifetime);
base::SimpleTestClock clock;
clock.SetNow(past);
hcsm->SetClockForTesting(&clock);
// Request permission, expect prompt and grant it.
GetUserMediaAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, true);
// Manually run expiry enforcement
hcsm->DeleteNearlyExpiredSettingsAndMaybeScheduleNextRun(
ContentSettingsType::MEDIASTREAM_MIC);
hcsm->DeleteNearlyExpiredSettingsAndMaybeScheduleNextRun(
ContentSettingsType::MEDIASTREAM_CAMERA);
// Reload and ensure permission remains accessible without prompt.
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT, GetWebrtcGurl()));
GetUserMediaAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE, false);
// Set clock time to now (i.e. 16 hours later) and then manually run expiry
// enforcement again.
clock.SetNow(now + base::Seconds(5));
hcsm->DeleteNearlyExpiredSettingsAndMaybeScheduleNextRun(
ContentSettingsType::MEDIASTREAM_MIC);
hcsm->DeleteNearlyExpiredSettingsAndMaybeScheduleNextRun(
ContentSettingsType::MEDIASTREAM_CAMERA);
// Ensure a request without reload triggers a prompt again only if active
// expiry is active
GetUserMediaAndExpectGrantedPermission(
permissions::PermissionRequestManager::ACCEPT_ONCE,
active_expiry_is_active);
// Check UMA records for expiry events (only recorded if active expiry is
// enabled)
histograms.ExpectTotalCount(kActiveExpiryHistogram,
active_expiry_is_active ? 2 : 0);
histograms.ExpectBucketCount(
kActiveExpiryHistogram,
static_cast<base::HistogramBase::Sample32>(
content_settings_uma_util::ContentSettingTypeToHistogramValue(
ContentSettingsType::MEDIASTREAM_MIC)),
active_expiry_is_active ? 1 : 0);
histograms.ExpectBucketCount(
kActiveExpiryHistogram,
static_cast<base::HistogramBase::Sample32>(
content_settings_uma_util::ContentSettingTypeToHistogramValue(
ContentSettingsType::MEDIASTREAM_CAMERA)),
active_expiry_is_active ? 1 : 0);
// Check UMA records for grant events (if expiry is disabled, there's only one
// grant event)
histograms.ExpectTotalCount(
permissions::PermissionUmaUtil::GetOneTimePermissionEventHistogram(
ContentSettingsType::MEDIASTREAM_CAMERA),
active_expiry_is_active ? 3 : 1);
histograms.ExpectBucketCount(
permissions::PermissionUmaUtil::GetOneTimePermissionEventHistogram(
ContentSettingsType::MEDIASTREAM_CAMERA),
static_cast<base::HistogramBase::Sample32>(
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME),
active_expiry_is_active ? 2 : 1);
histograms.ExpectBucketCount(
permissions::PermissionUmaUtil::GetOneTimePermissionEventHistogram(
ContentSettingsType::MEDIASTREAM_CAMERA),
static_cast<base::HistogramBase::Sample32>(
permissions::OneTimePermissionEvent::EXPIRED_AFTER_MAXIMUM_LIFETIME),
active_expiry_is_active ? 1 : 0);
histograms.ExpectTotalCount(
permissions::PermissionUmaUtil::GetOneTimePermissionEventHistogram(
ContentSettingsType::MEDIASTREAM_MIC),
active_expiry_is_active ? 3 : 1);
histograms.ExpectBucketCount(
permissions::PermissionUmaUtil::GetOneTimePermissionEventHistogram(
ContentSettingsType::MEDIASTREAM_MIC),
static_cast<base::HistogramBase::Sample32>(
permissions::OneTimePermissionEvent::GRANTED_ONE_TIME),
active_expiry_is_active ? 2 : 1);
histograms.ExpectBucketCount(
permissions::PermissionUmaUtil::GetOneTimePermissionEventHistogram(
ContentSettingsType::MEDIASTREAM_MIC),
static_cast<base::HistogramBase::Sample32>(
permissions::OneTimePermissionEvent::EXPIRED_AFTER_MAXIMUM_LIFETIME),
active_expiry_is_active ? 1 : 0);
hcsm->SetClockForTesting(base::DefaultClock::GetInstance());
}
|