OLD | NEW |
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_CONFIG_DIR_POLICY_PROVIDER_H_ | 5 #ifndef CHROME_BROWSER_POLICY_CONFIG_DIR_POLICY_PROVIDER_H_ |
6 #define CHROME_BROWSER_POLICY_CONFIG_DIR_POLICY_PROVIDER_H_ | 6 #define CHROME_BROWSER_POLICY_CONFIG_DIR_POLICY_PROVIDER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/lock.h" | 11 #include "base/lock.h" |
12 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
13 #include "base/scoped_ptr.h" | 13 #include "base/scoped_ptr.h" |
| 14 #include "base/time.h" |
14 #include "base/weak_ptr.h" | 15 #include "base/weak_ptr.h" |
15 #include "chrome/browser/file_path_watcher.h" | 16 #include "chrome/browser/file_path_watcher.h" |
16 #include "chrome/browser/policy/configuration_policy_provider.h" | 17 #include "chrome/browser/policy/configuration_policy_provider.h" |
17 | 18 |
| 19 class CancelableTask; |
18 class ConfigDirPolicyProvider; | 20 class ConfigDirPolicyProvider; |
19 class DictionaryValue; | 21 class DictionaryValue; |
20 class MessageLoop; | 22 class MessageLoop; |
21 | 23 |
22 // FilePathWatcher delegate implementation that handles change notifications for | 24 // FilePathWatcher delegate implementation that handles change notifications for |
23 // the configuration directory. It keeps the authorative version of the | 25 // the configuration directory. It keeps the authorative version of the |
24 // currently effective policy dictionary and updates it as appropriate. | 26 // currently effective policy dictionary and updates it as appropriate. |
25 class PolicyDirLoader : public FilePathWatcher::Delegate { | 27 class PolicyDirLoader : public FilePathWatcher::Delegate { |
26 public: | 28 public: |
27 // Create a new loader that'll load its data from |config_dir|. | 29 // Creates a new loader that'll load its data from |config_dir|. The |
| 30 // parameters |settle_interval_seconds| and |reload_interval_minutes| specify |
| 31 // the time to wait before reading the directory contents after a change and |
| 32 // the period for checking |config_dir| for changes, respectively. |
28 PolicyDirLoader(base::WeakPtr<ConfigDirPolicyProvider> provider, | 33 PolicyDirLoader(base::WeakPtr<ConfigDirPolicyProvider> provider, |
29 const FilePath& config_dir, | 34 const FilePath& config_dir, |
30 int settle_interval_second, | 35 int settle_interval_seconds, |
31 int reload_interval_minutes); | 36 int reload_interval_minutes); |
32 | 37 |
33 // Stops any pending reload tasks. | 38 // Stops any pending reload tasks. |
34 void Stop(); | 39 void Stop(); |
35 | 40 |
36 // Reloads the policies and sends out a notification, if appropriate. Must be | 41 // Reloads the policies and sends out a notification, if appropriate. Must be |
37 // called on the file thread. | 42 // called on the file thread. |
38 void Reload(); | 43 void Reload(); |
39 | 44 |
40 // Get the current dictionary value object. Ownership of the returned value is | 45 // Gets the current dictionary value object. Ownership of the returned value |
41 // transferred to the caller. | 46 // is transferred to the caller. |
42 DictionaryValue* GetPolicy(); | 47 DictionaryValue* GetPolicy(); |
43 | 48 |
44 const FilePath& config_dir() { return config_dir_; } | 49 const FilePath& config_dir() { return config_dir_; } |
45 | 50 |
46 // FilePathWatcher::Delegate implementation: | 51 // FilePathWatcher::Delegate implementation: |
47 void OnFilePathChanged(const FilePath& path); | 52 void OnFilePathChanged(const FilePath& path); |
48 void OnError(); | 53 void OnError(); |
49 | 54 |
50 private: | 55 private: |
51 // Load the policy information. Ownership of the return value is transferred | 56 // Loads the policy information. Ownership of the return value is transferred |
52 // to the caller. | 57 // to the caller. |
53 DictionaryValue* Load(); | 58 DictionaryValue* Load(); |
54 | 59 |
55 // Check the directory modification time to see whether reading the | 60 // Checks the directory modification time to see whether reading the |
56 // configuration directory is safe. If not, returns false and the delay until | 61 // configuration directory is safe. If not, returns false and the delay until |
57 // it is considered safe to reload in |delay|. | 62 // it is considered safe to reload in |delay|. |
58 bool IsSafeToReloadPolicy(const base::Time& now, base::TimeDelta* delay); | 63 bool IsSafeToReloadPolicy(const base::Time& now, base::TimeDelta* delay); |
59 | 64 |
60 // Post a reload task. Must be called on the file thread. | 65 // Schedules a reload task to run when |delay| expires. Must be called on the |
| 66 // file thread. |
61 void ScheduleReloadTask(const base::TimeDelta& delay); | 67 void ScheduleReloadTask(const base::TimeDelta& delay); |
62 | 68 |
63 // Notifies the policy provider to send out a policy changed notification. | 69 // Notifies the policy provider to send out a policy changed notification. |
64 // Must be called on |origin_loop_|. | 70 // Must be called on |origin_loop_|. |
65 void NotifyPolicyChanged(); | 71 void NotifyPolicyChanged(); |
66 | 72 |
67 // Invoked from the reload task on the file thread. | 73 // Invoked from the reload task on the file thread. |
68 void ReloadFromTask(); | 74 void ReloadFromTask(); |
69 | 75 |
70 // The provider this loader is associated with. Access only on the thread that | 76 // The provider this loader is associated with. Access only on the thread that |
(...skipping 29 matching lines...) Expand all Loading... |
100 | 106 |
101 // Settle and reload intervals. | 107 // Settle and reload intervals. |
102 const int settle_interval_seconds_; | 108 const int settle_interval_seconds_; |
103 const int reload_interval_minutes_; | 109 const int reload_interval_minutes_; |
104 | 110 |
105 DISALLOW_COPY_AND_ASSIGN(PolicyDirLoader); | 111 DISALLOW_COPY_AND_ASSIGN(PolicyDirLoader); |
106 }; | 112 }; |
107 | 113 |
108 // Wraps a FilePathWatcher for the configuration directory and takes care of | 114 // Wraps a FilePathWatcher for the configuration directory and takes care of |
109 // initializing the watcher object on the file thread. | 115 // initializing the watcher object on the file thread. |
110 class PolicyDirWatcher : public FilePathWatcher, | 116 class PolicyDirWatcher : public base::RefCountedThreadSafe<PolicyDirWatcher> { |
111 public base::RefCountedThreadSafe<PolicyDirWatcher> { | |
112 public: | 117 public: |
113 PolicyDirWatcher() {} | 118 PolicyDirWatcher() {} |
114 | 119 |
115 // Run initialization. This is in a separate method since we need to post a | 120 // Runs initialization. This is in a separate method since we need to post a |
116 // task (which cannot be done from the constructor). | 121 // task (which cannot be done from the constructor). |
117 void Init(PolicyDirLoader* loader); | 122 void Init(PolicyDirLoader* loader); |
118 | 123 |
119 private: | 124 private: |
| 125 // PolicyDirWatcher objects should only be deleted by RefCountedThreadSafe. |
| 126 friend class base::RefCountedThreadSafe<PolicyDirWatcher>; |
| 127 ~PolicyDirWatcher() {} |
| 128 |
120 // Actually sets up the watch with the FilePathWatcher code. | 129 // Actually sets up the watch with the FilePathWatcher code. |
121 void InitWatcher(const scoped_refptr<PolicyDirLoader>& loader); | 130 void InitWatcher(const scoped_refptr<PolicyDirLoader>& loader); |
122 | 131 |
| 132 // Wrapped watcher that takes care of the actual watching. |
| 133 FilePathWatcher watcher_; |
| 134 |
123 DISALLOW_COPY_AND_ASSIGN(PolicyDirWatcher); | 135 DISALLOW_COPY_AND_ASSIGN(PolicyDirWatcher); |
124 }; | 136 }; |
125 | 137 |
126 // A policy provider implementation backed by a set of files in a given | 138 // A policy provider implementation backed by a set of files in a given |
127 // directory. The files should contain JSON-formatted policy settings. They are | 139 // directory. The files should contain JSON-formatted policy settings. They are |
128 // merged together and the result is returned via the | 140 // merged together and the result is returned via the |
129 // ConfigurationPolicyProvider interface. The files are consulted in | 141 // ConfigurationPolicyProvider interface. The files are consulted in |
130 // lexicographic file name order, so the last value read takes precedence in | 142 // lexicographic file name order, so the last value read takes precedence in |
131 // case of preference key collisions. | 143 // case of preference key collisions. |
132 class ConfigDirPolicyProvider | 144 class ConfigDirPolicyProvider |
(...skipping 14 matching lines...) Expand all Loading... |
147 // Watches for changes to the configuration directory. | 159 // Watches for changes to the configuration directory. |
148 scoped_refptr<PolicyDirWatcher> watcher_; | 160 scoped_refptr<PolicyDirWatcher> watcher_; |
149 | 161 |
150 // The loader object we use internally. | 162 // The loader object we use internally. |
151 scoped_refptr<PolicyDirLoader> loader_; | 163 scoped_refptr<PolicyDirLoader> loader_; |
152 | 164 |
153 DISALLOW_COPY_AND_ASSIGN(ConfigDirPolicyProvider); | 165 DISALLOW_COPY_AND_ASSIGN(ConfigDirPolicyProvider); |
154 }; | 166 }; |
155 | 167 |
156 #endif // CHROME_BROWSER_POLICY_CONFIG_DIR_POLICY_PROVIDER_H_ | 168 #endif // CHROME_BROWSER_POLICY_CONFIG_DIR_POLICY_PROVIDER_H_ |
OLD | NEW |