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/cloud_policy_manager.h" | 5 #include "chrome/browser/policy/cloud_policy_manager.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/logging.h" | 9 #include "base/logging.h" |
10 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" | 10 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" |
11 #include "chrome/browser/policy/cloud_policy_service.h" | 11 #include "chrome/browser/policy/cloud_policy_service.h" |
12 #include "chrome/browser/policy/policy_bundle.h" | 12 #include "chrome/browser/policy/policy_bundle.h" |
13 #include "chrome/browser/policy/policy_map.h" | 13 #include "chrome/browser/policy/policy_map.h" |
14 | 14 |
15 namespace policy { | 15 namespace policy { |
16 | 16 |
17 CloudPolicyManager::CloudPolicyManager(scoped_ptr<CloudPolicyStore> store) | 17 CloudPolicyManager::CloudPolicyManager(CloudPolicyStore* store) |
18 : store_(store.Pass()), | 18 : store_(store), |
19 waiting_for_policy_refresh_(false) { | 19 waiting_for_policy_refresh_(false) { |
20 store_->AddObserver(this); | 20 store_->AddObserver(this); |
21 | 21 |
22 // If the underlying store is already initialized, publish the loaded | 22 // If the underlying store is already initialized, publish the loaded |
23 // policy. Otherwise, request a load now. | 23 // policy. Otherwise, request a load now. |
24 if (store_->is_initialized()) | 24 if (store_->is_initialized()) |
25 CheckAndPublishPolicy(); | 25 CheckAndPublishPolicy(); |
26 else | 26 else |
27 store_->Load(); | 27 store_->Load(); |
28 } | 28 } |
29 | 29 |
30 CloudPolicyManager::~CloudPolicyManager() {} | 30 CloudPolicyManager::~CloudPolicyManager() { |
| 31 DCHECK(!store_); |
| 32 } |
31 | 33 |
32 void CloudPolicyManager::Shutdown() { | 34 void CloudPolicyManager::Shutdown() { |
33 ShutdownService(); | 35 ShutdownService(); |
34 store_->RemoveObserver(this); | 36 store_->RemoveObserver(this); |
35 ConfigurationPolicyProvider::Shutdown(); | 37 ConfigurationPolicyProvider::Shutdown(); |
| 38 store_ = NULL; |
36 } | 39 } |
37 | 40 |
38 bool CloudPolicyManager::IsInitializationComplete() const { | 41 bool CloudPolicyManager::IsInitializationComplete() const { |
39 return store_->is_initialized(); | 42 return store_->is_initialized(); |
40 } | 43 } |
41 | 44 |
42 void CloudPolicyManager::RefreshPolicies() { | 45 void CloudPolicyManager::RefreshPolicies() { |
43 if (service_.get()) { | 46 if (service_.get()) { |
44 waiting_for_policy_refresh_ = true; | 47 waiting_for_policy_refresh_ = true; |
45 service_->RefreshPolicy( | 48 service_->RefreshPolicy( |
46 base::Bind(&CloudPolicyManager::OnRefreshComplete, | 49 base::Bind(&CloudPolicyManager::OnRefreshComplete, |
47 base::Unretained(this))); | 50 base::Unretained(this))); |
48 } else { | 51 } else { |
49 OnRefreshComplete(); | 52 OnRefreshComplete(); |
50 } | 53 } |
51 } | 54 } |
52 | 55 |
53 void CloudPolicyManager::OnStoreLoaded(CloudPolicyStore* store) { | 56 void CloudPolicyManager::OnStoreLoaded(CloudPolicyStore* store) { |
54 DCHECK_EQ(store_.get(), store); | 57 DCHECK_EQ(store_, store); |
55 CheckAndPublishPolicy(); | 58 CheckAndPublishPolicy(); |
56 } | 59 } |
57 | 60 |
58 void CloudPolicyManager::OnStoreError(CloudPolicyStore* store) { | 61 void CloudPolicyManager::OnStoreError(CloudPolicyStore* store) { |
59 DCHECK_EQ(store_.get(), store); | 62 DCHECK_EQ(store_, store); |
60 // No action required, the old policy is still valid. | 63 // No action required, the old policy is still valid. |
61 } | 64 } |
62 | 65 |
63 void CloudPolicyManager::InitializeService( | 66 void CloudPolicyManager::InitializeService( |
64 scoped_ptr<CloudPolicyClient> client) { | 67 scoped_ptr<CloudPolicyClient> client) { |
65 CHECK(!client_.get()); | 68 CHECK(!client_.get()); |
66 CHECK(client.get()); | 69 CHECK(client.get()); |
67 client_ = client.Pass(); | 70 client_ = client.Pass(); |
68 service_.reset(new CloudPolicyService(client_.get(), store_.get())); | 71 service_.reset(new CloudPolicyService(client_.get(), store_)); |
69 } | 72 } |
70 | 73 |
71 void CloudPolicyManager::ShutdownService() { | 74 void CloudPolicyManager::ShutdownService() { |
72 refresh_scheduler_.reset(); | 75 refresh_scheduler_.reset(); |
73 service_.reset(); | 76 service_.reset(); |
74 client_.reset(); | 77 client_.reset(); |
75 } | 78 } |
76 | 79 |
77 void CloudPolicyManager::StartRefreshScheduler( | 80 void CloudPolicyManager::StartRefreshScheduler( |
78 PrefService* local_state, | 81 PrefService* local_state, |
79 const std::string& refresh_rate_pref) { | 82 const std::string& refresh_rate_pref) { |
80 if (!refresh_scheduler_.get()) { | 83 if (!refresh_scheduler_.get()) { |
81 refresh_scheduler_.reset( | 84 refresh_scheduler_.reset( |
82 new CloudPolicyRefreshScheduler( | 85 new CloudPolicyRefreshScheduler( |
83 client_.get(), store_.get(), local_state, refresh_rate_pref, | 86 client_.get(), store_, local_state, refresh_rate_pref, |
84 MessageLoop::current()->message_loop_proxy())); | 87 MessageLoop::current()->message_loop_proxy())); |
85 } | 88 } |
86 } | 89 } |
87 | 90 |
88 void CloudPolicyManager::CheckAndPublishPolicy() { | 91 void CloudPolicyManager::CheckAndPublishPolicy() { |
89 if (IsInitializationComplete() && !waiting_for_policy_refresh_) { | 92 if (IsInitializationComplete() && !waiting_for_policy_refresh_) { |
90 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); | 93 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); |
91 bundle->Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( | 94 bundle->Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( |
92 store_->policy_map()); | 95 store_->policy_map()); |
93 UpdatePolicy(bundle.Pass()); | 96 UpdatePolicy(bundle.Pass()); |
94 } | 97 } |
95 } | 98 } |
96 | 99 |
97 void CloudPolicyManager::OnRefreshComplete() { | 100 void CloudPolicyManager::OnRefreshComplete() { |
98 waiting_for_policy_refresh_ = false; | 101 waiting_for_policy_refresh_ = false; |
99 CheckAndPublishPolicy(); | 102 CheckAndPublishPolicy(); |
100 } | 103 } |
101 | 104 |
102 } // namespace policy | 105 } // namespace policy |
OLD | NEW |