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. 4 5 #ifndef CHROME_BROWSER_EXTENSIONS_STATE_STORE_H_ 6 #define CHROME_BROWSER_EXTENSIONS_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 19 class Profile; 20 21 namespace content { 22 class BrowserContext; 23 } 24 25 namespace extensions { 26 27 class ExtensionRegistry; 28 29 // A storage area for per-extension state that needs to be persisted to disk. 30 class StateStore 31 : public base::SupportsWeakPtr<StateStore>, 32 public ExtensionRegistryObserver, 33 public content::NotificationObserver { 34 public: 35 typedef ValueStoreFrontend::ReadCallback ReadCallback; 36 37 // If |deferred_load| is true, we won't load the database until the first 38 // page has been loaded. 39 StateStore(Profile* profile, const base::FilePath& db_path, 40 bool deferred_load); 41 // This variant is useful for testing (using a mock ValueStore). 42 StateStore(Profile* profile, scoped_ptr<ValueStore> store); 43 virtual ~StateStore(); 44 45 // Register a key for removal upon extension install/uninstall. We remove 46 // for install to reset state when an extension upgrades. 47 void RegisterKey(const std::string& key); 48 49 // Get the value associated with the given extension and key, and pass 50 // it to |callback| asynchronously. 51 void GetExtensionValue(const std::string& extension_id, 52 const std::string& key, 53 ReadCallback callback); 54 55 // Sets a value for a given extension and key. 56 void SetExtensionValue(const std::string& extension_id, 57 const std::string& key, 58 scoped_ptr<base::Value> value); 59 60 // Removes a value for a given extension and key. 61 void RemoveExtensionValue(const std::string& extension_id, 62 const std::string& key); 63 64 // Return whether or not the StateStore has initialized itself. 65 bool IsInitialized() const; 66 67 private: 68 class DelayedTaskQueue; 69 70 // content::NotificationObserver 71 virtual void Observe(int type, 72 const content::NotificationSource& source, 73 const content::NotificationDetails& details) OVERRIDE; 74 75 void Init(); 76 77 // Removes all keys registered for the given extension. 78 void RemoveKeysForExtension(const std::string& extension_id); 79 80 // ExtensionRegistryObserver implementation. 81 virtual void OnExtensionUninstalled(content::BrowserContext* browser_context, 82 const Extension* extension) OVERRIDE; 83 virtual void OnExtensionWillBeInstalled( 84 content::BrowserContext* browser_context, 85 const Extension* extension, 86 bool is_update, 87 bool from_ephemeral, 88 const std::string& old_name) OVERRIDE; 89 90 // Path to our database, on disk. Empty during testing. 91 base::FilePath db_path_; 92 93 // The store that holds our key/values. 94 ValueStoreFrontend store_; 95 96 // List of all known keys. They will be cleared for each extension when it is 97 // (un)installed. 98 std::set<std::string> registered_keys_; 99 100 // Keeps track of tasks we have delayed while starting up. 101 scoped_ptr<DelayedTaskQueue> task_queue_; 102 103 content::NotificationRegistrar registrar_; 104 105 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> 106 extension_registry_observer_; 107 }; 108 109 } // namespace extensions 110 111 #endif // CHROME_BROWSER_EXTENSIONS_STATE_STORE_H_ 112