OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef REMOTING_HOST_POLICY_HACK_POLICY_WATCHER_H_ | 5 #ifndef REMOTING_HOST_POLICY_HACK_POLICY_WATCHER_H_ |
6 #define REMOTING_HOST_POLICY_HACK_POLICY_WATCHER_H_ | 6 #define REMOTING_HOST_POLICY_HACK_POLICY_WATCHER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/threading/non_thread_safe.h" | |
12 #include "components/policy/core/common/policy_service.h" | 13 #include "components/policy/core/common/policy_service.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 class DictionaryValue; | 16 class DictionaryValue; |
16 class SingleThreadTaskRunner; | 17 class SingleThreadTaskRunner; |
17 } // namespace base | 18 } // namespace base |
18 | 19 |
19 namespace policy { | 20 namespace policy { |
20 class AsyncPolicyLoader; | 21 class AsyncPolicyLoader; |
21 class ConfigurationPolicyProvider; | 22 class ConfigurationPolicyProvider; |
22 class SchemaRegistry; | 23 class SchemaRegistry; |
23 } // namespace policy | 24 } // namespace policy |
24 | 25 |
25 namespace remoting { | 26 namespace remoting { |
26 namespace policy_hack { | 27 namespace policy_hack { |
27 | 28 |
28 // Watches for changes to the managed remote access host policies. If | 29 // Watches for changes to the managed remote access host policies. |
29 // StartWatching() has been called, then before this object can be deleted, | 30 class PolicyWatcher : public policy::PolicyService::Observer, |
30 // StopWatching() has to be completed (the provided |done| event must be | 31 public base::NonThreadSafe { |
31 // signaled). | |
32 class PolicyWatcher : public policy::PolicyService::Observer { | |
33 public: | 32 public: |
34 // Called first with all policies, and subsequently with any changed policies. | 33 // Called first with all policies, and subsequently with any changed policies. |
35 typedef base::Callback<void(scoped_ptr<base::DictionaryValue>)> | 34 typedef base::Callback<void(scoped_ptr<base::DictionaryValue>)> |
36 PolicyUpdatedCallback; | 35 PolicyUpdatedCallback; |
37 | 36 |
38 // TODO(lukasza): PolicyErrorCallback never gets called by PolicyWatcher. | 37 // TODO(lukasza): PolicyErrorCallback never gets called by PolicyWatcher. |
39 // Need to either 1) remove error-handling from PolicyWatcher or 2) add | 38 // Need to either 1) remove error-handling from PolicyWatcher or 2) add |
40 // error-handling around PolicyService 2a) Add policy name/type validation via | 39 // error-handling around PolicyService 2a) Add policy name/type validation via |
41 // policy::Schema::Normalize. 2b) Consider exposing parsing errors from | 40 // policy::Schema::Normalize. 2b) Consider exposing parsing errors from |
42 // policy::ConfigDirPolicyLoader. | 41 // policy::ConfigDirPolicyLoader. |
(...skipping 11 matching lines...) Expand all Loading... | |
54 // |policy_error_callback| will be called when malformed policies are detected | 53 // |policy_error_callback| will be called when malformed policies are detected |
55 // (i.e. wrong type of policy value, or unparseable files under | 54 // (i.e. wrong type of policy value, or unparseable files under |
56 // /etc/opt/chrome/policies/managed). | 55 // /etc/opt/chrome/policies/managed). |
57 // When called, the |policy_error_callback| is responsible for mitigating the | 56 // When called, the |policy_error_callback| is responsible for mitigating the |
58 // security risk of running with incorrectly formulated policies (by either | 57 // security risk of running with incorrectly formulated policies (by either |
59 // shutting down or locking down the host). | 58 // shutting down or locking down the host). |
60 // After calling |policy_error_callback| PolicyWatcher will continue watching | 59 // After calling |policy_error_callback| PolicyWatcher will continue watching |
61 // for policy changes and will call |policy_updated_callback| when the error | 60 // for policy changes and will call |policy_updated_callback| when the error |
62 // is recovered from and may call |policy_error_callback| when new errors are | 61 // is recovered from and may call |policy_error_callback| when new errors are |
63 // found. | 62 // found. |
64 // | |
65 // See |Create| method's description for comments about which thread will | |
66 // be used to run the callbacks. | |
67 virtual void StartWatching( | 63 virtual void StartWatching( |
68 const PolicyUpdatedCallback& policy_updated_callback, | 64 const PolicyUpdatedCallback& policy_updated_callback, |
69 const PolicyErrorCallback& policy_error_callback); | 65 const PolicyErrorCallback& policy_error_callback); |
70 | 66 |
71 // Should be called after StartWatching() before the object is deleted. Calls | |
72 // should wait for |stopped_callback| to be called before deleting it. | |
73 virtual void StopWatching(const base::Closure& stopped_callback); | |
74 | |
75 // Specify a |policy_service| to borrow (on Chrome OS, from the browser | 67 // Specify a |policy_service| to borrow (on Chrome OS, from the browser |
76 // process) or specify nullptr to internally construct and use a new | 68 // process) or specify nullptr to internally construct and use a new |
77 // PolicyService (on other OS-es). | 69 // PolicyService (on other OS-es). PolicyWatcher must be used on th thread on |
Łukasz Anforowicz
2015/01/30 19:50:59
nit: typo ("th thread")
Sergey Ulanov
2015/01/30 20:06:07
Done.
| |
70 // which it is created. |policy_service| is called on the same thread. | |
78 // | 71 // |
79 // When |policy_service| is null, then |task_runner| is used for reading the | 72 // When |policy_service| is null, then |file_task_runner| is used for reading |
80 // policy from files / registry / preferences. PolicyUpdatedCallback and | 73 // the policy from files / registry / preferences (which are blocking |
81 // PolicyErrorCallback will be called on the same |task_runner|. | 74 // operations). |file_task_runner| should be of TYPE_IO type. |
82 // |task_runner| should be of TYPE_IO type. | |
83 // | 75 // |
84 // When |policy_service| is specified then |task_runner| argument is ignored | 76 // When |policy_service| is specified then |file_task_runner| argument is |
85 // and 1) BrowserThread::UI is used for PolicyUpdatedCallback and | 77 // ignored and 1) BrowserThread::UI is used for PolicyUpdatedCallback and |
86 // PolicyErrorCallback and 2) BrowserThread::FILE is used for reading the | 78 // PolicyErrorCallback and 2) BrowserThread::FILE is used for reading the |
87 // policy from files / registry / preferences (although (2) is just an | 79 // policy from files / registry / preferences (although (2) is just an |
88 // implementation detail and should likely be ignored outside of | 80 // implementation detail and should likely be ignored outside of |
89 // PolicyWatcher). | 81 // PolicyWatcher). |
90 static scoped_ptr<PolicyWatcher> Create( | 82 static scoped_ptr<PolicyWatcher> Create( |
91 policy::PolicyService* policy_service, | 83 policy::PolicyService* policy_service, |
92 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); | 84 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner); |
93 | 85 |
94 private: | 86 private: |
95 friend class PolicyWatcherTest; | 87 friend class PolicyWatcherTest; |
96 | 88 |
97 // Used to check if the class is on the right thread. | 89 // Used to check if the class is on the right thread. |
98 bool OnPolicyServiceThread() const; | 90 bool OnPolicyServiceThread() const; |
Łukasz Anforowicz
2015/01/30 19:50:59
OnPolicyServiceThread method can probably go away,
Sergey Ulanov
2015/01/30 20:06:07
Done.
| |
99 | 91 |
100 // Takes the policy dictionary from the OS specific store and extracts the | 92 // Takes the policy dictionary from the OS specific store and extracts the |
101 // relevant policies. | 93 // relevant policies. |
102 void UpdatePolicies(const base::DictionaryValue* new_policy); | 94 void UpdatePolicies(const base::DictionaryValue* new_policy); |
103 | 95 |
104 // Signals policy error to the registered |PolicyErrorCallback|. | 96 // Signals policy error to the registered |PolicyErrorCallback|. |
105 void SignalPolicyError(); | 97 void SignalPolicyError(); |
106 | 98 |
107 // Called whenever a transient error occurs during reading of policy files. | 99 // Called whenever a transient error occurs during reading of policy files. |
108 // This will increment a counter, and will trigger a call to | 100 // This will increment a counter, and will trigger a call to |
109 // SignalPolicyError() only after a threshold count is reached. | 101 // SignalPolicyError() only after a threshold count is reached. |
110 // The counter is reset whenever policy has been successfully read. | 102 // The counter is reset whenever policy has been successfully read. |
111 void SignalTransientPolicyError(); | 103 void SignalTransientPolicyError(); |
112 | 104 |
113 // |policy_service_task_runner| is the task runner where it is safe | 105 // |policy_service_task_runner| is the task runner where it is safe |
114 // to call |policy_service_| methods and where we expect to get callbacks | 106 // to call |policy_service_| methods and where we expect to get callbacks |
115 // from |policy_service_|. | 107 // from |policy_service_|. |
116 PolicyWatcher( | 108 PolicyWatcher( |
117 const scoped_refptr<base::SingleThreadTaskRunner>& | |
118 policy_service_task_runner, | |
119 policy::PolicyService* policy_service, | 109 policy::PolicyService* policy_service, |
120 scoped_ptr<policy::PolicyService> owned_policy_service, | 110 scoped_ptr<policy::PolicyService> owned_policy_service, |
121 scoped_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider, | 111 scoped_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider, |
122 scoped_ptr<policy::SchemaRegistry> owned_schema_registry); | 112 scoped_ptr<policy::SchemaRegistry> owned_schema_registry); |
123 | 113 |
124 // Creates PolicyWatcher that wraps the owned |async_policy_loader| with an | 114 // Creates PolicyWatcher that wraps the owned |async_policy_loader| with an |
125 // appropriate PolicySchema. | 115 // appropriate PolicySchema. |
126 // | 116 // |
127 // |policy_service_task_runner| is passed through to the constructor of | 117 // |policy_service_task_runner| is passed through to the constructor of |
128 // PolicyWatcher. | 118 // PolicyWatcher. |
129 static scoped_ptr<PolicyWatcher> CreateFromPolicyLoader( | 119 static scoped_ptr<PolicyWatcher> CreateFromPolicyLoader( |
130 const scoped_refptr<base::SingleThreadTaskRunner>& | |
131 policy_service_task_runner, | |
132 scoped_ptr<policy::AsyncPolicyLoader> async_policy_loader); | 120 scoped_ptr<policy::AsyncPolicyLoader> async_policy_loader); |
133 | 121 |
134 // PolicyService::Observer interface. | 122 // PolicyService::Observer interface. |
135 void OnPolicyUpdated(const policy::PolicyNamespace& ns, | 123 void OnPolicyUpdated(const policy::PolicyNamespace& ns, |
136 const policy::PolicyMap& previous, | 124 const policy::PolicyMap& previous, |
137 const policy::PolicyMap& current) override; | 125 const policy::PolicyMap& current) override; |
138 void OnPolicyServiceInitialized(policy::PolicyDomain domain) override; | 126 void OnPolicyServiceInitialized(policy::PolicyDomain domain) override; |
139 | 127 |
140 void StopWatchingOnPolicyServiceThread(); | |
141 | |
142 scoped_refptr<base::SingleThreadTaskRunner> policy_service_task_runner_; | |
143 | |
144 PolicyUpdatedCallback policy_updated_callback_; | 128 PolicyUpdatedCallback policy_updated_callback_; |
145 PolicyErrorCallback policy_error_callback_; | 129 PolicyErrorCallback policy_error_callback_; |
146 int transient_policy_error_retry_counter_; | 130 int transient_policy_error_retry_counter_; |
147 | 131 |
148 scoped_ptr<base::DictionaryValue> old_policies_; | 132 scoped_ptr<base::DictionaryValue> old_policies_; |
149 scoped_ptr<base::DictionaryValue> default_values_; | 133 scoped_ptr<base::DictionaryValue> default_values_; |
150 | 134 |
151 // TODO(lukasza): Remove - components/policy filters out mistyped values. | 135 // TODO(lukasza): Remove - components/policy filters out mistyped values. |
152 scoped_ptr<base::DictionaryValue> bad_type_values_; | 136 scoped_ptr<base::DictionaryValue> bad_type_values_; |
153 | 137 |
154 policy::PolicyService* policy_service_; | 138 policy::PolicyService* policy_service_; |
155 | 139 |
156 // Order of fields below is important to ensure destruction takes object | 140 // Order of fields below is important to ensure destruction takes object |
157 // dependencies into account: | 141 // dependencies into account: |
158 // - |owned_policy_service_| uses |owned_policy_provider_| | 142 // - |owned_policy_service_| uses |owned_policy_provider_| |
159 // - |owned_policy_provider_| uses |owned_schema_registry_| | 143 // - |owned_policy_provider_| uses |owned_schema_registry_| |
160 scoped_ptr<policy::SchemaRegistry> owned_schema_registry_; | 144 scoped_ptr<policy::SchemaRegistry> owned_schema_registry_; |
161 scoped_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider_; | 145 scoped_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider_; |
162 scoped_ptr<policy::PolicyService> owned_policy_service_; | 146 scoped_ptr<policy::PolicyService> owned_policy_service_; |
163 | 147 |
164 DISALLOW_COPY_AND_ASSIGN(PolicyWatcher); | 148 DISALLOW_COPY_AND_ASSIGN(PolicyWatcher); |
165 }; | 149 }; |
166 | 150 |
167 } // namespace policy_hack | 151 } // namespace policy_hack |
168 } // namespace remoting | 152 } // namespace remoting |
169 | 153 |
170 #endif // REMOTING_HOST_POLICY_HACK_POLICY_WATCHER_H_ | 154 #endif // REMOTING_HOST_POLICY_HACK_POLICY_WATCHER_H_ |
OLD | NEW |