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 CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CLASS_FACTORY_H_ 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CLASS_FACTORY_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 #include <set> 12 #include <utility> 13 14 #include "base/callback.h" 15 #include "base/lazy_instance.h" 16 #include "base/memory/ref_counted.h" 17 #include "base/strings/string16.h" 18 #include "components/services/storage/indexed_db/scopes/scopes_lock_manager.h" 19 #include "content/browser/indexed_db/indexed_db_backing_store.h" 20 #include "content/browser/indexed_db/indexed_db_database.h" 21 #include "content/browser/indexed_db/indexed_db_task_helper.h" 22 #include "content/browser/indexed_db/indexed_db_transaction.h" 23 #include "content/common/content_export.h" 24 #include "third_party/blink/public/common/indexeddb/web_idb_types.h" 25 #include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom.h" 26 #include "third_party/leveldatabase/env_chromium.h" 27 #include "third_party/leveldatabase/src/include/leveldb/status.h" 28 29 namespace content { 30 class IndexedDBBackingStore; 31 class IndexedDBConnection; 32 class IndexedDBFactory; 33 class IndexedDBTransaction; 34 class LevelDBFactory; 35 class TransactionalLevelDBFactory; 36 37 // Use this factory to create some IndexedDB objects. Exists solely to 38 // facilitate tests which sometimes need to inject mock objects into the system. 39 // TODO(dmurph): Remove this class in favor of dependency injection. This makes 40 // it really hard to iterate on the system. 41 class CONTENT_EXPORT IndexedDBClassFactory { 42 public: 43 typedef IndexedDBClassFactory* GetterCallback(); 44 // Used to report irrecoverable backend errors. The second argument can be 45 // null. 46 using ErrorCallback = 47 base::RepeatingCallback<void(leveldb::Status, const char*)>; 48 49 static IndexedDBClassFactory* Get(); 50 51 static void SetIndexedDBClassFactoryGetter(GetterCallback* cb); 52 53 // Visible for testing. 54 static leveldb_env::Options GetLevelDBOptions(); 55 56 virtual LevelDBFactory& leveldb_factory(); 57 virtual TransactionalLevelDBFactory& transactional_leveldb_factory(); 58 59 // Returns a constructed database, or a leveldb::Status error if there was a 60 // problem initializing the database. |run_tasks_callback| is called when the 61 // database has tasks to run. 62 virtual std::pair<std::unique_ptr<IndexedDBDatabase>, leveldb::Status> 63 CreateIndexedDBDatabase( 64 const base::string16& name, 65 IndexedDBBackingStore* backing_store, 66 IndexedDBFactory* factory, 67 TasksAvailableCallback tasks_available_callback, 68 std::unique_ptr<IndexedDBMetadataCoding> metadata_coding, 69 const IndexedDBDatabase::Identifier& unique_identifier, 70 ScopesLockManager* transaction_lock_manager); 71 72 // |tasks_available_callback| is called when the transaction has tasks to run. 73 virtual std::unique_ptr<IndexedDBTransaction> CreateIndexedDBTransaction( 74 int64_t id, 75 IndexedDBConnection* connection, 76 const std::set<int64_t>& scope, 77 blink::mojom::IDBTransactionMode mode, 78 TasksAvailableCallback tasks_available_callback, 79 IndexedDBTransaction::TearDownCallback tear_down_callback, 80 IndexedDBBackingStore::Transaction* backing_store_transaction); 81 82 void SetLevelDBFactoryForTesting(LevelDBFactory* leveldb_factory); 83 84 protected: 85 IndexedDBClassFactory(); 86 IndexedDBClassFactory( 87 LevelDBFactory* leveldb_factory, 88 TransactionalLevelDBFactory* transactional_leveldb_factory); 89 virtual ~IndexedDBClassFactory() = default; 90 friend struct base::LazyInstanceTraitsBase<IndexedDBClassFactory>; 91 92 LevelDBFactory* leveldb_factory_; 93 TransactionalLevelDBFactory* transactional_leveldb_factory_; 94 }; 95 96 } // namespace content 97 98 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CLASS_FACTORY_H_ 99