| 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/user_policy_cache.h" | 5 #include "chrome/browser/policy/user_policy_cache.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/task.h" | 13 #include "base/task.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 15 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 16 #include "chrome/browser/policy/enterprise_metrics.h" |
| 16 #include "chrome/browser/policy/policy_map.h" | 17 #include "chrome/browser/policy/policy_map.h" |
| 17 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | 18 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| 18 #include "chrome/browser/policy/proto/device_management_local.pb.h" | 19 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
| 19 #include "chrome/browser/policy/proto/old_generic_format.pb.h" | 20 #include "chrome/browser/policy/proto/old_generic_format.pb.h" |
| 20 #include "content/browser/browser_thread.h" | 21 #include "content/browser/browser_thread.h" |
| 21 #include "policy/configuration_policy_type.h" | 22 #include "policy/configuration_policy_type.h" |
| 22 | 23 |
| 23 namespace policy { | 24 namespace policy { |
| 24 | 25 |
| 26 namespace em = enterprise_management; |
| 27 |
| 25 // Decodes a CloudPolicySettings object into two maps with mandatory and | 28 // Decodes a CloudPolicySettings object into two maps with mandatory and |
| 26 // recommended settings, respectively. The implementation is generated code | 29 // recommended settings, respectively. The implementation is generated code |
| 27 // in policy/cloud_policy_generated.cc. | 30 // in policy/cloud_policy_generated.cc. |
| 28 void DecodePolicy(const em::CloudPolicySettings& policy, | 31 void DecodePolicy(const em::CloudPolicySettings& policy, |
| 29 PolicyMap* mandatory, PolicyMap* recommended); | 32 PolicyMap* mandatory, PolicyMap* recommended); |
| 30 | 33 |
| 31 // Handles the on-disk cache file used by UserPolicyCache. This class handles | 34 // Handles the on-disk cache file used by UserPolicyCache. This class handles |
| 32 // the necessary thread switching and may outlive the associated UserPolicyCache | 35 // the necessary thread switching and may outlive the associated UserPolicyCache |
| 33 // instance. | 36 // instance. |
| 34 class UserPolicyCache::DiskCache | 37 class UserPolicyCache::DiskCache |
| (...skipping 27 matching lines...) Expand all Loading... |
| 62 }; | 65 }; |
| 63 | 66 |
| 64 UserPolicyCache::DiskCache::DiskCache( | 67 UserPolicyCache::DiskCache::DiskCache( |
| 65 const base::WeakPtr<UserPolicyCache>& cache, | 68 const base::WeakPtr<UserPolicyCache>& cache, |
| 66 const FilePath& backing_file_path) | 69 const FilePath& backing_file_path) |
| 67 : cache_(cache), | 70 : cache_(cache), |
| 68 backing_file_path_(backing_file_path) {} | 71 backing_file_path_(backing_file_path) {} |
| 69 | 72 |
| 70 void UserPolicyCache::DiskCache::Load() { | 73 void UserPolicyCache::DiskCache::Load() { |
| 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 75 em::LogPolicyOperation(em::kPolicyLoadRequested); |
| 72 BrowserThread::PostTask( | 76 BrowserThread::PostTask( |
| 73 BrowserThread::FILE, FROM_HERE, | 77 BrowserThread::FILE, FROM_HERE, |
| 74 NewRunnableMethod(this, &DiskCache::LoadOnFileThread)); | 78 NewRunnableMethod(this, &DiskCache::LoadOnFileThread)); |
| 75 } | 79 } |
| 76 | 80 |
| 77 void UserPolicyCache::DiskCache::Store( | 81 void UserPolicyCache::DiskCache::Store( |
| 78 const em::CachedCloudPolicyResponse& policy) { | 82 const em::CachedCloudPolicyResponse& policy) { |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 84 em::LogPolicyOperation(em::kPolicyStoreRequested); |
| 80 BrowserThread::PostTask( | 85 BrowserThread::PostTask( |
| 81 BrowserThread::FILE, FROM_HERE, | 86 BrowserThread::FILE, FROM_HERE, |
| 82 NewRunnableMethod(this, &DiskCache::StoreOnFileThread, policy)); | 87 NewRunnableMethod(this, &DiskCache::StoreOnFileThread, policy)); |
| 83 } | 88 } |
| 84 | 89 |
| 85 void UserPolicyCache::DiskCache::LoadOnFileThread() { | 90 void UserPolicyCache::DiskCache::LoadOnFileThread() { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 87 if (!file_util::PathExists(backing_file_path_)) | 92 if (!file_util::PathExists(backing_file_path_)) |
| 88 return; | 93 return; |
| 89 | 94 |
| 90 // Read the protobuf from the file. | 95 // Read the protobuf from the file. |
| 91 std::string data; | 96 std::string data; |
| 92 if (!file_util::ReadFileToString(backing_file_path_, &data)) { | 97 if (!file_util::ReadFileToString(backing_file_path_, &data)) { |
| 93 LOG(WARNING) << "Failed to read policy data from " | 98 LOG(WARNING) << "Failed to read policy data from " |
| 94 << backing_file_path_.value(); | 99 << backing_file_path_.value(); |
| 100 em::LogPolicyOperation(em::kPolicyLoadFailed); |
| 95 return; | 101 return; |
| 96 } | 102 } |
| 97 | 103 |
| 98 // Decode it. | 104 // Decode it. |
| 99 em::CachedCloudPolicyResponse cached_response; | 105 em::CachedCloudPolicyResponse cached_response; |
| 100 if (!cached_response.ParseFromArray(data.c_str(), data.size())) { | 106 if (!cached_response.ParseFromArray(data.c_str(), data.size())) { |
| 101 LOG(WARNING) << "Failed to parse policy data read from " | 107 LOG(WARNING) << "Failed to parse policy data read from " |
| 102 << backing_file_path_.value(); | 108 << backing_file_path_.value(); |
| 109 em::LogPolicyOperation(em::kPolicyLoadFailed); |
| 103 return; | 110 return; |
| 104 } | 111 } |
| 105 | 112 |
| 106 BrowserThread::PostTask( | 113 BrowserThread::PostTask( |
| 107 BrowserThread::UI, FROM_HERE, | 114 BrowserThread::UI, FROM_HERE, |
| 108 NewRunnableMethod(this, | 115 NewRunnableMethod(this, |
| 109 &DiskCache::FinishLoadOnUIThread, | 116 &DiskCache::FinishLoadOnUIThread, |
| 110 cached_response)); | 117 cached_response)); |
| 111 } | 118 } |
| 112 | 119 |
| 113 void UserPolicyCache::DiskCache::FinishLoadOnUIThread( | 120 void UserPolicyCache::DiskCache::FinishLoadOnUIThread( |
| 114 const em::CachedCloudPolicyResponse& policy) { | 121 const em::CachedCloudPolicyResponse& policy) { |
| 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 116 if (cache_.get()) | 123 if (cache_.get()) |
| 117 cache_->OnDiskCacheLoaded(policy); | 124 cache_->OnDiskCacheLoaded(policy); |
| 118 } | 125 } |
| 119 | 126 |
| 120 | 127 |
| 121 void UserPolicyCache::DiskCache::StoreOnFileThread( | 128 void UserPolicyCache::DiskCache::StoreOnFileThread( |
| 122 const em::CachedCloudPolicyResponse& policy) { | 129 const em::CachedCloudPolicyResponse& policy) { |
| 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 124 std::string data; | 131 std::string data; |
| 125 if (!policy.SerializeToString(&data)) { | 132 if (!policy.SerializeToString(&data)) { |
| 126 LOG(WARNING) << "Failed to serialize policy data"; | 133 LOG(WARNING) << "Failed to serialize policy data"; |
| 134 em::LogPolicyOperation(em::kPolicyStoreFailed); |
| 127 return; | 135 return; |
| 128 } | 136 } |
| 129 | 137 |
| 130 if (!file_util::CreateDirectory(backing_file_path_.DirName())) { | 138 if (!file_util::CreateDirectory(backing_file_path_.DirName())) { |
| 131 LOG(WARNING) << "Failed to create directory " | 139 LOG(WARNING) << "Failed to create directory " |
| 132 << backing_file_path_.DirName().value(); | 140 << backing_file_path_.DirName().value(); |
| 141 em::LogPolicyOperation(em::kPolicyStoreFailed); |
| 133 return; | 142 return; |
| 134 } | 143 } |
| 135 | 144 |
| 136 int size = data.size(); | 145 int size = data.size(); |
| 137 if (file_util::WriteFile(backing_file_path_, data.c_str(), size) != size) { | 146 if (file_util::WriteFile(backing_file_path_, data.c_str(), size) != size) { |
| 138 LOG(WARNING) << "Failed to write " << backing_file_path_.value(); | 147 LOG(WARNING) << "Failed to write " << backing_file_path_.value(); |
| 148 em::LogPolicyOperation(em::kPolicyStoreFailed); |
| 139 return; | 149 return; |
| 140 } | 150 } |
| 141 } | 151 } |
| 142 | 152 |
| 143 UserPolicyCache::UserPolicyCache(const FilePath& backing_file_path) | 153 UserPolicyCache::UserPolicyCache(const FilePath& backing_file_path) |
| 144 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 154 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 145 disk_cache_ = new DiskCache(weak_ptr_factory_.GetWeakPtr(), | 155 disk_cache_ = new DiskCache(weak_ptr_factory_.GetWeakPtr(), |
| 146 backing_file_path); | 156 backing_file_path); |
| 147 } | 157 } |
| 148 | 158 |
| 149 UserPolicyCache::~UserPolicyCache() { | 159 UserPolicyCache::~UserPolicyCache() { |
| 150 } | 160 } |
| 151 | 161 |
| 152 void UserPolicyCache::Load() { | 162 void UserPolicyCache::Load() { |
| 153 disk_cache_->Load(); | 163 disk_cache_->Load(); |
| 154 } | 164 } |
| 155 | 165 |
| 156 void UserPolicyCache::SetPolicy(const em::PolicyFetchResponse& policy) { | 166 void UserPolicyCache::SetPolicy(const em::PolicyFetchResponse& policy) { |
| 157 base::Time now = base::Time::NowFromSystemTime(); | 167 base::Time now = base::Time::NowFromSystemTime(); |
| 158 set_last_policy_refresh_time(now); | 168 set_last_policy_refresh_time(now); |
| 159 base::Time timestamp; | 169 base::Time timestamp; |
| 160 if (!SetPolicyInternal(policy, ×tamp, false)) | 170 if (!SetPolicyInternal(policy, ×tamp, false)) |
| 161 return; | 171 return; |
| 172 em::LogPolicyOperation(em::kPolicyFetchOK); |
| 162 | 173 |
| 163 if (timestamp > base::Time::NowFromSystemTime() + | 174 if (timestamp > base::Time::NowFromSystemTime() + |
| 164 base::TimeDelta::FromMinutes(1)) { | 175 base::TimeDelta::FromMinutes(1)) { |
| 165 LOG(WARNING) << "Server returned policy with timestamp from the future, " | 176 LOG(WARNING) << "Server returned policy with timestamp from the future, " |
| 166 "not persisting to disk."; | 177 "not persisting to disk."; |
| 167 return; | 178 return; |
| 168 } | 179 } |
| 169 | 180 |
| 170 em::CachedCloudPolicyResponse cached_policy; | 181 em::CachedCloudPolicyResponse cached_policy; |
| 171 cached_policy.mutable_cloud_policy()->CopyFrom(policy); | 182 cached_policy.mutable_cloud_policy()->CopyFrom(policy); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 return list; | 352 return list; |
| 342 } | 353 } |
| 343 default: | 354 default: |
| 344 NOTREACHED() << "Unhandled value type"; | 355 NOTREACHED() << "Unhandled value type"; |
| 345 } | 356 } |
| 346 | 357 |
| 347 return NULL; | 358 return NULL; |
| 348 } | 359 } |
| 349 | 360 |
| 350 } // namespace policy | 361 } // namespace policy |
| OLD | NEW |