#include "remoting/host/policy_watcher.h"
#include <memory>
#include <utility>
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/values.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/policy/core/common/async_policy_loader.h"
#include "components/policy/core/common/async_policy_provider.h"
#include "components/policy/core/common/policy_namespace.h"
#include "components/policy/core/common/policy_service_impl.h"
#include "components/policy/core/common/schema.h"
#include "components/policy/core/common/schema_registry.h"
#include "components/policy/policy_constants.h"
#include "remoting/base/port_range.h"
#if !defined(NDEBUG)
#include "base/json/json_reader.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "components/policy/core/common/policy_loader_win.h"
#elif BUILDFLAG(IS_APPLE)
#include "base/apple/foundation_util.h"
#include "base/strings/sys_string_conversions.h"
#include "components/policy/core/common/policy_loader_mac.h"
#include "components/policy/core/common/preferences_mac.h"
#elif BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
#include "components/policy/core/common/config_dir_policy_loader.h"
#include "components/policy/core/common/policy_paths.h"
#endif
namespace remoting {
key;
namespace {
#if BUILDFLAG(IS_WIN)
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
constexpr wchar_t kChromePolicyKey[] = L"SOFTWARE\\Policies\\Google\\Chrome";
#else
constexpr wchar_t kChromePolicyKey[] = L"SOFTWARE\\Policies\\Chromium";
#endif
#endif
base::Value::Dict CopyValuesAndAddDefaults(
const base::Value::Dict& from,
const base::Value::Dict& default_values) { … }
policy::PolicyNamespace GetPolicyNamespace() { … }
std::unique_ptr<policy::SchemaRegistry> CreateSchemaRegistry() { … }
base::Value::Dict CopyChromotingPoliciesIntoDictionary(
const policy::PolicyMap& current) { … }
bool VerifyWellformedness(const base::Value::Dict& changed_policies) { … }
}
void PolicyWatcher::StartWatching(
const PolicyUpdatedCallback& policy_updated_callback,
const PolicyErrorCallback& policy_error_callback) { … }
base::Value::Dict PolicyWatcher::GetEffectivePolicies() { … }
base::Value::Dict PolicyWatcher::GetPlatformPolicies() { … }
base::Value::Dict PolicyWatcher::GetDefaultPolicies() { … }
void PolicyWatcher::SignalPolicyError() { … }
PolicyWatcher::PolicyWatcher(
policy::PolicyService* policy_service,
std::unique_ptr<policy::PolicyService> owned_policy_service,
std::unique_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider,
std::unique_ptr<policy::SchemaRegistry> owned_schema_registry)
: … { … }
PolicyWatcher::~PolicyWatcher() { … }
const policy::Schema* PolicyWatcher::GetPolicySchema() const { … }
bool PolicyWatcher::NormalizePolicies(base::Value* policy_dict) { … }
void PolicyWatcher::HandleDeprecatedPolicies(base::Value::Dict* dict) { … }
base::Value::Dict PolicyWatcher::StoreNewAndReturnChangedPolicies(
base::Value::Dict new_policies) { … }
void PolicyWatcher::OnPolicyUpdated(const policy::PolicyNamespace& ns,
const policy::PolicyMap& previous,
const policy::PolicyMap& current) { … }
void PolicyWatcher::OnPolicyServiceInitialized(policy::PolicyDomain domain) { … }
std::unique_ptr<PolicyWatcher> PolicyWatcher::CreateFromPolicyLoader(
std::unique_ptr<policy::AsyncPolicyLoader> async_policy_loader) { … }
std::unique_ptr<PolicyWatcher> PolicyWatcher::CreateWithPolicyService(
policy::PolicyService* policy_service) { … }
#if BUILDFLAG(IS_WIN)
void PolicyWatcher::WatchForRegistryChanges() {
if (!policy_key_.Valid()) {
auto open_result =
policy_key_.Open(HKEY_LOCAL_MACHINE, kChromePolicyKey, KEY_NOTIFY);
if (open_result != ERROR_SUCCESS) {
LOG(WARNING) << "Failed to open Chrome policy registry key due to error: "
<< open_result;
return;
}
}
auto watch_result = policy_key_.StartWatching(
base::BindOnce(&policy::PolicyService::RefreshPolicies,
base::Unretained(policy_service_),
base::BindOnce(&PolicyWatcher::WatchForRegistryChanges,
base::Unretained(this)),
policy::PolicyFetchReason::kCrdHostPolicyWatcher));
if (!watch_result) {
LOG(WARNING) << "Failed to register for Chrome policy registry key changes";
policy_key_.Close();
}
}
#endif
std::unique_ptr<PolicyWatcher> PolicyWatcher::CreateWithTaskRunner(
const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner,
policy::ManagementService* management_service) { … }
std::unique_ptr<PolicyWatcher> PolicyWatcher::CreateFromPolicyLoaderForTesting(
std::unique_ptr<policy::AsyncPolicyLoader> async_policy_loader) { … }
}