[go: nahoru, domu]

blob: 10717b0f6dd1d6687584bce48f50200855643e20 [file] [log] [blame]
Avi Drissmand6cdf9b2022-09-15 19:52:531// Copyright 2015 The Chromium Authors
simonmorris@chromium.org000d1f62012-07-24 01:56:542// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
lukaszac285c9a2015-01-29 23:18:285#ifndef REMOTING_HOST_POLICY_WATCHER_H_
6#define REMOTING_HOST_POLICY_WATCHER_H_
simonmorris@chromium.org000d1f62012-07-24 01:56:547
dcheng0765c492016-04-06 22:41:538#include <memory>
9
Avi Drissman135261e2023-01-11 22:43:1510#include "base/functional/callback.h"
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
Lei Zhangb2710ea2022-11-07 20:45:1412#include "base/memory/scoped_refptr.h"
gabbf77513a2017-06-01 14:35:3413#include "base/sequence_checker.h"
Morten Stenshorne4654b9c2022-08-31 14:12:5014#include "base/values.h"
Joe Downing21478b5d2021-10-25 16:50:2215#include "build/build_config.h"
lukaszaa9376cd2015-01-28 20:29:0116#include "components/policy/core/common/policy_service.h"
simonmorris@chromium.org000d1f62012-07-24 01:56:5417
Xiaohan Wang453b38672022-01-13 20:02:4418#if BUILDFLAG(IS_WIN)
Joe Downing21478b5d2021-10-25 16:50:2219#include "base/win/registry.h"
20#endif
21
simonmorris@chromium.org000d1f62012-07-24 01:56:5422namespace base {
23class SingleThreadTaskRunner;
simonmorris@chromium.org000d1f62012-07-24 01:56:5424} // namespace base
25
lukaszad5a6bda2015-01-17 00:43:2526namespace policy {
lukaszaa9376cd2015-01-28 20:29:0127class AsyncPolicyLoader;
28class ConfigurationPolicyProvider;
Yann Dagof101f662021-08-20 02:22:3729class ManagementService;
lukaszae5d69ea2015-02-23 21:00:3130class Schema;
lukaszaa9376cd2015-01-28 20:29:0131class SchemaRegistry;
lukaszad5a6bda2015-01-17 00:43:2532} // namespace policy
33
simonmorris@chromium.org000d1f62012-07-24 01:56:5434namespace remoting {
simonmorris@chromium.org000d1f62012-07-24 01:56:5435
sergeyub2ae7e32015-01-30 23:33:2536// Watches for changes to the managed remote access host policies.
gabbf77513a2017-06-01 14:35:3437class PolicyWatcher : public policy::PolicyService::Observer {
simonmorris@chromium.org000d1f62012-07-24 01:56:5438 public:
39 // Called first with all policies, and subsequently with any changed policies.
Yuwei Huang2390bd162024-02-19 09:30:1040 // Policies that are unchanged will be absent in the returned dictionary.
41 // If a policy has no default value but is unset, it will be an empty Value,
42 // i.e., of type NONE.
Lei Zhangdea94ed2023-10-06 21:04:5843 using PolicyUpdatedCallback =
44 base::RepeatingCallback<void(base::Value::Dict)>;
lukasza0fdd9512014-11-25 17:47:4645
46 // Called after detecting malformed policies.
Lei Zhangdea94ed2023-10-06 21:04:5847 using PolicyErrorCallback = base::RepeatingCallback<void()>;
simonmorris@chromium.org000d1f62012-07-24 01:56:5448
Peter Boströme9178e42021-09-22 18:11:4949 PolicyWatcher(const PolicyWatcher&) = delete;
50 PolicyWatcher& operator=(const PolicyWatcher&) = delete;
51
lukaszaa9376cd2015-01-28 20:29:0152 ~PolicyWatcher() override;
simonmorris@chromium.org000d1f62012-07-24 01:56:5453
lukasza0fdd9512014-11-25 17:47:4654 // This guarantees that the |policy_updated_callback| is called at least once
55 // with the current policies. After that, |policy_updated_callback| will be
56 // called whenever a change to any policy is detected. It will then be called
57 // only with the changed policies.
58 //
59 // |policy_error_callback| will be called when malformed policies are detected
60 // (i.e. wrong type of policy value, or unparseable files under
Lei Zhang2ccc622d2023-10-11 19:07:4761 // $POLICY_PATH/managed).
lukasza0fdd9512014-11-25 17:47:4662 // When called, the |policy_error_callback| is responsible for mitigating the
63 // security risk of running with incorrectly formulated policies (by either
64 // shutting down or locking down the host).
65 // After calling |policy_error_callback| PolicyWatcher will continue watching
66 // for policy changes and will call |policy_updated_callback| when the error
67 // is recovered from and may call |policy_error_callback| when new errors are
68 // found.
69 virtual void StartWatching(
70 const PolicyUpdatedCallback& policy_updated_callback,
71 const PolicyErrorCallback& policy_error_callback);
simonmorris@chromium.org000d1f62012-07-24 01:56:5472
jamiewalchc96bfee2017-05-11 17:10:5973 // Return the current policies. If the policies have not yet been read, or if
Joe Downingdcd2bb52020-12-15 16:20:0974 // an error occurred, the returned dictionary will be empty. The dictionary
75 // returned is the union of |platform_policies_| and |default_values_|.
Morten Stenshorne4654b9c2022-08-31 14:12:5076 base::Value::Dict GetEffectivePolicies();
jamiewalchcf2cc682017-05-06 00:51:4777
Joe Downingdcd2bb52020-12-15 16:20:0978 // Return the set of policies which have been explicitly set on the machine.
79 // If the policies have not yet been read, no policies have been set, or if
80 // an error occurred, the returned dictionary will be empty.
Morten Stenshorne4654b9c2022-08-31 14:12:5081 base::Value::Dict GetPlatformPolicies();
Joe Downingdcd2bb52020-12-15 16:20:0982
83 // Return the default policy values.
Morten Stenshorne4654b9c2022-08-31 14:12:5084 static base::Value::Dict GetDefaultPolicies();
jamiewalchcf2cc682017-05-06 00:51:4785
lukaszad5a6bda2015-01-17 00:43:2586 // Specify a |policy_service| to borrow (on Chrome OS, from the browser
Joe Downing734d0152017-07-18 02:56:1687 // process). PolicyWatcher must be used on the thread on which it is created.
88 // |policy_service| is called on the same thread.
lukaszad5a6bda2015-01-17 00:43:2589 //
Joe Downing734d0152017-07-18 02:56:1690 // When |policy_service| is specified then BrowserThread::UI is used for
91 // PolicyUpdatedCallback and PolicyErrorCallback.
92 static std::unique_ptr<PolicyWatcher> CreateWithPolicyService(
93 policy::PolicyService* policy_service);
94
95 // Construct and a new PolicyService for non-ChromeOS platforms.
96 // PolicyWatcher must be used on the thread on which it is created.
lukaszad5a6bda2015-01-17 00:43:2597 //
Joe Downing734d0152017-07-18 02:56:1698 // |file_task_runner| is used for reading the policy from files / registry /
99 // preferences (which are blocking operations). |file_task_runner| should be
100 // of TYPE_IO type.
101 static std::unique_ptr<PolicyWatcher> CreateWithTaskRunner(
Yann Dagof101f662021-08-20 02:22:37102 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner,
103 policy::ManagementService* management_service);
simonmorris@chromium.org000d1f62012-07-24 01:56:54104
rkjnsnd0aa1e52017-03-30 00:17:28105 // Creates a PolicyWatcher from the given loader instead of loading the policy
106 // from the default location.
107 //
108 // This can be used with FakeAsyncPolicyLoader to test policy handling of
109 // other components.
110 static std::unique_ptr<PolicyWatcher> CreateFromPolicyLoaderForTesting(
111 std::unique_ptr<policy::AsyncPolicyLoader> async_policy_loader);
112
lukaszaa9376cd2015-01-28 20:29:01113 private:
114 friend class PolicyWatcherTest;
simonmorris@chromium.org000d1f62012-07-24 01:56:54115
lukaszae5d69ea2015-02-23 21:00:31116 // Gets Chromoting schema stored inside |owned_schema_registry_|.
117 const policy::Schema* GetPolicySchema() const;
118
rkjnsn10cd2682017-04-28 22:04:01119 // Normalizes policies using Schema::Normalize and converts deprecated
120 // policies.
121 //
Joe Downingdcd2bb52020-12-15 16:20:09122 // - Returns false if |dict| is invalid, e.g. contains mistyped policy values.
lukasza0d40d8a2015-03-03 18:36:28123 // - Returns true if |dict| was valid or got normalized.
Morten Stenshorne4654b9c2022-08-31 14:12:50124 bool NormalizePolicies(base::Value* dict);
lukasza0d40d8a2015-03-03 18:36:28125
rkjnsn10cd2682017-04-28 22:04:01126 // Converts each deprecated policy to its replacement if and only if the
127 // replacement policy is not set, and removes deprecated policied from dict.
Morten Stenshorne4654b9c2022-08-31 14:12:50128 void HandleDeprecatedPolicies(base::Value::Dict* dict);
rkjnsn10cd2682017-04-28 22:04:01129
Joe Downingdcd2bb52020-12-15 16:20:09130 // Stores |new_policies| into |effective_policies_|. Returns dictionary with
131 // items from |new_policies| that are different from the old
132 // |effective_policies_|.
Morten Stenshorne4654b9c2022-08-31 14:12:50133 base::Value::Dict StoreNewAndReturnChangedPolicies(
134 base::Value::Dict new_policies);
lukasza0d40d8a2015-03-03 18:36:28135
lukasza0fdd9512014-11-25 17:47:46136 // Signals policy error to the registered |PolicyErrorCallback|.
137 void SignalPolicyError();
138
lukaszaa9376cd2015-01-28 20:29:01139 // |policy_service_task_runner| is the task runner where it is safe
140 // to call |policy_service_| methods and where we expect to get callbacks
141 // from |policy_service_|.
dcheng0765c492016-04-06 22:41:53142 PolicyWatcher(policy::PolicyService* policy_service,
143 std::unique_ptr<policy::PolicyService> owned_policy_service,
144 std::unique_ptr<policy::ConfigurationPolicyProvider>
145 owned_policy_provider,
146 std::unique_ptr<policy::SchemaRegistry> owned_schema_registry);
lukasza56dd1e92015-01-24 02:09:19147
lukaszaa9376cd2015-01-28 20:29:01148 // Creates PolicyWatcher that wraps the owned |async_policy_loader| with an
149 // appropriate PolicySchema.
150 //
151 // |policy_service_task_runner| is passed through to the constructor of
152 // PolicyWatcher.
dcheng0765c492016-04-06 22:41:53153 static std::unique_ptr<PolicyWatcher> CreateFromPolicyLoader(
154 std::unique_ptr<policy::AsyncPolicyLoader> async_policy_loader);
simonmorris@chromium.orgb9612a52012-07-28 02:17:48155
lukaszaa9376cd2015-01-28 20:29:01156 // PolicyService::Observer interface.
157 void OnPolicyUpdated(const policy::PolicyNamespace& ns,
158 const policy::PolicyMap& previous,
159 const policy::PolicyMap& current) override;
160 void OnPolicyServiceInitialized(policy::PolicyDomain domain) override;
161
Xiaohan Wang453b38672022-01-13 20:02:44162#if BUILDFLAG(IS_WIN)
Joe Downing21478b5d2021-10-25 16:50:22163 void WatchForRegistryChanges();
164#endif
165
lukasza0fdd9512014-11-25 17:47:46166 PolicyUpdatedCallback policy_updated_callback_;
167 PolicyErrorCallback policy_error_callback_;
simonmorris@chromium.org000d1f62012-07-24 01:56:54168
Joe Downingdcd2bb52020-12-15 16:20:09169 // The combined set of policies (|platform_policies_| + |default_values_|)
170 // which define the effective policy set.
Morten Stenshorne4654b9c2022-08-31 14:12:50171 base::Value::Dict effective_policies_;
Joe Downingdcd2bb52020-12-15 16:20:09172
173 // The policies which have had their values explicitly set via a policy entry.
Morten Stenshorne4654b9c2022-08-31 14:12:50174 base::Value::Dict platform_policies_;
Joe Downingdcd2bb52020-12-15 16:20:09175
176 // The set of policy values to use if a policy has not been explicitly set.
Morten Stenshorne4654b9c2022-08-31 14:12:50177 base::Value::Dict default_values_;
simonmorris@chromium.org000d1f62012-07-24 01:56:54178
lukaszaa9376cd2015-01-28 20:29:01179 // Order of fields below is important to ensure destruction takes object
180 // dependencies into account:
Tom Sepez61a1e8312023-04-06 23:26:30181 // - |policy_service_| may be |owned_policy_service_|.
lukaszaa9376cd2015-01-28 20:29:01182 // - |owned_policy_service_| uses |owned_policy_provider_|
183 // - |owned_policy_provider_| uses |owned_schema_registry_|
dcheng0765c492016-04-06 22:41:53184 std::unique_ptr<policy::SchemaRegistry> owned_schema_registry_;
185 std::unique_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider_;
186 std::unique_ptr<policy::PolicyService> owned_policy_service_;
Tom Sepez61a1e8312023-04-06 23:26:30187 raw_ptr<policy::PolicyService> policy_service_;
lukaszaa9376cd2015-01-28 20:29:01188
Xiaohan Wang453b38672022-01-13 20:02:44189#if BUILDFLAG(IS_WIN)
Joe Downing21478b5d2021-10-25 16:50:22190 // |policy_key_| relies on |policy_service_| to notify the host of policy
191 // changes. Make sure |policy_key_| is destroyed to prevent any notifications
192 // from firing while the above objects are being torn down.
193 base::win::RegKey policy_key_;
194#endif
195
gabbf77513a2017-06-01 14:35:34196 SEQUENCE_CHECKER(sequence_checker_);
simonmorris@chromium.org000d1f62012-07-24 01:56:54197};
198
simonmorris@chromium.org000d1f62012-07-24 01:56:54199} // namespace remoting
200
lukaszac285c9a2015-01-29 23:18:28201#endif // REMOTING_HOST_POLICY_WATCHER_H_