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