blob: ba4867c81b6727001ea9a5406d025bea0329f6d6 [file] [log] [blame]
[email protected]90e800c2012-06-12 23:11:001// 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.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_STATE_STORE_H_
6#define CHROME_BROWSER_EXTENSIONS_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"
[email protected]90e800c2012-06-12 23:11:0012#include "base/memory/weak_ptr.h"
[email protected]90e800c2012-06-12 23:11:0013#include "content/public/browser/notification_observer.h"
14#include "content/public/browser/notification_registrar.h"
[email protected]47b870f2014-03-01 00:34:0015#include "extensions/browser/value_store/value_store_frontend.h"
[email protected]90e800c2012-06-12 23:11:0016
17class Profile;
18
19namespace extensions {
20
21// A storage area for per-extension state that needs to be persisted to disk.
22class StateStore
23 : public base::SupportsWeakPtr<StateStore>,
24 public content::NotificationObserver {
25 public:
26 typedef ValueStoreFrontend::ReadCallback ReadCallback;
27
[email protected]a690e292012-12-19 19:22:4928 // If |deferred_load| is true, we won't load the database until the first
29 // page has been loaded.
[email protected]650b2d52013-02-10 03:41:4530 StateStore(Profile* profile, const base::FilePath& db_path,
31 bool deferred_load);
[email protected]a6695e62012-06-14 00:06:0432 // This variant is useful for testing (using a mock ValueStore).
[email protected]da2b622c2013-09-27 21:30:4033 StateStore(Profile* profile, scoped_ptr<ValueStore> store);
[email protected]90e800c2012-06-12 23:11:0034 virtual ~StateStore();
35
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);
39
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);
45
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);
50
[email protected]a690e292012-12-19 19:22:4951 // Removes a value for a given extension and key.
52 void RemoveExtensionValue(const std::string& extension_id,
53 const std::string& key);
54
[email protected]90e800c2012-06-12 23:11:0055 private:
[email protected]ebaa018d2012-12-11 21:42:5356 class DelayedTaskQueue;
57
[email protected]90e800c2012-06-12 23:11:0058 // content::NotificationObserver
59 virtual void Observe(int type,
60 const content::NotificationSource& source,
61 const content::NotificationDetails& details) OVERRIDE;
62
[email protected]87093442013-01-12 16:34:0563 void Init();
[email protected]ebaa018d2012-12-11 21:42:5364
[email protected]a690e292012-12-19 19:22:4965 // Removes all keys registered for the given extension.
66 void RemoveKeysForExtension(const std::string& extension_id);
67
[email protected]87093442013-01-12 16:34:0568 // Path to our database, on disk. Empty during testing.
[email protected]650b2d52013-02-10 03:41:4569 base::FilePath db_path_;
[email protected]87093442013-01-12 16:34:0570
[email protected]90e800c2012-06-12 23:11:0071 // The store that holds our key/values.
72 ValueStoreFrontend store_;
73
74 // List of all known keys. They will be cleared for each extension when it is
75 // (un)installed.
76 std::set<std::string> registered_keys_;
77
[email protected]ebaa018d2012-12-11 21:42:5378 // Keeps track of tasks we have delayed while starting up.
79 scoped_ptr<DelayedTaskQueue> task_queue_;
80
[email protected]90e800c2012-06-12 23:11:0081 content::NotificationRegistrar registrar_;
82};
83
84} // namespace extensions
85
86#endif // CHROME_BROWSER_EXTENSIONS_STATE_STORE_H_