| 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/metrics/histogram.h" |
| 7 #include "base/task.h" | 8 #include "base/task.h" |
| 9 #include "chrome/browser/policy/enterprise_metrics.h" |
| 8 #include "chrome/browser/policy/proto/device_management_local.pb.h" | 10 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
| 9 #include "chrome/browser/policy/user_policy_disk_cache.h" | 11 #include "chrome/browser/policy/user_policy_disk_cache.h" |
| 10 #include "content/browser/browser_thread.h" | 12 #include "content/browser/browser_thread.h" |
| 11 | 13 |
| 14 namespace { |
| 15 |
| 16 // Other places can sample on the same UMA counter, so make sure they all do |
| 17 // it on the same thread (UI). |
| 18 void SampleUMAOnUIThread(policy::MetricPolicy sample) { |
| 19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 20 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy, sample, |
| 21 policy::kMetricPolicySize); |
| 22 } |
| 23 |
| 24 void SampleUMA(policy::MetricPolicy sample) { |
| 25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 26 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 27 NewRunnableFunction(&SampleUMAOnUIThread, sample)); |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 12 namespace policy { | 32 namespace policy { |
| 13 | 33 |
| 14 UserPolicyDiskCache::Delegate::~Delegate() {} | 34 UserPolicyDiskCache::Delegate::~Delegate() {} |
| 15 | 35 |
| 16 UserPolicyDiskCache::UserPolicyDiskCache( | 36 UserPolicyDiskCache::UserPolicyDiskCache( |
| 17 const base::WeakPtr<Delegate>& delegate, | 37 const base::WeakPtr<Delegate>& delegate, |
| 18 const FilePath& backing_file_path) | 38 const FilePath& backing_file_path) |
| 19 : delegate_(delegate), | 39 : delegate_(delegate), |
| 20 backing_file_path_(backing_file_path) {} | 40 backing_file_path_(backing_file_path) {} |
| 21 | 41 |
| 22 void UserPolicyDiskCache::Load() { | 42 void UserPolicyDiskCache::Load() { |
| 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 44 UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyLoadRequested, |
| 45 kMetricPolicySize); |
| 24 BrowserThread::PostTask( | 46 BrowserThread::PostTask( |
| 25 BrowserThread::FILE, FROM_HERE, | 47 BrowserThread::FILE, FROM_HERE, |
| 26 NewRunnableMethod(this, &UserPolicyDiskCache::LoadOnFileThread)); | 48 NewRunnableMethod(this, &UserPolicyDiskCache::LoadOnFileThread)); |
| 27 } | 49 } |
| 28 | 50 |
| 29 void UserPolicyDiskCache::Store( | 51 void UserPolicyDiskCache::Store( |
| 30 const em::CachedCloudPolicyResponse& policy) { | 52 const em::CachedCloudPolicyResponse& policy) { |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 54 UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyStoreRequested, |
| 55 kMetricPolicySize); |
| 32 BrowserThread::PostTask( | 56 BrowserThread::PostTask( |
| 33 BrowserThread::FILE, FROM_HERE, | 57 BrowserThread::FILE, FROM_HERE, |
| 34 NewRunnableMethod(this, &UserPolicyDiskCache::StoreOnFileThread, policy)); | 58 NewRunnableMethod(this, &UserPolicyDiskCache::StoreOnFileThread, policy)); |
| 35 } | 59 } |
| 36 | 60 |
| 37 UserPolicyDiskCache::~UserPolicyDiskCache() {} | 61 UserPolicyDiskCache::~UserPolicyDiskCache() {} |
| 38 | 62 |
| 39 void UserPolicyDiskCache::LoadOnFileThread() { | 63 void UserPolicyDiskCache::LoadOnFileThread() { |
| 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 41 if (!file_util::PathExists(backing_file_path_)) | 65 if (!file_util::PathExists(backing_file_path_)) |
| 42 return; | 66 return; |
| 43 | 67 |
| 44 // Read the protobuf from the file. | 68 // Read the protobuf from the file. |
| 45 std::string data; | 69 std::string data; |
| 46 if (!file_util::ReadFileToString(backing_file_path_, &data)) { | 70 if (!file_util::ReadFileToString(backing_file_path_, &data)) { |
| 47 LOG(WARNING) << "Failed to read policy data from " | 71 LOG(WARNING) << "Failed to read policy data from " |
| 48 << backing_file_path_.value(); | 72 << backing_file_path_.value(); |
| 73 SampleUMA(kMetricPolicyLoadFailed); |
| 49 return; | 74 return; |
| 50 } | 75 } |
| 51 | 76 |
| 52 // Decode it. | 77 // Decode it. |
| 53 em::CachedCloudPolicyResponse cached_response; | 78 em::CachedCloudPolicyResponse cached_response; |
| 54 if (!cached_response.ParseFromArray(data.c_str(), data.size())) { | 79 if (!cached_response.ParseFromArray(data.c_str(), data.size())) { |
| 55 LOG(WARNING) << "Failed to parse policy data read from " | 80 LOG(WARNING) << "Failed to parse policy data read from " |
| 56 << backing_file_path_.value(); | 81 << backing_file_path_.value(); |
| 82 SampleUMA(kMetricPolicyLoadFailed); |
| 57 return; | 83 return; |
| 58 } | 84 } |
| 59 | 85 |
| 60 BrowserThread::PostTask( | 86 BrowserThread::PostTask( |
| 61 BrowserThread::UI, FROM_HERE, | 87 BrowserThread::UI, FROM_HERE, |
| 62 NewRunnableMethod(this, | 88 NewRunnableMethod(this, |
| 63 &UserPolicyDiskCache::FinishLoadOnUIThread, | 89 &UserPolicyDiskCache::FinishLoadOnUIThread, |
| 64 cached_response)); | 90 cached_response)); |
| 65 } | 91 } |
| 66 | 92 |
| 67 void UserPolicyDiskCache::FinishLoadOnUIThread( | 93 void UserPolicyDiskCache::FinishLoadOnUIThread( |
| 68 const em::CachedCloudPolicyResponse& policy) { | 94 const em::CachedCloudPolicyResponse& policy) { |
| 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 70 if (delegate_.get()) | 96 if (delegate_.get()) |
| 71 delegate_->OnDiskCacheLoaded(policy); | 97 delegate_->OnDiskCacheLoaded(policy); |
| 72 } | 98 } |
| 73 | 99 |
| 74 | 100 |
| 75 void UserPolicyDiskCache::StoreOnFileThread( | 101 void UserPolicyDiskCache::StoreOnFileThread( |
| 76 const em::CachedCloudPolicyResponse& policy) { | 102 const em::CachedCloudPolicyResponse& policy) { |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 78 std::string data; | 104 std::string data; |
| 79 if (!policy.SerializeToString(&data)) { | 105 if (!policy.SerializeToString(&data)) { |
| 80 LOG(WARNING) << "Failed to serialize policy data"; | 106 LOG(WARNING) << "Failed to serialize policy data"; |
| 107 SampleUMA(kMetricPolicyStoreFailed); |
| 81 return; | 108 return; |
| 82 } | 109 } |
| 83 | 110 |
| 84 if (!file_util::CreateDirectory(backing_file_path_.DirName())) { | 111 if (!file_util::CreateDirectory(backing_file_path_.DirName())) { |
| 85 LOG(WARNING) << "Failed to create directory " | 112 LOG(WARNING) << "Failed to create directory " |
| 86 << backing_file_path_.DirName().value(); | 113 << backing_file_path_.DirName().value(); |
| 114 SampleUMA(kMetricPolicyStoreFailed); |
| 87 return; | 115 return; |
| 88 } | 116 } |
| 89 | 117 |
| 90 int size = data.size(); | 118 int size = data.size(); |
| 91 if (file_util::WriteFile(backing_file_path_, data.c_str(), size) != size) { | 119 if (file_util::WriteFile(backing_file_path_, data.c_str(), size) != size) { |
| 92 LOG(WARNING) << "Failed to write " << backing_file_path_.value(); | 120 LOG(WARNING) << "Failed to write " << backing_file_path_.value(); |
| 121 SampleUMA(kMetricPolicyStoreFailed); |
| 93 return; | 122 return; |
| 94 } | 123 } |
| 95 } | 124 } |
| 96 | 125 |
| 97 } // namespace policy | 126 } // namespace policy |
| OLD | NEW |