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

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: Change to RefCountedThreadSafe Created 9 years, 4 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 ExtensionSettings::ExtensionSettings(const FilePath& base_path)
27 : base_path_(base_path) {
28 }
29
30 ExtensionSettings::~ExtensionSettings() {
31 std::map<std::string, ExtensionSettingsStorage*>::iterator it;
32 for (it = storage_objs_.begin(); it != storage_objs_.end(); ++it) {
33 it->second->DeleteSoon();
34 }
35 }
36
37 void ExtensionSettings::GetStorage(
38 const std::string& extension_id,
39 const Callback& callback) {
40 if (!GetExistingStorage(extension_id, callback)) {
41 StartCreationOfStorage(
42 extension_id,
43 ExtensionSettingsStorage::LEVELDB,
44 ExtensionSettingsStorage::NOOP,
45 true,
46 callback);
47 }
48 }
49
50 void ExtensionSettings::GetStorageForTesting(
51 ExtensionSettingsStorage::Type type,
52 bool cached,
53 const std::string& extension_id,
54 const Callback& callback) {
55 if (!GetExistingStorage(extension_id, callback)) {
56 StartCreationOfStorage(extension_id, type, type, cached, callback);
57 }
58 }
59
60 bool ExtensionSettings::GetExistingStorage(
61 const std::string& extension_id, const Callback& callback) {
62 std::map<std::string, ExtensionSettingsStorage*>::iterator existing =
63 storage_objs_.find(extension_id);
64 if (existing == storage_objs_.end()) {
65 // No existing storage.
66 return false;
67 }
68 // Existing storage. Reply with that.
69 ExtensionSettingsStorage* storage = existing->second;
70 DCHECK(storage != NULL);
71 MessageLoop::current()->PostTask(
72 FROM_HERE,
73 base::Bind(
74 &ExtensionSettings::RunWithStorage,
75 this,
76 new Callback(callback),
77 storage));
78 return true;
79 }
80
81 void ExtensionSettings::RunWithStorage(
82 Callback* callback, ExtensionSettingsStorage* storage) {
83 callback->Run(storage);
84 delete callback;
85 }
86
87 void ExtensionSettings::StartCreationOfStorage(
88 const std::string& extension_id,
89 ExtensionSettingsStorage::Type type,
90 ExtensionSettingsStorage::Type fallback_type,
91 bool cached,
92 const Callback& callback) {
93 BrowserThread::PostTask(
94 BrowserThread::FILE,
95 FROM_HERE,
96 base::Bind(
97 &ExtensionSettings::CreateStorageOnFileThread,
98 this,
99 extension_id,
100 type,
101 fallback_type,
102 cached,
103 callback));
104 }
105
106 void ExtensionSettings::CreateStorageOnFileThread(
107 const std::string& extension_id,
108 ExtensionSettingsStorage::Type type,
109 ExtensionSettingsStorage::Type fallback_type,
110 bool cached,
111 const Callback& callback) {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
113 ExtensionSettingsStorage* storage = CreateStorage(extension_id, type, cached);
114 if (storage == NULL && fallback_type != type) {
115 storage = CreateStorage(extension_id, fallback_type, cached);
116 }
117 DCHECK(storage != NULL);
118 BrowserThread::PostTask(
119 BrowserThread::UI,
120 FROM_HERE,
121 base::Bind(
122 &ExtensionSettings::EndCreationOfStorage,
123 this,
124 extension_id,
125 storage,
126 callback));
127 }
128
129 ExtensionSettingsStorage* ExtensionSettings::CreateStorage(
130 const std::string& extension_id,
131 ExtensionSettingsStorage::Type type,
132 bool cached) {
133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
134 ExtensionSettingsStorage* storage = NULL;
135 switch (type) {
136 case ExtensionSettingsStorage::NOOP:
137 storage = new ExtensionSettingsNoopStorage();
138 break;
139 case ExtensionSettingsStorage::LEVELDB:
140 storage = ExtensionSettingsLeveldbStorage::Create(
141 base_path_, extension_id);
142 break;
143 default:
144 NOTREACHED();
145 }
146 if (storage != NULL && cached) {
147 storage = new ExtensionSettingsStorageCache(storage);
148 }
149 return storage;
150 }
151
152 void ExtensionSettings::EndCreationOfStorage(
153 const std::string& extension_id,
154 ExtensionSettingsStorage* storage,
155 const Callback& callback) {
156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
157 // Cache the result now. To avoid a race condition, check again to see
158 // whether a storage has been created already; if so, use that one.
159 std::map<std::string, ExtensionSettingsStorage*>::iterator existing =
160 storage_objs_.find(extension_id);
161 if (existing == storage_objs_.end()) {
162 storage_objs_[extension_id] = storage;
163 } else {
164 delete storage;
165 storage = existing->second;
166 DCHECK(storage != NULL);
167 }
168 callback.Run(storage);
169 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698