blob: a12dc514bbf295de4fc96959b66382560d59055a [file] [log] [blame]
[email protected]90e800c2012-06-12 23:11:001// 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.
4
5#include "chrome/browser/extensions/state_store.h"
6
[email protected]ebaa018d2012-12-11 21:42:537#include "base/bind.h"
[email protected]b19fe572013-07-18 04:54:268#include "base/message_loop/message_loop.h"
[email protected]49a01e642013-07-12 00:29:459#include "chrome/browser/chrome_notification_types.h"
[email protected]90e800c2012-06-12 23:11:0010#include "content/public/browser/notification_service.h"
11#include "content/public/browser/notification_types.h"
[email protected]e4452d32013-11-15 23:07:4112#include "extensions/common/extension.h"
[email protected]90e800c2012-06-12 23:11:0013
14namespace {
15
[email protected]ebaa018d2012-12-11 21:42:5316// 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
[email protected]87093442013-01-12 16:34:0518const int kInitDelaySeconds = 1;
[email protected]ebaa018d2012-12-11 21:42:5319
[email protected]90e800c2012-06-12 23:11:0020std::string GetFullKey(const std::string& extension_id,
21 const std::string& key) {
22 return extension_id + "." + key;
23}
24
25} // namespace
26
27namespace extensions {
28
[email protected]ebaa018d2012-12-11 21:42:5329// Helper class to delay tasks until we're ready to start executing them.
30class StateStore::DelayedTaskQueue {
31 public:
32 DelayedTaskQueue() : ready_(false) {}
33 ~DelayedTaskQueue() {}
34
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);
38
39 // Marks us ready, and invokes all pending tasks.
40 void SetReady();
41
42 private:
43 bool ready_;
44 std::vector<base::Closure> pending_tasks_;
45};
46
47void StateStore::DelayedTaskQueue::InvokeWhenReady(base::Closure task) {
48 if (ready_) {
49 task.Run();
50 } else {
51 pending_tasks_.push_back(task);
52 }
53}
54
55void StateStore::DelayedTaskQueue::SetReady() {
56 ready_ = true;
57
58 for (size_t i = 0; i < pending_tasks_.size(); ++i)
59 pending_tasks_[i].Run();
60 pending_tasks_.clear();
61}
62
[email protected]a690e292012-12-19 19:22:4963StateStore::StateStore(Profile* profile,
[email protected]650b2d52013-02-10 03:41:4564 const base::FilePath& db_path,
[email protected]a690e292012-12-19 19:22:4965 bool deferred_load)
[email protected]87093442013-01-12 16:34:0566 : db_path_(db_path), task_queue_(new DelayedTaskQueue()) {
[email protected]90e800c2012-06-12 23:11:0067 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));
[email protected]ebaa018d2012-12-11 21:42:5371
[email protected]a690e292012-12-19 19:22:4972 if (deferred_load) {
[email protected]ac7ba322013-04-12 23:12:2473 // Don't Init until the first page is loaded or the session restored.
[email protected]87093442013-01-12 16:34:0574 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
75 content::NotificationService::
76 AllBrowserContextsAndSources());
[email protected]ac7ba322013-04-12 23:12:2477 registrar_.Add(this, chrome::NOTIFICATION_SESSION_RESTORE_DONE,
78 content::NotificationService::
79 AllBrowserContextsAndSources());
[email protected]a690e292012-12-19 19:22:4980 } else {
[email protected]87093442013-01-12 16:34:0581 Init();
[email protected]a690e292012-12-19 19:22:4982 }
[email protected]90e800c2012-06-12 23:11:0083}
84
[email protected]da2b622c2013-09-27 21:30:4085StateStore::StateStore(Profile* profile, scoped_ptr<ValueStore> value_store)
86 : store_(value_store.Pass()), task_queue_(new DelayedTaskQueue()) {
[email protected]bec64552012-06-13 20:25:4987 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));
[email protected]ebaa018d2012-12-11 21:42:5391
92 // This constructor is for testing. No need to delay Init.
[email protected]87093442013-01-12 16:34:0593 Init();
[email protected]bec64552012-06-13 20:25:4994}
95
[email protected]90e800c2012-06-12 23:11:0096StateStore::~StateStore() {
97}
98
99void StateStore::RegisterKey(const std::string& key) {
100 registered_keys_.insert(key);
101}
102
103void StateStore::GetExtensionValue(const std::string& extension_id,
104 const std::string& key,
105 ReadCallback callback) {
[email protected]ebaa018d2012-12-11 21:42:53106 task_queue_->InvokeWhenReady(
107 base::Bind(&ValueStoreFrontend::Get, base::Unretained(&store_),
108 GetFullKey(extension_id, key), callback));
[email protected]90e800c2012-06-12 23:11:00109}
110
111void StateStore::SetExtensionValue(
112 const std::string& extension_id,
113 const std::string& key,
114 scoped_ptr<base::Value> value) {
[email protected]ebaa018d2012-12-11 21:42:53115 task_queue_->InvokeWhenReady(
116 base::Bind(&ValueStoreFrontend::Set, base::Unretained(&store_),
[email protected]c02087b512013-02-04 03:09:20117 GetFullKey(extension_id, key), base::Passed(&value)));
[email protected]90e800c2012-06-12 23:11:00118}
119
[email protected]a690e292012-12-19 19:22:49120void 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)));
125}
126
[email protected]90e800c2012-06-12 23:11:00127void StateStore::Observe(int type,
128 const content::NotificationSource& source,
129 const content::NotificationDetails& details) {
[email protected]90e800c2012-06-12 23:11:00130 switch (type) {
131 case chrome::NOTIFICATION_EXTENSION_INSTALLED:
[email protected]41bb80bd2013-05-03 10:56:02132 RemoveKeysForExtension(
133 content::Details<const InstalledExtensionInfo>(details)->extension->
134 id());
135 break;
[email protected]90e800c2012-06-12 23:11:00136 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED:
[email protected]a690e292012-12-19 19:22:49137 RemoveKeysForExtension(
[email protected]41bb80bd2013-05-03 10:56:02138 content::Details<const Extension>(details)->id());
[email protected]90e800c2012-06-12 23:11:00139 break;
[email protected]ac7ba322013-04-12 23:12:24140 case chrome::NOTIFICATION_SESSION_RESTORE_DONE:
[email protected]87093442013-01-12 16:34:05141 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME:
142 registrar_.Remove(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
143 content::NotificationService::AllSources());
[email protected]ac7ba322013-04-12 23:12:24144 registrar_.Remove(this, chrome::NOTIFICATION_SESSION_RESTORE_DONE,
145 content::NotificationService::AllSources());
[email protected]b3a25092013-05-28 22:08:16146 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]87093442013-01-12 16:34:05147 base::Bind(&StateStore::Init, AsWeakPtr()),
148 base::TimeDelta::FromSeconds(kInitDelaySeconds));
149 break;
[email protected]90e800c2012-06-12 23:11:00150 default:
151 NOTREACHED();
152 return;
153 }
[email protected]a690e292012-12-19 19:22:49154}
[email protected]90e800c2012-06-12 23:11:00155
[email protected]87093442013-01-12 16:34:05156void StateStore::Init() {
157 if (!db_path_.empty())
158 store_.Init(db_path_);
159 task_queue_->SetReady();
160}
161
[email protected]a690e292012-12-19 19:22:49162void StateStore::RemoveKeysForExtension(const std::string& extension_id) {
[email protected]90e800c2012-06-12 23:11:00163 for (std::set<std::string>::iterator key = registered_keys_.begin();
164 key != registered_keys_.end(); ++key) {
[email protected]ebaa018d2012-12-11 21:42:53165 task_queue_->InvokeWhenReady(
166 base::Bind(&ValueStoreFrontend::Remove, base::Unretained(&store_),
167 GetFullKey(extension_id, *key)));
[email protected]90e800c2012-06-12 23:11:00168 }
169}
170
[email protected]90e800c2012-06-12 23:11:00171} // namespace extensions