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_API_STORAGE_STORAGE_FRONTEND_H_ 6 #define EXTENSIONS_BROWSER_API_STORAGE_STORAGE_FRONTEND_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/memory/ref_counted.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "extensions/browser/api/storage/settings_namespace.h" 14 #include "extensions/browser/api/storage/settings_observer.h" 15 #include "extensions/browser/api/storage/settings_storage_factory.h" 16 #include "extensions/browser/api/storage/value_store_cache.h" 17 #include "extensions/browser/browser_context_keyed_api_factory.h" 18 19 namespace content { 20 class BrowserContext; 21 } 22 23 namespace extensions { 24 25 // The component of the Storage API which runs on the UI thread. 26 class StorageFrontend : public BrowserContextKeyedAPI { 27 public: 28 // Returns the current instance for |context|. 29 static StorageFrontend* Get(content::BrowserContext* context); 30 31 // Creates with a specific |storage_factory|. Caller owns the object. 32 static StorageFrontend* CreateForTesting( 33 const scoped_refptr<SettingsStorageFactory>& storage_factory, 34 content::BrowserContext* context); 35 36 // Public so tests can create and delete their own instances. 37 virtual ~StorageFrontend(); 38 39 // Returns the value store cache for |settings_namespace|. 40 ValueStoreCache* GetValueStoreCache( 41 settings_namespace::Namespace settings_namespace) const; 42 43 // Returns true if |settings_namespace| is a valid namespace. 44 bool IsStorageEnabled(settings_namespace::Namespace settings_namespace) const; 45 46 // Runs |callback| with the storage area of the given |settings_namespace| 47 // for the |extension|. 48 void RunWithStorage(scoped_refptr<const Extension> extension, 49 settings_namespace::Namespace settings_namespace, 50 const ValueStoreCache::StorageCallback& callback); 51 52 // Deletes the settings for the given |extension_id|. 53 void DeleteStorageSoon(const std::string& extension_id); 54 55 // Gets the thread-safe observer list. 56 scoped_refptr<SettingsObserverList> GetObservers(); 57 58 void DisableStorageForTesting( 59 settings_namespace::Namespace settings_namespace); 60 61 // BrowserContextKeyedAPI implementation. 62 static BrowserContextKeyedAPIFactory<StorageFrontend>* GetFactoryInstance(); 63 static const char* service_name(); 64 static const bool kServiceRedirectedInIncognito = true; 65 static const bool kServiceIsNULLWhileTesting = true; 66 67 private: 68 friend class BrowserContextKeyedAPIFactory<StorageFrontend>; 69 70 typedef std::map<settings_namespace::Namespace, ValueStoreCache*> CacheMap; 71 72 // Constructor for normal BrowserContextKeyedAPI usage. 73 explicit StorageFrontend(content::BrowserContext* context); 74 75 // Constructor for tests. 76 StorageFrontend(const scoped_refptr<SettingsStorageFactory>& storage_factory, 77 content::BrowserContext* context); 78 79 void Init(const scoped_refptr<SettingsStorageFactory>& storage_factory); 80 81 // The (non-incognito) browser context this Frontend belongs to. 82 content::BrowserContext* const browser_context_; 83 84 // List of observers to settings changes. 85 scoped_refptr<SettingsObserverList> observers_; 86 87 // Observer for |browser_context_|. 88 scoped_ptr<SettingsObserver> browser_context_observer_; 89 90 // Maps a known namespace to its corresponding ValueStoreCache. The caches 91 // are owned by this object. 92 CacheMap caches_; 93 94 DISALLOW_COPY_AND_ASSIGN(StorageFrontend); 95 }; 96 97 } // namespace extensions 98 99 #endif // EXTENSIONS_BROWSER_API_STORAGE_STORAGE_FRONTEND_H_ 100