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

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

Powered by Google App Engine
This is Rietveld 408576698