Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / browser / state_store.h
blob034b07d9653e3646faae28c1dd87c95749e7a293
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 virtual ~StateStore();
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 virtual 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 virtual void OnExtensionUninstalled(
89 content::BrowserContext* browser_context,
90 const Extension* extension,
91 extensions::UninstallReason reason) OVERRIDE;
92 virtual void OnExtensionWillBeInstalled(
93 content::BrowserContext* browser_context,
94 const Extension* extension,
95 bool is_update,
96 bool from_ephemeral,
97 const std::string& old_name) OVERRIDE;
99 // Path to our database, on disk. Empty during testing.
100 base::FilePath db_path_;
102 // The store that holds our key/values.
103 ValueStoreFrontend store_;
105 // List of all known keys. They will be cleared for each extension when it is
106 // (un)installed.
107 std::set<std::string> registered_keys_;
109 // Keeps track of tasks we have delayed while starting up.
110 scoped_ptr<DelayedTaskQueue> task_queue_;
112 content::NotificationRegistrar registrar_;
114 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
115 extension_registry_observer_;
118 } // namespace extensions
120 #endif // EXTENSIONS_BROWSER_STATE_STORE_H_