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