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 "chrome/browser/value_store/value_store_frontend.h" 14 #include "content/public/browser/notification_observer.h" 15 #include "content/public/browser/notification_registrar.h" 16 17 class Profile; 18 19 namespace extensions { 20 21 // A storage area for per-extension state that needs to be persisted to disk. 22 class StateStore 23 : public base::SupportsWeakPtr<StateStore>, 24 public content::NotificationObserver { 25 public: 26 typedef ValueStoreFrontend::ReadCallback ReadCallback; 27 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, 31 bool deferred_load); 32 // This variant is useful for testing (using a mock ValueStore). 33 StateStore(Profile* profile, scoped_ptr<ValueStore> store); 34 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 51 // Removes a value for a given extension and key. 52 void RemoveExtensionValue(const std::string& extension_id, 53 const std::string& key); 54 55 private: 56 class DelayedTaskQueue; 57 58 // content::NotificationObserver 59 virtual void Observe(int type, 60 const content::NotificationSource& source, 61 const content::NotificationDetails& details) OVERRIDE; 62 63 void Init(); 64 65 // Removes all keys registered for the given extension. 66 void RemoveKeysForExtension(const std::string& extension_id); 67 68 // Path to our database, on disk. Empty during testing. 69 base::FilePath db_path_; 70 71 // 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 78 // Keeps track of tasks we have delayed while starting up. 79 scoped_ptr<DelayedTaskQueue> task_queue_; 80 81 content::NotificationRegistrar registrar_; 82 }; 83 84 } // namespace extensions 85 86 #endif // CHROME_BROWSER_EXTENSIONS_STATE_STORE_H_ 87