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.
5 #include "chrome/browser/extensions/state_store.h"
8 #include "base/message_loop/message_loop.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "content/public/browser/notification_service.h"
11 #include "content/public/browser/notification_types.h"
12 #include "extensions/common/extension.h"
16 // Delay, in seconds, before we should open the State Store database. We
17 // defer it to avoid slowing down startup. See http://crbug.com/161848
18 const int kInitDelaySeconds
= 1;
20 std::string
GetFullKey(const std::string
& extension_id
,
21 const std::string
& key
) {
22 return extension_id
+ "." + key
;
27 namespace extensions
{
29 // Helper class to delay tasks until we're ready to start executing them.
30 class StateStore::DelayedTaskQueue
{
32 DelayedTaskQueue() : ready_(false) {}
33 ~DelayedTaskQueue() {}
35 // Queues up a task for invoking once we're ready. Invokes immediately if
36 // we're already ready.
37 void InvokeWhenReady(base::Closure task
);
39 // Marks us ready, and invokes all pending tasks.
44 std::vector
<base::Closure
> pending_tasks_
;
47 void StateStore::DelayedTaskQueue::InvokeWhenReady(base::Closure task
) {
51 pending_tasks_
.push_back(task
);
55 void StateStore::DelayedTaskQueue::SetReady() {
58 for (size_t i
= 0; i
< pending_tasks_
.size(); ++i
)
59 pending_tasks_
[i
].Run();
60 pending_tasks_
.clear();
63 StateStore::StateStore(Profile
* profile
,
64 const base::FilePath
& db_path
,
66 : db_path_(db_path
), task_queue_(new DelayedTaskQueue()) {
67 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED
,
68 content::Source
<Profile
>(profile
));
69 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED
,
70 content::Source
<Profile
>(profile
));
73 // Don't Init until the first page is loaded or the session restored.
74 registrar_
.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
75 content::NotificationService::
76 AllBrowserContextsAndSources());
77 registrar_
.Add(this, chrome::NOTIFICATION_SESSION_RESTORE_DONE
,
78 content::NotificationService::
79 AllBrowserContextsAndSources());
85 StateStore::StateStore(Profile
* profile
, scoped_ptr
<ValueStore
> value_store
)
86 : store_(value_store
.Pass()), task_queue_(new DelayedTaskQueue()) {
87 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED
,
88 content::Source
<Profile
>(profile
));
89 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED
,
90 content::Source
<Profile
>(profile
));
92 // This constructor is for testing. No need to delay Init.
96 StateStore::~StateStore() {
99 void StateStore::RegisterKey(const std::string
& key
) {
100 registered_keys_
.insert(key
);
103 void StateStore::GetExtensionValue(const std::string
& extension_id
,
104 const std::string
& key
,
105 ReadCallback callback
) {
106 task_queue_
->InvokeWhenReady(
107 base::Bind(&ValueStoreFrontend::Get
, base::Unretained(&store_
),
108 GetFullKey(extension_id
, key
), callback
));
111 void StateStore::SetExtensionValue(
112 const std::string
& extension_id
,
113 const std::string
& key
,
114 scoped_ptr
<base::Value
> value
) {
115 task_queue_
->InvokeWhenReady(
116 base::Bind(&ValueStoreFrontend::Set
, base::Unretained(&store_
),
117 GetFullKey(extension_id
, key
), base::Passed(&value
)));
120 void StateStore::RemoveExtensionValue(const std::string
& extension_id
,
121 const std::string
& key
) {
122 task_queue_
->InvokeWhenReady(
123 base::Bind(&ValueStoreFrontend::Remove
, base::Unretained(&store_
),
124 GetFullKey(extension_id
, key
)));
127 void StateStore::Observe(int type
,
128 const content::NotificationSource
& source
,
129 const content::NotificationDetails
& details
) {
131 case chrome::NOTIFICATION_EXTENSION_INSTALLED
:
132 RemoveKeysForExtension(
133 content::Details
<const InstalledExtensionInfo
>(details
)->extension
->
136 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED
:
137 RemoveKeysForExtension(
138 content::Details
<const Extension
>(details
)->id());
140 case chrome::NOTIFICATION_SESSION_RESTORE_DONE
:
141 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
:
142 registrar_
.Remove(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
143 content::NotificationService::AllSources());
144 registrar_
.Remove(this, chrome::NOTIFICATION_SESSION_RESTORE_DONE
,
145 content::NotificationService::AllSources());
146 base::MessageLoop::current()->PostDelayedTask(FROM_HERE
,
147 base::Bind(&StateStore::Init
, AsWeakPtr()),
148 base::TimeDelta::FromSeconds(kInitDelaySeconds
));
156 void StateStore::Init() {
157 if (!db_path_
.empty())
158 store_
.Init(db_path_
);
159 task_queue_
->SetReady();
162 void StateStore::RemoveKeysForExtension(const std::string
& extension_id
) {
163 for (std::set
<std::string
>::iterator key
= registered_keys_
.begin();
164 key
!= registered_keys_
.end(); ++key
) {
165 task_queue_
->InvokeWhenReady(
166 base::Bind(&ValueStoreFrontend::Remove
, base::Unretained(&store_
),
167 GetFullKey(extension_id
, *key
)));
171 } // namespace extensions