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