| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_ASYNCHRONOUS_POLICY_LOADER_H_ | 5 #ifndef CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_LOADER_H_ |
| 6 #define CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_LOADER_H_ | 6 #define CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_LOADER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/policy/asynchronous_policy_provider.h" | 15 #include "chrome/browser/policy/asynchronous_policy_provider.h" |
| 16 #include "chrome/browser/policy/configuration_policy_provider.h" | 16 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 17 | 17 |
| 18 class MessageLoop; | 18 class MessageLoop; |
| 19 | 19 |
| 20 namespace policy { | 20 namespace policy { |
| 21 | 21 |
| 22 // Used by the implementation of asynchronous policy provider to manage the | 22 // Used by the implementation of asynchronous policy provider to manage the |
| 23 // tasks on the FILE thread that do the heavy lifting of loading policies. | 23 // tasks on the FILE thread that do the heavy lifting of loading policies. |
| 24 class AsynchronousPolicyLoader | 24 class AsynchronousPolicyLoader |
| 25 : public base::RefCountedThreadSafe<AsynchronousPolicyLoader> { | 25 : public base::RefCountedThreadSafe<AsynchronousPolicyLoader> { |
| 26 public: | 26 public: |
| 27 AsynchronousPolicyLoader(AsynchronousPolicyProvider::Delegate* delegate, | 27 AsynchronousPolicyLoader(AsynchronousPolicyProvider::Delegate* delegate, |
| 28 int reload_interval_minutes); | 28 int reload_interval_minutes); |
| 29 | 29 |
| 30 // Triggers initial policy load, and installs |callback| as the callback to | 30 // Triggers initial policy load, and installs |default_callback| as the |
| 31 // invoke on policy updates. | 31 // callback to invoke on policy updates that haven't been requested. |
| 32 virtual void Init(const base::Closure& callback); | 32 virtual void Init(const base::Closure& default_callback); |
| 33 | 33 |
| 34 // Reloads policy, sending notification of changes if necessary. Must be | 34 // Reloads policy and invokes |callback| on |origin_loop_| when done. |
| 35 // called on the FILE thread. When |force| is true, the loader should do an | 35 // The loader does a full reload immediately when |force| is true. |
| 36 // immediate full reload. | 36 // |callback| is guaranteed to be eventually invoked, even if the loading |
| 37 virtual void Reload(bool force); | 37 // fails. This method must be called on the FILE thread. |
| 38 virtual void Reload(const base::Closure& callback, bool force); |
| 38 | 39 |
| 39 // Stops any pending reload tasks. Updates callbacks won't be performed | 40 // Stops any pending reload tasks. Updates callbacks won't be performed |
| 40 // anymore once the loader is stopped. | 41 // anymore once the loader is stopped. |
| 41 virtual void Stop(); | 42 virtual void Stop(); |
| 42 | 43 |
| 43 const DictionaryValue* policy() const { return policy_.get(); } | 44 const DictionaryValue* policy() const { return policy_.get(); } |
| 44 | 45 |
| 45 protected: | 46 protected: |
| 46 // AsynchronousPolicyLoader objects should only be deleted by | 47 // AsynchronousPolicyLoader objects should only be deleted by |
| 47 // RefCountedThreadSafe. | 48 // RefCountedThreadSafe. |
| 48 friend class base::RefCountedThreadSafe<AsynchronousPolicyLoader>; | 49 friend class base::RefCountedThreadSafe<AsynchronousPolicyLoader>; |
| 49 virtual ~AsynchronousPolicyLoader(); | 50 virtual ~AsynchronousPolicyLoader(); |
| 50 | 51 |
| 51 // Schedules a call to UpdatePolicy on |origin_loop_|. Takes ownership of | 52 // Schedules a call to UpdatePolicy on |origin_loop_|. Takes ownership of |
| 52 // |new_policy|. | 53 // |new_policy|. |
| 53 void PostUpdatePolicyTask(DictionaryValue* new_policy); | 54 void PostUpdatePolicyTask(const base::Closure& callback, |
| 55 DictionaryValue* new_policy); |
| 54 | 56 |
| 55 AsynchronousPolicyProvider::Delegate* delegate() { | 57 AsynchronousPolicyProvider::Delegate* delegate() { |
| 56 return delegate_.get(); | 58 return delegate_.get(); |
| 57 } | 59 } |
| 58 | 60 |
| 61 const base::Closure& default_callback() { return default_callback_; } |
| 62 |
| 59 // Performs start operations that must be performed on the FILE thread. | 63 // Performs start operations that must be performed on the FILE thread. |
| 60 virtual void InitOnFileThread(); | 64 virtual void InitOnFileThread(); |
| 61 | 65 |
| 62 // Performs stop operations that must be performed on the FILE thread. | 66 // Performs stop operations that must be performed on the FILE thread. |
| 63 virtual void StopOnFileThread(); | 67 virtual void StopOnFileThread(); |
| 64 | 68 |
| 65 // Schedules a reload task to run when |delay| expires. Must be called on the | 69 // Schedules a reload task to run when |delay| expires. Must be called on the |
| 66 // FILE thread. | 70 // FILE thread. |
| 67 void ScheduleReloadTask(const base::TimeDelta& delay); | 71 void ScheduleReloadTask(const base::Closure& callback, |
| 72 const base::TimeDelta& delay); |
| 68 | 73 |
| 69 // Schedules a reload task to run after the number of minutes specified | 74 // Schedules a reload task to run after the number of minutes specified |
| 70 // in |reload_interval_minutes_|. Must be called on the FILE thread. | 75 // in |reload_interval_minutes_|. Must be called on the FILE thread. |
| 71 void ScheduleFallbackReloadTask(); | 76 void ScheduleFallbackReloadTask(); |
| 72 | 77 |
| 73 void CancelReloadTask(); | 78 void CancelReloadTask(); |
| 74 | 79 |
| 75 // Invoked from the reload task on the FILE thread. | |
| 76 void ReloadFromTask(); | |
| 77 | |
| 78 private: | 80 private: |
| 79 friend class AsynchronousPolicyLoaderTest; | 81 friend class AsynchronousPolicyLoaderTest; |
| 80 | 82 |
| 81 // Finishes loader initialization after the threading system has been fully | 83 // Finishes loader initialization after the threading system has been fully |
| 82 // intialized. | 84 // intialized. |
| 83 void InitAfterFileThreadAvailable(); | 85 void InitAfterFileThreadAvailable(); |
| 84 | 86 |
| 85 // Replaces the existing policy to value map with a new one, sending | 87 // Replaces the existing policy to value map with a new one, and invokes |
| 86 // notification to the observers if there is a policy change. Must be called | 88 // |callback|. Must be called on |origin_loop_| so that it's safe to callback |
| 87 // on |origin_loop_| so that it's safe to call back into the provider, which | 89 // into the provider, which is not thread-safe. Takes ownership of |
| 88 // is not thread-safe. Takes ownership of |new_policy|. | 90 // |new_policy|. |
| 89 void UpdatePolicy(DictionaryValue* new_policy); | 91 void UpdatePolicy(const base::Closure& callback, DictionaryValue* new_policy); |
| 90 | 92 |
| 91 // Provides the low-level mechanics for loading policy. | 93 // Provides the low-level mechanics for loading policy. |
| 92 scoped_ptr<AsynchronousPolicyProvider::Delegate> delegate_; | 94 scoped_ptr<AsynchronousPolicyProvider::Delegate> delegate_; |
| 93 | 95 |
| 94 // Current policy. | 96 // Current policy. |
| 95 scoped_ptr<DictionaryValue> policy_; | 97 scoped_ptr<DictionaryValue> policy_; |
| 96 | 98 |
| 97 // Used to create and invalidate WeakPtrs on the FILE thread. These are only | 99 // Used to create and invalidate WeakPtrs on the FILE thread. These are only |
| 98 // used to post reload tasks that can be cancelled. | 100 // used to post reload tasks that can be cancelled. |
| 99 base::WeakPtrFactory<AsynchronousPolicyLoader> weak_ptr_factory_; | 101 base::WeakPtrFactory<AsynchronousPolicyLoader> weak_ptr_factory_; |
| 100 | 102 |
| 101 // The interval at which a policy reload will be triggered as a fallback. | 103 // The interval at which a policy reload will be triggered as a fallback. |
| 102 const base::TimeDelta reload_interval_; | 104 const base::TimeDelta reload_interval_; |
| 103 | 105 |
| 104 // The message loop on which this object was constructed. Recorded so that | 106 // The message loop on which this object was constructed. Recorded so that |
| 105 // it's possible to call back into the non thread safe provider to fire the | 107 // it's possible to call back into the non thread safe provider to fire the |
| 106 // notification. | 108 // notification. |
| 107 MessageLoop* origin_loop_; | 109 MessageLoop* origin_loop_; |
| 108 | 110 |
| 109 // True if Stop has been called. | 111 // True if Stop has been called. |
| 110 bool stopped_; | 112 bool stopped_; |
| 111 | 113 |
| 112 // Callback to invoke on policy updates. | 114 // Default callback to invoke on policy updates that weren't explicitly |
| 113 base::Closure updates_callback_; | 115 // requested by the provider. |
| 116 base::Closure default_callback_; |
| 114 | 117 |
| 115 DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyLoader); | 118 DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyLoader); |
| 116 }; | 119 }; |
| 117 | 120 |
| 118 } // namespace policy | 121 } // namespace policy |
| 119 | 122 |
| 120 #endif // CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_LOADER_H_ | 123 #endif // CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_LOADER_H_ |
| OLD | NEW |