Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / user_policy_disk_cache.cc
blobc2eab4414c213ec05a6724016f8407184d394694
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"
7 #include "base/bind.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;
19 namespace policy {
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));
36 DCHECK(ret);
39 void UserPolicyDiskCache::Store(
40 const em::CachedCloudPolicyResponse& policy) {
41 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
42 background_task_runner_->PostTask(
43 FROM_HERE,
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);
55 return;
58 // Read the protobuf from the file.
59 std::string data;
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);
64 return;
67 // Decode it.
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);
72 return;
75 LoadDone(LOAD_RESULT_SUCCESS, cached_response);
78 void UserPolicyDiskCache::LoadDone(
79 LoadResult result,
80 const em::CachedCloudPolicyResponse& policy) {
81 origin_task_runner_->PostTask(
82 FROM_HERE,
83 base::Bind(
84 &UserPolicyDiskCache::ReportResultOnUIThread, this, result, policy));
87 void UserPolicyDiskCache::ReportResultOnUIThread(
88 LoadResult result,
89 const em::CachedCloudPolicyResponse& policy) {
90 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
92 switch (result) {
93 case LOAD_RESULT_NOT_FOUND:
94 break;
95 case LOAD_RESULT_READ_ERROR:
96 case LOAD_RESULT_PARSE_ERROR:
97 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy,
98 kMetricPolicyLoadFailed,
99 policy::kMetricPolicySize);
100 break;
101 case LOAD_RESULT_SUCCESS:
102 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy,
103 kMetricPolicyLoadSucceeded,
104 policy::kMetricPolicySize);
105 break;
108 if (delegate_.get())
109 delegate_->OnDiskCacheLoaded(result, policy);
112 void UserPolicyDiskCache::StoreOnFileThread(
113 const em::CachedCloudPolicyResponse& policy) {
114 DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
115 std::string data;
116 if (!policy.SerializeToString(&data)) {
117 LOG(WARNING) << "Failed to serialize policy data";
118 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy,
119 kMetricPolicyStoreFailed,
120 policy::kMetricPolicySize);
121 return;
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);
130 return;
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);
139 return;
141 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy,
142 kMetricPolicyStoreSucceeded,
143 policy::kMetricPolicySize);
146 } // namespace policy