Home | History | Annotate | Download | only in policy
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/chromeos/policy/user_policy_token_loader.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/file_util.h"
      9 #include "base/metrics/histogram.h"
     10 #include "chrome/browser/policy/cloud/enterprise_metrics.h"
     11 #include "chrome/browser/policy/proto/cloud/device_management_local.pb.h"
     12 #include "content/public/browser/browser_thread.h"
     13 
     14 using content::BrowserThread;
     15 
     16 namespace {
     17 
     18 // Other places can sample on the same UMA counter, so make sure they all do
     19 // it on the same thread (UI).
     20 void SampleUMAOnUIThread(policy::MetricToken sample) {
     21   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     22   UMA_HISTOGRAM_ENUMERATION(policy::kMetricToken, sample,
     23                             policy::kMetricTokenSize);
     24 }
     25 
     26 void SampleUMA(policy::MetricToken sample) {
     27   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
     28   BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
     29                           base::Bind(&SampleUMAOnUIThread, sample));
     30 }
     31 
     32 }  // namespace
     33 
     34 namespace policy {
     35 
     36 namespace em = enterprise_management;
     37 
     38 UserPolicyTokenLoader::Delegate::~Delegate() {}
     39 
     40 UserPolicyTokenLoader::UserPolicyTokenLoader(
     41     const base::WeakPtr<Delegate>& delegate,
     42     const base::FilePath& cache_file)
     43     : delegate_(delegate),
     44       cache_file_(cache_file) {}
     45 
     46 void UserPolicyTokenLoader::Load() {
     47   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     48   BrowserThread::PostTask(
     49       BrowserThread::FILE, FROM_HERE,
     50       base::Bind(&UserPolicyTokenLoader::LoadOnFileThread, this));
     51 }
     52 
     53 UserPolicyTokenLoader::~UserPolicyTokenLoader() {
     54 }
     55 
     56 void UserPolicyTokenLoader::LoadOnFileThread() {
     57   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
     58   std::string device_token;
     59   std::string device_id;
     60 
     61   if (base::PathExists(cache_file_)) {
     62     std::string data;
     63     em::DeviceCredentials device_credentials;
     64     if (file_util::ReadFileToString(cache_file_, &data) &&
     65         device_credentials.ParseFromArray(data.c_str(), data.size())) {
     66       device_token = device_credentials.device_token();
     67       device_id = device_credentials.device_id();
     68       SampleUMA(kMetricTokenLoadSucceeded);
     69     } else {
     70       SampleUMA(kMetricTokenLoadFailed);
     71     }
     72   }
     73 
     74   BrowserThread::PostTask(
     75       BrowserThread::UI, FROM_HERE,
     76       base::Bind(&UserPolicyTokenLoader::NotifyOnUIThread,
     77                  this,
     78                  device_token,
     79                  device_id));
     80 }
     81 
     82 void UserPolicyTokenLoader::NotifyOnUIThread(const std::string& token,
     83                                              const std::string& device_id) {
     84   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     85   if (delegate_.get())
     86     delegate_->OnTokenLoaded(token, device_id);
     87 }
     88 
     89 }  // namespace policy
     90