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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// Copyright (c) 2012 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 "chrome/browser/extensions/settings/policy_value_store.h"
#include "base/logging.h"
#include "base/values.h"
#include "chrome/browser/extensions/settings/settings_namespace.h"
#include "chrome/browser/policy/policy_map.h"
#include "chrome/browser/policy/policy_types.h"
#include "chrome/browser/value_store/value_store_change.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace extensions {
namespace {
const char kReadOnlyStoreErrorMessage[] = "This is a read-only store.";
ValueStore::WriteResult WriteResultError() {
return ValueStore::MakeWriteResult(kReadOnlyStoreErrorMessage);
}
} // namespace
PolicyValueStore::PolicyValueStore(
const std::string& extension_id,
const scoped_refptr<SettingsObserverList>& observers,
scoped_ptr<ValueStore> delegate)
: extension_id_(extension_id),
observers_(observers),
delegate_(delegate.Pass()) {}
PolicyValueStore::~PolicyValueStore() {}
void PolicyValueStore::SetCurrentPolicy(const policy::PolicyMap& policy,
bool notify_if_changed) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
// Convert |policy| to a dictionary value. Only include mandatory policies
// for now.
base::DictionaryValue current_policy;
for (policy::PolicyMap::const_iterator it = policy.begin();
it != policy.end(); ++it) {
if (it->second.level == policy::POLICY_LEVEL_MANDATORY) {
current_policy.SetWithoutPathExpansion(
it->first, it->second.value->DeepCopy());
}
}
// Get the previous policies stored in the database.
// TODO(joaodasilva): it'd be better to have a less expensive way of
// determining which keys are currently stored, or of determining which keys
// must be removed.
base::DictionaryValue previous_policy;
ValueStore::ReadResult read_result = delegate_->Get();
if (read_result->HasError()) {
LOG(WARNING) << "Failed to read managed settings for extension "
<< extension_id_ << ": " << read_result->error();
// Leave |previous_policy| empty, so that events are generated for every
// policy in |current_policy|.
} else {
read_result->settings()->Swap(&previous_policy);
}
// Now get two lists of changes: changes after setting the current policies,
// and changes after removing old policies that aren't in |current_policy|
// anymore.
std::vector<std::string> removed_keys;
for (base::DictionaryValue::Iterator it(previous_policy);
it.HasNext(); it.Advance()) {
if (!current_policy.HasKey(it.key()))
removed_keys.push_back(it.key());
}
ValueStoreChangeList changes;
WriteResult result = delegate_->Remove(removed_keys);
if (!result->HasError()) {
changes.insert(
changes.end(), result->changes().begin(), result->changes().end());
}
// IGNORE_QUOTA because these settings aren't writable by the extension, and
// are configured by the domain administrator.
ValueStore::WriteOptions options = ValueStore::IGNORE_QUOTA;
result = delegate_->Set(options, current_policy);
if (!result->HasError()) {
changes.insert(
changes.end(), result->changes().begin(), result->changes().end());
}
if (!changes.empty() && notify_if_changed) {
observers_->Notify(
&SettingsObserver::OnSettingsChanged,
extension_id_,
settings_namespace::MANAGED,
ValueStoreChange::ToJson(changes));
}
}
void PolicyValueStore::DeleteStorage() {
// This is called from our owner, indicating that storage for this extension
// should be removed.
delegate_->Clear();
}
size_t PolicyValueStore::GetBytesInUse(const std::string& key) {
// LeveldbValueStore doesn't implement this; and the underlying database
// isn't acccessible to the extension in any case; from the extension's
// perspective this is a read-only store.
return 0;
}
size_t PolicyValueStore::GetBytesInUse(const std::vector<std::string>& keys) {
// See note above.
return 0;
}
size_t PolicyValueStore::GetBytesInUse() {
// See note above.
return 0;
}
ValueStore::ReadResult PolicyValueStore::Get(const std::string& key) {
return delegate_->Get(key);
}
ValueStore::ReadResult PolicyValueStore::Get(
const std::vector<std::string>& keys) {
return delegate_->Get(keys);
}
ValueStore::ReadResult PolicyValueStore::Get() {
return delegate_->Get();
}
ValueStore::WriteResult PolicyValueStore::Set(
WriteOptions options, const std::string& key, const base::Value& value) {
return WriteResultError();
}
ValueStore::WriteResult PolicyValueStore::Set(
WriteOptions options, const base::DictionaryValue& settings) {
return WriteResultError();
}
ValueStore::WriteResult PolicyValueStore::Remove(const std::string& key) {
return WriteResultError();
}
ValueStore::WriteResult PolicyValueStore::Remove(
const std::vector<std::string>& keys) {
return WriteResultError();
}
ValueStore::WriteResult PolicyValueStore::Clear() {
return WriteResultError();
}
} // namespace extensions
|