Chromium Code Reviews| 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_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" | 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/time.h" |
| 15 #include "base/weak_ptr.h" | 15 #include "base/weak_ptr.h" |
| 16 #include "chrome/browser/file_path_watcher.h" | 16 #include "chrome/browser/file_path_watcher.h" |
| 17 #include "chrome/browser/policy/asynchronous_policy_provider.h" | |
| 17 #include "chrome/browser/policy/configuration_policy_provider.h" | 18 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 18 | 19 |
| 19 class CancelableTask; | 20 class CancelableTask; |
| 20 class DictionaryValue; | 21 class DictionaryValue; |
| 21 class MessageLoop; | 22 class MessageLoop; |
| 22 | 23 |
| 23 namespace policy { | 24 namespace policy { |
| 24 | 25 |
| 25 class FileBasedPolicyLoader; | |
| 26 class FileBasedPolicyWatcher; | |
| 27 | |
| 28 // File based policy provider that coordinates watching and reloading policy | 26 // File based policy provider that coordinates watching and reloading policy |
| 29 // information from the configuration path. Actual logic for loading policy | 27 // information from the configuration path. Actual logic for loading policy |
| 30 // information is handled by a delegate passed at construction time. | 28 // information is handled by a delegate passed at construction time. |
| 31 class FileBasedPolicyProvider | 29 class FileBasedPolicyProvider : public AsynchronousPolicyProvider { |
| 32 : public ConfigurationPolicyProvider, | |
| 33 public base::SupportsWeakPtr<FileBasedPolicyProvider> { | |
| 34 public: | 30 public: |
| 31 class WatcherDelegate : public FilePathWatcher::Delegate { | |
| 32 public: | |
| 33 WatcherDelegate( | |
| 34 const FilePath& config_file_path, | |
| 35 AsynchronousPolicyProvider::PolicyChangeObserver* observer); | |
| 36 void Init(); | |
| 37 void Stop(); | |
| 38 | |
| 39 // FilePathWatcher implementation. | |
| 40 void OnFilePathChanged(const FilePath& path); | |
| 41 void OnError(); | |
| 42 | |
| 43 private: | |
| 44 friend class base::RefCountedThreadSafe<WatcherDelegate>; | |
| 45 virtual ~WatcherDelegate() {} | |
| 46 | |
| 47 scoped_ptr<FilePathWatcher> watcher_; | |
| 48 FilePath config_file_path_; | |
| 49 scoped_ptr<AsynchronousPolicyProvider::PolicyChangeObserver> observer_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(WatcherDelegate); | |
| 52 }; | |
| 53 | |
| 35 // Delegate interface for actual policy loading from the system. | 54 // Delegate interface for actual policy loading from the system. |
| 36 class Delegate { | 55 class ProviderDelegate : public AsynchronousPolicyProvider::Delegate { |
| 37 public: | 56 public: |
| 38 virtual ~Delegate(); | 57 explicit ProviderDelegate(const FilePath& config_file_path); |
| 58 virtual ~ProviderDelegate(); | |
| 59 | |
| 60 void Init(AsynchronousPolicyProvider::PolicyChangeObserver* observer); | |
| 61 void Stop(); | |
| 62 | |
| 63 bool IsSafeToReloadPolicy(const base::Time& now, | |
| 64 base::TimeDelta* delay); | |
| 39 | 65 |
| 40 // Loads the policy information. Ownership of the return value is | 66 // Loads the policy information. Ownership of the return value is |
| 41 // transferred to the caller. | 67 // transferred to the caller. |
| 42 virtual DictionaryValue* Load() = 0; | 68 virtual DictionaryValue* Load() = 0; |
| 43 | 69 |
| 44 // Gets the last modification timestamp for the policy information from the | 70 // Gets the last modification timestamp for the policy information from the |
| 45 // filesystem. Returns base::Time() if the information is not present, in | 71 // filesystem. Returns base::Time() if the information is not present, in |
| 46 // which case Load() should return an empty dictionary. | 72 // which case Load() should return an empty dictionary. |
| 47 virtual base::Time GetLastModification() = 0; | 73 virtual base::Time GetLastModification() = 0; |
| 48 | 74 |
| 49 const FilePath& config_file_path() { return config_file_path_; } | 75 const FilePath& config_file_path() { return config_file_path_; } |
| 50 | 76 |
| 51 protected: | 77 private: |
| 52 explicit Delegate(const FilePath& config_file_path); | 78 // Watches for changes to the configuration directory. |
| 79 FilePathWatcher watcher_; | |
| 80 scoped_refptr<WatcherDelegate> watcher_delegate_; | |
| 53 | 81 |
| 54 private: | |
| 55 // The path at which we look for configuration files. | 82 // The path at which we look for configuration files. |
| 56 const FilePath config_file_path_; | 83 const FilePath config_file_path_; |
| 57 | 84 |
| 58 DISALLOW_COPY_AND_ASSIGN(Delegate); | 85 // Settle interval. |
| 86 const int settle_interval_seconds_; | |
|
battre (please use the other)
2010/12/02 17:40:25
TimeDelta?
danno
2010/12/03 17:05:38
Done.
| |
| 87 | |
| 88 // Records last known modification timestamp of |config_file_path_|. | |
| 89 base::Time last_modification_file_; | |
| 90 | |
| 91 // The wall clock time at which the last modification timestamp was | |
| 92 // recorded. It's better to not assume the file notification time and the | |
| 93 // wall clock times come from the same source, just in case there is some | |
| 94 // non-local filesystem involved. | |
| 95 base::Time last_modification_clock_; | |
| 96 | |
| 97 DISALLOW_COPY_AND_ASSIGN(ProviderDelegate); | |
| 59 }; | 98 }; |
| 60 | 99 |
| 61 // Assumes ownership of |delegate|. | 100 // Assumes ownership of |delegate|. |
| 62 FileBasedPolicyProvider(const PolicyDefinitionList* policy_list, | 101 FileBasedPolicyProvider(const PolicyDefinitionList* policy_list, |
| 63 Delegate* delegate); | 102 ProviderDelegate* delegate); |
| 64 virtual ~FileBasedPolicyProvider(); | 103 virtual ~FileBasedPolicyProvider() {} |
| 65 | |
| 66 // ConfigurationPolicyProvider implementation. | |
| 67 virtual bool Provide(ConfigurationPolicyStoreInterface* store); | |
| 68 | 104 |
| 69 private: | 105 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); | 106 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyProvider); |
| 77 }; | 107 }; |
| 78 | 108 |
| 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 void OnFilePathChanged(const FilePath& path); | |
| 110 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 | 109 } // namespace policy |
| 198 | 110 |
| 199 #endif // CHROME_BROWSER_POLICY_FILE_BASED_POLICY_PROVIDER_H_ | 111 #endif // CHROME_BROWSER_POLICY_FILE_BASED_POLICY_PROVIDER_H_ |
| OLD | NEW |