blob: 7ca9e8463d19a44cc9c14a930c86440d78c8e685 [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"
8#include "base/message_loop.h"
[email protected]90e800c2012-06-12 23:11:009#include "chrome/common/chrome_notification_types.h"
10#include "chrome/common/extensions/extension.h"
11#include "content/public/browser/notification_service.h"
12#include "content/public/browser/notification_types.h"
13
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,
64 const FilePath& db_path,
65 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]87093442013-01-12 16:34:0573 // Don't Init until the first page is loaded.
74 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
75 content::NotificationService::
76 AllBrowserContextsAndSources());
[email protected]a690e292012-12-19 19:22:4977 } else {
[email protected]87093442013-01-12 16:34:0578 Init();
[email protected]a690e292012-12-19 19:22:4979 }
[email protected]90e800c2012-06-12 23:11:0080}
81
[email protected]bec64552012-06-13 20:25:4982StateStore::StateStore(Profile* profile, ValueStore* value_store)
[email protected]a690e292012-12-19 19:22:4983 : store_(value_store), task_queue_(new DelayedTaskQueue()) {
[email protected]bec64552012-06-13 20:25:4984 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
85 content::Source<Profile>(profile));
86 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
87 content::Source<Profile>(profile));
[email protected]ebaa018d2012-12-11 21:42:5388
89 // This constructor is for testing. No need to delay Init.
[email protected]87093442013-01-12 16:34:0590 Init();
[email protected]bec64552012-06-13 20:25:4991}
92
[email protected]90e800c2012-06-12 23:11:0093StateStore::~StateStore() {
94}
95
96void StateStore::RegisterKey(const std::string& key) {
97 registered_keys_.insert(key);
98}
99
100void StateStore::GetExtensionValue(const std::string& extension_id,
101 const std::string& key,
102 ReadCallback callback) {
[email protected]ebaa018d2012-12-11 21:42:53103 task_queue_->InvokeWhenReady(
104 base::Bind(&ValueStoreFrontend::Get, base::Unretained(&store_),
105 GetFullKey(extension_id, key), callback));
[email protected]90e800c2012-06-12 23:11:00106}
107
108void StateStore::SetExtensionValue(
109 const std::string& extension_id,
110 const std::string& key,
111 scoped_ptr<base::Value> value) {
[email protected]ebaa018d2012-12-11 21:42:53112 task_queue_->InvokeWhenReady(
113 base::Bind(&ValueStoreFrontend::Set, base::Unretained(&store_),
[email protected]c02087b512013-02-04 03:09:20114 GetFullKey(extension_id, key), base::Passed(&value)));
[email protected]90e800c2012-06-12 23:11:00115}
116
[email protected]a690e292012-12-19 19:22:49117void StateStore::RemoveExtensionValue(const std::string& extension_id,
118 const std::string& key) {
119 task_queue_->InvokeWhenReady(
120 base::Bind(&ValueStoreFrontend::Remove, base::Unretained(&store_),
121 GetFullKey(extension_id, key)));
122}
123
[email protected]90e800c2012-06-12 23:11:00124void StateStore::Observe(int type,
125 const content::NotificationSource& source,
126 const content::NotificationDetails& details) {
[email protected]90e800c2012-06-12 23:11:00127 switch (type) {
128 case chrome::NOTIFICATION_EXTENSION_INSTALLED:
[email protected]90e800c2012-06-12 23:11:00129 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED:
[email protected]a690e292012-12-19 19:22:49130 RemoveKeysForExtension(
131 content::Details<const Extension>(details).ptr()->id());
[email protected]90e800c2012-06-12 23:11:00132 break;
[email protected]87093442013-01-12 16:34:05133 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME:
134 registrar_.Remove(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
135 content::NotificationService::AllSources());
136 MessageLoop::current()->PostDelayedTask(FROM_HERE,
137 base::Bind(&StateStore::Init, AsWeakPtr()),
138 base::TimeDelta::FromSeconds(kInitDelaySeconds));
139 break;
[email protected]90e800c2012-06-12 23:11:00140 default:
141 NOTREACHED();
142 return;
143 }
[email protected]a690e292012-12-19 19:22:49144}
[email protected]90e800c2012-06-12 23:11:00145
[email protected]87093442013-01-12 16:34:05146void StateStore::Init() {
147 if (!db_path_.empty())
148 store_.Init(db_path_);
149 task_queue_->SetReady();
150}
151
[email protected]a690e292012-12-19 19:22:49152void StateStore::RemoveKeysForExtension(const std::string& extension_id) {
[email protected]90e800c2012-06-12 23:11:00153 for (std::set<std::string>::iterator key = registered_keys_.begin();
154 key != registered_keys_.end(); ++key) {
[email protected]ebaa018d2012-12-11 21:42:53155 task_queue_->InvokeWhenReady(
156 base::Bind(&ValueStoreFrontend::Remove, base::Unretained(&store_),
157 GetFullKey(extension_id, *key)));
[email protected]90e800c2012-06-12 23:11:00158 }
159}
160
[email protected]90e800c2012-06-12 23:11:00161} // namespace extensions