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

Side by Side Diff: chrome/browser/policy/file_based_policy_provider.h

Issue 5562002: Refactor FileBasedPolicyProvider, introduce AsynchronousPolicyProvider. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge with TOT Created 10 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 CHROME_BROWSER_POLICY_FILE_BASED_POLICY_PROVIDER_H_ 5 #ifndef CHROME_BROWSER_POLICY_FILE_BASED_POLICY_PROVIDER_H_
6 #define CHROME_BROWSER_POLICY_FILE_BASED_POLICY_PROVIDER_H_ 6 #define CHROME_BROWSER_POLICY_FILE_BASED_POLICY_PROVIDER_H_
7 #pragma once 7 #pragma once
8 8
9 #include "base/basictypes.h"
10 #include "base/file_path.h" 9 #include "base/file_path.h"
11 #include "base/lock.h"
12 #include "base/ref_counted.h"
13 #include "base/scoped_ptr.h"
14 #include "base/time.h" 10 #include "base/time.h"
15 #include "base/weak_ptr.h" 11 #include "chrome/browser/policy/asynchronous_policy_provider.h"
16 #include "chrome/browser/file_path_watcher/file_path_watcher.h"
17 #include "chrome/browser/policy/configuration_policy_provider.h"
18
19 class CancelableTask;
20 class DictionaryValue;
21 class MessageLoop;
22 12
23 namespace policy { 13 namespace policy {
24 14
25 class FileBasedPolicyLoader;
26 class FileBasedPolicyWatcher;
27
28 // File based policy provider that coordinates watching and reloading policy 15 // File based policy provider that coordinates watching and reloading policy
29 // information from the configuration path. Actual logic for loading policy 16 // information from the configuration path. Actual logic for loading policy
30 // information is handled by a delegate passed at construction time. 17 // information is handled by a delegate passed at construction time.
31 class FileBasedPolicyProvider 18 class FileBasedPolicyProvider : public AsynchronousPolicyProvider {
32 : public ConfigurationPolicyProvider,
33 public base::SupportsWeakPtr<FileBasedPolicyProvider> {
34 public: 19 public:
20
35 // Delegate interface for actual policy loading from the system. 21 // Delegate interface for actual policy loading from the system.
36 class Delegate { 22 class ProviderDelegate : public AsynchronousPolicyProvider::Delegate {
37 public: 23 public:
38 virtual ~Delegate(); 24 explicit ProviderDelegate(const FilePath& config_file_path);
25 virtual ~ProviderDelegate();
39 26
40 // Loads the policy information. Ownership of the return value is 27 // AsynchronousPolicyProvider::Delegate implementation:
41 // transferred to the caller.
42 virtual DictionaryValue* Load() = 0; 28 virtual DictionaryValue* Load() = 0;
43 29
44 // Gets the last modification timestamp for the policy information from the 30 // Gets the last modification timestamp for the policy information from the
45 // filesystem. Returns base::Time() if the information is not present, in 31 // filesystem. Returns base::Time() if the information is not present, in
46 // which case Load() should return an empty dictionary. 32 // which case Load() should return an empty dictionary.
47 virtual base::Time GetLastModification() = 0; 33 virtual base::Time GetLastModification() = 0;
48 34
49 const FilePath& config_file_path() { return config_file_path_; } 35 const FilePath& config_file_path() { return config_file_path_; }
50 36
51 protected:
52 explicit Delegate(const FilePath& config_file_path);
53
54 private: 37 private:
55 // The path at which we look for configuration files.
56 const FilePath config_file_path_; 38 const FilePath config_file_path_;
57 39
58 DISALLOW_COPY_AND_ASSIGN(Delegate); 40 DISALLOW_COPY_AND_ASSIGN(ProviderDelegate);
59 }; 41 };
60 42
61 // Assumes ownership of |delegate|. 43 // Assumes ownership of |delegate|.
62 FileBasedPolicyProvider(const PolicyDefinitionList* policy_list, 44 FileBasedPolicyProvider(const PolicyDefinitionList* policy_list,
63 Delegate* delegate); 45 ProviderDelegate* delegate);
64 virtual ~FileBasedPolicyProvider(); 46 virtual ~FileBasedPolicyProvider() {}
65
66 // ConfigurationPolicyProvider implementation.
67 virtual bool Provide(ConfigurationPolicyStoreInterface* store);
68 47
69 private: 48 private:
70 // Watches for changes to the configuration directory.
71 scoped_refptr<FileBasedPolicyWatcher> watcher_;
72
73 // The loader object we use internally.
74 scoped_refptr<FileBasedPolicyLoader> loader_;
75
76 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyProvider); 49 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyProvider);
77 }; 50 };
78 51
79 // FilePathWatcher delegate implementation that handles change notifications for
80 // the configuration file or directory. It keeps the authorative version of the
81 // currently effective policy dictionary and updates it as appropriate. The
82 // actual loading logic is handled by a delegate.
83 class FileBasedPolicyLoader : public FilePathWatcher::Delegate {
84 public:
85 // Creates a new loader that'll load its data from |config_file_path|.
86 // Assumes ownership of |delegate|, which provides the actual loading logic.
87 // The parameters |settle_interval_seconds| and |reload_interval_minutes|
88 // specify the time to wait before reading the file contents after a change
89 // and the period for checking |config_file_path| for changes, respectively.
90 FileBasedPolicyLoader(base::WeakPtr<ConfigurationPolicyProvider> provider,
91 FileBasedPolicyProvider::Delegate* delegate,
92 int settle_interval_seconds,
93 int reload_interval_minutes);
94
95 // Stops any pending reload tasks.
96 void Stop();
97
98 // Reloads the policies and sends out a notification, if appropriate. Must be
99 // called on the file thread.
100 void Reload();
101
102 // Gets the current dictionary value object. Ownership of the returned value
103 // is transferred to the caller.
104 DictionaryValue* GetPolicy();
105
106 const FilePath& config_file_path() { return delegate_->config_file_path(); }
107
108 // FilePathWatcher::Delegate implementation:
109 virtual void OnFilePathChanged(const FilePath& path);
110 virtual void OnError();
111
112 private:
113 // FileBasedPolicyLoader objects should only be deleted by
114 // RefCountedThreadSafe.
115 friend class base::RefCountedThreadSafe<FileBasedPolicyLoader>;
116 virtual ~FileBasedPolicyLoader();
117
118 // Checks whether reading policy information is safe to do. If not, returns
119 // false and the delay until it is considered safe to reload in |delay|.
120 bool IsSafeToReloadPolicy(const base::Time& now, base::TimeDelta* delay);
121
122 // Schedules a reload task to run when |delay| expires. Must be called on the
123 // file thread.
124 void ScheduleReloadTask(const base::TimeDelta& delay);
125
126 // Notifies the policy provider to send out a policy changed notification.
127 // Must be called on |origin_loop_|.
128 void NotifyPolicyChanged();
129
130 // Invoked from the reload task on the file thread.
131 void ReloadFromTask();
132
133 // The delegate.
134 scoped_ptr<FileBasedPolicyProvider::Delegate> delegate_;
135
136 // The provider this loader is associated with. Access only on the thread that
137 // called the constructor. See |origin_loop_| below.
138 base::WeakPtr<ConfigurationPolicyProvider> provider_;
139
140 // The message loop on which this object was constructed and |provider_|
141 // received on. Recorded so we can call back into the non thread safe provider
142 // to fire the notification.
143 MessageLoop* origin_loop_;
144
145 // Records last known modification timestamp of |config_file_path_|.
146 base::Time last_modification_file_;
147
148 // The wall clock time at which the last modification timestamp was recorded.
149 // It's better to not assume the file notification time and the wall clock
150 // times come from the same source, just in case there is some non-local
151 // filesystem involved.
152 base::Time last_modification_clock_;
153
154 // Protects |policy_|.
155 Lock lock_;
156
157 // The current policy definition.
158 scoped_ptr<DictionaryValue> policy_;
159
160 // The reload task. Access only on the file thread. Holds a reference to the
161 // currently posted task, so we can cancel and repost it if necessary.
162 CancelableTask* reload_task_;
163
164 // Settle and reload intervals.
165 const int settle_interval_seconds_;
166 const int reload_interval_minutes_;
167
168 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyLoader);
169 };
170
171 // Wraps a FilePathWatcher for the configuration path and takes care of
172 // initializing the watcher object on the file thread.
173 class FileBasedPolicyWatcher
174 : public base::RefCountedThreadSafe<FileBasedPolicyWatcher> {
175 public:
176 FileBasedPolicyWatcher();
177
178 // Runs initialization. This is in a separate method since we need to post a
179 // task (which cannot be done from the constructor).
180 void Init(FileBasedPolicyLoader* loader);
181
182 private:
183 // FileBasedPolicyWatcher objects should only be deleted by
184 // RefCountedThreadSafe.
185 friend class base::RefCountedThreadSafe<FileBasedPolicyWatcher>;
186 virtual ~FileBasedPolicyWatcher();
187
188 // Actually sets up the watch with the FilePathWatcher code.
189 void InitWatcher(const scoped_refptr<FileBasedPolicyLoader>& loader);
190
191 // Wrapped watcher that takes care of the actual watching.
192 FilePathWatcher watcher_;
193
194 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyWatcher);
195 };
196
197 } // namespace policy 52 } // namespace policy
198 53
199 #endif // CHROME_BROWSER_POLICY_FILE_BASED_POLICY_PROVIDER_H_ 54 #endif // CHROME_BROWSER_POLICY_FILE_BASED_POLICY_PROVIDER_H_
OLDNEW
« no previous file with comments | « chrome/browser/policy/file_based_policy_loader.cc ('k') | chrome/browser/policy/file_based_policy_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698