1 // Copyright 2017 The Chromium Embedded Framework Authors. 2 // Portions copyright 2016 The Chromium Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 6 #ifndef CEF_LIBCEF_BROWSER_EXTENSIONS_VALUE_STORE_CEF_VALUE_STORE_FACTORY_H_ 7 #define CEF_LIBCEF_BROWSER_EXTENSIONS_VALUE_STORE_CEF_VALUE_STORE_FACTORY_H_ 8 9 #include <map> 10 #include <memory> 11 #include <set> 12 13 #include "base/files/file_path.h" 14 #include "extensions/browser/value_store/value_store_factory.h" 15 #include "extensions/common/extension_id.h" 16 17 class ValueStore; 18 19 namespace extensions { 20 21 // Will either open a database on disk (if path provided) returning a 22 // |LeveldbValueStore|. Otherwise a new |CefValueStore| instance will be 23 // returned. 24 class CefValueStoreFactory : public ValueStoreFactory { 25 public: 26 CefValueStoreFactory(); 27 explicit CefValueStoreFactory(const base::FilePath& db_path); 28 29 // ValueStoreFactory 30 std::unique_ptr<ValueStore> CreateRulesStore() override; 31 std::unique_ptr<ValueStore> CreateStateStore() override; 32 std::unique_ptr<ValueStore> CreateSettingsStore( 33 settings_namespace::Namespace settings_namespace, 34 ModelType model_type, 35 const ExtensionId& extension_id) override; 36 void DeleteSettings(settings_namespace::Namespace settings_namespace, 37 ModelType model_type, 38 const ExtensionId& extension_id) override; 39 bool HasSettings(settings_namespace::Namespace settings_namespace, 40 ModelType model_type, 41 const ExtensionId& extension_id) override; 42 std::set<ExtensionId> GetKnownExtensionIDs( 43 settings_namespace::Namespace settings_namespace, 44 ModelType model_type) const override; 45 46 // Return the last created |ValueStore|. Use with caution as this may return 47 // a dangling pointer since the creator now owns the ValueStore which can be 48 // deleted at any time. 49 ValueStore* LastCreatedStore() const; 50 // Return a previously created |ValueStore| for an extension. 51 ValueStore* GetExisting(const ExtensionId& extension_id) const; 52 // Reset this class (as if just created). 53 void Reset(); 54 55 private: 56 // Manages a collection of |ValueStore|'s created for an app/extension. 57 // One of these exists for each setting type. 58 class StorageHelper { 59 public: 60 StorageHelper(); 61 ~StorageHelper(); 62 std::set<ExtensionId> GetKnownExtensionIDs(ModelType model_type) const; 63 ValueStore* AddValueStore(const ExtensionId& extension_id, 64 ValueStore* value_store, 65 ModelType model_type); 66 void DeleteSettings(const ExtensionId& extension_id, ModelType model_type); 67 bool HasSettings(const ExtensionId& extension_id, 68 ModelType model_type) const; 69 void Reset(); 70 ValueStore* GetExisting(const ExtensionId& extension_id) const; 71 72 private: 73 std::map<ExtensionId, ValueStore*> app_stores_; 74 std::map<ExtensionId, ValueStore*> extension_stores_; 75 76 DISALLOW_COPY_AND_ASSIGN(StorageHelper); 77 }; 78 79 StorageHelper& GetStorageHelper( 80 settings_namespace::Namespace settings_namespace); 81 82 ~CefValueStoreFactory() override; 83 base::FilePath db_path_; 84 ValueStore* last_created_store_ = nullptr; 85 86 // None of these value stores are owned by this factory, so care must be 87 // taken when calling GetExisting. 88 StorageHelper local_helper_; 89 StorageHelper sync_helper_; 90 StorageHelper managed_helper_; 91 92 DISALLOW_COPY_AND_ASSIGN(CefValueStoreFactory); 93 }; 94 95 } // namespace extensions 96 97 #endif // CEF_LIBCEF_BROWSER_EXTENSIONS_VALUE_STORE_CEF_VALUE_STORE_FACTORY_H_ 98