- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / storage / policy_value_store.cc
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.
4
5 #include "chrome/browser/extensions/api/storage/policy_value_store.h"
6
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"
15
16 using content::BrowserThread;
17
18 namespace util = value_store_util;
19
20 namespace extensions {
21
22 namespace {
23
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()));
27 }
28
29 }  // namespace
30
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()) {}
38
39 PolicyValueStore::~PolicyValueStore() {}
40
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
45   // for now.
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());
52     }
53   }
54
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
58   // must be removed.
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|.
66   } else {
67     read_result->settings().Swap(&previous_policy);
68   }
69
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|
72   // anymore.
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());
78   }
79
80   ValueStoreChangeList changes;
81
82   WriteResult result = delegate_->Remove(removed_keys);
83   if (!result->HasError()) {
84     changes.insert(
85         changes.end(), result->changes().begin(), result->changes().end());
86   }
87
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()) {
93     changes.insert(
94         changes.end(), result->changes().begin(), result->changes().end());
95   }
96
97   if (!changes.empty() && notify_if_changed) {
98     observers_->Notify(
99         &SettingsObserver::OnSettingsChanged,
100         extension_id_,
101         settings_namespace::MANAGED,
102         ValueStoreChange::ToJson(changes));
103   }
104 }
105
106 void PolicyValueStore::DeleteStorage() {
107   // This is called from our owner, indicating that storage for this extension
108   // should be removed.
109   delegate_->Clear();
110 }
111
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.
116   return 0;
117 }
118
119 size_t PolicyValueStore::GetBytesInUse(const std::vector<std::string>& keys) {
120   // See note above.
121   return 0;
122 }
123
124 size_t PolicyValueStore::GetBytesInUse() {
125   // See note above.
126   return 0;
127 }
128
129 ValueStore::ReadResult PolicyValueStore::Get(const std::string& key) {
130   return delegate_->Get(key);
131 }
132
133 ValueStore::ReadResult PolicyValueStore::Get(
134     const std::vector<std::string>& keys) {
135   return delegate_->Get(keys);
136 }
137
138 ValueStore::ReadResult PolicyValueStore::Get() {
139   return delegate_->Get();
140 }
141
142 ValueStore::WriteResult PolicyValueStore::Set(
143     WriteOptions options, const std::string& key, const base::Value& value) {
144   return MakeWriteResult(ReadOnlyError(util::NewKey(key)));
145 }
146
147 ValueStore::WriteResult PolicyValueStore::Set(
148     WriteOptions options, const base::DictionaryValue& settings) {
149   return MakeWriteResult(ReadOnlyError(util::NoKey()));
150 }
151
152 ValueStore::WriteResult PolicyValueStore::Remove(const std::string& key) {
153   return MakeWriteResult(ReadOnlyError(util::NewKey(key)));
154 }
155
156 ValueStore::WriteResult PolicyValueStore::Remove(
157     const std::vector<std::string>& keys) {
158   return MakeWriteResult(ReadOnlyError(util::NoKey()));
159 }
160
161 ValueStore::WriteResult PolicyValueStore::Clear() {
162   return MakeWriteResult(ReadOnlyError(util::NoKey()));
163 }
164
165 }  // namespace extensions