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