[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chrome/browser/extensions/state_store.h" |
| 6 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/message_loop.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 9 | #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 | |
| 14 | namespace { |
| 15 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 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 = 5; |
| 19 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 20 | std::string GetFullKey(const std::string& extension_id, |
| 21 | const std::string& key) { |
| 22 | return extension_id + "." + key; |
| 23 | } |
| 24 | |
| 25 | } // namespace |
| 26 | |
| 27 | namespace extensions { |
| 28 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 29 | // Helper class to delay tasks until we're ready to start executing them. |
| 30 | class 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 | |
| 47 | void StateStore::DelayedTaskQueue::InvokeWhenReady(base::Closure task) { |
| 48 | if (ready_) { |
| 49 | task.Run(); |
| 50 | } else { |
| 51 | pending_tasks_.push_back(task); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void 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] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame^] | 63 | StateStore::StateStore(Profile* profile, |
| 64 | const FilePath& db_path, |
| 65 | bool deferred_load) |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 66 | : task_queue_(new DelayedTaskQueue()) { |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 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)); |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 71 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame^] | 72 | if (deferred_load) { |
| 73 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 74 | base::Bind(&StateStore::Init, AsWeakPtr(), db_path), |
| 75 | base::TimeDelta::FromSeconds(kInitDelaySeconds)); |
| 76 | } else { |
| 77 | Init(db_path); |
| 78 | } |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 81 | StateStore::StateStore(Profile* profile, ValueStore* value_store) |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame^] | 82 | : store_(value_store), task_queue_(new DelayedTaskQueue()) { |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 83 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
| 84 | content::Source<Profile>(profile)); |
| 85 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 86 | content::Source<Profile>(profile)); |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 87 | |
| 88 | // This constructor is for testing. No need to delay Init. |
| 89 | Init(FilePath()); |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 92 | StateStore::~StateStore() { |
| 93 | } |
| 94 | |
| 95 | void StateStore::RegisterKey(const std::string& key) { |
| 96 | registered_keys_.insert(key); |
| 97 | } |
| 98 | |
| 99 | void StateStore::GetExtensionValue(const std::string& extension_id, |
| 100 | const std::string& key, |
| 101 | ReadCallback callback) { |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 102 | task_queue_->InvokeWhenReady( |
| 103 | base::Bind(&ValueStoreFrontend::Get, base::Unretained(&store_), |
| 104 | GetFullKey(extension_id, key), callback)); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void StateStore::SetExtensionValue( |
| 108 | const std::string& extension_id, |
| 109 | const std::string& key, |
| 110 | scoped_ptr<base::Value> value) { |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 111 | task_queue_->InvokeWhenReady( |
| 112 | base::Bind(&ValueStoreFrontend::Set, base::Unretained(&store_), |
| 113 | GetFullKey(extension_id, key), base::Passed(value.Pass()))); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 114 | } |
| 115 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame^] | 116 | void StateStore::RemoveExtensionValue(const std::string& extension_id, |
| 117 | const std::string& key) { |
| 118 | task_queue_->InvokeWhenReady( |
| 119 | base::Bind(&ValueStoreFrontend::Remove, base::Unretained(&store_), |
| 120 | GetFullKey(extension_id, key))); |
| 121 | } |
| 122 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 123 | void StateStore::Observe(int type, |
| 124 | const content::NotificationSource& source, |
| 125 | const content::NotificationDetails& details) { |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 126 | switch (type) { |
| 127 | case chrome::NOTIFICATION_EXTENSION_INSTALLED: |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 128 | case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame^] | 129 | RemoveKeysForExtension( |
| 130 | content::Details<const Extension>(details).ptr()->id()); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 131 | break; |
| 132 | default: |
| 133 | NOTREACHED(); |
| 134 | return; |
| 135 | } |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame^] | 136 | } |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 137 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame^] | 138 | void StateStore::RemoveKeysForExtension(const std::string& extension_id) { |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 139 | for (std::set<std::string>::iterator key = registered_keys_.begin(); |
| 140 | key != registered_keys_.end(); ++key) { |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 141 | task_queue_->InvokeWhenReady( |
| 142 | base::Bind(&ValueStoreFrontend::Remove, base::Unretained(&store_), |
| 143 | GetFullKey(extension_id, *key))); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 147 | void StateStore::Init(const FilePath& db_path) { |
| 148 | if (!db_path.empty()) |
| 149 | store_.Init(db_path); |
| 150 | task_queue_->SetReady(); |
| 151 | } |
| 152 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 153 | } // namespace extensions |