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_LOADER_H_ |
| 6 #define CHROME_BROWSER_POLICY_FILE_BASED_POLICY_LOADER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "chrome/browser/file_path_watcher/file_path_watcher.h" |
| 10 #include "chrome/browser/policy/asynchronous_policy_loader.h" |
| 11 #include "chrome/browser/policy/file_based_policy_provider.h" |
| 12 |
| 13 namespace policy { |
| 14 |
| 15 // A customized asynchronous policy loader that handles loading policy from a |
| 16 // file using a FilePathWatcher. The loader creates a fallback task to load |
| 17 // policy periodically in case the watcher fails and retries policy loads when |
| 18 // the watched file is in flux. |
| 19 class FileBasedPolicyLoader : public AsynchronousPolicyLoader { |
| 20 public: |
| 21 FileBasedPolicyLoader( |
| 22 FileBasedPolicyProvider::ProviderDelegate* provider_delegate); |
| 23 |
| 24 // AsynchronousPolicyLoader implementation: |
| 25 virtual void Init(); |
| 26 virtual void Stop(); |
| 27 virtual void Reload(); |
| 28 |
| 29 void OnFilePathChanged(const FilePath& path); |
| 30 void OnError(); |
| 31 |
| 32 protected: |
| 33 // FileBasedPolicyLoader objects should only be deleted by |
| 34 // RefCountedThreadSafe. |
| 35 friend class base::RefCountedThreadSafe<AsynchronousPolicyLoader>; |
| 36 virtual ~FileBasedPolicyLoader() {} |
| 37 |
| 38 const FilePath& config_file_path() { return config_file_path_; } |
| 39 |
| 40 private: |
| 41 // Finishes initialization after the threading system has been fully |
| 42 // intiialized. |
| 43 void InitAfterFileThreadAvailable(); |
| 44 |
| 45 // Creates the file path watcher and configures it watch |config_file_path_|. |
| 46 // Must be called on the file thread. |
| 47 void InitWatcher(); |
| 48 |
| 49 // Cancels file path watch notification and destroys the watcher. |
| 50 // Must be called on file thread. |
| 51 void StopOnFileThread(); |
| 52 |
| 53 // Schedules a reload task to run when |delay| expires. Must be called on the |
| 54 // file thread. |
| 55 void ScheduleReloadTask(const base::TimeDelta& delay); |
| 56 |
| 57 // Schedules a reload task to run after the number of minutes specified |
| 58 // in |reload_interval_minutes_|. Must be called on the file thread. |
| 59 void ScheduleFallbackReloadTask(); |
| 60 |
| 61 // Invoked from the reload task on the file thread. |
| 62 void ReloadFromTask(); |
| 63 |
| 64 // Checks whether policy information is safe to read. If not, returns false |
| 65 // and then delays until it is considered safe to reload in |delay|. |
| 66 // Must be called on the file thread. |
| 67 bool IsSafeToReloadPolicy(const base::Time& now, base::TimeDelta* delay); |
| 68 |
| 69 // The path at which we look for configuration files. |
| 70 const FilePath config_file_path_; |
| 71 |
| 72 // Managed with a scoped_ptr rather than being declared as an inline member to |
| 73 // decouple the watcher's life cycle from the loader's. This decoupling makes |
| 74 // it possible to destroy the watcher before the loader's destructor is called |
| 75 // (e.g. during Stop), since |watcher_| internally holds a reference to the |
| 76 // loader and keeps it alive. |
| 77 scoped_ptr<FilePathWatcher> watcher_; |
| 78 |
| 79 // The reload task. Access only on the file thread. Holds a reference to the |
| 80 // currently posted task, so we can cancel and repost it if necessary. |
| 81 CancelableTask* reload_task_; |
| 82 |
| 83 // The interval that a policy reload will be triggered as a fallback even if |
| 84 // the delegate doesn't indicate that one is needed. |
| 85 const base::TimeDelta reload_interval_; |
| 86 |
| 87 // Settle interval. |
| 88 const base::TimeDelta settle_interval_; |
| 89 |
| 90 // Records last known modification timestamp of |config_file_path_|. |
| 91 base::Time last_modification_file_; |
| 92 |
| 93 // The wall clock time at which the last modification timestamp was |
| 94 // recorded. It's better to not assume the file notification time and the |
| 95 // wall clock times come from the same source, just in case there is some |
| 96 // non-local filesystem involved. |
| 97 base::Time last_modification_clock_; |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyLoader); |
| 100 }; |
| 101 |
| 102 } // namespace policy |
| 103 |
| 104 #endif // CHROME_BROWSER_POLICY_FILE_BASED_POLICY_LOADER_H_ |
OLD | NEW |