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
|
// 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 "components/policy/core/common/management/management_service.h"
#include <ostream>
#include <tuple>
#include <variant>
#include "base/barrier_closure.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/persistent_pref_store.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
namespace policy {
ManagementStatusProvider::ManagementStatusProvider() = default;
ManagementStatusProvider::ManagementStatusProvider(
const std::string& cache_pref_name)
: cache_pref_name_(cache_pref_name) {}
ManagementStatusProvider::~ManagementStatusProvider() = default;
EnterpriseManagementAuthority ManagementStatusProvider::GetAuthority() {
if (!RequiresCache())
return FetchAuthority();
if (std::holds_alternative<PrefService*>(cache_) &&
std::get<PrefService*>(cache_) &&
std::get<PrefService*>(cache_)->HasPrefPath(cache_pref_name_)) {
return static_cast<EnterpriseManagementAuthority>(
std::get<PrefService*>(cache_)->GetInteger(cache_pref_name_));
}
if (std::holds_alternative<scoped_refptr<PersistentPrefStore>>(cache_) &&
std::holds_alternative<scoped_refptr<PersistentPrefStore>>(cache_)) {
const base::Value* value = nullptr;
if (std::get<scoped_refptr<PersistentPrefStore>>(cache_)->GetValue(
cache_pref_name_, &value) &&
value->is_int()) {
return static_cast<EnterpriseManagementAuthority>(value->GetInt());
}
}
return EnterpriseManagementAuthority::NONE;
}
bool ManagementStatusProvider::RequiresCache() const {
return !cache_pref_name_.empty();
}
void ManagementStatusProvider::UpdateCache(
EnterpriseManagementAuthority authority) {
DCHECK(std::holds_alternative<PrefService*>(cache_))
<< "A PrefService is required to refresh the management "
"status provider cache.";
std::get<PrefService*>(cache_)->SetInteger(cache_pref_name_, authority);
}
void ManagementStatusProvider::UsePrefStoreAsCache(
scoped_refptr<PersistentPrefStore> pref_store) {
DCHECK(!cache_pref_name_.empty())
<< "This management status provider does not support caching";
cache_ = pref_store;
}
void ManagementStatusProvider::UsePrefServiceAsCache(PrefService* prefs) {
DCHECK(!cache_pref_name_.empty())
<< "This management status provider does not support caching";
cache_ = prefs;
}
ManagementService::ManagementService(
std::vector<std::unique_ptr<ManagementStatusProvider>> providers)
: management_status_providers_(std::move(providers)) {}
ManagementService::~ManagementService() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
void ManagementService::UsePrefServiceAsCache(PrefService* prefs) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (const auto& provider : management_status_providers_) {
if (provider->RequiresCache())
provider->UsePrefServiceAsCache(prefs);
}
}
void ManagementService::UsePrefStoreAsCache(
scoped_refptr<PersistentPrefStore> pref_store) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (const auto& provider : management_status_providers_) {
if (provider->RequiresCache())
provider->UsePrefStoreAsCache(pref_store);
}
}
void ManagementService::RefreshCache(CacheRefreshCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ManagementAuthorityTrustworthiness previous =
GetManagementAuthorityTrustworthiness();
for (const auto& provider : management_status_providers_) {
if (provider->RequiresCache()) {
provider->UpdateCache(provider->FetchAuthority());
}
ManagementAuthorityTrustworthiness next =
GetManagementAuthorityTrustworthiness();
if (callback)
std::move(callback).Run(previous, next);
}
}
ui::ImageModel* ManagementService::GetManagementIconForProfile() {
return nullptr;
}
gfx::Image* ManagementService::GetManagementIconForBrowser() {
return nullptr;
}
bool ManagementService::HasManagementAuthority(
EnterpriseManagementAuthority authority) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return GetManagementAuthorities() & authority;
}
ManagementAuthorityTrustworthiness
ManagementService::GetManagementAuthorityTrustworthiness() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (HasManagementAuthority(EnterpriseManagementAuthority::CLOUD_DOMAIN))
return ManagementAuthorityTrustworthiness::FULLY_TRUSTED;
if (HasManagementAuthority(EnterpriseManagementAuthority::CLOUD))
return ManagementAuthorityTrustworthiness::TRUSTED;
if (HasManagementAuthority(EnterpriseManagementAuthority::DOMAIN_LOCAL))
return ManagementAuthorityTrustworthiness::TRUSTED;
if (HasManagementAuthority(EnterpriseManagementAuthority::COMPUTER_LOCAL))
return ManagementAuthorityTrustworthiness::LOW;
return ManagementAuthorityTrustworthiness::NONE;
}
int ManagementService::GetManagementAuthorities() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (management_authorities_for_testing_)
return management_authorities_for_testing_.value();
int result = 0;
for (const auto& provider : management_status_providers_)
result |= provider->GetAuthority();
return result;
}
void ManagementService::SetManagementStatusProviderForTesting(
std::vector<std::unique_ptr<ManagementStatusProvider>> providers) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SetManagementStatusProvider(std::move(providers));
}
void ManagementService::SetManagementAuthoritiesForTesting(
int management_authorities) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
management_authorities_for_testing_ = management_authorities;
}
void ManagementService::ClearManagementAuthoritiesForTesting() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
management_authorities_for_testing_.reset();
}
// static
void ManagementService::RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
#if BUILDFLAG(IS_WIN)
registry->RegisterIntegerPref(policy_prefs::kAzureActiveDirectoryManagement,
NONE);
registry->RegisterIntegerPref(policy_prefs::kEnterpriseMDMManagementWindows,
NONE);
#elif BUILDFLAG(IS_MAC)
registry->RegisterIntegerPref(policy_prefs::kEnterpriseMDMManagementMac,
NONE);
#endif
}
bool ManagementService::IsManaged() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return GetManagementAuthorityTrustworthiness() >
ManagementAuthorityTrustworthiness::NONE;
}
bool ManagementService::IsAccountManaged() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return HasManagementAuthority(policy::EnterpriseManagementAuthority::CLOUD);
}
bool ManagementService::IsBrowserManaged() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return HasManagementAuthority(
policy::EnterpriseManagementAuthority::CLOUD_DOMAIN) ||
HasManagementAuthority(
policy::EnterpriseManagementAuthority::DOMAIN_LOCAL) ||
HasManagementAuthority(
policy::EnterpriseManagementAuthority::COMPUTER_LOCAL);
}
void ManagementService::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void ManagementService::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void ManagementService::SetManagementStatusProvider(
std::vector<std::unique_ptr<ManagementStatusProvider>> providers) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
management_status_providers_ = std::move(providers);
}
void ManagementService::AddManagementStatusProvider(
std::unique_ptr<ManagementStatusProvider> provider) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
management_status_providers_.push_back(std::move(provider));
}
void ManagementService::NotifyEnterpriseLabelUpdated() {
for (auto& observer : observers_) {
observer.OnEnterpriseLabelUpdated();
}
}
void ManagementService::NotifyEnterpriseLogoForBrowserUpdated() {
for (auto& observer : observers_) {
observer.OnEnterpriseLogoUpdatedForBrowser();
}
}
} // namespace policy
|