[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" |
[email protected] | b19fe57 | 2013-07-18 04:54:26 | [diff] [blame] | 8 | #include "base/message_loop/message_loop.h" |
[email protected] | 49a01e64 | 2013-07-12 00:29:45 | [diff] [blame] | 9 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 10 | #include "content/public/browser/notification_service.h" |
| 11 | #include "content/public/browser/notification_types.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 12 | #include "extensions/common/extension.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 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 |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 18 | const int kInitDelaySeconds = 1; |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 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 | |
[email protected] | c6ea4b4 | 2014-03-10 23:25:11 | [diff] [blame^] | 42 | // Return whether or not the DelayedTaskQueue is |ready_|. |
| 43 | bool ready() const { return ready_; } |
| 44 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 45 | private: |
| 46 | bool ready_; |
| 47 | std::vector<base::Closure> pending_tasks_; |
| 48 | }; |
| 49 | |
| 50 | void StateStore::DelayedTaskQueue::InvokeWhenReady(base::Closure task) { |
| 51 | if (ready_) { |
| 52 | task.Run(); |
| 53 | } else { |
| 54 | pending_tasks_.push_back(task); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void StateStore::DelayedTaskQueue::SetReady() { |
| 59 | ready_ = true; |
| 60 | |
| 61 | for (size_t i = 0; i < pending_tasks_.size(); ++i) |
| 62 | pending_tasks_[i].Run(); |
| 63 | pending_tasks_.clear(); |
| 64 | } |
| 65 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 66 | StateStore::StateStore(Profile* profile, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 67 | const base::FilePath& db_path, |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 68 | bool deferred_load) |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 69 | : db_path_(db_path), task_queue_(new DelayedTaskQueue()) { |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 70 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
| 71 | content::Source<Profile>(profile)); |
| 72 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 73 | content::Source<Profile>(profile)); |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 74 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 75 | if (deferred_load) { |
[email protected] | ac7ba32 | 2013-04-12 23:12:24 | [diff] [blame] | 76 | // Don't Init until the first page is loaded or the session restored. |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 77 | registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 78 | content::NotificationService:: |
| 79 | AllBrowserContextsAndSources()); |
[email protected] | ac7ba32 | 2013-04-12 23:12:24 | [diff] [blame] | 80 | registrar_.Add(this, chrome::NOTIFICATION_SESSION_RESTORE_DONE, |
| 81 | content::NotificationService:: |
| 82 | AllBrowserContextsAndSources()); |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 83 | } else { |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 84 | Init(); |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 85 | } |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | da2b622c | 2013-09-27 21:30:40 | [diff] [blame] | 88 | StateStore::StateStore(Profile* profile, scoped_ptr<ValueStore> value_store) |
| 89 | : store_(value_store.Pass()), task_queue_(new DelayedTaskQueue()) { |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 90 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
| 91 | content::Source<Profile>(profile)); |
| 92 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 93 | content::Source<Profile>(profile)); |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 94 | |
| 95 | // This constructor is for testing. No need to delay Init. |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 96 | Init(); |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 99 | StateStore::~StateStore() { |
| 100 | } |
| 101 | |
| 102 | void StateStore::RegisterKey(const std::string& key) { |
| 103 | registered_keys_.insert(key); |
| 104 | } |
| 105 | |
| 106 | void StateStore::GetExtensionValue(const std::string& extension_id, |
| 107 | const std::string& key, |
| 108 | ReadCallback callback) { |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 109 | task_queue_->InvokeWhenReady( |
| 110 | base::Bind(&ValueStoreFrontend::Get, base::Unretained(&store_), |
| 111 | GetFullKey(extension_id, key), callback)); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void StateStore::SetExtensionValue( |
| 115 | const std::string& extension_id, |
| 116 | const std::string& key, |
| 117 | scoped_ptr<base::Value> value) { |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 118 | task_queue_->InvokeWhenReady( |
| 119 | base::Bind(&ValueStoreFrontend::Set, base::Unretained(&store_), |
[email protected] | c02087b51 | 2013-02-04 03:09:20 | [diff] [blame] | 120 | GetFullKey(extension_id, key), base::Passed(&value))); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 123 | void StateStore::RemoveExtensionValue(const std::string& extension_id, |
| 124 | const std::string& key) { |
| 125 | task_queue_->InvokeWhenReady( |
| 126 | base::Bind(&ValueStoreFrontend::Remove, base::Unretained(&store_), |
| 127 | GetFullKey(extension_id, key))); |
| 128 | } |
| 129 | |
[email protected] | c6ea4b4 | 2014-03-10 23:25:11 | [diff] [blame^] | 130 | bool StateStore::IsInitialized() const { return task_queue_->ready(); } |
| 131 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 132 | void StateStore::Observe(int type, |
| 133 | const content::NotificationSource& source, |
| 134 | const content::NotificationDetails& details) { |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 135 | switch (type) { |
| 136 | case chrome::NOTIFICATION_EXTENSION_INSTALLED: |
[email protected] | 41bb80bd | 2013-05-03 10:56:02 | [diff] [blame] | 137 | RemoveKeysForExtension( |
| 138 | content::Details<const InstalledExtensionInfo>(details)->extension-> |
| 139 | id()); |
| 140 | break; |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 141 | case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 142 | RemoveKeysForExtension( |
[email protected] | 41bb80bd | 2013-05-03 10:56:02 | [diff] [blame] | 143 | content::Details<const Extension>(details)->id()); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 144 | break; |
[email protected] | ac7ba32 | 2013-04-12 23:12:24 | [diff] [blame] | 145 | case chrome::NOTIFICATION_SESSION_RESTORE_DONE: |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 146 | case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME: |
| 147 | registrar_.Remove(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 148 | content::NotificationService::AllSources()); |
[email protected] | ac7ba32 | 2013-04-12 23:12:24 | [diff] [blame] | 149 | registrar_.Remove(this, chrome::NOTIFICATION_SESSION_RESTORE_DONE, |
| 150 | content::NotificationService::AllSources()); |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 151 | base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 152 | base::Bind(&StateStore::Init, AsWeakPtr()), |
| 153 | base::TimeDelta::FromSeconds(kInitDelaySeconds)); |
| 154 | break; |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 155 | default: |
| 156 | NOTREACHED(); |
| 157 | return; |
| 158 | } |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 159 | } |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 160 | |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 161 | void StateStore::Init() { |
| 162 | if (!db_path_.empty()) |
| 163 | store_.Init(db_path_); |
| 164 | task_queue_->SetReady(); |
| 165 | } |
| 166 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 167 | void StateStore::RemoveKeysForExtension(const std::string& extension_id) { |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 168 | for (std::set<std::string>::iterator key = registered_keys_.begin(); |
| 169 | key != registered_keys_.end(); ++key) { |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 170 | task_queue_->InvokeWhenReady( |
| 171 | base::Bind(&ValueStoreFrontend::Remove, base::Unretained(&store_), |
| 172 | GetFullKey(extension_id, *key))); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 176 | } // namespace extensions |