| 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" | 8 #include "base/metrics/histogram.h" |
| 9 #include "chrome/browser/policy/enterprise_metrics.h" | 9 #include "chrome/browser/policy/enterprise_metrics.h" |
| 10 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
| 10 #include "content/browser/browser_thread.h" | 11 #include "content/browser/browser_thread.h" |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 // Other places can sample on the same UMA counter, so make sure they all do | 15 // Other places can sample on the same UMA counter, so make sure they all do |
| 15 // it on the same thread (UI). | 16 // it on the same thread (UI). |
| 16 void SampleUMAOnUIThread(policy::MetricToken sample) { | 17 void SampleUMAOnUIThread(policy::MetricToken sample) { |
| 17 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 18 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 18 UMA_HISTOGRAM_ENUMERATION(policy::kMetricToken, sample, | 19 UMA_HISTOGRAM_ENUMERATION(policy::kMetricToken, sample, |
| 19 policy::kMetricTokenSize); | 20 policy::kMetricTokenSize); |
| 20 } | 21 } |
| 21 | 22 |
| 22 void SampleUMA(policy::MetricToken sample) { | 23 void SampleUMA(policy::MetricToken sample) { |
| 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 24 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 25 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 25 NewRunnableFunction(&SampleUMAOnUIThread, sample)); | 26 NewRunnableFunction(&SampleUMAOnUIThread, sample)); |
| 26 } | 27 } |
| 27 | 28 |
| 28 } // namespace | 29 } // namespace |
| 29 | 30 |
| 30 namespace policy { | 31 namespace policy { |
| 31 | 32 |
| 32 namespace em = enterprise_management; | 33 namespace em = enterprise_management; |
| 33 | 34 |
| 34 UserPolicyTokenCache::Delegate::~Delegate() {} | |
| 35 | |
| 36 UserPolicyTokenCache::UserPolicyTokenCache( | 35 UserPolicyTokenCache::UserPolicyTokenCache( |
| 37 const base::WeakPtr<Delegate>& delegate, | 36 const base::WeakPtr<CloudPolicyDataStore>& data_store, |
| 38 const FilePath& cache_file) | 37 const FilePath& cache_file) |
| 39 : delegate_(delegate), | 38 : data_store_(data_store), |
| 40 cache_file_(cache_file) {} | 39 cache_file_(cache_file) { |
| 40 data_store_->AddObserver(this); |
| 41 } |
| 41 | 42 |
| 42 void UserPolicyTokenCache::Load() { | 43 void UserPolicyTokenCache::Load() { |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 44 BrowserThread::PostTask( | 45 BrowserThread::PostTask( |
| 45 BrowserThread::FILE, FROM_HERE, | 46 BrowserThread::FILE, FROM_HERE, |
| 46 NewRunnableMethod(this, &UserPolicyTokenCache::LoadOnFileThread)); | 47 NewRunnableMethod(this, &UserPolicyTokenCache::LoadOnFileThread)); |
| 47 } | 48 } |
| 48 | 49 |
| 49 void UserPolicyTokenCache::Store(const std::string& token, | 50 void UserPolicyTokenCache::Store(const std::string& token, |
| 50 const std::string& device_id) { | 51 const std::string& device_id) { |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 52 BrowserThread::PostTask( | 53 BrowserThread::PostTask( |
| 53 BrowserThread::FILE, FROM_HERE, | 54 BrowserThread::FILE, FROM_HERE, |
| 54 NewRunnableMethod(this, | 55 NewRunnableMethod(this, |
| 55 &UserPolicyTokenCache::StoreOnFileThread, | 56 &UserPolicyTokenCache::StoreOnFileThread, |
| 56 token, | 57 token, |
| 57 device_id)); | 58 device_id)); |
| 58 } | 59 } |
| 59 | 60 |
| 60 UserPolicyTokenCache::~UserPolicyTokenCache() { | 61 UserPolicyTokenCache::~UserPolicyTokenCache() { |
| 62 if (data_store_) |
| 63 data_store_->RemoveObserver(this); |
| 61 } | 64 } |
| 62 | 65 |
| 63 void UserPolicyTokenCache::LoadOnFileThread() { | 66 void UserPolicyTokenCache::LoadOnFileThread() { |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 65 std::string device_token; | 68 std::string device_token; |
| 66 std::string device_id; | 69 std::string device_id; |
| 67 | 70 |
| 68 if (file_util::PathExists(cache_file_)) { | 71 if (file_util::PathExists(cache_file_)) { |
| 69 std::string data; | 72 std::string data; |
| 70 em::DeviceCredentials device_credentials; | 73 em::DeviceCredentials device_credentials; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 82 BrowserThread::UI, FROM_HERE, | 85 BrowserThread::UI, FROM_HERE, |
| 83 NewRunnableMethod(this, | 86 NewRunnableMethod(this, |
| 84 &UserPolicyTokenCache::NotifyOnUIThread, | 87 &UserPolicyTokenCache::NotifyOnUIThread, |
| 85 device_token, | 88 device_token, |
| 86 device_id)); | 89 device_id)); |
| 87 } | 90 } |
| 88 | 91 |
| 89 void UserPolicyTokenCache::NotifyOnUIThread(const std::string& token, | 92 void UserPolicyTokenCache::NotifyOnUIThread(const std::string& token, |
| 90 const std::string& device_id) { | 93 const std::string& device_id) { |
| 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 92 if (delegate_.get()) | 95 if (data_store_) { |
| 93 delegate_->OnTokenCacheLoaded(token, device_id); | 96 data_store_->set_device_id(device_id); |
| 97 data_store_->SetDeviceToken(token, true); |
| 98 data_store_->NotifyDeviceTokenChanged(); |
| 99 } |
| 94 } | 100 } |
| 95 | 101 |
| 96 void UserPolicyTokenCache::StoreOnFileThread(const std::string& token, | 102 void UserPolicyTokenCache::StoreOnFileThread(const std::string& token, |
| 97 const std::string& device_id) { | 103 const std::string& device_id) { |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 99 em::DeviceCredentials device_credentials; | 105 em::DeviceCredentials device_credentials; |
| 100 device_credentials.set_device_token(token); | 106 device_credentials.set_device_token(token); |
| 101 device_credentials.set_device_id(device_id); | 107 device_credentials.set_device_id(device_id); |
| 102 std::string data; | 108 std::string data; |
| 103 bool success = device_credentials.SerializeToString(&data); | 109 bool success = device_credentials.SerializeToString(&data); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 117 | 123 |
| 118 int size = data.size(); | 124 int size = data.size(); |
| 119 if (file_util::WriteFile(cache_file_, data.c_str(), size) != size) { | 125 if (file_util::WriteFile(cache_file_, data.c_str(), size) != size) { |
| 120 LOG(WARNING) << "Failed to write " << cache_file_.value(); | 126 LOG(WARNING) << "Failed to write " << cache_file_.value(); |
| 121 SampleUMA(kMetricTokenStoreFailed); | 127 SampleUMA(kMetricTokenStoreFailed); |
| 122 } | 128 } |
| 123 | 129 |
| 124 SampleUMA(kMetricTokenStoreSucceeded); | 130 SampleUMA(kMetricTokenStoreSucceeded); |
| 125 } | 131 } |
| 126 | 132 |
| 133 void UserPolicyTokenCache::OnDeviceTokenChanged() { |
| 134 if (!data_store_->device_token().empty() && |
| 135 !data_store_->device_id().empty()) { |
| 136 // This will also happen after loading the cache. |
| 137 Store(data_store_->device_token(), data_store_->device_id()); |
| 138 } |
| 139 } |
| 140 |
| 141 void UserPolicyTokenCache::OnCredentialsChanged() { |
| 142 } |
| 143 |
| 144 void UserPolicyTokenCache::OnDataStoreGoingAway() { |
| 145 data_store_->RemoveObserver(this); |
| 146 } |
| 147 |
| 127 } // namespace policy | 148 } // namespace policy |
| OLD | NEW |