Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / browser / state_store.h
blob0cb475b600743a8e1d0bc2523a1aba1495c8db6c
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 std::string& uma_client_name,
38 const base::FilePath& db_path,
39 bool deferred_load);
40 // This variant is useful for testing (using a mock ValueStore).
41 StateStore(content::BrowserContext* context, scoped_ptr<ValueStore> store);
42 ~StateStore() override;
44 // Requests that the state store to be initialized after its usual delay. Can
45 // be explicitly called by an embedder when the embedder does not trigger the
46 // usual page load notifications.
47 void RequestInitAfterDelay();
49 // Register a key for removal upon extension install/uninstall. We remove
50 // for install to reset state when an extension upgrades.
51 void RegisterKey(const std::string& key);
53 // Get the value associated with the given extension and key, and pass
54 // it to |callback| asynchronously.
55 void GetExtensionValue(const std::string& extension_id,
56 const std::string& key,
57 ReadCallback callback);
59 // Sets a value for a given extension and key.
60 void SetExtensionValue(const std::string& extension_id,
61 const std::string& key,
62 scoped_ptr<base::Value> value);
64 // Removes a value for a given extension and key.
65 void RemoveExtensionValue(const std::string& extension_id,
66 const std::string& key);
68 // Return whether or not the StateStore has initialized itself.
69 bool IsInitialized() const;
71 private:
72 class DelayedTaskQueue;
74 // content::NotificationObserver
75 void Observe(int type,
76 const content::NotificationSource& source,
77 const content::NotificationDetails& details) override;
79 void Init();
81 // When StateStore is constructed with |deferred_load| its initialization is
82 // delayed to avoid slowing down startup.
83 void InitAfterDelay();
85 // Removes all keys registered for the given extension.
86 void RemoveKeysForExtension(const std::string& extension_id);
88 // ExtensionRegistryObserver implementation.
89 void OnExtensionUninstalled(content::BrowserContext* browser_context,
90 const Extension* extension,
91 extensions::UninstallReason reason) override;
92 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
93 const Extension* extension,
94 bool is_update,
95 bool from_ephemeral,
96 const std::string& old_name) override;
98 // Path to our database, on disk. Empty during testing.
99 base::FilePath db_path_;
101 // Database client name used for UMA logging by database backend.
102 const std::string uma_client_name_;
104 // The store that holds our key/values.
105 ValueStoreFrontend store_;
107 // List of all known keys. They will be cleared for each extension when it is
108 // (un)installed.
109 std::set<std::string> registered_keys_;
111 // Keeps track of tasks we have delayed while starting up.
112 scoped_ptr<DelayedTaskQueue> task_queue_;
114 content::NotificationRegistrar registrar_;
116 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
117 extension_registry_observer_;
120 } // namespace extensions
122 #endif // EXTENSIONS_BROWSER_STATE_STORE_H_