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