-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolicy_writer.cc
120 lines (95 loc) · 3.93 KB
/
policy_writer.cc
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2018 The Chromium OS 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 "policy_utils/policy_writer.h"
#include <vector>
#include <base/files/file_util.h>
#include <base/json/json_writer.h>
#include <base/values.h>
namespace {
// Property name and corresponding file name for policies.
const char kPolicyDeviceAllowBluetooth[] = "DeviceAllowBluetooth";
const char kPolicyDeviceAllowBluetoothFileName[] =
"device_allow_bluetooth.json";
const char kPolicyShowHomeButton[] = "ShowHomeButton";
const char kPolicyShowHomeButtonFileName[] = "show_home_bbuton.json";
const char kPolicyBookmarkBarEnabled[] = "BookmarkBarEnabled";
const char kPolicyBookmarkBarEnabledFileName[] = "bookmark_bar_enabled.json";
// Create all non-existent directories in path |full_path. Each directory is
// created with permission 0755. Return whether successful.
bool CreateDirectories(const base::FilePath& full_path) {
std::vector<base::FilePath> subpaths;
// Collect a list of all parent directories.
base::FilePath last_path = full_path;
subpaths.push_back(full_path);
for (base::FilePath path = full_path.DirName();
path.value() != last_path.value(); path = path.DirName()) {
subpaths.push_back(path);
last_path = path;
}
// Iterate through the parents and create the missing ones.
for (std::vector<base::FilePath>::reverse_iterator i = subpaths.rbegin();
i != subpaths.rend(); ++i) {
if (!base::DirectoryExists(*i) && mkdir(i->value().c_str(), 0755) != 0) {
return false;
}
}
return true;
}
// Converts the given policy to a JSON string and writes it to file
// <dir_path>/<file_name>. Returns whether successul.
bool WritePolicyToFile(const base::Value& policy,
const base::FilePath& dir_path,
const char* file_name) {
if (!file_name) {
return false;
}
if (!CreateDirectories(dir_path))
return false;
std::string json_string;
base::JSONWriter::Write(policy, &json_string);
base::FilePath file_path = dir_path.Append(file_name);
return base::WriteFile(file_path, json_string.data(), json_string.length()) ==
json_string.length();
}
// Deletes the policy file <dir_path>/<file_name> if it exists. Returns whether
// successful.
bool DeletePolicyFile(const base::FilePath& dir_path, const char* file_name) {
if (!file_name) {
return false;
}
return base::DeleteFile(dir_path.Append(file_name));
}
} // anonymous namespace
namespace policy_utils {
PolicyWriter::PolicyWriter(const std::string& dest_dir_path)
: dest_dir_path_(dest_dir_path) {}
PolicyWriter::~PolicyWriter() {}
bool PolicyWriter::SetDeviceAllowBluetooth(bool is_allowed) const {
base::Value policy(base::Value::Type::DICTIONARY);
policy.SetBoolKey(kPolicyDeviceAllowBluetooth, is_allowed);
return WritePolicyToFile(policy, dest_dir_path_,
kPolicyDeviceAllowBluetoothFileName);
}
bool PolicyWriter::SetShowHomeButton(bool show) const {
base::Value policy(base::Value::Type::DICTIONARY);
policy.SetBoolKey(kPolicyShowHomeButton, show);
return WritePolicyToFile(policy, dest_dir_path_,
kPolicyShowHomeButtonFileName);
}
bool PolicyWriter::SetBookmarkBarEnabled(bool is_enabled) const {
base::Value policy(base::Value::Type::DICTIONARY);
policy.SetBoolKey(kPolicyBookmarkBarEnabled, is_enabled);
return WritePolicyToFile(policy, dest_dir_path_,
kPolicyBookmarkBarEnabledFileName);
}
bool PolicyWriter::ClearDeviceAllowBluetooth() const {
return DeletePolicyFile(dest_dir_path_, kPolicyDeviceAllowBluetoothFileName);
}
bool PolicyWriter::ClearShowHomeButton() const {
return DeletePolicyFile(dest_dir_path_, kPolicyShowHomeButtonFileName);
}
bool PolicyWriter::ClearBookmarkBarEnabled() const {
return DeletePolicyFile(dest_dir_path_, kPolicyBookmarkBarEnabledFileName);
}
} // namespace policy_utils