[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] | 479e392 | 2014-07-30 07:12:57 | [diff] [blame] | 5 | #ifndef EXTENSIONS_BROWSER_STATE_STORE_H_ |
| 6 | #define EXTENSIONS_BROWSER_STATE_STORE_H_ |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 7 | |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
Devlin Cronin | 7f7901f6 | 2017-12-06 19:08:29 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
Devlin Cronin | 7f7901f6 | 2017-12-06 19:08:29 | [diff] [blame] | 14 | #include "base/observer_list.h" |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 15 | #include "base/scoped_observer.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 16 | #include "content/public/browser/notification_observer.h" |
| 17 | #include "content/public/browser/notification_registrar.h" |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 18 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | 47b870f | 2014-03-01 00:34:00 | [diff] [blame] | 19 | #include "extensions/browser/value_store/value_store_frontend.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 20 | |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 21 | namespace content { |
| 22 | class BrowserContext; |
| 23 | } |
| 24 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 25 | namespace extensions { |
| 26 | |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 27 | class ExtensionRegistry; |
cmumford | 6ae8d46 | 2016-03-24 20:35:27 | [diff] [blame] | 28 | class ValueStoreFactory; |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 29 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 30 | // A storage area for per-extension state that needs to be persisted to disk. |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 31 | class StateStore : public base::SupportsWeakPtr<StateStore>, |
| 32 | public ExtensionRegistryObserver, |
| 33 | public content::NotificationObserver { |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 34 | public: |
| 35 | typedef ValueStoreFrontend::ReadCallback ReadCallback; |
| 36 | |
Devlin Cronin | 7f7901f6 | 2017-12-06 19:08:29 | [diff] [blame] | 37 | class TestObserver { |
| 38 | public: |
| 39 | virtual ~TestObserver() {} |
| 40 | virtual void WillSetExtensionValue(const std::string& extension_id, |
| 41 | const std::string& key) = 0; |
| 42 | }; |
| 43 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 44 | // If |deferred_load| is true, we won't load the database until the first |
| 45 | // page has been loaded. |
[email protected] | daf3ffda | 2014-06-25 06:44:57 | [diff] [blame] | 46 | StateStore(content::BrowserContext* context, |
cmumford | 6ae8d46 | 2016-03-24 20:35:27 | [diff] [blame] | 47 | const scoped_refptr<ValueStoreFactory>& store_factory, |
| 48 | ValueStoreFrontend::BackendType backend_type, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 49 | bool deferred_load); |
[email protected] | a6695e6 | 2012-06-14 00:06:04 | [diff] [blame] | 50 | // This variant is useful for testing (using a mock ValueStore). |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 51 | StateStore(content::BrowserContext* context, |
| 52 | std::unique_ptr<ValueStore> store); |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 53 | ~StateStore() override; |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 54 | |
[email protected] | 479e392 | 2014-07-30 07:12:57 | [diff] [blame] | 55 | // Requests that the state store to be initialized after its usual delay. Can |
| 56 | // be explicitly called by an embedder when the embedder does not trigger the |
| 57 | // usual page load notifications. |
| 58 | void RequestInitAfterDelay(); |
| 59 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 60 | // Register a key for removal upon extension install/uninstall. We remove |
| 61 | // for install to reset state when an extension upgrades. |
| 62 | void RegisterKey(const std::string& key); |
| 63 | |
| 64 | // Get the value associated with the given extension and key, and pass |
| 65 | // it to |callback| asynchronously. |
| 66 | void GetExtensionValue(const std::string& extension_id, |
| 67 | const std::string& key, |
| 68 | ReadCallback callback); |
| 69 | |
| 70 | // Sets a value for a given extension and key. |
| 71 | void SetExtensionValue(const std::string& extension_id, |
| 72 | const std::string& key, |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 73 | std::unique_ptr<base::Value> value); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 74 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 75 | // Removes a value for a given extension and key. |
| 76 | void RemoveExtensionValue(const std::string& extension_id, |
| 77 | const std::string& key); |
| 78 | |
[email protected] | c6ea4b4 | 2014-03-10 23:25:11 | [diff] [blame] | 79 | // Return whether or not the StateStore has initialized itself. |
| 80 | bool IsInitialized() const; |
| 81 | |
Devlin Cronin | 7f7901f6 | 2017-12-06 19:08:29 | [diff] [blame] | 82 | void AddObserver(TestObserver* observer); |
| 83 | void RemoveObserver(TestObserver* observer); |
| 84 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 85 | private: |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 86 | class DelayedTaskQueue; |
| 87 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 88 | // content::NotificationObserver |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 89 | void Observe(int type, |
| 90 | const content::NotificationSource& source, |
| 91 | const content::NotificationDetails& details) override; |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 92 | |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 93 | void Init(); |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 94 | |
[email protected] | 479e392 | 2014-07-30 07:12:57 | [diff] [blame] | 95 | // When StateStore is constructed with |deferred_load| its initialization is |
| 96 | // delayed to avoid slowing down startup. |
| 97 | void InitAfterDelay(); |
| 98 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 99 | // Removes all keys registered for the given extension. |
| 100 | void RemoveKeysForExtension(const std::string& extension_id); |
| 101 | |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 102 | // ExtensionRegistryObserver implementation. |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 103 | void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 104 | const Extension* extension, |
| 105 | extensions::UninstallReason reason) override; |
| 106 | void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, |
| 107 | const Extension* extension, |
| 108 | bool is_update, |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 109 | const std::string& old_name) override; |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 110 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 111 | // The store that holds our key/values. |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 112 | std::unique_ptr<ValueStoreFrontend> store_; |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 113 | |
| 114 | // List of all known keys. They will be cleared for each extension when it is |
| 115 | // (un)installed. |
| 116 | std::set<std::string> registered_keys_; |
| 117 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 118 | // Keeps track of tasks we have delayed while starting up. |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 119 | std::unique_ptr<DelayedTaskQueue> task_queue_; |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 120 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 121 | base::ObserverList<TestObserver>::Unchecked observers_; |
Devlin Cronin | 7f7901f6 | 2017-12-06 19:08:29 | [diff] [blame] | 122 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 123 | content::NotificationRegistrar registrar_; |
[email protected] | 3cb79a5 | 2014-06-15 04:54:45 | [diff] [blame] | 124 | |
| 125 | ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 126 | extension_registry_observer_; |
Devlin Cronin | 7f7901f6 | 2017-12-06 19:08:29 | [diff] [blame] | 127 | |
| 128 | DISALLOW_COPY_AND_ASSIGN(StateStore); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } // namespace extensions |
| 132 | |
[email protected] | 479e392 | 2014-07-30 07:12:57 | [diff] [blame] | 133 | #endif // EXTENSIONS_BROWSER_STATE_STORE_H_ |