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