[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 5 | #include "extensions/browser/state_store.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 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] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 10 | #include "content/public/browser/browser_context.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 11 | #include "content/public/browser/notification_service.h" |
| 12 | #include "content/public/browser/notification_types.h" |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 13 | #include "extensions/browser/extension_registry.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 14 | #include "extensions/common/extension.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 15 | |
| 16 | namespace { |
| 17 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 18 | // Delay, in seconds, before we should open the State Store database. We |
| 19 | // defer it to avoid slowing down startup. See http://crbug.com/161848 |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 20 | const int kInitDelaySeconds = 1; |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 21 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 22 | std::string GetFullKey(const std::string& extension_id, |
| 23 | const std::string& key) { |
| 24 | return extension_id + "." + key; |
| 25 | } |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | namespace extensions { |
| 30 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 31 | // Helper class to delay tasks until we're ready to start executing them. |
| 32 | class StateStore::DelayedTaskQueue { |
| 33 | public: |
| 34 | DelayedTaskQueue() : ready_(false) {} |
| 35 | ~DelayedTaskQueue() {} |
| 36 | |
| 37 | // Queues up a task for invoking once we're ready. Invokes immediately if |
| 38 | // we're already ready. |
| 39 | void InvokeWhenReady(base::Closure task); |
| 40 | |
| 41 | // Marks us ready, and invokes all pending tasks. |
| 42 | void SetReady(); |
| 43 | |
[email protected] | c6ea4b4 | 2014-03-10 23:25:11 | [diff] [blame] | 44 | // Return whether or not the DelayedTaskQueue is |ready_|. |
| 45 | bool ready() const { return ready_; } |
| 46 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 47 | private: |
| 48 | bool ready_; |
| 49 | std::vector<base::Closure> pending_tasks_; |
| 50 | }; |
| 51 | |
| 52 | void StateStore::DelayedTaskQueue::InvokeWhenReady(base::Closure task) { |
| 53 | if (ready_) { |
| 54 | task.Run(); |
| 55 | } else { |
| 56 | pending_tasks_.push_back(task); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void StateStore::DelayedTaskQueue::SetReady() { |
| 61 | ready_ = true; |
| 62 | |
| 63 | for (size_t i = 0; i < pending_tasks_.size(); ++i) |
| 64 | pending_tasks_[i].Run(); |
| 65 | pending_tasks_.clear(); |
| 66 | } |
| 67 | |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 68 | StateStore::StateStore(content::BrowserContext* context, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 69 | const base::FilePath& db_path, |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 70 | bool deferred_load) |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 71 | : db_path_(db_path), |
| 72 | task_queue_(new DelayedTaskQueue()), |
| 73 | extension_registry_observer_(this) { |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 74 | extension_registry_observer_.Add(ExtensionRegistry::Get(context)); |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 75 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 76 | if (deferred_load) { |
[email protected] | ac7ba32 | 2013-04-12 23:12:24 | [diff] [blame] | 77 | // Don't Init until the first page is loaded or the session restored. |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 78 | registrar_.Add( |
| 79 | this, |
| 80 | content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 81 | content::NotificationService::AllBrowserContextsAndSources()); |
| 82 | registrar_.Add( |
| 83 | this, |
| 84 | chrome::NOTIFICATION_SESSION_RESTORE_DONE, |
| 85 | content::NotificationService::AllBrowserContextsAndSources()); |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 86 | } else { |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 87 | Init(); |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 88 | } |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 91 | StateStore::StateStore(content::BrowserContext* context, |
| 92 | scoped_ptr<ValueStore> value_store) |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 93 | : store_(value_store.Pass()), |
| 94 | task_queue_(new DelayedTaskQueue()), |
| 95 | extension_registry_observer_(this) { |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 96 | extension_registry_observer_.Add(ExtensionRegistry::Get(context)); |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 97 | |
| 98 | // This constructor is for testing. No need to delay Init. |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 99 | Init(); |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 102 | StateStore::~StateStore() { |
| 103 | } |
| 104 | |
| 105 | void StateStore::RegisterKey(const std::string& key) { |
| 106 | registered_keys_.insert(key); |
| 107 | } |
| 108 | |
| 109 | void StateStore::GetExtensionValue(const std::string& extension_id, |
| 110 | const std::string& key, |
| 111 | ReadCallback callback) { |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 112 | task_queue_->InvokeWhenReady(base::Bind(&ValueStoreFrontend::Get, |
| 113 | base::Unretained(&store_), |
| 114 | GetFullKey(extension_id, key), |
| 115 | callback)); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 118 | void StateStore::SetExtensionValue(const std::string& extension_id, |
| 119 | const std::string& key, |
| 120 | scoped_ptr<base::Value> value) { |
| 121 | task_queue_->InvokeWhenReady(base::Bind(&ValueStoreFrontend::Set, |
| 122 | base::Unretained(&store_), |
| 123 | GetFullKey(extension_id, key), |
| 124 | base::Passed(&value))); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 127 | void StateStore::RemoveExtensionValue(const std::string& extension_id, |
| 128 | const std::string& key) { |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 129 | task_queue_->InvokeWhenReady(base::Bind(&ValueStoreFrontend::Remove, |
| 130 | base::Unretained(&store_), |
| 131 | GetFullKey(extension_id, key))); |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 132 | } |
| 133 | |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 134 | bool StateStore::IsInitialized() const { |
| 135 | return task_queue_->ready(); |
| 136 | } |
[email protected] | c6ea4b4 | 2014-03-10 23:25:11 | [diff] [blame] | 137 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 138 | void StateStore::Observe(int type, |
| 139 | const content::NotificationSource& source, |
| 140 | const content::NotificationDetails& details) { |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 141 | DCHECK(type == chrome::NOTIFICATION_SESSION_RESTORE_DONE || |
| 142 | type == content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME); |
| 143 | registrar_.RemoveAll(); |
| 144 | |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 145 | base::MessageLoop::current()->PostDelayedTask( |
| 146 | FROM_HERE, |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 147 | base::Bind(&StateStore::Init, AsWeakPtr()), |
| 148 | base::TimeDelta::FromSeconds(kInitDelaySeconds)); |
| 149 | } |
| 150 | |
| 151 | void StateStore::OnExtensionWillBeInstalled( |
| 152 | content::BrowserContext* browser_context, |
| 153 | const Extension* extension, |
| 154 | bool is_update, |
| 155 | bool from_ephemeral, |
| 156 | const std::string& old_name) { |
| 157 | RemoveKeysForExtension(extension->id()); |
| 158 | } |
| 159 | |
| 160 | void StateStore::OnExtensionUninstalled( |
| 161 | content::BrowserContext* browser_context, |
[email protected] | e43c61f | 2014-07-20 21:46:34 | [diff] [blame] | 162 | const Extension* extension, |
| 163 | extensions::UninstallReason reason) { |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 164 | RemoveKeysForExtension(extension->id()); |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 165 | } |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 166 | |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 167 | void StateStore::Init() { |
| 168 | if (!db_path_.empty()) |
| 169 | store_.Init(db_path_); |
| 170 | task_queue_->SetReady(); |
| 171 | } |
| 172 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 173 | void StateStore::RemoveKeysForExtension(const std::string& extension_id) { |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 174 | for (std::set<std::string>::iterator key = registered_keys_.begin(); |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 175 | key != registered_keys_.end(); |
| 176 | ++key) { |
| 177 | task_queue_->InvokeWhenReady(base::Bind(&ValueStoreFrontend::Remove, |
| 178 | base::Unretained(&store_), |
| 179 | GetFullKey(extension_id, *key))); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 183 | } // namespace extensions |