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/extensions/api/storage/policy_value_store.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/api/storage/settings_namespace.h"
10 #include "chrome/browser/policy/policy_map.h"
11 #include "chrome/browser/policy/policy_types.h"
12 #include "chrome/browser/value_store/value_store_change.h"
13 #include "chrome/browser/value_store/value_store_util.h"
14 #include "content/public/browser/browser_thread.h"
16 using content::BrowserThread;
18 namespace util = value_store_util;
20 namespace extensions {
24 scoped_ptr<ValueStore::Error> ReadOnlyError(scoped_ptr<std::string> key) {
25 return make_scoped_ptr(new ValueStore::Error(
26 ValueStore::READ_ONLY, "This is a read-only store.", key.Pass()));
31 PolicyValueStore::PolicyValueStore(
32 const std::string& extension_id,
33 const scoped_refptr<SettingsObserverList>& observers,
34 scoped_ptr<ValueStore> delegate)
35 : extension_id_(extension_id),
36 observers_(observers),
37 delegate_(delegate.Pass()) {}
39 PolicyValueStore::~PolicyValueStore() {}
41 void PolicyValueStore::SetCurrentPolicy(const policy::PolicyMap& policy,
42 bool notify_if_changed) {
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
44 // Convert |policy| to a dictionary value. Only include mandatory policies
46 base::DictionaryValue current_policy;
47 for (policy::PolicyMap::const_iterator it = policy.begin();
48 it != policy.end(); ++it) {
49 if (it->second.level == policy::POLICY_LEVEL_MANDATORY) {
50 current_policy.SetWithoutPathExpansion(
51 it->first, it->second.value->DeepCopy());
55 // Get the previous policies stored in the database.
56 // TODO(joaodasilva): it'd be better to have a less expensive way of
57 // determining which keys are currently stored, or of determining which keys
59 base::DictionaryValue previous_policy;
60 ValueStore::ReadResult read_result = delegate_->Get();
61 if (read_result->HasError()) {
62 LOG(WARNING) << "Failed to read managed settings for extension "
63 << extension_id_ << ": " << read_result->error().message;
64 // Leave |previous_policy| empty, so that events are generated for every
65 // policy in |current_policy|.
67 read_result->settings().Swap(&previous_policy);
70 // Now get two lists of changes: changes after setting the current policies,
71 // and changes after removing old policies that aren't in |current_policy|
73 std::vector<std::string> removed_keys;
74 for (base::DictionaryValue::Iterator it(previous_policy);
75 !it.IsAtEnd(); it.Advance()) {
76 if (!current_policy.HasKey(it.key()))
77 removed_keys.push_back(it.key());
80 ValueStoreChangeList changes;
82 WriteResult result = delegate_->Remove(removed_keys);
83 if (!result->HasError()) {
85 changes.end(), result->changes().begin(), result->changes().end());
88 // IGNORE_QUOTA because these settings aren't writable by the extension, and
89 // are configured by the domain administrator.
90 ValueStore::WriteOptions options = ValueStore::IGNORE_QUOTA;
91 result = delegate_->Set(options, current_policy);
92 if (!result->HasError()) {
94 changes.end(), result->changes().begin(), result->changes().end());
97 if (!changes.empty() && notify_if_changed) {
99 &SettingsObserver::OnSettingsChanged,
101 settings_namespace::MANAGED,
102 ValueStoreChange::ToJson(changes));
106 void PolicyValueStore::DeleteStorage() {
107 // This is called from our owner, indicating that storage for this extension
108 // should be removed.
112 size_t PolicyValueStore::GetBytesInUse(const std::string& key) {
113 // LeveldbValueStore doesn't implement this; and the underlying database
114 // isn't acccessible to the extension in any case; from the extension's
115 // perspective this is a read-only store.
119 size_t PolicyValueStore::GetBytesInUse(const std::vector<std::string>& keys) {
124 size_t PolicyValueStore::GetBytesInUse() {
129 ValueStore::ReadResult PolicyValueStore::Get(const std::string& key) {
130 return delegate_->Get(key);
133 ValueStore::ReadResult PolicyValueStore::Get(
134 const std::vector<std::string>& keys) {
135 return delegate_->Get(keys);
138 ValueStore::ReadResult PolicyValueStore::Get() {
139 return delegate_->Get();
142 ValueStore::WriteResult PolicyValueStore::Set(
143 WriteOptions options, const std::string& key, const base::Value& value) {
144 return MakeWriteResult(ReadOnlyError(util::NewKey(key)));
147 ValueStore::WriteResult PolicyValueStore::Set(
148 WriteOptions options, const base::DictionaryValue& settings) {
149 return MakeWriteResult(ReadOnlyError(util::NoKey()));
152 ValueStore::WriteResult PolicyValueStore::Remove(const std::string& key) {
153 return MakeWriteResult(ReadOnlyError(util::NewKey(key)));
156 ValueStore::WriteResult PolicyValueStore::Remove(
157 const std::vector<std::string>& keys) {
158 return MakeWriteResult(ReadOnlyError(util::NoKey()));
161 ValueStore::WriteResult PolicyValueStore::Clear() {
162 return MakeWriteResult(ReadOnlyError(util::NoKey()));
165 } // namespace extensions