[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 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 | |
jsbell | 4238c66 | 2016-08-18 00:23:02 | [diff] [blame] | 8 | #include <memory> |
dmurph | 9d00e05d | 2016-12-01 23:00:34 | [diff] [blame] | 9 | #include <set> |
jsbell | 4238c66 | 2016-08-18 00:23:02 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
Daniel Murphy | 7c75043 | 2019-08-20 01:39:47 | [diff] [blame] | 12 | #include "base/containers/flat_map.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
cmumford | 5c0b3e0 | 2014-11-11 01:22:51 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 16 | #include "content/browser/indexed_db/indexed_db_database.h" |
Daniel Murphy | 4c0f9c1 | 2019-05-23 01:14:35 | [diff] [blame] | 17 | #include "content/browser/indexed_db/indexed_db_origin_state_handle.h" |
Henrique Ferreiro | da0a55c | 2019-11-12 14:06:04 | [diff] [blame] | 18 | #include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom-forward.h" |
[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 19 | |
| 20 | namespace content { |
dmurph | 9d00e05d | 2016-12-01 23:00:34 | [diff] [blame] | 21 | class IndexedDBDatabaseCallbacks; |
| 22 | class IndexedDBDatabaseError; |
dmurph | 9d00e05d | 2016-12-01 23:00:34 | [diff] [blame] | 23 | class IndexedDBTransaction; |
Daniel Murphy | 4c0f9c1 | 2019-05-23 01:14:35 | [diff] [blame] | 24 | class IndexedDBOriginStateHandle; |
[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 25 | |
| 26 | class CONTENT_EXPORT IndexedDBConnection { |
| 27 | public: |
Adrienne Walker | 80d95f0 | 2020-02-07 22:37:04 | [diff] [blame] | 28 | IndexedDBConnection(IndexedDBOriginStateHandle origin_state_handle, |
Daniel Murphy | 1df1bc4 | 2019-06-17 21:34:17 | [diff] [blame] | 29 | IndexedDBClassFactory* indexed_db_class_factory, |
Daniel Murphy | 4c0f9c1 | 2019-05-23 01:14:35 | [diff] [blame] | 30 | base::WeakPtr<IndexedDBDatabase> database, |
| 31 | base::RepeatingClosure on_version_change_ignored, |
| 32 | base::OnceCallback<void(IndexedDBConnection*)> on_close, |
[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 33 | scoped_refptr<IndexedDBDatabaseCallbacks> callbacks); |
| 34 | virtual ~IndexedDBConnection(); |
| 35 | |
Daniel Murphy | f5fc092 | 2019-09-13 07:42:03 | [diff] [blame] | 36 | enum class CloseErrorHandling { |
| 37 | // Returns from the function on the first encounter with an error. |
| 38 | kReturnOnFirstError, |
| 39 | // Continues to call Abort() on all transactions despite any errors. |
| 40 | // The last error encountered is returned. |
| 41 | kAbortAllReturnLastError, |
| 42 | }; |
| 43 | |
| 44 | leveldb::Status AbortTransactionsAndClose(CloseErrorHandling error_handling); |
| 45 | |
| 46 | leveldb::Status CloseAndReportForceClose(); |
Daniel Murphy | 4c0f9c1 | 2019-05-23 01:14:35 | [diff] [blame] | 47 | bool IsConnected(); |
[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 48 | |
[email protected] | 75a854a | 2014-07-07 15:57:36 | [diff] [blame] | 49 | void VersionChangeIgnored(); |
| 50 | |
palakj | 3fee5f7 | 2016-07-20 00:21:25 | [diff] [blame] | 51 | int32_t id() const { return id_; } |
palakj | 3fee5f7 | 2016-07-20 00:21:25 | [diff] [blame] | 52 | |
Daniel Murphy | 4c0f9c1 | 2019-05-23 01:14:35 | [diff] [blame] | 53 | base::WeakPtr<IndexedDBDatabase> database() const { return database_; } |
jsbell | 1869ef4 | 2014-10-03 17:34:38 | [diff] [blame] | 54 | IndexedDBDatabaseCallbacks* callbacks() const { return callbacks_.get(); } |
palakj | 05f35ea | 2016-07-08 00:59:21 | [diff] [blame] | 55 | base::WeakPtr<IndexedDBConnection> GetWeakPtr() { |
| 56 | return weak_factory_.GetWeakPtr(); |
| 57 | } |
[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 58 | |
dmurph | 9d00e05d | 2016-12-01 23:00:34 | [diff] [blame] | 59 | // Creates a transaction for this connection. |
| 60 | IndexedDBTransaction* CreateTransaction( |
| 61 | int64_t id, |
| 62 | const std::set<int64_t>& scope, |
Chase Phillips | b2851f32 | 2018-11-16 00:27:48 | [diff] [blame] | 63 | blink::mojom::IDBTransactionMode mode, |
dmurph | 9d00e05d | 2016-12-01 23:00:34 | [diff] [blame] | 64 | IndexedDBBackingStore::Transaction* backing_store_transaction); |
| 65 | |
Daniel Murphy | f5fc092 | 2019-09-13 07:42:03 | [diff] [blame] | 66 | void AbortTransactionAndTearDownOnError(IndexedDBTransaction* transaction, |
| 67 | const IndexedDBDatabaseError& error); |
dmurph | 9d00e05d | 2016-12-01 23:00:34 | [diff] [blame] | 68 | |
Daniel Murphy | f5fc092 | 2019-09-13 07:42:03 | [diff] [blame] | 69 | leveldb::Status AbortAllTransactions(const IndexedDBDatabaseError& error); |
| 70 | |
| 71 | // Returns the last error that occurred, if there is any. |
| 72 | leveldb::Status AbortAllTransactionsAndIgnoreErrors( |
| 73 | const IndexedDBDatabaseError& error); |
dmurph | 9d00e05d | 2016-12-01 23:00:34 | [diff] [blame] | 74 | |
| 75 | IndexedDBTransaction* GetTransaction(int64_t id) const; |
| 76 | |
| 77 | base::WeakPtr<IndexedDBTransaction> AddTransactionForTesting( |
| 78 | std::unique_ptr<IndexedDBTransaction> transaction); |
| 79 | |
| 80 | // We ignore calls where the id doesn't exist to facilitate the AbortAll call. |
| 81 | // TODO(dmurph): Change that so this doesn't need to ignore unknown ids. |
| 82 | void RemoveTransaction(int64_t id); |
| 83 | |
Daniel Murphy | 7c75043 | 2019-08-20 01:39:47 | [diff] [blame] | 84 | const base::flat_map<int64_t, std::unique_ptr<IndexedDBTransaction>>& |
Daniel Murphy | a6d1c4f9 | 2019-01-16 18:41:10 | [diff] [blame] | 85 | transactions() const { |
| 86 | return transactions_; |
| 87 | } |
| 88 | |
[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 89 | private: |
Daniel Murphy | 4c0f9c1 | 2019-05-23 01:14:35 | [diff] [blame] | 90 | void ClearStateAfterClose(); |
| 91 | |
reillyg | ef35213 | 2016-10-24 19:36:37 | [diff] [blame] | 92 | const int32_t id_; |
palakj | 3fee5f7 | 2016-07-20 00:21:25 | [diff] [blame] | 93 | |
Daniel Murphy | 4c0f9c1 | 2019-05-23 01:14:35 | [diff] [blame] | 94 | // Keeps the factory for this origin alive. |
| 95 | IndexedDBOriginStateHandle origin_state_handle_; |
Daniel Murphy | 1df1bc4 | 2019-06-17 21:34:17 | [diff] [blame] | 96 | IndexedDBClassFactory* const indexed_db_class_factory_; |
Daniel Murphy | 4c0f9c1 | 2019-05-23 01:14:35 | [diff] [blame] | 97 | |
| 98 | base::WeakPtr<IndexedDBDatabase> database_; |
| 99 | base::RepeatingClosure on_version_change_ignored_; |
Daniel Murphy | 4c0f9c1 | 2019-05-23 01:14:35 | [diff] [blame] | 100 | base::OnceCallback<void(IndexedDBConnection*)> on_close_; |
[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 101 | |
dmurph | 9d00e05d | 2016-12-01 23:00:34 | [diff] [blame] | 102 | // The connection owns transactions created on this connection. |
Daniel Murphy | 7c75043 | 2019-08-20 01:39:47 | [diff] [blame] | 103 | // This is |flat_map| to preserve ordering, and because the vast majority of |
| 104 | // users have less than 200 transactions. |
| 105 | base::flat_map<int64_t, std::unique_ptr<IndexedDBTransaction>> transactions_; |
dmurph | 9d00e05d | 2016-12-01 23:00:34 | [diff] [blame] | 106 | |
[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 107 | // The callbacks_ member is cleared when the connection is closed. |
Adrienne Walker | 9165a15f | 2020-03-10 20:27:03 | [diff] [blame] | 108 | // May be nullptr in unit tests. |
[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 109 | scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_; |
Chase Phillips | 77bf9a95 | 2019-02-21 02:07:09 | [diff] [blame] | 110 | |
| 111 | SEQUENCE_CHECKER(sequence_checker_); |
| 112 | |
Daniel Murphy | 4c0f9c1 | 2019-05-23 01:14:35 | [diff] [blame] | 113 | base::WeakPtrFactory<IndexedDBConnection> weak_factory_{this}; |
cmumford | 5c0b3e0 | 2014-11-11 01:22:51 | [diff] [blame] | 114 | |
[email protected] | e8ca0f5 | 2014-06-07 08:46:34 | [diff] [blame] | 115 | DISALLOW_COPY_AND_ASSIGN(IndexedDBConnection); |
[email protected] | da6bce0c | 2013-07-03 04:33:37 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } // namespace content |
| 119 | |
| 120 | #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONNECTION_H_ |