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