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.
5 #include "chrome/browser/chromeos/policy/user_policy_token_loader.h"
8 #include "base/files/file_util.h"
9 #include "base/location.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/metrics/histogram.h"
12 #include "base/sequenced_task_runner.h"
13 #include "components/policy/core/common/cloud/enterprise_metrics.h"
14 #include "policy/proto/device_management_local.pb.h"
18 namespace em
= enterprise_management
;
20 UserPolicyTokenLoader::Delegate::~Delegate() {}
22 UserPolicyTokenLoader::UserPolicyTokenLoader(
23 const base::WeakPtr
<Delegate
>& delegate
,
24 const base::FilePath
& cache_file
,
25 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner
)
26 : delegate_(delegate
),
27 cache_file_(cache_file
),
28 origin_task_runner_(base::MessageLoopProxy::current()),
29 background_task_runner_(background_task_runner
) {}
31 void UserPolicyTokenLoader::Load() {
32 DCHECK(origin_task_runner_
->RunsTasksOnCurrentThread());
33 background_task_runner_
->PostTask(
35 base::Bind(&UserPolicyTokenLoader::LoadOnBackgroundThread
, this));
38 UserPolicyTokenLoader::~UserPolicyTokenLoader() {
41 void UserPolicyTokenLoader::LoadOnBackgroundThread() {
42 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
43 std::string device_token
;
44 std::string device_id
;
46 if (base::PathExists(cache_file_
)) {
48 em::DeviceCredentials device_credentials
;
49 if (base::ReadFileToString(cache_file_
, &data
) &&
50 device_credentials
.ParseFromArray(data
.c_str(), data
.size())) {
51 device_token
= device_credentials
.device_token();
52 device_id
= device_credentials
.device_id();
53 UMA_HISTOGRAM_ENUMERATION(policy::kMetricToken
,
54 kMetricTokenLoadSucceeded
,
55 policy::kMetricTokenSize
);
57 UMA_HISTOGRAM_ENUMERATION(policy::kMetricToken
,
58 kMetricTokenLoadFailed
,
59 policy::kMetricTokenSize
);
63 origin_task_runner_
->PostTask(
65 base::Bind(&UserPolicyTokenLoader::NotifyOnOriginThread
,
71 void UserPolicyTokenLoader::NotifyOnOriginThread(const std::string
& token
,
72 const std::string
& device_id
) {
73 DCHECK(origin_task_runner_
->RunsTasksOnCurrentThread());
75 delegate_
->OnTokenLoaded(token
, device_id
);