| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "base/compiler_specific.h" | |
| 10 #include "base/file_path.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/time.h" | |
| 13 #include "chrome/browser/policy/asynchronous_policy_loader.h" | |
| 14 #include "chrome/browser/policy/file_based_policy_provider.h" | |
| 15 | |
| 16 namespace base { | |
| 17 namespace files { | |
| 18 | |
| 19 class FilePathWatcher; | |
| 20 | |
| 21 } // namespace files | |
| 22 } // namespace base | |
| 23 | |
| 24 namespace policy { | |
| 25 | |
| 26 // A customized asynchronous policy loader that handles loading policy from a | |
| 27 // file using a FilePathWatcher. The loader creates a fallback task to load | |
| 28 // policy periodically in case the watcher fails and retries policy loads when | |
| 29 // the watched file is in flux. | |
| 30 class FileBasedPolicyLoader : public AsynchronousPolicyLoader { | |
| 31 public: | |
| 32 explicit FileBasedPolicyLoader( | |
| 33 FileBasedPolicyProvider::ProviderDelegate* provider_delegate); | |
| 34 | |
| 35 // AsynchronousPolicyLoader overrides: | |
| 36 virtual void Reload(bool force) OVERRIDE; | |
| 37 | |
| 38 void OnFilePathChanged(const FilePath& path); | |
| 39 void OnFilePathError(const FilePath& path); | |
| 40 | |
| 41 protected: | |
| 42 // FileBasedPolicyLoader objects should only be deleted by | |
| 43 // RefCountedThreadSafe. | |
| 44 friend class base::RefCountedThreadSafe<AsynchronousPolicyLoader>; | |
| 45 virtual ~FileBasedPolicyLoader(); | |
| 46 | |
| 47 const FilePath& config_file_path() { return config_file_path_; } | |
| 48 | |
| 49 // AsynchronousPolicyLoader overrides: | |
| 50 | |
| 51 // Creates the file path watcher and configures it to watch | |
| 52 // |config_file_path_|. Must be called on the file thread. | |
| 53 virtual void InitOnFileThread() OVERRIDE; | |
| 54 virtual void StopOnFileThread() OVERRIDE; | |
| 55 | |
| 56 private: | |
| 57 // Checks whether policy information is safe to read. If not, returns false | |
| 58 // and then delays until it is considered safe to reload in |delay|. | |
| 59 // Must be called on the file thread. | |
| 60 bool IsSafeToReloadPolicy(const base::Time& now, base::TimeDelta* delay); | |
| 61 | |
| 62 // The path at which we look for configuration files. | |
| 63 const FilePath config_file_path_; | |
| 64 | |
| 65 // Managed with a scoped_ptr rather than being declared as an inline member to | |
| 66 // decouple the watcher's life cycle from the loader's. This decoupling makes | |
| 67 // it possible to destroy the watcher before the loader's destructor is called | |
| 68 // (e.g. during Stop), since |watcher_| internally holds a reference to the | |
| 69 // loader and keeps it alive. | |
| 70 scoped_ptr<base::files::FilePathWatcher> watcher_; | |
| 71 | |
| 72 // Settle interval. | |
| 73 const base::TimeDelta settle_interval_; | |
| 74 | |
| 75 // Records last known modification timestamp of |config_file_path_|. | |
| 76 base::Time last_modification_file_; | |
| 77 | |
| 78 // The wall clock time at which the last modification timestamp was | |
| 79 // recorded. It's better to not assume the file notification time and the | |
| 80 // wall clock times come from the same source, just in case there is some | |
| 81 // non-local filesystem involved. | |
| 82 base::Time last_modification_clock_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyLoader); | |
| 85 }; | |
| 86 | |
| 87 } // namespace policy | |
| 88 | |
| 89 #endif // CHROME_BROWSER_POLICY_FILE_BASED_POLICY_LOADER_H_ | |
| OLD | NEW |