| 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/asynchronous_policy_loader.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "chrome/browser/policy/policy_bundle.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 | |
| 13 using content::BrowserThread; | |
| 14 | |
| 15 namespace policy { | |
| 16 | |
| 17 AsynchronousPolicyLoader::AsynchronousPolicyLoader( | |
| 18 AsynchronousPolicyProvider::Delegate* delegate, | |
| 19 int reload_interval_minutes) | |
| 20 : delegate_(delegate), | |
| 21 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | |
| 22 reload_interval_(base::TimeDelta::FromMinutes(reload_interval_minutes)), | |
| 23 origin_loop_(MessageLoop::current()), | |
| 24 stopped_(false) {} | |
| 25 | |
| 26 void AsynchronousPolicyLoader::Init(const UpdateCallback& callback) { | |
| 27 update_callback_ = callback; | |
| 28 | |
| 29 // Load initial policy synchronously at startup. | |
| 30 scoped_ptr<PolicyBundle> policy(delegate_->Load()); | |
| 31 if (policy.get()) | |
| 32 UpdatePolicy(policy.Pass()); | |
| 33 | |
| 34 // Initialization can happen early when the file thread is not yet available, | |
| 35 // but the subclass of the loader must do some of their initialization on the | |
| 36 // file thread. Posting to the file thread directly before it is initialized | |
| 37 // will cause the task to be forgotten. Instead, post a task to the ui thread | |
| 38 // to delay the remainder of initialization until threading is fully | |
| 39 // initialized. | |
| 40 BrowserThread::PostTask( | |
| 41 BrowserThread::UI, FROM_HERE, | |
| 42 base::Bind(&AsynchronousPolicyLoader::InitAfterFileThreadAvailable, | |
| 43 this)); | |
| 44 } | |
| 45 | |
| 46 void AsynchronousPolicyLoader::Stop() { | |
| 47 if (!stopped_) { | |
| 48 stopped_ = true; | |
| 49 BrowserThread::PostTask( | |
| 50 BrowserThread::FILE, FROM_HERE, | |
| 51 base::Bind(&AsynchronousPolicyLoader::StopOnFileThread, this)); | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 AsynchronousPolicyLoader::~AsynchronousPolicyLoader() { | |
| 56 } | |
| 57 | |
| 58 void AsynchronousPolicyLoader::Reload(bool force) { | |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 60 if (delegate_.get()) | |
| 61 PostUpdatePolicyTask(delegate_->Load().Pass()); | |
| 62 } | |
| 63 | |
| 64 void AsynchronousPolicyLoader::CancelReloadTask() { | |
| 65 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 66 } | |
| 67 | |
| 68 void AsynchronousPolicyLoader::ScheduleReloadTask( | |
| 69 const base::TimeDelta& delay) { | |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 71 | |
| 72 CancelReloadTask(); | |
| 73 | |
| 74 BrowserThread::PostDelayedTask( | |
| 75 BrowserThread::FILE, FROM_HERE, | |
| 76 base::Bind(&AsynchronousPolicyLoader::ReloadFromTask, | |
| 77 weak_ptr_factory_.GetWeakPtr()), | |
| 78 delay); | |
| 79 } | |
| 80 | |
| 81 void AsynchronousPolicyLoader::ScheduleFallbackReloadTask() { | |
| 82 // As a safeguard in case that the load delegate failed to timely notice a | |
| 83 // change in policy, schedule a reload task that'll make us recheck after a | |
| 84 // reasonable interval. | |
| 85 ScheduleReloadTask(reload_interval_); | |
| 86 } | |
| 87 | |
| 88 void AsynchronousPolicyLoader::ReloadFromTask() { | |
| 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 90 Reload(false); | |
| 91 } | |
| 92 | |
| 93 void AsynchronousPolicyLoader::InitOnFileThread() { | |
| 94 } | |
| 95 | |
| 96 void AsynchronousPolicyLoader::StopOnFileThread() { | |
| 97 delegate_.reset(); | |
| 98 CancelReloadTask(); | |
| 99 } | |
| 100 | |
| 101 void AsynchronousPolicyLoader::PostUpdatePolicyTask( | |
| 102 scoped_ptr<PolicyBundle> bundle) { | |
| 103 origin_loop_->PostTask( | |
| 104 FROM_HERE, | |
| 105 base::Bind(&AsynchronousPolicyLoader::UpdatePolicy, | |
| 106 this, base::Passed(&bundle))); | |
| 107 } | |
| 108 | |
| 109 void AsynchronousPolicyLoader::UpdatePolicy(scoped_ptr<PolicyBundle> bundle) { | |
| 110 if (!stopped_) | |
| 111 update_callback_.Run(bundle.Pass()); | |
| 112 } | |
| 113 | |
| 114 void AsynchronousPolicyLoader::InitAfterFileThreadAvailable() { | |
| 115 if (!stopped_) { | |
| 116 BrowserThread::PostTask( | |
| 117 BrowserThread::FILE, FROM_HERE, | |
| 118 base::Bind(&AsynchronousPolicyLoader::InitOnFileThread, this)); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 } // namespace policy | |
| OLD | NEW |