1 // Copyright 2013 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_CONNECTION_H_ 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONNECTION_H_ 7 8 #include <memory> 9 #include <set> 10 #include <vector> 11 12 #include "base/containers/flat_map.h" 13 #include "base/macros.h" 14 #include "base/memory/ref_counted.h" 15 #include "base/memory/weak_ptr.h" 16 #include "content/browser/indexed_db/indexed_db_database.h" 17 #include "content/browser/indexed_db/indexed_db_origin_state_handle.h" 18 #include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom-forward.h" 19 20 namespace content { 21 class IndexedDBDatabaseCallbacks; 22 class IndexedDBDatabaseError; 23 class IndexedDBObserver; 24 class IndexedDBTransaction; 25 class IndexedDBOriginStateHandle; 26 27 class CONTENT_EXPORT IndexedDBConnection { 28 public: 29 IndexedDBConnection(IndexedDBOriginStateHandle origin_state_handle, 30 IndexedDBClassFactory* indexed_db_class_factory, 31 base::WeakPtr<IndexedDBDatabase> database, 32 base::RepeatingClosure on_version_change_ignored, 33 base::OnceCallback<void(IndexedDBConnection*)> on_close, 34 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks); 35 virtual ~IndexedDBConnection(); 36 37 enum class CloseErrorHandling { 38 // Returns from the function on the first encounter with an error. 39 kReturnOnFirstError, 40 // Continues to call Abort() on all transactions despite any errors. 41 // The last error encountered is returned. 42 kAbortAllReturnLastError, 43 }; 44 45 leveldb::Status AbortTransactionsAndClose(CloseErrorHandling error_handling); 46 47 leveldb::Status CloseAndReportForceClose(); 48 bool IsConnected(); 49 50 void VersionChangeIgnored(); 51 52 virtual void ActivatePendingObservers( 53 std::vector<std::unique_ptr<IndexedDBObserver>> pending_observers); 54 // Removes observer listed in |remove_observer_ids| from active_observer of 55 // connection or pending_observer of transactions associated with this 56 // connection. 57 virtual void RemoveObservers(const std::vector<int32_t>& remove_observer_ids); 58 id()59 int32_t id() const { return id_; } 60 database()61 base::WeakPtr<IndexedDBDatabase> database() const { return database_; } callbacks()62 IndexedDBDatabaseCallbacks* callbacks() const { return callbacks_.get(); } active_observers()63 const std::vector<std::unique_ptr<IndexedDBObserver>>& active_observers() 64 const { 65 return active_observers_; 66 } GetWeakPtr()67 base::WeakPtr<IndexedDBConnection> GetWeakPtr() { 68 return weak_factory_.GetWeakPtr(); 69 } 70 71 // Creates a transaction for this connection. 72 IndexedDBTransaction* CreateTransaction( 73 int64_t id, 74 const std::set<int64_t>& scope, 75 blink::mojom::IDBTransactionMode mode, 76 IndexedDBBackingStore::Transaction* backing_store_transaction); 77 78 void AbortTransactionAndTearDownOnError(IndexedDBTransaction* transaction, 79 const IndexedDBDatabaseError& error); 80 81 leveldb::Status AbortAllTransactions(const IndexedDBDatabaseError& error); 82 83 // Returns the last error that occurred, if there is any. 84 leveldb::Status AbortAllTransactionsAndIgnoreErrors( 85 const IndexedDBDatabaseError& error); 86 87 IndexedDBTransaction* GetTransaction(int64_t id) const; 88 89 base::WeakPtr<IndexedDBTransaction> AddTransactionForTesting( 90 std::unique_ptr<IndexedDBTransaction> transaction); 91 92 // We ignore calls where the id doesn't exist to facilitate the AbortAll call. 93 // TODO(dmurph): Change that so this doesn't need to ignore unknown ids. 94 void RemoveTransaction(int64_t id); 95 96 const base::flat_map<int64_t, std::unique_ptr<IndexedDBTransaction>>& transactions()97 transactions() const { 98 return transactions_; 99 } 100 101 private: 102 void ClearStateAfterClose(); 103 104 const int32_t id_; 105 106 // Keeps the factory for this origin alive. 107 IndexedDBOriginStateHandle origin_state_handle_; 108 IndexedDBClassFactory* const indexed_db_class_factory_; 109 110 base::WeakPtr<IndexedDBDatabase> database_; 111 base::RepeatingClosure on_version_change_ignored_; 112 base::OnceCallback<void(IndexedDBConnection*)> on_close_; 113 114 // The connection owns transactions created on this connection. 115 // This is |flat_map| to preserve ordering, and because the vast majority of 116 // users have less than 200 transactions. 117 base::flat_map<int64_t, std::unique_ptr<IndexedDBTransaction>> transactions_; 118 119 // The callbacks_ member is cleared when the connection is closed. 120 // May be nullptr in unit tests. 121 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_; 122 std::vector<std::unique_ptr<IndexedDBObserver>> active_observers_; 123 124 SEQUENCE_CHECKER(sequence_checker_); 125 126 base::WeakPtrFactory<IndexedDBConnection> weak_factory_{this}; 127 128 DISALLOW_COPY_AND_ASSIGN(IndexedDBConnection); 129 }; 130 131 } // namespace content 132 133 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONNECTION_H_ 134