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