Reland the ULONG -> SIZE_T change from 317177
[chromium-blink-merge.git] / extensions / browser / state_store.h
blob19b3bd98a78effe49fa2f3406f75a2a5f4f82c41
1 // Copyright 2014 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.
5 #ifndef EXTENSIONS_BROWSER_STATE_STORE_H_
6 #define EXTENSIONS_BROWSER_STATE_STORE_H_
8 #include <set>
9 #include <string>
11 #include "base/files/file_path.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/scoped_observer.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "extensions/browser/extension_registry_observer.h"
17 #include "extensions/browser/value_store/value_store_frontend.h"
19 namespace content {
20 class BrowserContext;
23 namespace extensions {
25 class ExtensionRegistry;
27 // A storage area for per-extension state that needs to be persisted to disk.
28 class StateStore : public base::SupportsWeakPtr<StateStore>,
29 public ExtensionRegistryObserver,
30 public content::NotificationObserver {
31 public:
32 typedef ValueStoreFrontend::ReadCallback ReadCallback;
34 // If |deferred_load| is true, we won't load the database until the first
35 // page has been loaded.
36 StateStore(content::BrowserContext* context,
37 const base::FilePath& db_path,
38 bool deferred_load);
39 // This variant is useful for testing (using a mock ValueStore).
40 StateStore(content::BrowserContext* context, scoped_ptr<ValueStore> store);
41 ~StateStore() override;
43 // Requests that the state store to be initialized after its usual delay. Can
44 // be explicitly called by an embedder when the embedder does not trigger the
45 // usual page load notifications.
46 void RequestInitAfterDelay();
48 // Register a key for removal upon extension install/uninstall. We remove
49 // for install to reset state when an extension upgrades.
50 void RegisterKey(const std::string& key);
52 // Get the value associated with the given extension and key, and pass
53 // it to |callback| asynchronously.
54 void GetExtensionValue(const std::string& extension_id,
55 const std::string& key,
56 ReadCallback callback);
58 // Sets a value for a given extension and key.
59 void SetExtensionValue(const std::string& extension_id,
60 const std::string& key,
61 scoped_ptr<base::Value> value);
63 // Removes a value for a given extension and key.
64 void RemoveExtensionValue(const std::string& extension_id,
65 const std::string& key);
67 // Return whether or not the StateStore has initialized itself.
68 bool IsInitialized() const;
70 private:
71 class DelayedTaskQueue;
73 // content::NotificationObserver
74 void Observe(int type,
75 const content::NotificationSource& source,
76 const content::NotificationDetails& details) override;
78 void Init();
80 // When StateStore is constructed with |deferred_load| its initialization is
81 // delayed to avoid slowing down startup.
82 void InitAfterDelay();
84 // Removes all keys registered for the given extension.
85 void RemoveKeysForExtension(const std::string& extension_id);
87 // ExtensionRegistryObserver implementation.
88 void OnExtensionUninstalled(content::BrowserContext* browser_context,
89 const Extension* extension,
90 extensions::UninstallReason reason) override;
91 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
92 const Extension* extension,
93 bool is_update,
94 bool from_ephemeral,
95 const std::string& old_name) override;
97 // Path to our database, on disk. Empty during testing.
98 base::FilePath db_path_;
100 // The store that holds our key/values.
101 ValueStoreFrontend store_;
103 // List of all known keys. They will be cleared for each extension when it is
104 // (un)installed.
105 std::set<std::string> registered_keys_;
107 // Keeps track of tasks we have delayed while starting up.
108 scoped_ptr<DelayedTaskQueue> task_queue_;
110 content::NotificationRegistrar registrar_;
112 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
113 extension_registry_observer_;
116 } // namespace extensions
118 #endif // EXTENSIONS_BROWSER_STATE_STORE_H_