Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: remoting/host/policy_hack/policy_watcher.h

Issue 867463006: Merged PolicyServiceWatcher into PolicyWatcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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; 96
97 // TODO(lukasza): s/Internal/OnWatcherThread/g
Sergey Ulanov 2015/01/26 18:20:01 maybe rename them in this CL?
Łukasz Anforowicz 2015/01/26 19:24:20 Done. Initially I wanted to just make this change
98 virtual void StartWatchingInternal();
99 virtual void StopWatchingInternal();
100 100
101 // Used to check if the class is on the right thread. 101 // Used to check if the class is on the right thread.
102 bool OnPolicyWatcherThread() const; 102 bool OnPolicyWatcherThread() const;
103 103
104 // Takes the policy dictionary from the OS specific store and extracts the 104 // Takes the policy dictionary from the OS specific store and extracts the
105 // relevant policies. 105 // relevant policies.
106 void UpdatePolicies(const base::DictionaryValue* new_policy); 106 void UpdatePolicies(const base::DictionaryValue* new_policy);
107 107
108 // Signals policy error to the registered |PolicyErrorCallback|. 108 // Signals policy error to the registered |PolicyErrorCallback|.
109 void SignalPolicyError(); 109 void SignalPolicyError();
110 110
111 // Called whenever a transient error occurs during reading of policy files. 111 // Called whenever a transient error occurs during reading of policy files.
112 // This will increment a counter, and will trigger a call to 112 // This will increment a counter, and will trigger a call to
113 // SignalPolicyError() only after a threshold count is reached. 113 // SignalPolicyError() only after a threshold count is reached.
114 // The counter is reset whenever policy has been successfully read. 114 // The counter is reset whenever policy has been successfully read.
115 void SignalTransientPolicyError(); 115 void SignalTransientPolicyError();
116 116
117 friend class PolicyWatcherTest; 117 // |policy_service_task_runner| is the task runner where it is safe
118 // to call |policy_service_| methods and where we expect to get callbacks
119 // from |policy_service_|.
120 explicit PolicyWatcher(const scoped_refptr<base::SingleThreadTaskRunner>&
121 policy_service_task_runner);
118 122
119 // Returns a DictionaryValue containing the default values for each policy. 123 // Constructor for the case when |policy_service| is borrowed.
120 const base::DictionaryValue& Defaults() const; 124 PolicyWatcher(const scoped_refptr<base::SingleThreadTaskRunner>&
125 policy_service_task_runner,
126 policy::PolicyService* policy_service);
121 127
122 private: 128 // Constructor for the case when |policy_service| is owned (and uses also
129 // owned |owned_policy_provider| and |owned_schema_registry|.
130 PolicyWatcher(
131 const scoped_refptr<base::SingleThreadTaskRunner>&
132 policy_service_task_runner,
133 scoped_ptr<policy::PolicyService> owned_policy_service,
134 scoped_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider,
135 scoped_ptr<policy::SchemaRegistry> owned_schema_registry);
136
137 // Creates PolicyWatcher that wraps the owned |async_policy_loader| with an
138 // appropriate PolicySchema.
139 //
140 // |policy_service_task_runner| is passed through to the constructor of
141 // PolicyWatcher.
142 static scoped_ptr<PolicyWatcher> CreateFromPolicyLoader(
143 const scoped_refptr<base::SingleThreadTaskRunner>&
144 policy_service_task_runner,
145 scoped_ptr<policy::AsyncPolicyLoader> async_policy_loader);
146
147 // PolicyService::Observer interface.
148 void OnPolicyUpdated(const policy::PolicyNamespace& ns,
149 const policy::PolicyMap& previous,
150 const policy::PolicyMap& current) override;
151 void OnPolicyServiceInitialized(policy::PolicyDomain domain) override;
152
153 // TODO(lukasza): Remove and merge with StopWatchingInternal.
123 void StopWatchingOnPolicyWatcherThread(); 154 void StopWatchingOnPolicyWatcherThread();
124 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 155
156 scoped_refptr<base::SingleThreadTaskRunner> policy_service_task_runner_;
125 157
126 PolicyUpdatedCallback policy_updated_callback_; 158 PolicyUpdatedCallback policy_updated_callback_;
127 PolicyErrorCallback policy_error_callback_; 159 PolicyErrorCallback policy_error_callback_;
128 int transient_policy_error_retry_counter_; 160 int transient_policy_error_retry_counter_;
129 161
130 scoped_ptr<base::DictionaryValue> old_policies_; 162 scoped_ptr<base::DictionaryValue> old_policies_;
131 scoped_ptr<base::DictionaryValue> default_values_; 163 scoped_ptr<base::DictionaryValue> default_values_;
164
165 // TODO(lukasza): Remove - components/policy filters out mistyped values.
132 scoped_ptr<base::DictionaryValue> bad_type_values_; 166 scoped_ptr<base::DictionaryValue> bad_type_values_;
133 167
134 // Allows us to cancel any inflight FileWatcher events or scheduled reloads. 168 policy::PolicyService* policy_service_;
135 base::WeakPtrFactory<PolicyWatcher> weak_factory_; 169
170 // Order of fields below is important to ensure destruction takes object
171 // dependencies into account:
172 // - |owned_policy_service_| uses |owned_policy_provider_|
173 // - |owned_policy_provider_| uses |owned_schema_registry_|
174 scoped_ptr<policy::SchemaRegistry> owned_schema_registry_;
175 scoped_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider_;
176 scoped_ptr<policy::PolicyService> owned_policy_service_;
177
178 DISALLOW_COPY_AND_ASSIGN(PolicyWatcher);
136 }; 179 };
137 180
138 } // namespace policy_hack 181 } // namespace policy_hack
139 } // namespace remoting 182 } // namespace remoting
140 183
141 #endif // REMOTING_HOST_POLICY_HACK_POLICY_WATCHER_H_ 184 #endif // REMOTING_HOST_POLICY_HACK_POLICY_WATCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698