Chromium Code Reviews| Index: chrome/browser/policy/async_policy_loader.h |
| diff --git a/chrome/browser/policy/async_policy_loader.h b/chrome/browser/policy/async_policy_loader.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e2fdbff2a06534b1e9370602089c18256ce0efea |
| --- /dev/null |
| +++ b/chrome/browser/policy/async_policy_loader.h |
| @@ -0,0 +1,119 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_POLICY_ASYNC_POLICY_LOADER_H_ |
| +#define CHROME_BROWSER_POLICY_ASYNC_POLICY_LOADER_H_ |
| +#pragma once |
| + |
| +#include "base/callback.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/time.h" |
| + |
| +class FilePath; |
| + |
| +namespace base { |
| +namespace files { |
| +class FilePathWatcher; |
| +} // namespace files |
| +} // namespace base |
| + |
| +namespace policy { |
| + |
| +class PolicyBundle; |
| + |
| +// Base implementation for platform-specific policy loaders. Together with the |
| +// AsyncPolicyProvider, this base implementation takes care of the initial load, |
| +// periodic reloads, watching file changes, refreshing policies and object |
| +// lifetime. |
| +// |
| +// All methods are invoked on FILE, including the destructor. The only |
| +// exceptions are the constructor (which may be called on any thread), and the |
| +// initial Load() which is called on UI. LastModificationTime() is also invoked |
| +// once on UI at startup. |
| +class AsyncPolicyLoader { |
| + public: |
| + AsyncPolicyLoader(); |
| + virtual ~AsyncPolicyLoader(); |
| + |
| + // Returns the currently configured policies. Load() is always invoked on |
| + // FILE, except for the initial Load() at startup which is invoked from UI. |
|
Mattias Nissler (ping if slow)
2012/06/04 08:48:26
say FILE _thread_, otherwise this won't make any s
Joao da Silva
2012/06/04 14:05:06
Done.
|
| + virtual scoped_ptr<PolicyBundle> Load() = 0; |
| + |
| + // Allows implementations to finalize their initialization on the FILE |
| + // thread (e.g. setup file watchers with WatchPath()). |
| + virtual void InitOnFile() = 0; |
| + |
| + // Implementations should return the time of the last modification detected, |
| + // or base::Time() if it doesn't apply, which is the default. |
| + virtual base::Time LastModificationTime(); |
| + |
| + // Implementations should invoke Reload() when a change is detected. This |
| + // must be invoked from FILE and will trigger a Load(), and pass the |
| + // returned bundle to the provider. |
| + // The load is immediate when |force| is true. Otherwise, the loader |
| + // reschedules the reload until the LastModificationTime() is a couple of |
| + // seconds in the past. |
|
Mattias Nissler (ping if slow)
2012/06/04 08:48:26
explain why.
Joao da Silva
2012/06/04 14:05:06
Done.
|
| + // |
| + // A reload is posted periodically, and whenever changes are detected to |
| + // paths passed to WatchPath(). |
| + void Reload(bool force); |
| + |
| + // Starts watching |path| and posts Reload() whenever a change is detected. |
| + // Must be invoked on FILE. |
| + void WatchPath(const FilePath& path); |
|
Mattias Nissler (ping if slow)
2012/06/04 08:48:26
seems like this should be protected.
Joao da Silva
2012/06/04 14:05:06
Obsolete; this has been removed, as suggested else
|
| + |
| + private: |
| + // Allow AsyncPolicyProvider to call Init(). |
| + friend class AsyncPolicyProvider; |
| + |
| + typedef base::Callback<void(scoped_ptr<PolicyBundle>)> UpdateCallback; |
| + |
| + // Used by the AsyncPolicyProvider to do the initial Load(). The first load |
| + // is also used to initialize |last_modification_time_|. |
| + scoped_ptr<PolicyBundle> InitialLoad(); |
| + |
| + // Used by the AsyncPolicyProvider to install the |update_callback_|. |
| + // Invoked on FILE. |
| + void Init(const UpdateCallback& update_callback); |
| + |
| + // Cancels any pending periodic reload and posts one |delay| time units from |
| + // now. |
| + void ScheduleNextReload(base::TimeDelta delay); |
| + |
| + // Checks if the underlying files haven't changed recently, by checking the |
| + // LastModificationTime(). |delay| is updated with a suggested time to wait |
| + // before retrying when this returns false. |
| + bool IsSafeToReload(const base::Time& now, base::TimeDelta* delay); |
| + |
| + // Callback for updates, passed in Init(). |
| + UpdateCallback update_callback_; |
| + |
| + // |reload_weak_factory_| is used to get WeakPtrs for the periodic reload |
| + // tasks, and |watchers_weak_factory_| to get WeakPtrs for the update callback |
| + // passed to the |watchers_|. They're separate because |reload_weak_factory_| |
| + // is invalidated whenever a new reload is scheduled, while |
|
Mattias Nissler (ping if slow)
2012/06/04 08:48:26
use base/timer.h instead?
Joao da Silva
2012/06/04 14:05:06
The weak factory for the watchers is now gone. I t
|
| + // |watchers_weak_factory_| is valid until destruction of |this| object. |
| + base::WeakPtrFactory<AsyncPolicyLoader> reload_weak_factory_; |
| + base::WeakPtrFactory<AsyncPolicyLoader> watchers_weak_factory_; |
| + |
| + // Records last known modification timestamp. |
| + base::Time last_modification_time_; |
| + |
| + // The wall clock time at which the last modification timestamp was |
| + // recorded. It's better to not assume the file notification time and the |
| + // wall clock times come from the same source, just in case there is some |
| + // non-local filesystem involved. |
| + base::Time last_modification_clock_; |
| + |
| + // List of watchers, one per call to WatchPath. |
| + ScopedVector<base::files::FilePathWatcher> watchers_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AsyncPolicyLoader); |
| +}; |
| + |
| +} // namespace policy |
| + |
| +#endif // CHROME_BROWSER_POLICY_ASYNC_POLICY_LOADER_H_ |