| 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 #include "chrome/browser/policy/asynchronous_policy_loader.h" | 5 #include "chrome/browser/policy/asynchronous_policy_loader.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "chrome/browser/policy/policy_bundle.h" | 10 #include "chrome/browser/policy/policy_bundle.h" |
| 11 #include "chrome/browser/policy/policy_map.h" | |
| 12 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 13 | 12 |
| 14 using content::BrowserThread; | 13 using content::BrowserThread; |
| 15 | 14 |
| 16 namespace policy { | 15 namespace policy { |
| 17 | 16 |
| 18 AsynchronousPolicyLoader::AsynchronousPolicyLoader( | 17 AsynchronousPolicyLoader::AsynchronousPolicyLoader( |
| 19 AsynchronousPolicyProvider::Delegate* delegate, | 18 AsynchronousPolicyProvider::Delegate* delegate, |
| 20 int reload_interval_minutes) | 19 int reload_interval_minutes) |
| 21 : delegate_(delegate), | 20 : delegate_(delegate), |
| 22 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 21 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
| 23 reload_interval_(base::TimeDelta::FromMinutes(reload_interval_minutes)), | 22 reload_interval_(base::TimeDelta::FromMinutes(reload_interval_minutes)), |
| 24 origin_loop_(MessageLoop::current()), | 23 origin_loop_(MessageLoop::current()), |
| 25 stopped_(false) {} | 24 stopped_(false) {} |
| 26 | 25 |
| 27 void AsynchronousPolicyLoader::Init(const UpdateCallback& callback) { | 26 void AsynchronousPolicyLoader::Init(const UpdateCallback& callback) { |
| 28 update_callback_ = callback; | 27 update_callback_ = callback; |
| 29 | 28 |
| 30 // Load initial policy synchronously at startup. | 29 // Load initial policy synchronously at startup. |
| 31 scoped_ptr<PolicyMap> policy(delegate_->Load()); | 30 scoped_ptr<PolicyBundle> policy(delegate_->Load()); |
| 32 if (policy.get()) | 31 if (policy.get()) |
| 33 UpdatePolicy(policy.Pass()); | 32 UpdatePolicy(policy.Pass()); |
| 34 | 33 |
| 35 // Initialization can happen early when the file thread is not yet available, | 34 // Initialization can happen early when the file thread is not yet available, |
| 36 // but the subclass of the loader must do some of their initialization on the | 35 // but the subclass of the loader must do some of their initialization on the |
| 37 // file thread. Posting to the file thread directly before it is initialized | 36 // file thread. Posting to the file thread directly before it is initialized |
| 38 // will cause the task to be forgotten. Instead, post a task to the ui thread | 37 // will cause the task to be forgotten. Instead, post a task to the ui thread |
| 39 // to delay the remainder of initialization until threading is fully | 38 // to delay the remainder of initialization until threading is fully |
| 40 // initialized. | 39 // initialized. |
| 41 BrowserThread::PostTask( | 40 BrowserThread::PostTask( |
| 42 BrowserThread::UI, FROM_HERE, | 41 BrowserThread::UI, FROM_HERE, |
| 43 base::Bind(&AsynchronousPolicyLoader::InitAfterFileThreadAvailable, | 42 base::Bind(&AsynchronousPolicyLoader::InitAfterFileThreadAvailable, |
| 44 this)); | 43 this)); |
| 45 } | 44 } |
| 46 | 45 |
| 47 void AsynchronousPolicyLoader::Stop() { | 46 void AsynchronousPolicyLoader::Stop() { |
| 48 if (!stopped_) { | 47 if (!stopped_) { |
| 49 stopped_ = true; | 48 stopped_ = true; |
| 50 BrowserThread::PostTask( | 49 BrowserThread::PostTask( |
| 51 BrowserThread::FILE, FROM_HERE, | 50 BrowserThread::FILE, FROM_HERE, |
| 52 base::Bind(&AsynchronousPolicyLoader::StopOnFileThread, this)); | 51 base::Bind(&AsynchronousPolicyLoader::StopOnFileThread, this)); |
| 53 } | 52 } |
| 54 } | 53 } |
| 55 | 54 |
| 56 AsynchronousPolicyLoader::~AsynchronousPolicyLoader() { | 55 AsynchronousPolicyLoader::~AsynchronousPolicyLoader() { |
| 57 } | 56 } |
| 58 | 57 |
| 59 void AsynchronousPolicyLoader::Reload(bool force) { | 58 void AsynchronousPolicyLoader::Reload(bool force) { |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 61 if (delegate_.get()) { | 60 if (delegate_.get()) |
| 62 PostUpdatePolicyTask(delegate_->Load()); | 61 PostUpdatePolicyTask(delegate_->Load().Pass()); |
| 63 } | |
| 64 } | 62 } |
| 65 | 63 |
| 66 void AsynchronousPolicyLoader::CancelReloadTask() { | 64 void AsynchronousPolicyLoader::CancelReloadTask() { |
| 67 weak_ptr_factory_.InvalidateWeakPtrs(); | 65 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 68 } | 66 } |
| 69 | 67 |
| 70 void AsynchronousPolicyLoader::ScheduleReloadTask( | 68 void AsynchronousPolicyLoader::ScheduleReloadTask( |
| 71 const base::TimeDelta& delay) { | 69 const base::TimeDelta& delay) { |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 73 | 71 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 93 } | 91 } |
| 94 | 92 |
| 95 void AsynchronousPolicyLoader::InitOnFileThread() { | 93 void AsynchronousPolicyLoader::InitOnFileThread() { |
| 96 } | 94 } |
| 97 | 95 |
| 98 void AsynchronousPolicyLoader::StopOnFileThread() { | 96 void AsynchronousPolicyLoader::StopOnFileThread() { |
| 99 delegate_.reset(); | 97 delegate_.reset(); |
| 100 CancelReloadTask(); | 98 CancelReloadTask(); |
| 101 } | 99 } |
| 102 | 100 |
| 103 void AsynchronousPolicyLoader::PostUpdatePolicyTask(PolicyMap* new_policy) { | 101 void AsynchronousPolicyLoader::PostUpdatePolicyTask( |
| 104 scoped_ptr<PolicyMap> policy(new_policy); | 102 scoped_ptr<PolicyBundle> bundle) { |
| 105 origin_loop_->PostTask( | 103 origin_loop_->PostTask( |
| 106 FROM_HERE, | 104 FROM_HERE, |
| 107 base::Bind(&AsynchronousPolicyLoader::UpdatePolicy, | 105 base::Bind(&AsynchronousPolicyLoader::UpdatePolicy, |
| 108 this, base::Passed(&policy))); | 106 this, base::Passed(&bundle))); |
| 109 } | 107 } |
| 110 | 108 |
| 111 void AsynchronousPolicyLoader::UpdatePolicy(scoped_ptr<PolicyMap> policy) { | 109 void AsynchronousPolicyLoader::UpdatePolicy(scoped_ptr<PolicyBundle> bundle) { |
| 112 if (!stopped_) { | 110 if (!stopped_) |
| 113 // TODO(joaodasilva): make this load policy from other namespaces too. | |
| 114 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); | |
| 115 bundle->Get(POLICY_DOMAIN_CHROME, std::string()).Swap(policy.get()); | |
| 116 update_callback_.Run(bundle.Pass()); | 111 update_callback_.Run(bundle.Pass()); |
| 117 } | |
| 118 } | 112 } |
| 119 | 113 |
| 120 void AsynchronousPolicyLoader::InitAfterFileThreadAvailable() { | 114 void AsynchronousPolicyLoader::InitAfterFileThreadAvailable() { |
| 121 if (!stopped_) { | 115 if (!stopped_) { |
| 122 BrowserThread::PostTask( | 116 BrowserThread::PostTask( |
| 123 BrowserThread::FILE, FROM_HERE, | 117 BrowserThread::FILE, FROM_HERE, |
| 124 base::Bind(&AsynchronousPolicyLoader::InitOnFileThread, this)); | 118 base::Bind(&AsynchronousPolicyLoader::InitOnFileThread, this)); |
| 125 } | 119 } |
| 126 } | 120 } |
| 127 | 121 |
| 128 } // namespace policy | 122 } // namespace policy |
| OLD | NEW |