Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / api / storage / policy_value_store.cc
blobcb40641d05428f9dff9af2f1f7d63baa562255bf
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 "components/policy/core/common/policy_map.h"
10 #include "components/policy/core/common/policy_types.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "extensions/browser/api/storage/settings_namespace.h"
13 #include "extensions/browser/value_store/value_store_change.h"
14 #include "extensions/browser/value_store/value_store_util.h"
16 using content::BrowserThread;
18 namespace util = value_store_util;
20 namespace extensions {
22 namespace {
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()));
29 } // namespace
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 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
43 // Convert |policy| to a dictionary value. Only include mandatory policies
44 // for now.
45 base::DictionaryValue current_policy;
46 for (policy::PolicyMap::const_iterator it = policy.begin();
47 it != policy.end(); ++it) {
48 if (it->second.level == policy::POLICY_LEVEL_MANDATORY) {
49 current_policy.SetWithoutPathExpansion(
50 it->first, it->second.value->DeepCopy());
54 // Get the previous policies stored in the database.
55 // TODO(joaodasilva): it'd be better to have a less expensive way of
56 // determining which keys are currently stored, or of determining which keys
57 // must be removed.
58 base::DictionaryValue previous_policy;
59 ValueStore::ReadResult read_result = delegate_->Get();
61 // If the database is corrupted, try to restore it.
62 // This may have the unfortunate side-effect of incorrectly informing the
63 // extension of a "new" key, which isn't new and was corrupted. Unfortunately,
64 // there's not always a way around this - if the database is corrupted, there
65 // may be no way of telling which keys were previously present.
66 if (read_result->IsCorrupted()) {
67 if (delegate_->Restore())
68 read_result = delegate_->Get();
71 if (read_result->HasError()) {
72 LOG(WARNING) << "Failed to read managed settings for extension "
73 << extension_id_ << ": " << read_result->error().message;
74 // Leave |previous_policy| empty, so that events are generated for every
75 // policy in |current_policy|.
76 } else {
77 read_result->settings().Swap(&previous_policy);
80 // Now get two lists of changes: changes after setting the current policies,
81 // and changes after removing old policies that aren't in |current_policy|
82 // anymore.
83 std::vector<std::string> removed_keys;
84 for (base::DictionaryValue::Iterator it(previous_policy);
85 !it.IsAtEnd(); it.Advance()) {
86 if (!current_policy.HasKey(it.key()))
87 removed_keys.push_back(it.key());
90 ValueStoreChangeList changes;
92 WriteResult result = delegate_->Remove(removed_keys);
93 if (!result->HasError()) {
94 changes.insert(
95 changes.end(), result->changes().begin(), result->changes().end());
98 // IGNORE_QUOTA because these settings aren't writable by the extension, and
99 // are configured by the domain administrator.
100 ValueStore::WriteOptions options = ValueStore::IGNORE_QUOTA;
101 result = delegate_->Set(options, current_policy);
102 if (!result->HasError()) {
103 changes.insert(
104 changes.end(), result->changes().begin(), result->changes().end());
107 if (!changes.empty()) {
108 observers_->Notify(FROM_HERE, &SettingsObserver::OnSettingsChanged,
109 extension_id_, settings_namespace::MANAGED,
110 ValueStoreChange::ToJson(changes));
114 void PolicyValueStore::DeleteStorage() {
115 // This is called from our owner, indicating that storage for this extension
116 // should be removed.
117 delegate_->Clear();
120 size_t PolicyValueStore::GetBytesInUse(const std::string& key) {
121 // LeveldbValueStore doesn't implement this; and the underlying database
122 // isn't acccessible to the extension in any case; from the extension's
123 // perspective this is a read-only store.
124 return 0;
127 size_t PolicyValueStore::GetBytesInUse(const std::vector<std::string>& keys) {
128 // See note above.
129 return 0;
132 size_t PolicyValueStore::GetBytesInUse() {
133 // See note above.
134 return 0;
137 ValueStore::ReadResult PolicyValueStore::Get(const std::string& key) {
138 return delegate_->Get(key);
141 ValueStore::ReadResult PolicyValueStore::Get(
142 const std::vector<std::string>& keys) {
143 return delegate_->Get(keys);
146 ValueStore::ReadResult PolicyValueStore::Get() {
147 return delegate_->Get();
150 ValueStore::WriteResult PolicyValueStore::Set(
151 WriteOptions options, const std::string& key, const base::Value& value) {
152 return MakeWriteResult(ReadOnlyError(util::NewKey(key)));
155 ValueStore::WriteResult PolicyValueStore::Set(
156 WriteOptions options, const base::DictionaryValue& settings) {
157 return MakeWriteResult(ReadOnlyError(util::NoKey()));
160 ValueStore::WriteResult PolicyValueStore::Remove(const std::string& key) {
161 return MakeWriteResult(ReadOnlyError(util::NewKey(key)));
164 ValueStore::WriteResult PolicyValueStore::Remove(
165 const std::vector<std::string>& keys) {
166 return MakeWriteResult(ReadOnlyError(util::NoKey()));
169 ValueStore::WriteResult PolicyValueStore::Clear() {
170 return MakeWriteResult(ReadOnlyError(util::NoKey()));
173 bool PolicyValueStore::Restore() { return delegate_->Restore(); }
175 bool PolicyValueStore::RestoreKey(const std::string& key) {
176 return delegate_->RestoreKey(key);
179 } // namespace extensions