Home | History | Annotate | Download | only in policy
      1 // Copyright (c) 2012 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_CONFIG_DIR_POLICY_LOADER_H_
      6 #define CHROME_BROWSER_POLICY_CONFIG_DIR_POLICY_LOADER_H_
      7 
      8 #include "base/files/file_path.h"
      9 #include "base/files/file_path_watcher.h"
     10 #include "chrome/browser/policy/async_policy_loader.h"
     11 #include "chrome/browser/policy/policy_types.h"
     12 
     13 namespace base {
     14 class Value;
     15 }
     16 
     17 namespace policy {
     18 
     19 // A policy loader implementation backed by a set of files in a given
     20 // directory. The files should contain JSON-formatted policy settings. They are
     21 // merged together and the result is returned in a PolicyBundle.
     22 // The files are consulted in lexicographic file name order, so the
     23 // last value read takes precedence in case of policy key collisions.
     24 class ConfigDirPolicyLoader : public AsyncPolicyLoader {
     25  public:
     26   ConfigDirPolicyLoader(const base::FilePath& config_dir, PolicyScope scope);
     27   virtual ~ConfigDirPolicyLoader();
     28 
     29   // AsyncPolicyLoader implementation.
     30   virtual void InitOnFile() OVERRIDE;
     31   virtual scoped_ptr<PolicyBundle> Load() OVERRIDE;
     32   virtual base::Time LastModificationTime() OVERRIDE;
     33 
     34  private:
     35   // Loads the policy files at |path| into the |bundle|, with the given |level|.
     36   void LoadFromPath(const base::FilePath& path,
     37                     PolicyLevel level,
     38                     PolicyBundle* bundle);
     39 
     40   // Merges the 3rd party |policies| into the |bundle|, with the given |level|.
     41   void Merge3rdPartyPolicy(const base::Value* policies,
     42                            PolicyLevel level,
     43                            PolicyBundle* bundle);
     44 
     45   // Callback for the FilePathWatchers.
     46   void OnFileUpdated(const base::FilePath& path, bool error);
     47 
     48   // The directory containing the policy files.
     49   base::FilePath config_dir_;
     50 
     51   // Policies loaded by this provider will have this scope.
     52   PolicyScope scope_;
     53 
     54   // Watchers for events on the mandatory and recommended subdirectories of
     55   // |config_dir_|.
     56   base::FilePathWatcher mandatory_watcher_;
     57   base::FilePathWatcher recommended_watcher_;
     58 
     59   DISALLOW_COPY_AND_ASSIGN(ConfigDirPolicyLoader);
     60 };
     61 
     62 }  // namespace policy
     63 
     64 #endif  // CHROME_BROWSER_POLICY_CONFIG_DIR_POLICY_LOADER_H_
     65