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/message_loop/message_loop_proxy.h"
12 #include "base/metrics/histogram.h"
13 #include "base/sequenced_task_runner.h"
14 #include "components/policy/core/common/cloud/enterprise_metrics.h"
15 #include "policy/proto/device_management_local.pb.h"
17 namespace em
= enterprise_management
;
21 UserPolicyDiskCache::Delegate::~Delegate() {}
23 UserPolicyDiskCache::UserPolicyDiskCache(
24 const base::WeakPtr
<Delegate
>& delegate
,
25 const base::FilePath
& backing_file_path
,
26 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner
)
27 : delegate_(delegate
),
28 backing_file_path_(backing_file_path
),
29 origin_task_runner_(base::MessageLoopProxy::current()),
30 background_task_runner_(background_task_runner
) {}
32 void UserPolicyDiskCache::Load() {
33 DCHECK(origin_task_runner_
->RunsTasksOnCurrentThread());
34 bool ret
= background_task_runner_
->PostTask(
35 FROM_HERE
, base::Bind(&UserPolicyDiskCache::LoadOnFileThread
, this));
39 void UserPolicyDiskCache::Store(
40 const em::CachedCloudPolicyResponse
& policy
) {
41 DCHECK(origin_task_runner_
->RunsTasksOnCurrentThread());
42 background_task_runner_
->PostTask(
44 base::Bind(&UserPolicyDiskCache::StoreOnFileThread
, this, policy
));
47 UserPolicyDiskCache::~UserPolicyDiskCache() {}
49 void UserPolicyDiskCache::LoadOnFileThread() {
50 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
52 em::CachedCloudPolicyResponse cached_response
;
53 if (!base::PathExists(backing_file_path_
)) {
54 LoadDone(LOAD_RESULT_NOT_FOUND
, cached_response
);
58 // Read the protobuf from the file.
60 if (!base::ReadFileToString(backing_file_path_
, &data
)) {
61 LOG(WARNING
) << "Failed to read policy data from "
62 << backing_file_path_
.value();
63 LoadDone(LOAD_RESULT_READ_ERROR
, cached_response
);
68 if (!cached_response
.ParseFromArray(data
.c_str(), data
.size())) {
69 LOG(WARNING
) << "Failed to parse policy data read from "
70 << backing_file_path_
.value();
71 LoadDone(LOAD_RESULT_PARSE_ERROR
, cached_response
);
75 LoadDone(LOAD_RESULT_SUCCESS
, cached_response
);
78 void UserPolicyDiskCache::LoadDone(
80 const em::CachedCloudPolicyResponse
& policy
) {
81 origin_task_runner_
->PostTask(
84 &UserPolicyDiskCache::ReportResultOnUIThread
, this, result
, policy
));
87 void UserPolicyDiskCache::ReportResultOnUIThread(
89 const em::CachedCloudPolicyResponse
& policy
) {
90 DCHECK(origin_task_runner_
->RunsTasksOnCurrentThread());
93 case LOAD_RESULT_NOT_FOUND
:
95 case LOAD_RESULT_READ_ERROR
:
96 case LOAD_RESULT_PARSE_ERROR
:
97 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy
,
98 kMetricPolicyLoadFailed
,
99 policy::kMetricPolicySize
);
101 case LOAD_RESULT_SUCCESS
:
102 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy
,
103 kMetricPolicyLoadSucceeded
,
104 policy::kMetricPolicySize
);
109 delegate_
->OnDiskCacheLoaded(result
, policy
);
112 void UserPolicyDiskCache::StoreOnFileThread(
113 const em::CachedCloudPolicyResponse
& policy
) {
114 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
116 if (!policy
.SerializeToString(&data
)) {
117 LOG(WARNING
) << "Failed to serialize policy data";
118 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy
,
119 kMetricPolicyStoreFailed
,
120 policy::kMetricPolicySize
);
124 if (!base::CreateDirectory(backing_file_path_
.DirName())) {
125 LOG(WARNING
) << "Failed to create directory "
126 << backing_file_path_
.DirName().value();
127 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy
,
128 kMetricPolicyStoreFailed
,
129 policy::kMetricPolicySize
);
133 int size
= data
.size();
134 if (base::WriteFile(backing_file_path_
, data
.c_str(), size
) != size
) {
135 LOG(WARNING
) << "Failed to write " << backing_file_path_
.value();
136 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy
,
137 kMetricPolicyStoreFailed
,
138 policy::kMetricPolicySize
);
141 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy
,
142 kMetricPolicyStoreSucceeded
,
143 policy::kMetricPolicySize
);
146 } // namespace policy