[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 | |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 11 | #include "base/bind.h" |
fdoray | e6609ac8 | 2016-06-06 19:21:40 | [diff] [blame] | 12 | #include "base/location.h" |
| 13 | #include "base/single_thread_task_runner.h" |
| 14 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 15 | #include "content/public/browser/browser_context.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 16 | #include "content/public/browser/notification_service.h" |
| 17 | #include "content/public/browser/notification_types.h" |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 18 | #include "extensions/browser/extension_registry.h" |
cmumford | 6ae8d46 | 2016-03-24 20:35:27 | [diff] [blame] | 19 | #include "extensions/browser/value_store/value_store_factory.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 20 | #include "extensions/common/extension.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 24 | // Delay, in seconds, before we should open the State Store database. We |
| 25 | // defer it to avoid slowing down startup. See http://crbug.com/161848 |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 26 | const int kInitDelaySeconds = 1; |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 27 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 28 | std::string GetFullKey(const std::string& extension_id, |
| 29 | const std::string& key) { |
| 30 | return extension_id + "." + key; |
| 31 | } |
| 32 | |
| 33 | } // namespace |
| 34 | |
| 35 | namespace extensions { |
| 36 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 37 | // Helper class to delay tasks until we're ready to start executing them. |
| 38 | class StateStore::DelayedTaskQueue { |
| 39 | public: |
| 40 | DelayedTaskQueue() : ready_(false) {} |
| 41 | ~DelayedTaskQueue() {} |
| 42 | |
| 43 | // Queues up a task for invoking once we're ready. Invokes immediately if |
| 44 | // we're already ready. |
rdevlin.cronin | 0a0942f | 2016-07-08 22:22:35 | [diff] [blame] | 45 | void InvokeWhenReady(const base::Closure& task); |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 46 | |
| 47 | // Marks us ready, and invokes all pending tasks. |
| 48 | void SetReady(); |
| 49 | |
[email protected] | c6ea4b4 | 2014-03-10 23:25:11 | [diff] [blame] | 50 | // Return whether or not the DelayedTaskQueue is |ready_|. |
| 51 | bool ready() const { return ready_; } |
| 52 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 53 | private: |
| 54 | bool ready_; |
| 55 | std::vector<base::Closure> pending_tasks_; |
| 56 | }; |
| 57 | |
rdevlin.cronin | 0a0942f | 2016-07-08 22:22:35 | [diff] [blame] | 58 | void StateStore::DelayedTaskQueue::InvokeWhenReady(const base::Closure& task) { |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 59 | if (ready_) { |
| 60 | task.Run(); |
| 61 | } else { |
| 62 | pending_tasks_.push_back(task); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void StateStore::DelayedTaskQueue::SetReady() { |
| 67 | ready_ = true; |
| 68 | |
| 69 | for (size_t i = 0; i < pending_tasks_.size(); ++i) |
| 70 | pending_tasks_[i].Run(); |
| 71 | pending_tasks_.clear(); |
| 72 | } |
| 73 | |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 74 | StateStore::StateStore(content::BrowserContext* context, |
cmumford | 6ae8d46 | 2016-03-24 20:35:27 | [diff] [blame] | 75 | const scoped_refptr<ValueStoreFactory>& store_factory, |
| 76 | ValueStoreFrontend::BackendType backend_type, |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 77 | bool deferred_load) |
cmumford | 6ae8d46 | 2016-03-24 20:35:27 | [diff] [blame] | 78 | : store_(new ValueStoreFrontend(store_factory, backend_type)), |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 79 | task_queue_(new DelayedTaskQueue()), |
| 80 | extension_registry_observer_(this) { |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 81 | extension_registry_observer_.Add(ExtensionRegistry::Get(context)); |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 82 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 83 | if (deferred_load) { |
[email protected] | 479e392 | 2014-07-30 07:12:57 | [diff] [blame] | 84 | // Don't Init() until the first page is loaded or the embedder explicitly |
| 85 | // requests it. |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 86 | registrar_.Add( |
| 87 | this, |
| 88 | content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 89 | content::NotificationService::AllBrowserContextsAndSources()); |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 90 | } else { |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 91 | Init(); |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 92 | } |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 95 | StateStore::~StateStore() { |
| 96 | } |
| 97 | |
[email protected] | 479e392 | 2014-07-30 07:12:57 | [diff] [blame] | 98 | void StateStore::RequestInitAfterDelay() { |
| 99 | InitAfterDelay(); |
| 100 | } |
| 101 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 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) { |
cmumford | 6ae8d46 | 2016-03-24 20:35:27 | [diff] [blame] | 109 | task_queue_->InvokeWhenReady( |
| 110 | base::Bind(&ValueStoreFrontend::Get, base::Unretained(store_.get()), |
| 111 | GetFullKey(extension_id, key), callback)); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 114 | void StateStore::SetExtensionValue(const std::string& extension_id, |
| 115 | const std::string& key, |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 116 | std::unique_ptr<base::Value> value) { |
cmumford | 6ae8d46 | 2016-03-24 20:35:27 | [diff] [blame] | 117 | task_queue_->InvokeWhenReady( |
| 118 | base::Bind(&ValueStoreFrontend::Set, base::Unretained(store_.get()), |
| 119 | GetFullKey(extension_id, key), base::Passed(&value))); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 122 | void StateStore::RemoveExtensionValue(const std::string& extension_id, |
| 123 | const std::string& key) { |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 124 | task_queue_->InvokeWhenReady(base::Bind(&ValueStoreFrontend::Remove, |
cmumford | 6ae8d46 | 2016-03-24 20:35:27 | [diff] [blame] | 125 | base::Unretained(store_.get()), |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 126 | GetFullKey(extension_id, key))); |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 129 | bool StateStore::IsInitialized() const { |
| 130 | return task_queue_->ready(); |
| 131 | } |
[email protected] | c6ea4b4 | 2014-03-10 23:25:11 | [diff] [blame] | 132 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 133 | void StateStore::Observe(int type, |
| 134 | const content::NotificationSource& source, |
| 135 | const content::NotificationDetails& details) { |
[email protected] | 479e392 | 2014-07-30 07:12:57 | [diff] [blame] | 136 | DCHECK_EQ(type, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME); |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 137 | registrar_.RemoveAll(); |
[email protected] | 479e392 | 2014-07-30 07:12:57 | [diff] [blame] | 138 | InitAfterDelay(); |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void StateStore::OnExtensionWillBeInstalled( |
| 142 | content::BrowserContext* browser_context, |
| 143 | const Extension* extension, |
| 144 | bool is_update, |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 145 | const std::string& old_name) { |
| 146 | RemoveKeysForExtension(extension->id()); |
| 147 | } |
| 148 | |
| 149 | void StateStore::OnExtensionUninstalled( |
| 150 | content::BrowserContext* browser_context, |
[email protected] | e43c61f | 2014-07-20 21:46:34 | [diff] [blame] | 151 | const Extension* extension, |
| 152 | extensions::UninstallReason reason) { |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 153 | RemoveKeysForExtension(extension->id()); |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 154 | } |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 155 | |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 156 | void StateStore::Init() { |
[email protected] | 479e392 | 2014-07-30 07:12:57 | [diff] [blame] | 157 | // Could be called twice if InitAfterDelay() is requested explicitly by the |
| 158 | // embedder in addition to internally after first page load. |
| 159 | if (IsInitialized()) |
| 160 | return; |
| 161 | |
cmumford | 6ae8d46 | 2016-03-24 20:35:27 | [diff] [blame] | 162 | // TODO(cmumford): The store now always lazily initializes upon first access. |
| 163 | // A follow-on CL will remove this deferred initialization implementation |
| 164 | // which is now vestigial. |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 165 | task_queue_->SetReady(); |
| 166 | } |
| 167 | |
[email protected] | 479e392 | 2014-07-30 07:12:57 | [diff] [blame] | 168 | void StateStore::InitAfterDelay() { |
| 169 | if (IsInitialized()) |
| 170 | return; |
| 171 | |
fdoray | e6609ac8 | 2016-06-06 19:21:40 | [diff] [blame] | 172 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 173 | FROM_HERE, base::Bind(&StateStore::Init, AsWeakPtr()), |
[email protected] | 479e392 | 2014-07-30 07:12:57 | [diff] [blame] | 174 | base::TimeDelta::FromSeconds(kInitDelaySeconds)); |
| 175 | } |
| 176 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 177 | void StateStore::RemoveKeysForExtension(const std::string& extension_id) { |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 178 | for (std::set<std::string>::iterator key = registered_keys_.begin(); |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 179 | key != registered_keys_.end(); |
| 180 | ++key) { |
| 181 | task_queue_->InvokeWhenReady(base::Bind(&ValueStoreFrontend::Remove, |
cmumford | 6ae8d46 | 2016-03-24 20:35:27 | [diff] [blame] | 182 | base::Unretained(store_.get()), |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 183 | GetFullKey(extension_id, *key))); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 187 | } // namespace extensions |