| 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" | |
| 16 #include "chrome/browser/policy/configuration_policy_provider.h" | 11 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 17 | 12 |
| 18 class ConfigDirPolicyProvider; | |
| 19 class DictionaryValue; | 13 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 // Protects |policy_|. | |
| 83 Lock lock_; | |
| 84 | |
| 85 // The current policy definition. | |
| 86 scoped_ptr<DictionaryValue> policy_; | |
| 87 | |
| 88 // The reload task. Access only on the file thread. Holds a reference to the | |
| 89 // currently posted task, so we can cancel and repost it if necessary. | |
| 90 CancelableTask* reload_task_; | |
| 91 | |
| 92 // Settle and reload intervals. | |
| 93 const int settle_interval_seconds_; | |
| 94 const int reload_interval_minutes_; | |
| 95 | |
| 96 DISALLOW_COPY_AND_ASSIGN(PolicyDirLoader); | |
| 97 }; | |
| 98 | |
| 99 // Wraps a FilePathWatcher for the configuration directory and takes care of | |
| 100 // initializing the watcher object on the file thread. | |
| 101 class PolicyDirWatcher : public FilePathWatcher, | |
| 102 public base::RefCountedThreadSafe<PolicyDirWatcher> { | |
| 103 public: | |
| 104 PolicyDirWatcher() {} | |
| 105 | |
| 106 // Run initialization. This is in a separate method since we need to post a | |
| 107 // task (which cannot be done from the constructor). | |
| 108 void Init(PolicyDirLoader* loader); | |
| 109 | |
| 110 private: | |
| 111 // Actually sets up the watch with the FilePathWatcher code. | |
| 112 void InitWatcher(const scoped_refptr<PolicyDirLoader>& loader); | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(PolicyDirWatcher); | |
| 115 }; | |
| 116 | 14 |
| 117 // A policy provider implementation backed by a set of files in a given | 15 // A policy provider implementation backed by a set of files in a given |
| 118 // directory. The files should contain JSON-formatted policy settings. They are | 16 // directory. The files should contain JSON-formatted policy settings. They are |
| 119 // merged together and the result is returned via the | 17 // merged together and the result is returned via the |
| 120 // ConfigurationPolicyProvider interface. The files are consulted in | 18 // ConfigurationPolicyProvider interface. The files are consulted in |
| 121 // lexicographic file name order, so the last value read takes precedence in | 19 // lexicographic file name order, so the last value read takes precedence in |
| 122 // case of preference key collisions. | 20 // case of preference key collisions. |
| 123 class ConfigDirPolicyProvider | 21 class ConfigDirPolicyProvider : public ConfigurationPolicyProvider { |
| 124 : public ConfigurationPolicyProvider, | |
| 125 public base::SupportsWeakPtr<ConfigDirPolicyProvider> { | |
| 126 public: | 22 public: |
| 127 explicit ConfigDirPolicyProvider(const FilePath& config_dir); | 23 explicit ConfigDirPolicyProvider(const FilePath& config_dir); |
| 128 virtual ~ConfigDirPolicyProvider(); | 24 virtual ~ConfigDirPolicyProvider() { } |
| 129 | 25 |
| 130 // ConfigurationPolicyProvider implementation. | 26 // ConfigurationPolicyProvider implementation. |
| 131 virtual bool Provide(ConfigurationPolicyStore* store); | 27 virtual bool Provide(ConfigurationPolicyStore* store); |
| 132 | 28 |
| 133 private: | 29 private: |
| 30 // Read and merge the files from the configuration directory. |
| 31 DictionaryValue* ReadPolicies(); |
| 32 |
| 134 // Decodes the value tree and writes the configuration to the given |store|. | 33 // Decodes the value tree and writes the configuration to the given |store|. |
| 135 void DecodePolicyValueTree(DictionaryValue* policies, | 34 void DecodePolicyValueTree(DictionaryValue* policies, |
| 136 ConfigurationPolicyStore* store); | 35 ConfigurationPolicyStore* store); |
| 137 | 36 |
| 138 // Watches for changes to the configuration directory. | 37 // The directory in which we look for configuration files. |
| 139 scoped_refptr<PolicyDirWatcher> watcher_; | 38 const FilePath config_dir_; |
| 140 | |
| 141 // The loader object we use internally. | |
| 142 scoped_refptr<PolicyDirLoader> loader_; | |
| 143 | 39 |
| 144 DISALLOW_COPY_AND_ASSIGN(ConfigDirPolicyProvider); | 40 DISALLOW_COPY_AND_ASSIGN(ConfigDirPolicyProvider); |
| 145 }; | 41 }; |
| 146 | 42 |
| 147 #endif // CHROME_BROWSER_POLICY_CONFIG_DIR_POLICY_PROVIDER_H_ | 43 #endif // CHROME_BROWSER_POLICY_CONFIG_DIR_POLICY_PROVIDER_H_ |
| OLD | NEW |