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_token_cache.h" | 5 #include "chrome/browser/policy/user_policy_token_cache.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/metrics/histogram.h" |
| 9 #include "chrome/browser/policy/enterprise_metrics.h" |
8 #include "content/browser/browser_thread.h" | 10 #include "content/browser/browser_thread.h" |
9 | 11 |
| 12 namespace { |
| 13 |
| 14 // Other places can sample on the same UMA counter, so make sure they all do |
| 15 // it on the same thread (UI). |
| 16 void SampleUMAOnUIThread(policy::MetricToken sample) { |
| 17 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 18 UMA_HISTOGRAM_ENUMERATION(policy::kMetricToken, sample, |
| 19 policy::kMetricTokenSize); |
| 20 } |
| 21 |
| 22 void SampleUMA(policy::MetricToken sample) { |
| 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 24 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 25 NewRunnableFunction(&SampleUMAOnUIThread, sample)); |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
10 namespace policy { | 30 namespace policy { |
11 | 31 |
12 namespace em = enterprise_management; | 32 namespace em = enterprise_management; |
13 | 33 |
14 UserPolicyTokenCache::Delegate::~Delegate() {} | 34 UserPolicyTokenCache::Delegate::~Delegate() {} |
15 | 35 |
16 UserPolicyTokenCache::UserPolicyTokenCache( | 36 UserPolicyTokenCache::UserPolicyTokenCache( |
17 const base::WeakPtr<Delegate>& delegate, | 37 const base::WeakPtr<Delegate>& delegate, |
18 const FilePath& cache_file) | 38 const FilePath& cache_file) |
19 : delegate_(delegate), | 39 : delegate_(delegate), |
(...skipping 25 matching lines...) Expand all Loading... |
45 std::string device_token; | 65 std::string device_token; |
46 std::string device_id; | 66 std::string device_id; |
47 | 67 |
48 if (file_util::PathExists(cache_file_)) { | 68 if (file_util::PathExists(cache_file_)) { |
49 std::string data; | 69 std::string data; |
50 em::DeviceCredentials device_credentials; | 70 em::DeviceCredentials device_credentials; |
51 if (file_util::ReadFileToString(cache_file_, &data) && | 71 if (file_util::ReadFileToString(cache_file_, &data) && |
52 device_credentials.ParseFromArray(data.c_str(), data.size())) { | 72 device_credentials.ParseFromArray(data.c_str(), data.size())) { |
53 device_token = device_credentials.device_token(); | 73 device_token = device_credentials.device_token(); |
54 device_id = device_credentials.device_id(); | 74 device_id = device_credentials.device_id(); |
| 75 SampleUMA(kMetricTokenLoadSucceeded); |
| 76 } else { |
| 77 SampleUMA(kMetricTokenLoadFailed); |
55 } | 78 } |
56 } | 79 } |
57 | 80 |
58 BrowserThread::PostTask( | 81 BrowserThread::PostTask( |
59 BrowserThread::UI, FROM_HERE, | 82 BrowserThread::UI, FROM_HERE, |
60 NewRunnableMethod(this, | 83 NewRunnableMethod(this, |
61 &UserPolicyTokenCache::NotifyOnUIThread, | 84 &UserPolicyTokenCache::NotifyOnUIThread, |
62 device_token, | 85 device_token, |
63 device_id)); | 86 device_id)); |
64 } | 87 } |
65 | 88 |
66 void UserPolicyTokenCache::NotifyOnUIThread(const std::string& token, | 89 void UserPolicyTokenCache::NotifyOnUIThread(const std::string& token, |
67 const std::string& device_id) { | 90 const std::string& device_id) { |
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
69 if (delegate_.get()) | 92 if (delegate_.get()) |
70 delegate_->OnTokenCacheLoaded(token, device_id); | 93 delegate_->OnTokenCacheLoaded(token, device_id); |
71 } | 94 } |
72 | 95 |
73 void UserPolicyTokenCache::StoreOnFileThread(const std::string& token, | 96 void UserPolicyTokenCache::StoreOnFileThread(const std::string& token, |
74 const std::string& device_id) { | 97 const std::string& device_id) { |
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
76 em::DeviceCredentials device_credentials; | 99 em::DeviceCredentials device_credentials; |
77 device_credentials.set_device_token(token); | 100 device_credentials.set_device_token(token); |
78 device_credentials.set_device_id(device_id); | 101 device_credentials.set_device_id(device_id); |
79 std::string data; | 102 std::string data; |
80 bool success = device_credentials.SerializeToString(&data); | 103 bool success = device_credentials.SerializeToString(&data); |
81 if (!success) { | 104 if (!success) { |
82 LOG(WARNING) << "Failed serialize device token data, will not write " | 105 LOG(WARNING) << "Failed serialize device token data, will not write " |
83 << cache_file_.value(); | 106 << cache_file_.value(); |
| 107 SampleUMA(kMetricTokenStoreFailed); |
84 return; | 108 return; |
85 } | 109 } |
86 | 110 |
87 if (!file_util::CreateDirectory(cache_file_.DirName())) { | 111 if (!file_util::CreateDirectory(cache_file_.DirName())) { |
88 LOG(WARNING) << "Failed to create directory " | 112 LOG(WARNING) << "Failed to create directory " |
89 << cache_file_.DirName().value(); | 113 << cache_file_.DirName().value(); |
| 114 SampleUMA(kMetricTokenStoreFailed); |
90 return; | 115 return; |
91 } | 116 } |
92 | 117 |
93 file_util::WriteFile(cache_file_, data.c_str(), data.length()); | 118 int size = data.size(); |
| 119 if (file_util::WriteFile(cache_file_, data.c_str(), size) != size) { |
| 120 LOG(WARNING) << "Failed to write " << cache_file_.value(); |
| 121 SampleUMA(kMetricTokenStoreFailed); |
| 122 } |
| 123 |
| 124 SampleUMA(kMetricTokenStoreSucceeded); |
94 } | 125 } |
95 | 126 |
96 } // namespace policy | 127 } // namespace policy |
OLD | NEW |