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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/permissions/system/system_permission_settings.h"
#include <map>
#include <memory>
#include <utility>
#include "base/auto_reset.h"
#include "base/check.h"
#include "base/check_deref.h"
#include "base/no_destructor.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/global_features.h"
#include "chrome/browser/permissions/system/platform_handle.h"
namespace system_permission_settings {
namespace {
PlatformHandle* instance_for_testing_ = nullptr;
bool g_mock_system_prompt_for_testing_ = false;
bool g_mock_system_permission_denied_for_testing_ = false;
std::map<ContentSettingsType, bool>& GlobalTestingBlockOverrides() {
static base::NoDestructor<std::map<ContentSettingsType, bool>>
g_testing_block_overrides;
return *g_testing_block_overrides;
}
PlatformHandle* GetPlatformHandle() {
if (instance_for_testing_) {
return instance_for_testing_;
}
return CHECK_DEREF(CHECK_DEREF(g_browser_process).GetFeatures())
.system_permissions_platform_handle();
}
} // namespace
// static
void SetInstanceForTesting(PlatformHandle* instance_for_testing) {
instance_for_testing_ = instance_for_testing;
}
// static
bool CanPrompt(ContentSettingsType type) {
return g_mock_system_prompt_for_testing_ ||
GetPlatformHandle()->CanPrompt(type);
}
// static
base::AutoReset<bool> MockSystemPromptForTesting() {
return base::AutoReset<bool>(&g_mock_system_prompt_for_testing_, true);
}
// static
base::AutoReset<bool> MockShowSystemSettingsForTesting() {
return base::AutoReset<bool>(&g_mock_system_permission_denied_for_testing_,
true);
}
// static
bool IsDenied(ContentSettingsType type) {
if (g_mock_system_permission_denied_for_testing_) {
return true;
}
if (GlobalTestingBlockOverrides().find(type) !=
GlobalTestingBlockOverrides().end()) {
return GlobalTestingBlockOverrides().at(type);
}
return GetPlatformHandle()->IsDenied(type);
}
// static
bool IsAllowed(ContentSettingsType type) {
if (g_mock_system_permission_denied_for_testing_) {
return false;
}
if (GlobalTestingBlockOverrides().find(type) !=
GlobalTestingBlockOverrides().end()) {
return !GlobalTestingBlockOverrides().at(type);
}
return GetPlatformHandle()->IsAllowed(type);
}
// static
void OpenSystemSettings(content::WebContents* web_contents,
ContentSettingsType type) {
GetPlatformHandle()->OpenSystemSettings(web_contents, type);
}
// static
void Request(ContentSettingsType type,
SystemPermissionResponseCallback callback) {
// If 'g_mock_system_prompt_for_testing_' is true, don't request the
// permission.
if (g_mock_system_prompt_for_testing_) {
return;
}
GetPlatformHandle()->Request(type, std::move(callback));
}
// static
std::unique_ptr<ScopedObservation> Observe(
SystemPermissionChangedCallback observer) {
return GetPlatformHandle()->Observe(observer);
}
ScopedSettingsForTesting::ScopedSettingsForTesting(ContentSettingsType type,
bool blocked)
: type_(type) {
CHECK(GlobalTestingBlockOverrides().find(type) ==
GlobalTestingBlockOverrides().end());
GlobalTestingBlockOverrides()[type] = blocked;
}
ScopedSettingsForTesting::~ScopedSettingsForTesting() {
GlobalTestingBlockOverrides().erase(type_);
}
} // namespace system_permission_settings
|