Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: chrome/browser/extensions/extension_settings.cc

Issue 7189029: Implement an initial extension settings API. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: dgrogan comments #2, mihai comments #1 Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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/extension_settings.h"
6
7 #include "base/bind.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "content/browser/browser_thread.h"
13 #include "chrome/browser/extensions/extension_settings_leveldb_storage.h"
14 #include "chrome/browser/extensions/extension_settings_noop_storage.h"
15 #include "chrome/browser/extensions/extension_settings_storage_cache.h"
16 #include "third_party/leveldb/include/leveldb/iterator.h"
17 #include "third_party/leveldb/include/leveldb/write_batch.h"
18
19 // TODO(kalman): Policy for dots in the key names. Make sure the behaviour of
20 // DictionaryValue and JSON classes is consistent with leveldb.
21 // TODO(kalman): Integration tests.
22 // TODO(kalman): More unit tests (see extension_settings_storage_unittest.cc).
23 // TODO(kalman): Use structured cloning rather than JSON.
24 // TODO(kalman): Quotas.
25
26 namespace {
27
28 // Creates a storage area of a requested type on the FILE thread.
29 class CreateStorageClosure {
30 public:
31 // Ownership of callback is taken.
32 CreateStorageClosure(
33 const FilePath& base_path,
34 std::map<std::string, ExtensionSettingsStorage*>* storage_objs,
35 const std::string& extension_id,
36 ExtensionSettingsStorage::Type type,
37 ExtensionSettingsStorage::Type fallback_type,
38 bool cached,
39 ExtensionSettings::Callback* callback)
40 : base_path_(base_path), storage_objs_(storage_objs),
41 extension_id_(extension_id), type_(type), fallback_type_(fallback_type),
42 cached_(cached), callback_(callback), storage_(NULL) {}
43
44 ~CreateStorageClosure() {}
45
46 void Run() {
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
48 BrowserThread::PostTask(
49 BrowserThread::FILE,
50 FROM_HERE,
51 base::Bind(
52 &CreateStorageClosure::RunOnFileThread, base::Unretained(this)));
53 }
54
55 private:
56 void RunOnFileThread() {
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
58 storage_ = CreateStorage(type_);
59 if (storage_ == NULL && type_ != fallback_type_) {
60 storage_ = CreateStorage(fallback_type_);
61 }
62 BrowserThread::PostTask(
63 BrowserThread::UI,
64 FROM_HERE,
65 base::Bind(&CreateStorageClosure::Done, base::Unretained(this)));
66 }
67
68 void Done() {
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
70 DCHECK(storage_ != NULL);
71 // Cache the result now. To avoid a race condition, check again to see
72 // whether a storage has been created already; if so, use that one.
73 std::map<std::string, ExtensionSettingsStorage*>::iterator existing =
74 storage_objs_->find(extension_id_);
75 if (existing == storage_objs_->end()) {
76 (*storage_objs_)[extension_id_] = storage_;
77 } else {
78 delete storage_;
79 storage_ = existing->second;
80 DCHECK(storage_ != NULL);
81 }
82 callback_->Run(storage_);
83 delete this;
84 }
85
86 // Creates a storage object of a given type. If that fails, returns NULL.
87 ExtensionSettingsStorage* CreateStorage(ExtensionSettingsStorage::Type type) {
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
89 ExtensionSettingsStorage* storage = NULL;
90
91 switch (type) {
92 case ExtensionSettingsStorage::NOOP:
93 storage = new ExtensionSettingsNoopStorage();
94 break;
95 case ExtensionSettingsStorage::LEVELDB:
96 storage = ExtensionSettingsLeveldbStorage::Create(
97 base_path_, extension_id_);
98 break;
99 default:
100 NOTREACHED();
101 }
102
103 if (storage != NULL && cached_) {
104 storage = new ExtensionSettingsStorageCache(storage);
105 }
106
107 return storage;
108 }
109
110 const FilePath& base_path_;
111 std::map<std::string, ExtensionSettingsStorage*>* storage_objs_;
112 std::string extension_id_;
113 ExtensionSettingsStorage::Type type_;
114 ExtensionSettingsStorage::Type fallback_type_;
115 bool cached_;
116 scoped_ptr<ExtensionSettings::Callback> callback_;
117
118 ExtensionSettingsStorage* storage_;
119 };
120
121 // Returns a specified storage object on the message loop of the current thread.
122 // Used to force an asynchronous API even when a storage area already exists.
123 class GetExistingStorageClosure {
124 public:
125 GetExistingStorageClosure(
126 ExtensionSettings::Callback* callback,
127 ExtensionSettingsStorage* storage)
128 : callback_(callback), storage_(storage) {}
129
130 ~GetExistingStorageClosure() {}
131
132 void Run() {
133 MessageLoop::current()->PostTask(FROM_HERE,
134 base::Bind(&GetExistingStorageClosure::Run2, base::Unretained(this)));
135 }
136
137 private:
138 void Run2() {
139 callback_->Run(storage_);
140 delete this;
141 }
142
143 scoped_ptr<ExtensionSettings::Callback> callback_;
144 ExtensionSettingsStorage* storage_;
145 };
146
147 } // namespace
148
149 ExtensionSettings::ExtensionSettings(const FilePath& base_path)
150 : base_path_(base_path) {
151 }
152
153 ExtensionSettings::~ExtensionSettings() {
154 std::map<std::string, ExtensionSettingsStorage*>::iterator it;
155 for (it = storage_objs_.begin(); it != storage_objs_.end(); ++it) {
156 it->second->DestroyEventually();
157 }
158 }
159
160 void ExtensionSettings::GetStorage(const std::string& extension_id,
161 ExtensionSettings::Callback* callback) {
162 if (!GetExistingStorage(extension_id, callback)) {
163 (new CreateStorageClosure(
164 base_path_,
165 &storage_objs_,
Matt Perry 2011/06/23 18:11:45 What if ExtensionSettings is deleted before Create
not at google - send to devlin 2011/06/27 08:51:02 Hm. So I guess in the case where 1. GetStorage()
166 extension_id,
167 ExtensionSettingsStorage::LEVELDB,
168 ExtensionSettingsStorage::NOOP,
169 true,
170 callback))->Run();
171 }
172 }
173
174 void ExtensionSettings::GetStorageForTesting(
175 ExtensionSettingsStorage::Type type,
176 bool cached,
177 const std::string& extension_id,
178 Callback* callback) {
179 if (!GetExistingStorage(extension_id, callback)) {
180 (new CreateStorageClosure(
181 base_path_,
182 &storage_objs_,
183 extension_id,
184 type,
185 type,
186 cached,
187 callback))->Run();
188 }
189 }
190
191 bool ExtensionSettings::GetExistingStorage(const std::string& extension_id,
192 ExtensionSettings::Callback* callback) {
193 std::map<std::string, ExtensionSettingsStorage*>::iterator existing =
194 storage_objs_.find(extension_id);
195 if (existing == storage_objs_.end()) {
196 // No existing storage.
197 return false;
198 }
199 // Existing storage. Reply with that.
200 ExtensionSettingsStorage* storage = existing->second;
201 DCHECK(storage != NULL);
202 (new GetExistingStorageClosure(callback, storage))->Run();
203 return true;
204 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698