Fix the duplicatedly commited composition text when switching IME.
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / user_policy_disk_cache.cc
blob84fca893e0e8e83bbf5a3c63961ead7c6f1ab7d2
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/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;
20 namespace policy {
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));
38 DCHECK(ret);
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));
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);
57 return;
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;
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;
77 LoadDone(LOAD_RESULT_SUCCESS, cached_response);
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));
89 void UserPolicyDiskCache::ReportResultOnUIThread(
90 LoadResult result,
91 const em::CachedCloudPolicyResponse& policy) {
92 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
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;
110 if (delegate_.get())
111 delegate_->OnDiskCacheLoaded(result, policy);
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;
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;
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;
143 UMA_HISTOGRAM_ENUMERATION(policy::kMetricPolicy,
144 kMetricPolicyStoreSucceeded,
145 policy::kMetricPolicySize);
148 } // namespace policy