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_provider.h" | 5 #include "chrome/browser/policy/asynchronous_policy_provider.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "chrome/browser/policy/asynchronous_policy_loader.h" | 8 #include "chrome/browser/policy/asynchronous_policy_loader.h" |
9 #include "chrome/browser/policy/policy_map.h" | 9 #include "chrome/browser/policy/policy_bundle.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
11 | 11 |
12 using content::BrowserThread; | 12 using content::BrowserThread; |
13 | 13 |
14 namespace policy { | 14 namespace policy { |
15 | 15 |
16 AsynchronousPolicyProvider::AsynchronousPolicyProvider( | 16 AsynchronousPolicyProvider::AsynchronousPolicyProvider( |
17 const PolicyDefinitionList* policy_list, | 17 const PolicyDefinitionList* policy_list, |
18 scoped_refptr<AsynchronousPolicyLoader> loader) | 18 scoped_refptr<AsynchronousPolicyLoader> loader) |
19 : ConfigurationPolicyProvider(policy_list), | 19 : ConfigurationPolicyProvider(policy_list), |
20 loader_(loader), | 20 loader_(loader), |
21 pending_refreshes_(0), | 21 pending_refreshes_(0), |
22 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 22 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
23 loader_->Init( | 23 loader_->Init( |
24 base::Bind(&AsynchronousPolicyProvider::OnLoaderReloaded, | 24 base::Bind(&AsynchronousPolicyProvider::OnLoaderReloaded, |
25 base::Unretained(this))); | 25 base::Unretained(this))); |
26 } | 26 } |
27 | 27 |
28 AsynchronousPolicyProvider::~AsynchronousPolicyProvider() { | 28 AsynchronousPolicyProvider::~AsynchronousPolicyProvider() { |
29 DCHECK(CalledOnValidThread()); | 29 DCHECK(CalledOnValidThread()); |
30 // |loader_| won't invoke its callback anymore after Stop(), therefore | 30 // |loader_| won't invoke its callback anymore after Stop(), therefore |
31 // Unretained(this) is safe in the ctor. | 31 // Unretained(this) is safe in the ctor. |
32 loader_->Stop(); | 32 loader_->Stop(); |
33 } | 33 } |
34 | 34 |
35 bool AsynchronousPolicyProvider::ProvideInternal(PolicyMap* map) { | |
36 DCHECK(CalledOnValidThread()); | |
37 map->CopyFrom(loader_->policy()); | |
38 return true; | |
39 } | |
40 | |
41 void AsynchronousPolicyProvider::RefreshPolicies() { | 35 void AsynchronousPolicyProvider::RefreshPolicies() { |
42 DCHECK(CalledOnValidThread()); | 36 DCHECK(CalledOnValidThread()); |
43 pending_refreshes_++; | 37 pending_refreshes_++; |
44 BrowserThread::PostTaskAndReply( | 38 BrowserThread::PostTaskAndReply( |
45 BrowserThread::FILE, FROM_HERE, | 39 BrowserThread::FILE, FROM_HERE, |
46 base::Bind(&AsynchronousPolicyProvider::PostReloadOnFileThread, | 40 base::Bind(&AsynchronousPolicyProvider::PostReloadOnFileThread, |
47 loader_), | 41 loader_), |
48 base::Bind(&AsynchronousPolicyProvider::OnReloadPosted, | 42 base::Bind(&AsynchronousPolicyProvider::OnReloadPosted, |
49 weak_ptr_factory_.GetWeakPtr())); | 43 weak_ptr_factory_.GetWeakPtr())); |
50 } | 44 } |
51 | 45 |
52 // static | 46 // static |
53 void AsynchronousPolicyProvider::PostReloadOnFileThread( | 47 void AsynchronousPolicyProvider::PostReloadOnFileThread( |
54 AsynchronousPolicyLoader* loader) { | 48 AsynchronousPolicyLoader* loader) { |
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
56 BrowserThread::PostTask( | 50 BrowserThread::PostTask( |
57 BrowserThread::FILE, FROM_HERE, | 51 BrowserThread::FILE, FROM_HERE, |
58 base::Bind(&AsynchronousPolicyLoader::Reload, loader, true)); | 52 base::Bind(&AsynchronousPolicyLoader::Reload, loader, true)); |
59 } | 53 } |
60 | 54 |
61 void AsynchronousPolicyProvider::OnReloadPosted() { | 55 void AsynchronousPolicyProvider::OnReloadPosted() { |
62 DCHECK(CalledOnValidThread()); | 56 DCHECK(CalledOnValidThread()); |
63 pending_refreshes_--; | 57 pending_refreshes_--; |
64 } | 58 } |
65 | 59 |
66 void AsynchronousPolicyProvider::OnLoaderReloaded() { | 60 void AsynchronousPolicyProvider::OnLoaderReloaded( |
| 61 scoped_ptr<PolicyBundle> bundle) { |
67 DCHECK(CalledOnValidThread()); | 62 DCHECK(CalledOnValidThread()); |
68 if (pending_refreshes_ == 0) | 63 if (pending_refreshes_ == 0) |
69 NotifyPolicyUpdated(); | 64 UpdatePolicy(bundle.Pass()); |
70 } | 65 } |
71 | 66 |
72 } // namespace policy | 67 } // namespace policy |
OLD | NEW |