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.
4
5#ifndef EXTENSIONS_BROWSER_STATE_STORE_H_
6#define EXTENSIONS_BROWSER_STATE_STORE_H_
7
8#include <set>
9#include <string>
10
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"
18
19namespace content {
20class BrowserContext;
21}
22
23namespace extensions {
24
25class ExtensionRegistry;
26
27// A storage area for per-extension state that needs to be persisted to disk.
28class StateStore : public base::SupportsWeakPtr<StateStore>,
29                   public ExtensionRegistryObserver,
30                   public content::NotificationObserver {
31 public:
32  typedef ValueStoreFrontend::ReadCallback ReadCallback;
33
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();
42
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();
47
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);
51
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);
57
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);
62
63  // Removes a value for a given extension and key.
64  void RemoveExtensionValue(const std::string& extension_id,
65                            const std::string& key);
66
67  // Return whether or not the StateStore has initialized itself.
68  bool IsInitialized() const;
69
70 private:
71  class DelayedTaskQueue;
72
73  // content::NotificationObserver
74  virtual void Observe(int type,
75                       const content::NotificationSource& source,
76                       const content::NotificationDetails& details) OVERRIDE;
77
78  void Init();
79
80  // When StateStore is constructed with |deferred_load| its initialization is
81  // delayed to avoid slowing down startup.
82  void InitAfterDelay();
83
84  // Removes all keys registered for the given extension.
85  void RemoveKeysForExtension(const std::string& extension_id);
86
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;
98
99  // Path to our database, on disk. Empty during testing.
100  base::FilePath db_path_;
101
102  // The store that holds our key/values.
103  ValueStoreFrontend store_;
104
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_;
108
109  // Keeps track of tasks we have delayed while starting up.
110  scoped_ptr<DelayedTaskQueue> task_queue_;
111
112  content::NotificationRegistrar registrar_;
113
114  ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
115      extension_registry_observer_;
116};
117
118}  // namespace extensions
119
120#endif  // EXTENSIONS_BROWSER_STATE_STORE_H_
121