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/memory/weak_ptr.h" | 9 #include "base/macros.h" |
10 #include "base/values.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "components/policy/core/common/policy_service.h" |
11 | 13 |
12 namespace base { | 14 namespace base { |
| 15 class DictionaryValue; |
13 class SingleThreadTaskRunner; | 16 class SingleThreadTaskRunner; |
14 class TimeDelta; | |
15 class WaitableEvent; | |
16 } // namespace base | 17 } // namespace base |
17 | 18 |
18 namespace policy { | 19 namespace policy { |
19 class PolicyService; | 20 class AsyncPolicyLoader; |
| 21 class ConfigurationPolicyProvider; |
| 22 class SchemaRegistry; |
20 } // namespace policy | 23 } // namespace policy |
21 | 24 |
22 namespace remoting { | 25 namespace remoting { |
23 namespace policy_hack { | 26 namespace policy_hack { |
24 | 27 |
25 // Watches for changes to the managed remote access host policies. | 28 // Watches for changes to the managed remote access host policies. If |
26 // If StartWatching() has been called, then before this object can be deleted, | 29 // StartWatching() has been called, then before this object can be deleted, |
27 // StopWatching() have completed (the provided |done| event must be signaled). | 30 // StopWatching() has to be completed (the provided |done| event must be |
28 class PolicyWatcher { | 31 // signaled). |
| 32 class PolicyWatcher : public policy::PolicyService::Observer { |
29 public: | 33 public: |
30 // Called first with all policies, and subsequently with any changed policies. | 34 // Called first with all policies, and subsequently with any changed policies. |
31 typedef base::Callback<void(scoped_ptr<base::DictionaryValue>)> | 35 typedef base::Callback<void(scoped_ptr<base::DictionaryValue>)> |
32 PolicyUpdatedCallback; | 36 PolicyUpdatedCallback; |
33 | 37 |
34 // TODO(lukasza): PolicyErrorCallback never gets called by | 38 // TODO(lukasza): PolicyErrorCallback never gets called by PolicyWatcher. |
35 // PolicyServiceWatcher. Need to either 1) remove error-handling from | 39 // Need to either 1) remove error-handling from PolicyWatcher or 2) add |
36 // PolicyWatcher or 2) add error-handling around PolicyService | 40 // error-handling around PolicyService 2a) Add policy name/type validation via |
37 // 2a) Add policy name/type validation via policy::Schema::Normalize. | 41 // policy::Schema::Normalize. 2b) Consider exposing parsing errors from |
38 // 2b) Consider exposing parsing errors from policy::ConfigDirPolicyLoader. | 42 // policy::ConfigDirPolicyLoader. |
39 | 43 |
40 // Called after detecting malformed policies. | 44 // Called after detecting malformed policies. |
41 typedef base::Callback<void()> PolicyErrorCallback; | 45 typedef base::Callback<void()> PolicyErrorCallback; |
42 | 46 |
43 // Derived classes specify which |task_runner| should be used for calling | 47 ~PolicyWatcher() override; |
44 // their StartWatchingInternal and StopWatchingInternal methods. | |
45 // Derived classes promise back to call UpdatePolicies and other instance | |
46 // methods on the same |task_runner|. | |
47 explicit PolicyWatcher( | |
48 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); | |
49 | |
50 virtual ~PolicyWatcher(); | |
51 | 48 |
52 // This guarantees that the |policy_updated_callback| is called at least once | 49 // This guarantees that the |policy_updated_callback| is called at least once |
53 // with the current policies. After that, |policy_updated_callback| will be | 50 // with the current policies. After that, |policy_updated_callback| will be |
54 // called whenever a change to any policy is detected. It will then be called | 51 // called whenever a change to any policy is detected. It will then be called |
55 // only with the changed policies. | 52 // only with the changed policies. |
56 // | 53 // |
57 // |policy_error_callback| will be called when malformed policies are detected | 54 // |policy_error_callback| will be called when malformed policies are detected |
58 // (i.e. wrong type of policy value, or unparseable files under | 55 // (i.e. wrong type of policy value, or unparseable files under |
59 // /etc/opt/chrome/policies/managed). | 56 // /etc/opt/chrome/policies/managed). |
60 // When called, the |policy_error_callback| is responsible for mitigating the | 57 // When called, the |policy_error_callback| is responsible for mitigating the |
(...skipping 26 matching lines...) Expand all Loading... |
87 // When |policy_service| is specified then |task_runner| argument is ignored | 84 // When |policy_service| is specified then |task_runner| argument is ignored |
88 // and 1) BrowserThread::UI is used for PolicyUpdatedCallback and | 85 // and 1) BrowserThread::UI is used for PolicyUpdatedCallback and |
89 // PolicyErrorCallback and 2) BrowserThread::FILE is used for reading the | 86 // PolicyErrorCallback and 2) BrowserThread::FILE is used for reading the |
90 // policy from files / registry / preferences (although (2) is just an | 87 // policy from files / registry / preferences (although (2) is just an |
91 // implementation detail and should likely be ignored outside of | 88 // implementation detail and should likely be ignored outside of |
92 // PolicyWatcher). | 89 // PolicyWatcher). |
93 static scoped_ptr<PolicyWatcher> Create( | 90 static scoped_ptr<PolicyWatcher> Create( |
94 policy::PolicyService* policy_service, | 91 policy::PolicyService* policy_service, |
95 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); | 92 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); |
96 | 93 |
97 protected: | 94 private: |
98 virtual void StartWatchingInternal() = 0; | 95 friend class PolicyWatcherTest; |
99 virtual void StopWatchingInternal() = 0; | |
100 | 96 |
101 // Used to check if the class is on the right thread. | 97 // Used to check if the class is on the right thread. |
102 bool OnPolicyWatcherThread() const; | 98 bool OnPolicyServiceThread() const; |
103 | 99 |
104 // Takes the policy dictionary from the OS specific store and extracts the | 100 // Takes the policy dictionary from the OS specific store and extracts the |
105 // relevant policies. | 101 // relevant policies. |
106 void UpdatePolicies(const base::DictionaryValue* new_policy); | 102 void UpdatePolicies(const base::DictionaryValue* new_policy); |
107 | 103 |
108 // Signals policy error to the registered |PolicyErrorCallback|. | 104 // Signals policy error to the registered |PolicyErrorCallback|. |
109 void SignalPolicyError(); | 105 void SignalPolicyError(); |
110 | 106 |
111 // Called whenever a transient error occurs during reading of policy files. | 107 // Called whenever a transient error occurs during reading of policy files. |
112 // This will increment a counter, and will trigger a call to | 108 // This will increment a counter, and will trigger a call to |
113 // SignalPolicyError() only after a threshold count is reached. | 109 // SignalPolicyError() only after a threshold count is reached. |
114 // The counter is reset whenever policy has been successfully read. | 110 // The counter is reset whenever policy has been successfully read. |
115 void SignalTransientPolicyError(); | 111 void SignalTransientPolicyError(); |
116 | 112 |
117 friend class PolicyWatcherTest; | 113 // |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 |
| 115 // from |policy_service_|. |
| 116 PolicyWatcher( |
| 117 const scoped_refptr<base::SingleThreadTaskRunner>& |
| 118 policy_service_task_runner, |
| 119 policy::PolicyService* policy_service, |
| 120 scoped_ptr<policy::PolicyService> owned_policy_service, |
| 121 scoped_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider, |
| 122 scoped_ptr<policy::SchemaRegistry> owned_schema_registry); |
118 | 123 |
119 // Returns a DictionaryValue containing the default values for each policy. | 124 // Creates PolicyWatcher that wraps the owned |async_policy_loader| with an |
120 const base::DictionaryValue& Defaults() const; | 125 // appropriate PolicySchema. |
| 126 // |
| 127 // |policy_service_task_runner| is passed through to the constructor of |
| 128 // PolicyWatcher. |
| 129 static scoped_ptr<PolicyWatcher> CreateFromPolicyLoader( |
| 130 const scoped_refptr<base::SingleThreadTaskRunner>& |
| 131 policy_service_task_runner, |
| 132 scoped_ptr<policy::AsyncPolicyLoader> async_policy_loader); |
121 | 133 |
122 private: | 134 // PolicyService::Observer interface. |
123 void StopWatchingOnPolicyWatcherThread(); | 135 void OnPolicyUpdated(const policy::PolicyNamespace& ns, |
124 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 136 const policy::PolicyMap& previous, |
| 137 const policy::PolicyMap& current) override; |
| 138 void OnPolicyServiceInitialized(policy::PolicyDomain domain) override; |
| 139 |
| 140 void StopWatchingOnPolicyServiceThread(); |
| 141 |
| 142 scoped_refptr<base::SingleThreadTaskRunner> policy_service_task_runner_; |
125 | 143 |
126 PolicyUpdatedCallback policy_updated_callback_; | 144 PolicyUpdatedCallback policy_updated_callback_; |
127 PolicyErrorCallback policy_error_callback_; | 145 PolicyErrorCallback policy_error_callback_; |
128 int transient_policy_error_retry_counter_; | 146 int transient_policy_error_retry_counter_; |
129 | 147 |
130 scoped_ptr<base::DictionaryValue> old_policies_; | 148 scoped_ptr<base::DictionaryValue> old_policies_; |
131 scoped_ptr<base::DictionaryValue> default_values_; | 149 scoped_ptr<base::DictionaryValue> default_values_; |
| 150 |
| 151 // TODO(lukasza): Remove - components/policy filters out mistyped values. |
132 scoped_ptr<base::DictionaryValue> bad_type_values_; | 152 scoped_ptr<base::DictionaryValue> bad_type_values_; |
133 | 153 |
134 // Allows us to cancel any inflight FileWatcher events or scheduled reloads. | 154 policy::PolicyService* policy_service_; |
135 base::WeakPtrFactory<PolicyWatcher> weak_factory_; | 155 |
| 156 // Order of fields below is important to ensure destruction takes object |
| 157 // dependencies into account: |
| 158 // - |owned_policy_service_| uses |owned_policy_provider_| |
| 159 // - |owned_policy_provider_| uses |owned_schema_registry_| |
| 160 scoped_ptr<policy::SchemaRegistry> owned_schema_registry_; |
| 161 scoped_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider_; |
| 162 scoped_ptr<policy::PolicyService> owned_policy_service_; |
| 163 |
| 164 DISALLOW_COPY_AND_ASSIGN(PolicyWatcher); |
136 }; | 165 }; |
137 | 166 |
138 } // namespace policy_hack | 167 } // namespace policy_hack |
139 } // namespace remoting | 168 } // namespace remoting |
140 | 169 |
141 #endif // REMOTING_HOST_POLICY_HACK_POLICY_WATCHER_H_ | 170 #endif // REMOTING_HOST_POLICY_HACK_POLICY_WATCHER_H_ |
OLD | NEW |