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/async_policy_provider.h" | 5 #include "chrome/browser/policy/async_policy_provider.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/location.h" | 9 #include "base/location.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
12 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
13 #include "chrome/browser/policy/async_policy_loader.h" | 13 #include "chrome/browser/policy/async_policy_loader.h" |
14 #include "chrome/browser/policy/policy_bundle.h" | 14 #include "chrome/browser/policy/policy_bundle.h" |
15 #include "chrome/browser/policy/policy_domain_descriptor.h" | 15 #include "chrome/browser/policy/schema_registry.h" |
16 | 16 |
17 namespace policy { | 17 namespace policy { |
18 | 18 |
19 AsyncPolicyProvider::AsyncPolicyProvider(scoped_ptr<AsyncPolicyLoader> loader) | 19 AsyncPolicyProvider::AsyncPolicyProvider( |
| 20 SchemaRegistry* registry, |
| 21 scoped_ptr<AsyncPolicyLoader> loader) |
20 : loader_(loader.release()), | 22 : loader_(loader.release()), |
21 weak_factory_(this) { | 23 weak_factory_(this) { |
22 // Make an immediate synchronous load on startup. | 24 // Make an immediate synchronous load on startup. |
23 OnLoaderReloaded(loader_->InitialLoad()); | 25 OnLoaderReloaded(loader_->InitialLoad(registry->schema_map())); |
24 } | 26 } |
25 | 27 |
26 AsyncPolicyProvider::~AsyncPolicyProvider() { | 28 AsyncPolicyProvider::~AsyncPolicyProvider() { |
27 DCHECK(CalledOnValidThread()); | 29 DCHECK(CalledOnValidThread()); |
28 // Shutdown() must have been called before. | 30 // Shutdown() must have been called before. |
29 DCHECK(!loader_); | 31 DCHECK(!loader_); |
30 } | 32 } |
31 | 33 |
32 void AsyncPolicyProvider::Init() { | 34 void AsyncPolicyProvider::Init(SchemaRegistry* registry) { |
33 DCHECK(CalledOnValidThread()); | 35 DCHECK(CalledOnValidThread()); |
34 ConfigurationPolicyProvider::Init(); | 36 ConfigurationPolicyProvider::Init(registry); |
35 | 37 |
36 if (!loader_) | 38 if (!loader_) |
37 return; | 39 return; |
38 | 40 |
39 AsyncPolicyLoader::UpdateCallback callback = | 41 AsyncPolicyLoader::UpdateCallback callback = |
40 base::Bind(&AsyncPolicyProvider::LoaderUpdateCallback, | 42 base::Bind(&AsyncPolicyProvider::LoaderUpdateCallback, |
41 base::MessageLoopProxy::current(), | 43 base::MessageLoopProxy::current(), |
42 weak_factory_.GetWeakPtr()); | 44 weak_factory_.GetWeakPtr()); |
43 bool post = loader_->task_runner()->PostTask( | 45 bool post = loader_->task_runner()->PostTask( |
44 FROM_HERE, | 46 FROM_HERE, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 return; | 84 return; |
83 refresh_callback_.Reset( | 85 refresh_callback_.Reset( |
84 base::Bind(&AsyncPolicyProvider::ReloadAfterRefreshSync, | 86 base::Bind(&AsyncPolicyProvider::ReloadAfterRefreshSync, |
85 weak_factory_.GetWeakPtr())); | 87 weak_factory_.GetWeakPtr())); |
86 loader_->task_runner()->PostTaskAndReply( | 88 loader_->task_runner()->PostTaskAndReply( |
87 FROM_HERE, | 89 FROM_HERE, |
88 base::Bind(base::DoNothing), | 90 base::Bind(base::DoNothing), |
89 refresh_callback_.callback()); | 91 refresh_callback_.callback()); |
90 } | 92 } |
91 | 93 |
92 void AsyncPolicyProvider::RegisterPolicyDomain( | |
93 scoped_refptr<const PolicyDomainDescriptor> descriptor) { | |
94 if (loader_) { | |
95 loader_->task_runner()->PostTask( | |
96 FROM_HERE, | |
97 base::Bind(&AsyncPolicyLoader::RegisterPolicyDomain, | |
98 base::Unretained(loader_), | |
99 descriptor)); | |
100 } | |
101 } | |
102 | |
103 void AsyncPolicyProvider::ReloadAfterRefreshSync() { | 94 void AsyncPolicyProvider::ReloadAfterRefreshSync() { |
104 DCHECK(CalledOnValidThread()); | 95 DCHECK(CalledOnValidThread()); |
105 // This task can only enter if it was posted from RefreshPolicies(), and it | 96 // This task can only enter if it was posted from RefreshPolicies(), and it |
106 // hasn't been cancelled meanwhile by another call to RefreshPolicies(). | 97 // hasn't been cancelled meanwhile by another call to RefreshPolicies(). |
107 DCHECK(!refresh_callback_.IsCancelled()); | 98 DCHECK(!refresh_callback_.IsCancelled()); |
108 // There can't be another refresh callback pending now, since its creation | 99 // There can't be another refresh callback pending now, since its creation |
109 // in RefreshPolicies() would have cancelled the current execution. So it's | 100 // in RefreshPolicies() would have cancelled the current execution. So it's |
110 // safe to cancel the |refresh_callback_| now, so that OnLoaderReloaded() | 101 // safe to cancel the |refresh_callback_| now, so that OnLoaderReloaded() |
111 // sees that there is no refresh pending. | 102 // sees that there is no refresh pending. |
112 refresh_callback_.Cancel(); | 103 refresh_callback_.Cancel(); |
113 | 104 |
114 if (!loader_) | 105 if (!loader_) |
115 return; | 106 return; |
116 | 107 |
117 loader_->task_runner()->PostTask(FROM_HERE, | 108 loader_->task_runner()->PostTask( |
118 base::Bind(&AsyncPolicyLoader::Reload, | 109 FROM_HERE, |
119 base::Unretained(loader_), | 110 base::Bind(&AsyncPolicyLoader::RefreshPolicies, |
120 true /* force */)); | 111 base::Unretained(loader_), |
| 112 schema_map())); |
121 } | 113 } |
122 | 114 |
123 void AsyncPolicyProvider::OnLoaderReloaded(scoped_ptr<PolicyBundle> bundle) { | 115 void AsyncPolicyProvider::OnLoaderReloaded(scoped_ptr<PolicyBundle> bundle) { |
124 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
125 // Only propagate policy updates if there are no pending refreshes, and if | 117 // Only propagate policy updates if there are no pending refreshes, and if |
126 // Shutdown() hasn't been called yet. | 118 // Shutdown() hasn't been called yet. |
127 if (refresh_callback_.IsCancelled() && loader_) | 119 if (refresh_callback_.IsCancelled() && loader_) |
128 UpdatePolicy(bundle.Pass()); | 120 UpdatePolicy(bundle.Pass()); |
129 } | 121 } |
130 | 122 |
131 // static | 123 // static |
132 void AsyncPolicyProvider::LoaderUpdateCallback( | 124 void AsyncPolicyProvider::LoaderUpdateCallback( |
133 scoped_refptr<base::MessageLoopProxy> loop, | 125 scoped_refptr<base::MessageLoopProxy> loop, |
134 base::WeakPtr<AsyncPolicyProvider> weak_this, | 126 base::WeakPtr<AsyncPolicyProvider> weak_this, |
135 scoped_ptr<PolicyBundle> bundle) { | 127 scoped_ptr<PolicyBundle> bundle) { |
136 loop->PostTask(FROM_HERE, | 128 loop->PostTask(FROM_HERE, |
137 base::Bind(&AsyncPolicyProvider::OnLoaderReloaded, | 129 base::Bind(&AsyncPolicyProvider::OnLoaderReloaded, |
138 weak_this, | 130 weak_this, |
139 base::Passed(&bundle))); | 131 base::Passed(&bundle))); |
140 } | 132 } |
141 | 133 |
142 } // namespace policy | 134 } // namespace policy |
OLD | NEW |