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