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/user_cloud_policy_store.h" | 5 #include "chrome/browser/policy/user_cloud_policy_store.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | 9 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
10 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 10 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 90 |
91 } // namespace | 91 } // namespace |
92 | 92 |
93 UserCloudPolicyStore::UserCloudPolicyStore(Profile* profile, | 93 UserCloudPolicyStore::UserCloudPolicyStore(Profile* profile, |
94 const FilePath& path) | 94 const FilePath& path) |
95 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 95 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
96 profile_(profile), | 96 profile_(profile), |
97 backing_file_path_(path) { | 97 backing_file_path_(path) { |
98 } | 98 } |
99 | 99 |
100 UserCloudPolicyStore::~UserCloudPolicyStore() { | 100 UserCloudPolicyStore::~UserCloudPolicyStore() {} |
| 101 |
| 102 // static |
| 103 scoped_ptr<UserCloudPolicyStore> UserCloudPolicyStore::Create( |
| 104 Profile* profile) { |
| 105 FilePath path = |
| 106 profile->GetPath().Append(kPolicyDir).Append(kPolicyCacheFile); |
| 107 return make_scoped_ptr(new UserCloudPolicyStore(profile, path)); |
101 } | 108 } |
102 | 109 |
103 void UserCloudPolicyStore::LoadImmediately() { | 110 void UserCloudPolicyStore::LoadImmediately() { |
104 DVLOG(1) << "Initiating immediate policy load from disk"; | 111 DVLOG(1) << "Initiating immediate policy load from disk"; |
105 // Cancel any pending Load/Store/Validate operations. | 112 // Cancel any pending Load/Store/Validate operations. |
106 weak_factory_.InvalidateWeakPtrs(); | 113 weak_factory_.InvalidateWeakPtrs(); |
107 // Load the policy from disk... | 114 // Load the policy from disk... |
108 PolicyLoadResult result = LoadPolicyFromDisk(backing_file_path_); | 115 PolicyLoadResult result = LoadPolicyFromDisk(backing_file_path_); |
109 // ...and install it, reporting success/failure to any observers. | 116 // ...and install it, reporting success/failure to any observers. |
110 PolicyLoaded(false, result); | 117 PolicyLoaded(false, result); |
111 } | 118 } |
112 | 119 |
| 120 void UserCloudPolicyStore::Clear() { |
| 121 content::BrowserThread::PostTask( |
| 122 content::BrowserThread::FILE, FROM_HERE, |
| 123 base::Bind(base::IgnoreResult(&file_util::Delete), |
| 124 backing_file_path_, |
| 125 false)); |
| 126 policy_.reset(); |
| 127 policy_map_.Clear(); |
| 128 NotifyStoreLoaded(); |
| 129 } |
| 130 |
113 void UserCloudPolicyStore::Load() { | 131 void UserCloudPolicyStore::Load() { |
114 DVLOG(1) << "Initiating policy load from disk"; | 132 DVLOG(1) << "Initiating policy load from disk"; |
115 // Cancel any pending Load/Store/Validate operations. | 133 // Cancel any pending Load/Store/Validate operations. |
116 weak_factory_.InvalidateWeakPtrs(); | 134 weak_factory_.InvalidateWeakPtrs(); |
117 | 135 |
118 // Start a new Load operation and have us get called back when it is | 136 // Start a new Load operation and have us get called back when it is |
119 // complete. | 137 // complete. |
120 content::BrowserThread::PostTaskAndReplyWithResult( | 138 content::BrowserThread::PostTaskAndReplyWithResult( |
121 content::BrowserThread::FILE, FROM_HERE, | 139 content::BrowserThread::FILE, FROM_HERE, |
122 base::Bind(&LoadPolicyFromDisk, backing_file_path_), | 140 base::Bind(&LoadPolicyFromDisk, backing_file_path_), |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 184 |
167 DVLOG(1) << "Validation succeeded - installing policy with dm_token: " << | 185 DVLOG(1) << "Validation succeeded - installing policy with dm_token: " << |
168 validator->policy_data()->request_token(); | 186 validator->policy_data()->request_token(); |
169 DVLOG(1) << "Device ID: " << validator->policy_data()->device_id(); | 187 DVLOG(1) << "Device ID: " << validator->policy_data()->device_id(); |
170 | 188 |
171 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); | 189 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); |
172 status_ = STATUS_OK; | 190 status_ = STATUS_OK; |
173 NotifyStoreLoaded(); | 191 NotifyStoreLoaded(); |
174 } | 192 } |
175 | 193 |
176 void UserCloudPolicyStore::RemoveStoredPolicy() { | |
177 content::BrowserThread::PostTask( | |
178 content::BrowserThread::FILE, FROM_HERE, | |
179 base::Bind(base::IgnoreResult(&file_util::Delete), | |
180 backing_file_path_, | |
181 false)); | |
182 } | |
183 | |
184 void UserCloudPolicyStore::Store(const em::PolicyFetchResponse& policy) { | 194 void UserCloudPolicyStore::Store(const em::PolicyFetchResponse& policy) { |
185 // Stop any pending requests to store policy, then validate the new policy | 195 // Stop any pending requests to store policy, then validate the new policy |
186 // before storing it. | 196 // before storing it. |
187 weak_factory_.InvalidateWeakPtrs(); | 197 weak_factory_.InvalidateWeakPtrs(); |
188 scoped_ptr<em::PolicyFetchResponse> policy_copy( | 198 scoped_ptr<em::PolicyFetchResponse> policy_copy( |
189 new em::PolicyFetchResponse(policy)); | 199 new em::PolicyFetchResponse(policy)); |
190 Validate(policy_copy.Pass(), | 200 Validate(policy_copy.Pass(), |
191 true, | 201 true, |
192 base::Bind(&UserCloudPolicyStore::StorePolicyAfterValidation, | 202 base::Bind(&UserCloudPolicyStore::StorePolicyAfterValidation, |
193 weak_factory_.GetWeakPtr())); | 203 weak_factory_.GetWeakPtr())); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 // reply because we can't do anything if it fails). | 242 // reply because we can't do anything if it fails). |
233 content::BrowserThread::PostTask( | 243 content::BrowserThread::PostTask( |
234 content::BrowserThread::FILE, FROM_HERE, | 244 content::BrowserThread::FILE, FROM_HERE, |
235 base::Bind(&StorePolicyToDiskOnFileThread, | 245 base::Bind(&StorePolicyToDiskOnFileThread, |
236 backing_file_path_, *validator->policy())); | 246 backing_file_path_, *validator->policy())); |
237 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); | 247 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); |
238 status_ = STATUS_OK; | 248 status_ = STATUS_OK; |
239 NotifyStoreLoaded(); | 249 NotifyStoreLoaded(); |
240 } | 250 } |
241 | 251 |
242 // static | |
243 scoped_ptr<CloudPolicyStore> CloudPolicyStore::CreateUserPolicyStore( | |
244 Profile* profile, | |
245 bool force_immediate_policy_load) { | |
246 FilePath path = | |
247 profile->GetPath().Append(kPolicyDir).Append(kPolicyCacheFile); | |
248 UserCloudPolicyStore* store = new UserCloudPolicyStore(profile, path); | |
249 if (force_immediate_policy_load) | |
250 store->LoadImmediately(); | |
251 return scoped_ptr<CloudPolicyStore>(store); | |
252 } | |
253 | |
254 } // namespace policy | 252 } // namespace policy |
OLD | NEW |