| 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 #include "chrome/browser/policy/async_policy_provider.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/message_loop_proxy.h" |
| 11 #include "chrome/browser/policy/async_policy_loader.h" |
| 12 #include "chrome/browser/policy/policy_bundle.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 |
| 15 using content::BrowserThread; |
| 16 |
| 17 namespace policy { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Helper for a PostTaskAndReply used as a synchronization point between the |
| 22 // main thread and FILE thread. See AsyncPolicyProvider::RefreshPolicies. |
| 23 void Nop() {} |
| 24 |
| 25 } // namespace |
| 26 |
| 27 AsyncPolicyProvider::AsyncPolicyProvider( |
| 28 const PolicyDefinitionList* policy_list, |
| 29 scoped_ptr<AsyncPolicyLoader> loader) |
| 30 : ConfigurationPolicyProvider(policy_list), |
| 31 loader_(loader.release()), |
| 32 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 33 // The FILE thread isn't ready early during startup. Post a task to the |
| 34 // current loop to resume initialization on FILE once the loops are spinning. |
| 35 MessageLoop::current()->PostTask( |
| 36 FROM_HERE, |
| 37 base::Bind(&AsyncPolicyProvider::InitWithLoopsReady, |
| 38 weak_factory_.GetWeakPtr())); |
| 39 // Make an immediate synchronous load on startup. |
| 40 OnLoaderReloaded(loader_->InitialLoad()); |
| 41 } |
| 42 |
| 43 AsyncPolicyProvider::~AsyncPolicyProvider() { |
| 44 DCHECK(CalledOnValidThread()); |
| 45 |
| 46 // Note on the lifetime of |loader_|: |
| 47 // The |loader_| lives on the FILE thread, and is deleted from here. This |
| 48 // means that posting tasks on the |loader_| to FILE from the |
| 49 // AsyncPolicyProvider is always safe, since a potential DeleteSoon() is only |
| 50 // posted from here. The |loader_| posts back to the AsyncPolicyProvider |
| 51 // through the |update_callback_|, which has a WeakPtr to |this|. |
| 52 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, loader_); |
| 53 } |
| 54 |
| 55 void AsyncPolicyProvider::RefreshPolicies() { |
| 56 DCHECK(CalledOnValidThread()); |
| 57 |
| 58 // Subtle: RefreshPolicies() has a contract that requires the next policy |
| 59 // update notification (triggered from UpdatePolicy()) to reflect any changes |
| 60 // made before this call. So if a caller has modified the policy settings and |
| 61 // invoked RefreshPolicies(), then by the next notification these policies |
| 62 // should already be provided. |
| 63 // However, it's also possible that an asynchronous Reload() is in progress |
| 64 // and just posted OnLoaderReloaded(). Therefore a task is posted to the |
| 65 // FILE thread before posting the next Reload, to prevent a potential |
| 66 // concurrent Reload() from triggering a notification too early. If another |
| 67 // refresh task has been posted, it is invalidated now. |
| 68 refresh_callback_.Reset( |
| 69 base::Bind(&AsyncPolicyProvider::ReloadAfterRefreshSync, |
| 70 base::Unretained(this))); |
| 71 BrowserThread::PostTaskAndReply( |
| 72 BrowserThread::FILE, FROM_HERE, |
| 73 base::Bind(Nop), |
| 74 refresh_callback_.callback()); |
| 75 } |
| 76 |
| 77 void AsyncPolicyProvider::InitWithLoopsReady() { |
| 78 DCHECK(CalledOnValidThread()); |
| 79 BrowserThread::PostTask( |
| 80 BrowserThread::FILE, FROM_HERE, |
| 81 base::Bind(&AsyncPolicyLoader::Init, |
| 82 base::Unretained(loader_), |
| 83 base::MessageLoopProxy::current(), |
| 84 base::Bind(&AsyncPolicyProvider::OnLoaderReloaded, |
| 85 weak_factory_.GetWeakPtr()))); |
| 86 } |
| 87 |
| 88 void AsyncPolicyProvider::ReloadAfterRefreshSync() { |
| 89 DCHECK(CalledOnValidThread()); |
| 90 // This task can only enter if it was posted from RefreshPolicies(), and it |
| 91 // hasn't been cancelled meanwhile by another call to RefreshPolicies(). |
| 92 DCHECK(!refresh_callback_.IsCancelled()); |
| 93 // There can't be another refresh callback pending now, since its creation |
| 94 // in RefreshPolicies() would have cancelled the current execution. So it's |
| 95 // safe to cancel the |refresh_callback_| now, so that OnLoaderReloaded() |
| 96 // sees that there is no refresh pending. |
| 97 refresh_callback_.Cancel(); |
| 98 |
| 99 BrowserThread::PostTask( |
| 100 BrowserThread::FILE, FROM_HERE, |
| 101 base::Bind(&AsyncPolicyLoader::Reload, |
| 102 base::Unretained(loader_), |
| 103 true /* force */)); |
| 104 } |
| 105 |
| 106 void AsyncPolicyProvider::OnLoaderReloaded(scoped_ptr<PolicyBundle> bundle) { |
| 107 DCHECK(CalledOnValidThread()); |
| 108 if (refresh_callback_.IsCancelled()) |
| 109 UpdatePolicy(bundle.Pass()); |
| 110 } |
| 111 |
| 112 } // namespace policy |
| OLD | NEW |