1 // Copyright (c) 2012 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_disk_cache.h"
8 #include "base/files/file_util.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "components/policy/core/common/cloud/enterprise_metrics.h"
16 #include "policy/proto/device_management_local.pb.h"
18 namespace em
= enterprise_management
;
22 UserPolicyDiskCache::Delegate::~Delegate() {}
24 UserPolicyDiskCache::UserPolicyDiskCache(
25 const base::WeakPtr
<Delegate
>& delegate
,
26 const base::FilePath
& backing_file_path
,
27 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner
)
28 : delegate_(delegate
),
29 backing_file_path_(backing_file_path
),
30 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()),
31 background_task_runner_(background_task_runner
) {
34 void UserPolicyDiskCache::Load() {
35 DCHECK(origin_task_runner_
->RunsTasksOnCurrentThread());
36 bool ret
= background_task_runner_
->PostTask(
37 FROM_HERE
, base::Bind(&UserPolicyDiskCache::LoadOnFileThread
, this));
41 void UserPolicyDiskCache::Store(
42 const em::CachedCloudPolicyResponse
& policy
) {
43 DCHECK(origin_task_runner_
->RunsTasksOnCurrentThread());
44 background_task_runner_
->PostTask(
46 base::Bind(&UserPolicyDiskCache::StoreOnFileThread
, this, policy
));
49 UserPolicyDiskCache::~UserPolicyDiskCache() {}
51 void UserPolicyDiskCache::LoadOnFileThread() {
52 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
54 em::CachedCloudPolicyResponse cached_response
;
55 if (!base::PathExists(backing_file_path_
)) {
56 LoadDone(LOAD_RESULT_NOT_FOUND
, cached_response
);
60 // Read the protobuf from the file.
62 if (!base::ReadFileToString(backing_file_path_
, &data
)) {
63 LOG(WARNING
) << "Failed to read policy data from "
64 << backing_file_path_
.value();
65 LoadDone(LOAD_RESULT_READ_ERROR
, cached_response
);
70 if (!cached_response
.ParseFromArray(data
.c_str(), data
.size())) {
71 LOG(WARNING
) << "Failed to parse policy data read from "
72 << backing_file_path_
.value();
73 LoadDone(LOAD_RESULT_PARSE_ERROR
, cached_response
);
77 LoadDone(LOAD_RESULT_SUCCESS
, cached_response
);
80 void UserPolicyDiskCache::LoadDone(
82 const em::CachedCloudPolicyResponse
& policy
) {
83 origin_task_runner_
->PostTask(
86 &UserPolicyDiskCache::ReportResultOnUIThread
, this, result
, policy
));
89 void UserPolicyDiskCache::ReportResultOnUIThread(
91 const em::CachedCloudPolicyResponse
& policy
) {
92 DCHECK(origin_task_runner_
->RunsTasksOnCurrentThread());
95 case LOAD_RESULT_NOT_FOUND
:
97 case LOAD_RESULT_READ_ERROR
:
98 case LOAD_RESULT_PARSE_ERROR
:
99 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy
,
100 kMetricPolicyLoadFailed
,
101 policy::kMetricPolicySize
);
103 case LOAD_RESULT_SUCCESS
:
104 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy
,
105 kMetricPolicyLoadSucceeded
,
106 policy::kMetricPolicySize
);
111 delegate_
->OnDiskCacheLoaded(result
, policy
);
114 void UserPolicyDiskCache::StoreOnFileThread(
115 const em::CachedCloudPolicyResponse
& policy
) {
116 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
118 if (!policy
.SerializeToString(&data
)) {
119 LOG(WARNING
) << "Failed to serialize policy data";
120 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy
,
121 kMetricPolicyStoreFailed
,
122 policy::kMetricPolicySize
);
126 if (!base::CreateDirectory(backing_file_path_
.DirName())) {
127 LOG(WARNING
) << "Failed to create directory "
128 << backing_file_path_
.DirName().value();
129 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy
,
130 kMetricPolicyStoreFailed
,
131 policy::kMetricPolicySize
);
135 int size
= data
.size();
136 if (base::WriteFile(backing_file_path_
, data
.c_str(), size
) != size
) {
137 LOG(WARNING
) << "Failed to write " << backing_file_path_
.value();
138 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy
,
139 kMetricPolicyStoreFailed
,
140 policy::kMetricPolicySize
);
143 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy
,
144 kMetricPolicyStoreSucceeded
,
145 policy::kMetricPolicySize
);
148 } // namespace policy