blob: cde14f4a6fe5fd3750df3d773825bcc98c697c03 [file] [log] [blame]
[email protected]da6bce0c2013-07-03 04:33:371// 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
jsbell4238c662016-08-18 00:23:028#include <memory>
dmurph9d00e05d2016-12-01 23:00:349#include <set>
jsbell4238c662016-08-18 00:23:0210#include <vector>
11
Daniel Murphy7c750432019-08-20 01:39:4712#include "base/containers/flat_map.h"
avib7348942015-12-25 20:57:1013#include "base/macros.h"
[email protected]da6bce0c2013-07-03 04:33:3714#include "base/memory/ref_counted.h"
cmumford5c0b3e02014-11-11 01:22:5115#include "base/memory/weak_ptr.h"
[email protected]da6bce0c2013-07-03 04:33:3716#include "content/browser/indexed_db/indexed_db_database.h"
Daniel Murphy4c0f9c12019-05-23 01:14:3517#include "content/browser/indexed_db/indexed_db_origin_state_handle.h"
Henrique Ferreiroda0a55c2019-11-12 14:06:0418#include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom-forward.h"
[email protected]da6bce0c2013-07-03 04:33:3719
20namespace content {
dmurph9d00e05d2016-12-01 23:00:3421class IndexedDBDatabaseCallbacks;
22class IndexedDBDatabaseError;
dmurph9d00e05d2016-12-01 23:00:3423class IndexedDBTransaction;
Daniel Murphy4c0f9c12019-05-23 01:14:3524class IndexedDBOriginStateHandle;
[email protected]da6bce0c2013-07-03 04:33:3725
26class CONTENT_EXPORT IndexedDBConnection {
27 public:
Adrienne Walker80d95f02020-02-07 22:37:0428 IndexedDBConnection(IndexedDBOriginStateHandle origin_state_handle,
Daniel Murphy1df1bc42019-06-17 21:34:1729 IndexedDBClassFactory* indexed_db_class_factory,
Daniel Murphy4c0f9c12019-05-23 01:14:3530 base::WeakPtr<IndexedDBDatabase> database,
31 base::RepeatingClosure on_version_change_ignored,
32 base::OnceCallback<void(IndexedDBConnection*)> on_close,
[email protected]da6bce0c2013-07-03 04:33:3733 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks);
34 virtual ~IndexedDBConnection();
35
Daniel Murphyf5fc0922019-09-13 07:42:0336 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 Murphy4c0f9c12019-05-23 01:14:3547 bool IsConnected();
[email protected]da6bce0c2013-07-03 04:33:3748
[email protected]75a854a2014-07-07 15:57:3649 void VersionChangeIgnored();
50
palakj3fee5f72016-07-20 00:21:2551 int32_t id() const { return id_; }
palakj3fee5f72016-07-20 00:21:2552
Daniel Murphy4c0f9c12019-05-23 01:14:3553 base::WeakPtr<IndexedDBDatabase> database() const { return database_; }
jsbell1869ef42014-10-03 17:34:3854 IndexedDBDatabaseCallbacks* callbacks() const { return callbacks_.get(); }
palakj05f35ea2016-07-08 00:59:2155 base::WeakPtr<IndexedDBConnection> GetWeakPtr() {
56 return weak_factory_.GetWeakPtr();
57 }
[email protected]da6bce0c2013-07-03 04:33:3758
dmurph9d00e05d2016-12-01 23:00:3459 // Creates a transaction for this connection.
60 IndexedDBTransaction* CreateTransaction(
61 int64_t id,
62 const std::set<int64_t>& scope,
Chase Phillipsb2851f322018-11-16 00:27:4863 blink::mojom::IDBTransactionMode mode,
dmurph9d00e05d2016-12-01 23:00:3464 IndexedDBBackingStore::Transaction* backing_store_transaction);
65
Daniel Murphyf5fc0922019-09-13 07:42:0366 void AbortTransactionAndTearDownOnError(IndexedDBTransaction* transaction,
67 const IndexedDBDatabaseError& error);
dmurph9d00e05d2016-12-01 23:00:3468
Daniel Murphyf5fc0922019-09-13 07:42:0369 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);
dmurph9d00e05d2016-12-01 23:00:3474
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 Murphy7c750432019-08-20 01:39:4784 const base::flat_map<int64_t, std::unique_ptr<IndexedDBTransaction>>&
Daniel Murphya6d1c4f92019-01-16 18:41:1085 transactions() const {
86 return transactions_;
87 }
88
[email protected]da6bce0c2013-07-03 04:33:3789 private:
Daniel Murphy4c0f9c12019-05-23 01:14:3590 void ClearStateAfterClose();
91
reillygef352132016-10-24 19:36:3792 const int32_t id_;
palakj3fee5f72016-07-20 00:21:2593
Daniel Murphy4c0f9c12019-05-23 01:14:3594 // Keeps the factory for this origin alive.
95 IndexedDBOriginStateHandle origin_state_handle_;
Daniel Murphy1df1bc42019-06-17 21:34:1796 IndexedDBClassFactory* const indexed_db_class_factory_;
Daniel Murphy4c0f9c12019-05-23 01:14:3597
98 base::WeakPtr<IndexedDBDatabase> database_;
99 base::RepeatingClosure on_version_change_ignored_;
Daniel Murphy4c0f9c12019-05-23 01:14:35100 base::OnceCallback<void(IndexedDBConnection*)> on_close_;
[email protected]da6bce0c2013-07-03 04:33:37101
dmurph9d00e05d2016-12-01 23:00:34102 // The connection owns transactions created on this connection.
Daniel Murphy7c750432019-08-20 01:39:47103 // 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_;
dmurph9d00e05d2016-12-01 23:00:34106
[email protected]da6bce0c2013-07-03 04:33:37107 // The callbacks_ member is cleared when the connection is closed.
Adrienne Walker9165a15f2020-03-10 20:27:03108 // May be nullptr in unit tests.
[email protected]da6bce0c2013-07-03 04:33:37109 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_;
Chase Phillips77bf9a952019-02-21 02:07:09110
111 SEQUENCE_CHECKER(sequence_checker_);
112
Daniel Murphy4c0f9c12019-05-23 01:14:35113 base::WeakPtrFactory<IndexedDBConnection> weak_factory_{this};
cmumford5c0b3e02014-11-11 01:22:51114
[email protected]e8ca0f52014-06-07 08:46:34115 DISALLOW_COPY_AND_ASSIGN(IndexedDBConnection);
[email protected]da6bce0c2013-07-03 04:33:37116};
117
118} // namespace content
119
120#endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONNECTION_H_