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" |
| 12 #include "base/ref_counted.h" |
| 13 #include "base/scoped_ptr.h" |
| 14 #include "base/weak_ptr.h" |
| 15 #include "chrome/browser/file_path_watcher.h" |
11 #include "chrome/browser/policy/configuration_policy_provider.h" | 16 #include "chrome/browser/policy/configuration_policy_provider.h" |
12 | 17 |
| 18 class ConfigDirPolicyProvider; |
13 class DictionaryValue; | 19 class DictionaryValue; |
| 20 class MessageLoop; |
| 21 |
| 22 // FilePathWatcher delegate implementation that handles change notifications for |
| 23 // the configuration directory. It keeps the authorative version of the |
| 24 // currently effective policy dictionary and updates it as appropriate. |
| 25 class PolicyDirLoader : public FilePathWatcher::Delegate { |
| 26 public: |
| 27 // Create a new loader that'll load its data from |config_dir|. |
| 28 PolicyDirLoader(base::WeakPtr<ConfigDirPolicyProvider> provider, |
| 29 const FilePath& config_dir, |
| 30 int settle_interval_second, |
| 31 int reload_interval_minutes); |
| 32 |
| 33 // Stops any pending reload tasks. |
| 34 void Stop(); |
| 35 |
| 36 // Reloads the policies and sends out a notification, if appropriate. Must be |
| 37 // called on the file thread. |
| 38 void Reload(); |
| 39 |
| 40 // Get the current dictionary value object. Ownership of the returned value is |
| 41 // transferred to the caller. |
| 42 DictionaryValue* GetPolicy(); |
| 43 |
| 44 const FilePath& config_dir() { return config_dir_; } |
| 45 |
| 46 // FilePathWatcher::Delegate implementation: |
| 47 void OnFilePathChanged(const FilePath& path); |
| 48 void OnError(); |
| 49 |
| 50 private: |
| 51 // Load the policy information. Ownership of the return value is transferred |
| 52 // to the caller. |
| 53 DictionaryValue* Load(); |
| 54 |
| 55 // Check the directory modification time to see whether reading the |
| 56 // configuration directory is safe. If not, returns false and the delay until |
| 57 // it is considered safe to reload in |delay|. |
| 58 bool IsSafeToReloadPolicy(const base::Time& now, base::TimeDelta* delay); |
| 59 |
| 60 // Post a reload task. Must be called on the file thread. |
| 61 void ScheduleReloadTask(const base::TimeDelta& delay); |
| 62 |
| 63 // Notifies the policy provider to send out a policy changed notification. |
| 64 // Must be called on |origin_loop_|. |
| 65 void NotifyPolicyChanged(); |
| 66 |
| 67 // Invoked from the reload task on the file thread. |
| 68 void ReloadFromTask(); |
| 69 |
| 70 // The provider this loader is associated with. Access only on the thread that |
| 71 // called the constructor. See |origin_loop_| below. |
| 72 base::WeakPtr<ConfigDirPolicyProvider> provider_; |
| 73 |
| 74 // The message loop on which this object was constructed and |provider_| |
| 75 // received on. Recorded so we can call back into the non thread safe provider |
| 76 // to fire the notification. |
| 77 MessageLoop* origin_loop_; |
| 78 |
| 79 // The directory in which we look for configuration files. |
| 80 const FilePath config_dir_; |
| 81 |
| 82 // Records last known modification timestamp of |config_dir_|. |
| 83 base::Time last_modification_file_; |
| 84 |
| 85 // The wall clock time at which the last modification timestamp was recorded. |
| 86 // It's better to not assume the file notification time and the wall clock |
| 87 // times come from the same source, just in case there is some non-local |
| 88 // filesystem involved. |
| 89 base::Time last_modification_clock_; |
| 90 |
| 91 // Protects |policy_|. |
| 92 Lock lock_; |
| 93 |
| 94 // The current policy definition. |
| 95 scoped_ptr<DictionaryValue> policy_; |
| 96 |
| 97 // The reload task. Access only on the file thread. Holds a reference to the |
| 98 // currently posted task, so we can cancel and repost it if necessary. |
| 99 CancelableTask* reload_task_; |
| 100 |
| 101 // Settle and reload intervals. |
| 102 const int settle_interval_seconds_; |
| 103 const int reload_interval_minutes_; |
| 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(PolicyDirLoader); |
| 106 }; |
| 107 |
| 108 // Wraps a FilePathWatcher for the configuration directory and takes care of |
| 109 // initializing the watcher object on the file thread. |
| 110 class PolicyDirWatcher : public FilePathWatcher, |
| 111 public base::RefCountedThreadSafe<PolicyDirWatcher> { |
| 112 public: |
| 113 PolicyDirWatcher() {} |
| 114 |
| 115 // Run initialization. This is in a separate method since we need to post a |
| 116 // task (which cannot be done from the constructor). |
| 117 void Init(PolicyDirLoader* loader); |
| 118 |
| 119 private: |
| 120 // Actually sets up the watch with the FilePathWatcher code. |
| 121 void InitWatcher(const scoped_refptr<PolicyDirLoader>& loader); |
| 122 |
| 123 DISALLOW_COPY_AND_ASSIGN(PolicyDirWatcher); |
| 124 }; |
14 | 125 |
15 // A policy provider implementation backed by a set of files in a given | 126 // A policy provider implementation backed by a set of files in a given |
16 // directory. The files should contain JSON-formatted policy settings. They are | 127 // directory. The files should contain JSON-formatted policy settings. They are |
17 // merged together and the result is returned via the | 128 // merged together and the result is returned via the |
18 // ConfigurationPolicyProvider interface. The files are consulted in | 129 // ConfigurationPolicyProvider interface. The files are consulted in |
19 // lexicographic file name order, so the last value read takes precedence in | 130 // lexicographic file name order, so the last value read takes precedence in |
20 // case of preference key collisions. | 131 // case of preference key collisions. |
21 class ConfigDirPolicyProvider : public ConfigurationPolicyProvider { | 132 class ConfigDirPolicyProvider |
| 133 : public ConfigurationPolicyProvider, |
| 134 public base::SupportsWeakPtr<ConfigDirPolicyProvider> { |
22 public: | 135 public: |
23 explicit ConfigDirPolicyProvider(const FilePath& config_dir); | 136 explicit ConfigDirPolicyProvider(const FilePath& config_dir); |
24 virtual ~ConfigDirPolicyProvider() { } | 137 virtual ~ConfigDirPolicyProvider(); |
25 | 138 |
26 // ConfigurationPolicyProvider implementation. | 139 // ConfigurationPolicyProvider implementation. |
27 virtual bool Provide(ConfigurationPolicyStore* store); | 140 virtual bool Provide(ConfigurationPolicyStore* store); |
28 | 141 |
29 private: | 142 private: |
30 // Read and merge the files from the configuration directory. | |
31 DictionaryValue* ReadPolicies(); | |
32 | |
33 // Decodes the value tree and writes the configuration to the given |store|. | 143 // Decodes the value tree and writes the configuration to the given |store|. |
34 void DecodePolicyValueTree(DictionaryValue* policies, | 144 void DecodePolicyValueTree(DictionaryValue* policies, |
35 ConfigurationPolicyStore* store); | 145 ConfigurationPolicyStore* store); |
36 | 146 |
37 // The directory in which we look for configuration files. | 147 // Watches for changes to the configuration directory. |
38 const FilePath config_dir_; | 148 scoped_refptr<PolicyDirWatcher> watcher_; |
| 149 |
| 150 // The loader object we use internally. |
| 151 scoped_refptr<PolicyDirLoader> loader_; |
39 | 152 |
40 DISALLOW_COPY_AND_ASSIGN(ConfigDirPolicyProvider); | 153 DISALLOW_COPY_AND_ASSIGN(ConfigDirPolicyProvider); |
41 }; | 154 }; |
42 | 155 |
43 #endif // CHROME_BROWSER_POLICY_CONFIG_DIR_POLICY_PROVIDER_H_ | 156 #endif // CHROME_BROWSER_POLICY_CONFIG_DIR_POLICY_PROVIDER_H_ |
OLD | NEW |