1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/file_util.h"
#include "base/logging.h"
#include "base/task.h"
#include "chrome/browser/policy/proto/device_management_local.pb.h"
#include "chrome/browser/policy/user_policy_disk_cache.h"
#include "content/browser/browser_thread.h"
namespace policy {
UserPolicyDiskCache::Delegate::~Delegate() {}
UserPolicyDiskCache::UserPolicyDiskCache(
const base::WeakPtr<Delegate>& delegate,
const FilePath& backing_file_path)
: delegate_(delegate),
backing_file_path_(backing_file_path) {}
void UserPolicyDiskCache::Load() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
NewRunnableMethod(this, &UserPolicyDiskCache::LoadOnFileThread));
}
void UserPolicyDiskCache::Store(
const em::CachedCloudPolicyResponse& policy) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
NewRunnableMethod(this, &UserPolicyDiskCache::StoreOnFileThread, policy));
}
UserPolicyDiskCache::~UserPolicyDiskCache() {}
void UserPolicyDiskCache::LoadOnFileThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
if (!file_util::PathExists(backing_file_path_))
return;
// Read the protobuf from the file.
std::string data;
if (!file_util::ReadFileToString(backing_file_path_, &data)) {
LOG(WARNING) << "Failed to read policy data from "
<< backing_file_path_.value();
return;
}
// Decode it.
em::CachedCloudPolicyResponse cached_response;
if (!cached_response.ParseFromArray(data.c_str(), data.size())) {
LOG(WARNING) << "Failed to parse policy data read from "
<< backing_file_path_.value();
return;
}
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableMethod(this,
&UserPolicyDiskCache::FinishLoadOnUIThread,
cached_response));
}
void UserPolicyDiskCache::FinishLoadOnUIThread(
const em::CachedCloudPolicyResponse& policy) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (delegate_.get())
delegate_->OnDiskCacheLoaded(policy);
}
void UserPolicyDiskCache::StoreOnFileThread(
const em::CachedCloudPolicyResponse& policy) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
std::string data;
if (!policy.SerializeToString(&data)) {
LOG(WARNING) << "Failed to serialize policy data";
return;
}
if (!file_util::CreateDirectory(backing_file_path_.DirName())) {
LOG(WARNING) << "Failed to create directory "
<< backing_file_path_.DirName().value();
return;
}
int size = data.size();
if (file_util::WriteFile(backing_file_path_, data.c_str(), size) != size) {
LOG(WARNING) << "Failed to write " << backing_file_path_.value();
return;
}
}
} // namespace policy
|