xref: /aosp_15_r20/external/libchrome/components/policy/core/common/config_dir_policy_loader.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
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 COMPONENTS_POLICY_CORE_COMMON_CONFIG_DIR_POLICY_LOADER_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_CONFIG_DIR_POLICY_LOADER_H_
7 
8 #include "base/files/file_path.h"
9 #include "base/files/file_path_watcher.h"
10 #include "base/macros.h"
11 #include "base/sequenced_task_runner.h"
12 #include "components/policy/core/common/async_policy_loader.h"
13 #include "components/policy/core/common/policy_types.h"
14 #include "components/policy/policy_export.h"
15 
16 namespace base {
17 class Value;
18 }
19 
20 namespace policy {
21 
22 // A policy loader implementation backed by a set of files in a given
23 // directory. The files should contain JSON-formatted policy settings. They are
24 // merged together and the result is returned in a PolicyBundle.
25 // The files are consulted in lexicographic file name order, so the
26 // last value read takes precedence in case of policy key collisions.
27 class POLICY_EXPORT ConfigDirPolicyLoader : public AsyncPolicyLoader {
28  public:
29   ConfigDirPolicyLoader(scoped_refptr<base::SequencedTaskRunner> task_runner,
30                         const base::FilePath& config_dir,
31                         PolicyScope scope);
32   ~ConfigDirPolicyLoader() override;
33 
34   // AsyncPolicyLoader implementation.
35   void InitOnBackgroundThread() override;
36   std::unique_ptr<PolicyBundle> Load() override;
37   base::Time LastModificationTime() override;
38 
39  private:
40   // Loads the policy files at |path| into the |bundle|, with the given |level|.
41   void LoadFromPath(const base::FilePath& path,
42                     PolicyLevel level,
43                     PolicyBundle* bundle);
44 
45   // Merges the 3rd party |policies| into the |bundle|, with the given |level|.
46   void Merge3rdPartyPolicy(const base::Value* policies,
47                            PolicyLevel level,
48                            PolicyBundle* bundle);
49 
50   // Callback for the FilePathWatchers.
51   void OnFileUpdated(const base::FilePath& path, bool error);
52 
53   // Task runner for running background jobs.
54   const scoped_refptr<base::SequencedTaskRunner> task_runner_;
55 
56   // The directory containing the policy files.
57   const base::FilePath config_dir_;
58 
59   // Policies loaded by this provider will have this scope.
60   const PolicyScope scope_;
61 
62   // Watchers for events on the mandatory and recommended subdirectories of
63   // |config_dir_|.
64   base::FilePathWatcher mandatory_watcher_;
65   base::FilePathWatcher recommended_watcher_;
66 
67   DISALLOW_COPY_AND_ASSIGN(ConfigDirPolicyLoader);
68 };
69 
70 }  // namespace policy
71 
72 #endif  // COMPONENTS_POLICY_CORE_COMMON_CONFIG_DIR_POLICY_LOADER_H_
73