1 // Copyright 2014 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 "extensions/browser/state_store.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/public/browser/browser_context.h"
10 #include "content/public/browser/notification_service.h"
11 #include "content/public/browser/notification_types.h"
12 #include "extensions/browser/extension_registry.h"
13 #include "extensions/common/extension.h"
17 // Delay, in seconds, before we should open the State Store database. We
18 // defer it to avoid slowing down startup. See http://crbug.com/161848
19 const int kInitDelaySeconds
= 1;
21 std::string
GetFullKey(const std::string
& extension_id
,
22 const std::string
& key
) {
23 return extension_id
+ "." + key
;
28 namespace extensions
{
30 // Helper class to delay tasks until we're ready to start executing them.
31 class StateStore::DelayedTaskQueue
{
33 DelayedTaskQueue() : ready_(false) {}
34 ~DelayedTaskQueue() {}
36 // Queues up a task for invoking once we're ready. Invokes immediately if
37 // we're already ready.
38 void InvokeWhenReady(base::Closure task
);
40 // Marks us ready, and invokes all pending tasks.
43 // Return whether or not the DelayedTaskQueue is |ready_|.
44 bool ready() const { return ready_
; }
48 std::vector
<base::Closure
> pending_tasks_
;
51 void StateStore::DelayedTaskQueue::InvokeWhenReady(base::Closure task
) {
55 pending_tasks_
.push_back(task
);
59 void StateStore::DelayedTaskQueue::SetReady() {
62 for (size_t i
= 0; i
< pending_tasks_
.size(); ++i
)
63 pending_tasks_
[i
].Run();
64 pending_tasks_
.clear();
67 StateStore::StateStore(content::BrowserContext
* context
,
68 const base::FilePath
& db_path
,
71 task_queue_(new DelayedTaskQueue()),
72 extension_registry_observer_(this) {
73 extension_registry_observer_
.Add(ExtensionRegistry::Get(context
));
76 // Don't Init() until the first page is loaded or the embedder explicitly
80 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
81 content::NotificationService::AllBrowserContextsAndSources());
87 StateStore::StateStore(content::BrowserContext
* context
,
88 scoped_ptr
<ValueStore
> value_store
)
89 : store_(value_store
.Pass()),
90 task_queue_(new DelayedTaskQueue()),
91 extension_registry_observer_(this) {
92 extension_registry_observer_
.Add(ExtensionRegistry::Get(context
));
94 // This constructor is for testing. No need to delay Init.
98 StateStore::~StateStore() {
101 void StateStore::RequestInitAfterDelay() {
105 void StateStore::RegisterKey(const std::string
& key
) {
106 registered_keys_
.insert(key
);
109 void StateStore::GetExtensionValue(const std::string
& extension_id
,
110 const std::string
& key
,
111 ReadCallback callback
) {
112 task_queue_
->InvokeWhenReady(base::Bind(&ValueStoreFrontend::Get
,
113 base::Unretained(&store_
),
114 GetFullKey(extension_id
, key
),
118 void StateStore::SetExtensionValue(const std::string
& extension_id
,
119 const std::string
& key
,
120 scoped_ptr
<base::Value
> value
) {
121 task_queue_
->InvokeWhenReady(base::Bind(&ValueStoreFrontend::Set
,
122 base::Unretained(&store_
),
123 GetFullKey(extension_id
, key
),
124 base::Passed(&value
)));
127 void StateStore::RemoveExtensionValue(const std::string
& extension_id
,
128 const std::string
& key
) {
129 task_queue_
->InvokeWhenReady(base::Bind(&ValueStoreFrontend::Remove
,
130 base::Unretained(&store_
),
131 GetFullKey(extension_id
, key
)));
134 bool StateStore::IsInitialized() const {
135 return task_queue_
->ready();
138 void StateStore::Observe(int type
,
139 const content::NotificationSource
& source
,
140 const content::NotificationDetails
& details
) {
141 DCHECK_EQ(type
, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
);
142 registrar_
.RemoveAll();
146 void StateStore::OnExtensionWillBeInstalled(
147 content::BrowserContext
* browser_context
,
148 const Extension
* extension
,
151 const std::string
& old_name
) {
152 RemoveKeysForExtension(extension
->id());
155 void StateStore::OnExtensionUninstalled(
156 content::BrowserContext
* browser_context
,
157 const Extension
* extension
,
158 extensions::UninstallReason reason
) {
159 RemoveKeysForExtension(extension
->id());
162 void StateStore::Init() {
163 // Could be called twice if InitAfterDelay() is requested explicitly by the
164 // embedder in addition to internally after first page load.
168 if (!db_path_
.empty())
169 store_
.Init(db_path_
);
170 task_queue_
->SetReady();
173 void StateStore::InitAfterDelay() {
177 base::MessageLoop::current()->PostDelayedTask(
179 base::Bind(&StateStore::Init
, AsWeakPtr()),
180 base::TimeDelta::FromSeconds(kInitDelaySeconds
));
183 void StateStore::RemoveKeysForExtension(const std::string
& extension_id
) {
184 for (std::set
<std::string
>::iterator key
= registered_keys_
.begin();
185 key
!= registered_keys_
.end();
187 task_queue_
->InvokeWhenReady(base::Bind(&ValueStoreFrontend::Remove
,
188 base::Unretained(&store_
),
189 GetFullKey(extension_id
, *key
)));
193 } // namespace extensions