[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 | #ifndef CHROME_BROWSER_EXTENSIONS_STATE_STORE_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_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" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 13 | #include "content/public/browser/notification_observer.h" |
| 14 | #include "content/public/browser/notification_registrar.h" |
[email protected] | 47b870f | 2014-03-01 00:34:00 | [diff] [blame^] | 15 | #include "extensions/browser/value_store/value_store_frontend.h" |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 16 | |
| 17 | class Profile; |
| 18 | |
| 19 | namespace extensions { |
| 20 | |
| 21 | // A storage area for per-extension state that needs to be persisted to disk. |
| 22 | class StateStore |
| 23 | : public base::SupportsWeakPtr<StateStore>, |
| 24 | public content::NotificationObserver { |
| 25 | public: |
| 26 | typedef ValueStoreFrontend::ReadCallback ReadCallback; |
| 27 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 28 | // If |deferred_load| is true, we won't load the database until the first |
| 29 | // page has been loaded. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 30 | StateStore(Profile* profile, const base::FilePath& db_path, |
| 31 | bool deferred_load); |
[email protected] | a6695e6 | 2012-06-14 00:06:04 | [diff] [blame] | 32 | // This variant is useful for testing (using a mock ValueStore). |
[email protected] | da2b622c | 2013-09-27 21:30:40 | [diff] [blame] | 33 | StateStore(Profile* profile, scoped_ptr<ValueStore> store); |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 34 | virtual ~StateStore(); |
| 35 | |
| 36 | // Register a key for removal upon extension install/uninstall. We remove |
| 37 | // for install to reset state when an extension upgrades. |
| 38 | void RegisterKey(const std::string& key); |
| 39 | |
| 40 | // Get the value associated with the given extension and key, and pass |
| 41 | // it to |callback| asynchronously. |
| 42 | void GetExtensionValue(const std::string& extension_id, |
| 43 | const std::string& key, |
| 44 | ReadCallback callback); |
| 45 | |
| 46 | // Sets a value for a given extension and key. |
| 47 | void SetExtensionValue(const std::string& extension_id, |
| 48 | const std::string& key, |
| 49 | scoped_ptr<base::Value> value); |
| 50 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 51 | // Removes a value for a given extension and key. |
| 52 | void RemoveExtensionValue(const std::string& extension_id, |
| 53 | const std::string& key); |
| 54 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 55 | private: |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 56 | class DelayedTaskQueue; |
| 57 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 58 | // content::NotificationObserver |
| 59 | virtual void Observe(int type, |
| 60 | const content::NotificationSource& source, |
| 61 | const content::NotificationDetails& details) OVERRIDE; |
| 62 | |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 63 | void Init(); |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 64 | |
[email protected] | a690e29 | 2012-12-19 19:22:49 | [diff] [blame] | 65 | // Removes all keys registered for the given extension. |
| 66 | void RemoveKeysForExtension(const std::string& extension_id); |
| 67 | |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 68 | // Path to our database, on disk. Empty during testing. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 69 | base::FilePath db_path_; |
[email protected] | 8709344 | 2013-01-12 16:34:05 | [diff] [blame] | 70 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 71 | // The store that holds our key/values. |
| 72 | ValueStoreFrontend store_; |
| 73 | |
| 74 | // List of all known keys. They will be cleared for each extension when it is |
| 75 | // (un)installed. |
| 76 | std::set<std::string> registered_keys_; |
| 77 | |
[email protected] | ebaa018d | 2012-12-11 21:42:53 | [diff] [blame] | 78 | // Keeps track of tasks we have delayed while starting up. |
| 79 | scoped_ptr<DelayedTaskQueue> task_queue_; |
| 80 | |
[email protected] | 90e800c | 2012-06-12 23:11:00 | [diff] [blame] | 81 | content::NotificationRegistrar registrar_; |
| 82 | }; |
| 83 | |
| 84 | } // namespace extensions |
| 85 | |
| 86 | #endif // CHROME_BROWSER_EXTENSIONS_STATE_STORE_H_ |