xref: /aosp_15_r20/external/libchrome/components/policy/core/common/async_policy_loader.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1 // Copyright 2013 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_ASYNC_POLICY_LOADER_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_ASYNC_POLICY_LOADER_H_
7 
8 #include <memory>
9 
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h"
15 #include "components/policy/core/common/schema_map.h"
16 #include "components/policy/policy_export.h"
17 
18 namespace base {
19 class SequencedTaskRunner;
20 }
21 
22 namespace policy {
23 
24 class PolicyBundle;
25 
26 // Base implementation for platform-specific policy loaders. Together with the
27 // AsyncPolicyProvider, this base implementation takes care of the initial load,
28 // periodic reloads, watching file changes, refreshing policies and object
29 // lifetime.
30 //
31 // All methods are invoked on the background |task_runner_|, including the
32 // destructor. The only exceptions are the constructor (which may be called on
33 // any thread), InitialLoad() which is called on the thread that owns the
34 // provider and the calls of Load() and LastModificationTime() during the
35 // initial load.
36 // Also, during tests the destructor may be called on the main thread.
37 class POLICY_EXPORT AsyncPolicyLoader {
38  public:
39   explicit AsyncPolicyLoader(
40       const scoped_refptr<base::SequencedTaskRunner>& task_runner);
41   virtual ~AsyncPolicyLoader();
42 
43   // Gets a SequencedTaskRunner backed by the background thread.
task_runner()44   base::SequencedTaskRunner* task_runner() const { return task_runner_.get(); }
45 
46   // Returns the currently configured policies. Load() is always invoked on
47   // the background thread, except for the initial Load() at startup which is
48   // invoked from the thread that owns the provider.
49   virtual std::unique_ptr<PolicyBundle> Load() = 0;
50 
51   // Allows implementations to finalize their initialization on the background
52   // thread (e.g. setup file watchers).
53   virtual void InitOnBackgroundThread() = 0;
54 
55   // Implementations should return the time of the last modification detected,
56   // or base::Time() if it doesn't apply, which is the default.
57   virtual base::Time LastModificationTime();
58 
59   // Used by the AsyncPolicyProvider to do the initial Load(). The first load
60   // is also used to initialize |last_modification_time_| and
61   // |schema_map_|.
62   std::unique_ptr<PolicyBundle> InitialLoad(
63       const scoped_refptr<SchemaMap>& schemas);
64 
65   // Implementations should invoke Reload() when a change is detected. This
66   // must be invoked from the background thread and will trigger a Load(),
67   // and pass the returned bundle to the provider.
68   // The load is immediate when |force| is true. Otherwise, the loader
69   // reschedules the reload until the LastModificationTime() is a couple of
70   // seconds in the past. This mitigates the problem of reading files that are
71   // currently being written to, and whose contents are incomplete.
72   // A reload is posted periodically, if it hasn't been triggered recently. This
73   // makes sure the policies are reloaded if the update events aren't triggered.
74   void Reload(bool force);
75 
schema_map()76   const scoped_refptr<SchemaMap>& schema_map() const { return schema_map_; }
77 
78  private:
79   // Allow AsyncPolicyProvider to call Init().
80   friend class AsyncPolicyProvider;
81 
82   typedef base::Callback<void(std::unique_ptr<PolicyBundle>)> UpdateCallback;
83 
84   // Used by the AsyncPolicyProvider to install the |update_callback_|.
85   // Invoked on the background thread.
86   void Init(const UpdateCallback& update_callback);
87 
88   // Used by the AsyncPolicyProvider to reload with an updated SchemaMap.
89   void RefreshPolicies(scoped_refptr<SchemaMap> schema_map);
90 
91   // Cancels any pending periodic reload and posts one |delay| time units from
92   // now.
93   void ScheduleNextReload(base::TimeDelta delay);
94 
95   // Checks if the underlying files haven't changed recently, by checking the
96   // LastModificationTime(). |delay| is updated with a suggested time to wait
97   // before retrying when this returns false.
98   bool IsSafeToReload(const base::Time& now, base::TimeDelta* delay);
99 
100   // Task runner for running background jobs.
101   const scoped_refptr<base::SequencedTaskRunner> task_runner_;
102 
103   // Callback for updates, passed in Init().
104   UpdateCallback update_callback_;
105 
106   // Records last known modification timestamp.
107   base::Time last_modification_time_;
108 
109   // The wall clock time at which the last modification timestamp was
110   // recorded.  It's better to not assume the file notification time and the
111   // wall clock times come from the same source, just in case there is some
112   // non-local filesystem involved.
113   base::Time last_modification_clock_;
114 
115   // The current policy schemas that this provider should load.
116   scoped_refptr<SchemaMap> schema_map_;
117 
118   // Used to get WeakPtrs for the periodic reload task.
119   base::WeakPtrFactory<AsyncPolicyLoader> weak_factory_{this};
120 
121   DISALLOW_COPY_AND_ASSIGN(AsyncPolicyLoader);
122 };
123 
124 }  // namespace policy
125 
126 #endif  // COMPONENTS_POLICY_CORE_COMMON_ASYNC_POLICY_LOADER_H_
127