Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: chrome/browser/chromeos/policy/user_policy_disk_cache.cc

Issue 2681093005: Remove UserPolicyDiskCache; it is not used. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/chromeos/policy/user_policy_disk_cache.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/chromeos/policy/user_policy_disk_cache.h"
6
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/metrics/histogram_macros.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/threading/thread_task_runner_handle.h"
15 #include "components/policy/core/common/cloud/enterprise_metrics.h"
16 #include "components/policy/proto/device_management_local.pb.h"
17
18 namespace em = enterprise_management;
19
20 namespace policy {
21
22 UserPolicyDiskCache::Delegate::~Delegate() {}
23
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) {
32 }
33
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));
38 DCHECK(ret);
39 }
40
41 void UserPolicyDiskCache::Store(
42 const em::CachedCloudPolicyResponse& policy) {
43 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
44 background_task_runner_->PostTask(
45 FROM_HERE,
46 base::Bind(&UserPolicyDiskCache::StoreOnFileThread, this, policy));
47 }
48
49 UserPolicyDiskCache::~UserPolicyDiskCache() {}
50
51 void UserPolicyDiskCache::LoadOnFileThread() {
52 DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
53
54 em::CachedCloudPolicyResponse cached_response;
55 if (!base::PathExists(backing_file_path_)) {
56 LoadDone(LOAD_RESULT_NOT_FOUND, cached_response);
57 return;
58 }
59
60 // Read the protobuf from the file.
61 std::string data;
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);
66 return;
67 }
68
69 // Decode it.
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);
74 return;
75 }
76
77 LoadDone(LOAD_RESULT_SUCCESS, cached_response);
78 }
79
80 void UserPolicyDiskCache::LoadDone(
81 LoadResult result,
82 const em::CachedCloudPolicyResponse& policy) {
83 origin_task_runner_->PostTask(
84 FROM_HERE,
85 base::Bind(
86 &UserPolicyDiskCache::ReportResultOnUIThread, this, result, policy));
87 }
88
89 void UserPolicyDiskCache::ReportResultOnUIThread(
90 LoadResult result,
91 const em::CachedCloudPolicyResponse& policy) {
92 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
93
94 switch (result) {
95 case LOAD_RESULT_NOT_FOUND:
96 break;
97 case LOAD_RESULT_READ_ERROR:
98 case LOAD_RESULT_PARSE_ERROR:
99 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy,
100 kMetricPolicyLoadFailed,
101 policy::kMetricPolicySize);
102 break;
103 case LOAD_RESULT_SUCCESS:
104 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy,
105 kMetricPolicyLoadSucceeded,
106 policy::kMetricPolicySize);
107 break;
108 }
109
110 if (delegate_.get())
111 delegate_->OnDiskCacheLoaded(result, policy);
112 }
113
114 void UserPolicyDiskCache::StoreOnFileThread(
115 const em::CachedCloudPolicyResponse& policy) {
116 DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
117 std::string data;
118 if (!policy.SerializeToString(&data)) {
119 LOG(WARNING) << "Failed to serialize policy data";
120 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy,
121 kMetricPolicyStoreFailed,
122 policy::kMetricPolicySize);
123 return;
124 }
125
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);
132 return;
133 }
134
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);
141 return;
142 }
143 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy,
144 kMetricPolicyStoreSucceeded,
145 policy::kMetricPolicySize);
146 }
147
148 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/policy/user_policy_disk_cache.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698