blob: af44a32b13619726945e72def9961c3c3b451dbe [file] [log] [blame]
[email protected]daf3ffda2014-06-25 06:44:571// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]90e800c2012-06-12 23:11:002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]479e3922014-07-30 07:12:575#ifndef EXTENSIONS_BROWSER_STATE_STORE_H_
6#define EXTENSIONS_BROWSER_STATE_STORE_H_
[email protected]90e800c2012-06-12 23:11:007
8#include <set>
9#include <string>
10
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
Devlin Cronin7f7901f62017-12-06 19:08:2912#include "base/macros.h"
[email protected]90e800c2012-06-12 23:11:0013#include "base/memory/weak_ptr.h"
Devlin Cronin7f7901f62017-12-06 19:08:2914#include "base/observer_list.h"
[email protected]3cb79a52014-06-15 04:54:4515#include "base/scoped_observer.h"
[email protected]90e800c2012-06-12 23:11:0016#include "content/public/browser/notification_observer.h"
17#include "content/public/browser/notification_registrar.h"
[email protected]3cb79a52014-06-15 04:54:4518#include "extensions/browser/extension_registry_observer.h"
[email protected]47b870f2014-03-01 00:34:0019#include "extensions/browser/value_store/value_store_frontend.h"
[email protected]90e800c2012-06-12 23:11:0020
[email protected]3cb79a52014-06-15 04:54:4521namespace content {
22class BrowserContext;
23}
24
[email protected]90e800c2012-06-12 23:11:0025namespace extensions {
26
[email protected]3cb79a52014-06-15 04:54:4527class ExtensionRegistry;
cmumford6ae8d462016-03-24 20:35:2728class ValueStoreFactory;
[email protected]3cb79a52014-06-15 04:54:4529
[email protected]90e800c2012-06-12 23:11:0030// A storage area for per-extension state that needs to be persisted to disk.
[email protected]daf3ffda2014-06-25 06:44:5731class StateStore : public base::SupportsWeakPtr<StateStore>,
32 public ExtensionRegistryObserver,
33 public content::NotificationObserver {
[email protected]90e800c2012-06-12 23:11:0034 public:
35 typedef ValueStoreFrontend::ReadCallback ReadCallback;
36
Devlin Cronin7f7901f62017-12-06 19:08:2937 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]a690e292012-12-19 19:22:4944 // If |deferred_load| is true, we won't load the database until the first
45 // page has been loaded.
[email protected]daf3ffda2014-06-25 06:44:5746 StateStore(content::BrowserContext* context,
cmumford6ae8d462016-03-24 20:35:2747 const scoped_refptr<ValueStoreFactory>& store_factory,
48 ValueStoreFrontend::BackendType backend_type,
[email protected]650b2d52013-02-10 03:41:4549 bool deferred_load);
[email protected]a6695e62012-06-14 00:06:0450 // This variant is useful for testing (using a mock ValueStore).
dchengf5d241082016-04-21 03:43:1151 StateStore(content::BrowserContext* context,
52 std::unique_ptr<ValueStore> store);
dcheng9168b2f2014-10-21 12:38:2453 ~StateStore() override;
[email protected]90e800c2012-06-12 23:11:0054
[email protected]479e3922014-07-30 07:12:5755 // 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]90e800c2012-06-12 23:11:0060 // 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,
dchengf5d241082016-04-21 03:43:1173 std::unique_ptr<base::Value> value);
[email protected]90e800c2012-06-12 23:11:0074
[email protected]a690e292012-12-19 19:22:4975 // 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]c6ea4b42014-03-10 23:25:1179 // Return whether or not the StateStore has initialized itself.
80 bool IsInitialized() const;
81
Devlin Cronin7f7901f62017-12-06 19:08:2982 void AddObserver(TestObserver* observer);
83 void RemoveObserver(TestObserver* observer);
84
[email protected]90e800c2012-06-12 23:11:0085 private:
[email protected]ebaa018d2012-12-11 21:42:5386 class DelayedTaskQueue;
87
[email protected]90e800c2012-06-12 23:11:0088 // content::NotificationObserver
dcheng9168b2f2014-10-21 12:38:2489 void Observe(int type,
90 const content::NotificationSource& source,
91 const content::NotificationDetails& details) override;
[email protected]90e800c2012-06-12 23:11:0092
[email protected]87093442013-01-12 16:34:0593 void Init();
[email protected]ebaa018d2012-12-11 21:42:5394
[email protected]479e3922014-07-30 07:12:5795 // When StateStore is constructed with |deferred_load| its initialization is
96 // delayed to avoid slowing down startup.
97 void InitAfterDelay();
98
[email protected]a690e292012-12-19 19:22:4999 // Removes all keys registered for the given extension.
100 void RemoveKeysForExtension(const std::string& extension_id);
101
[email protected]3cb79a52014-06-15 04:54:45102 // ExtensionRegistryObserver implementation.
dcheng9168b2f2014-10-21 12:38:24103 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,
dcheng9168b2f2014-10-21 12:38:24109 const std::string& old_name) override;
[email protected]3cb79a52014-06-15 04:54:45110
[email protected]90e800c2012-06-12 23:11:00111 // The store that holds our key/values.
dchengf5d241082016-04-21 03:43:11112 std::unique_ptr<ValueStoreFrontend> store_;
[email protected]90e800c2012-06-12 23:11:00113
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]ebaa018d2012-12-11 21:42:53118 // Keeps track of tasks we have delayed while starting up.
dchengf5d241082016-04-21 03:43:11119 std::unique_ptr<DelayedTaskQueue> task_queue_;
[email protected]ebaa018d2012-12-11 21:42:53120
Trent Apteda250ec3ab2018-08-19 08:52:19121 base::ObserverList<TestObserver>::Unchecked observers_;
Devlin Cronin7f7901f62017-12-06 19:08:29122
[email protected]90e800c2012-06-12 23:11:00123 content::NotificationRegistrar registrar_;
[email protected]3cb79a52014-06-15 04:54:45124
125 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
126 extension_registry_observer_;
Devlin Cronin7f7901f62017-12-06 19:08:29127
128 DISALLOW_COPY_AND_ASSIGN(StateStore);
[email protected]90e800c2012-06-12 23:11:00129};
130
131} // namespace extensions
132
[email protected]479e3922014-07-30 07:12:57133#endif // EXTENSIONS_BROWSER_STATE_STORE_H_