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/files/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 overrides: 25 virtual void Reload(); 26 27 void OnFilePathChanged(const FilePath& path); 28 void OnFilePathError(const FilePath& path); 29 30 protected: 31 // FileBasedPolicyLoader objects should only be deleted by 32 // RefCountedThreadSafe. 33 friend class base::RefCountedThreadSafe<AsynchronousPolicyLoader>; 34 virtual ~FileBasedPolicyLoader(); 35 36 const FilePath& config_file_path() { return config_file_path_; } 37 38 // AsynchronousPolicyLoader overrides: 39 40 // Creates the file path watcher and configures it to watch 41 // |config_file_path_|. Must be called on the file thread. 42 virtual void InitOnFileThread(); 43 virtual void StopOnFileThread(); 44 45 private: 46 // Checks whether policy information is safe to read. If not, returns false 47 // and then delays until it is considered safe to reload in |delay|. 48 // Must be called on the file thread. 49 bool IsSafeToReloadPolicy(const base::Time& now, base::TimeDelta* delay); 50 51 // The path at which we look for configuration files. 52 const FilePath config_file_path_; 53 54 // Managed with a scoped_ptr rather than being declared as an inline member to 55 // decouple the watcher's life cycle from the loader's. This decoupling makes 56 // it possible to destroy the watcher before the loader's destructor is called 57 // (e.g. during Stop), since |watcher_| internally holds a reference to the 58 // loader and keeps it alive. 59 scoped_ptr<base::files::FilePathWatcher> watcher_; 60 61 // Settle interval. 62 const base::TimeDelta settle_interval_; 63 64 // Records last known modification timestamp of |config_file_path_|. 65 base::Time last_modification_file_; 66 67 // The wall clock time at which the last modification timestamp was 68 // recorded. It's better to not assume the file notification time and the 69 // wall clock times come from the same source, just in case there is some 70 // non-local filesystem involved. 71 base::Time last_modification_clock_; 72 73 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyLoader); 74 }; 75 76 } // namespace policy 77 78 #endif // CHROME_BROWSER_POLICY_FILE_BASED_POLICY_LOADER_H_ 79