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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_STATE_STORE_H_
6 #define CHROME_BROWSER_EXTENSIONS_STATE_STORE_H_
11 #include "base/files/file_path.h"
12 #include "base/memory/weak_ptr.h"
13 #include "chrome/browser/value_store/value_store_frontend.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
19 namespace extensions
{
21 // A storage area for per-extension state that needs to be persisted to disk.
23 : public base::SupportsWeakPtr
<StateStore
>,
24 public content::NotificationObserver
{
26 typedef ValueStoreFrontend::ReadCallback ReadCallback
;
28 // If |deferred_load| is true, we won't load the database until the first
29 // page has been loaded.
30 StateStore(Profile
* profile
, const base::FilePath
& db_path
,
32 // This variant is useful for testing (using a mock ValueStore).
33 StateStore(Profile
* profile
, scoped_ptr
<ValueStore
> store
);
34 virtual ~StateStore();
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
);
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
);
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
);
51 // Removes a value for a given extension and key.
52 void RemoveExtensionValue(const std::string
& extension_id
,
53 const std::string
& key
);
56 class DelayedTaskQueue
;
58 // content::NotificationObserver
59 virtual void Observe(int type
,
60 const content::NotificationSource
& source
,
61 const content::NotificationDetails
& details
) OVERRIDE
;
65 // Removes all keys registered for the given extension.
66 void RemoveKeysForExtension(const std::string
& extension_id
);
68 // Path to our database, on disk. Empty during testing.
69 base::FilePath db_path_
;
71 // The store that holds our key/values.
72 ValueStoreFrontend store_
;
74 // List of all known keys. They will be cleared for each extension when it is
76 std::set
<std::string
> registered_keys_
;
78 // Keeps track of tasks we have delayed while starting up.
79 scoped_ptr
<DelayedTaskQueue
> task_queue_
;
81 content::NotificationRegistrar registrar_
;
84 } // namespace extensions
86 #endif // CHROME_BROWSER_EXTENSIONS_STATE_STORE_H_