1 // Copyright 2019 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_TRANSACTION_IMPL_H_
6 #define CONTENT_BROWSER_INDEXED_DB_TRANSACTION_IMPL_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/sequence_checker.h"
14 #include "base/strings/string16.h"
15 #include "content/browser/indexed_db/indexed_db_external_object.h"
16 #include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom.h"
17 #include "third_party/blink/public/mojom/quota/quota_types.mojom.h"
18 #include "url/origin.h"
19 
20 namespace base {
21 class SequencedTaskRunner;
22 }
23 
24 namespace content {
25 class IndexedDBContextImpl;
26 class IndexedDBDispatcherHost;
27 class IndexedDBTransaction;
28 
29 class TransactionImpl : public blink::mojom::IDBTransaction {
30  public:
31   explicit TransactionImpl(
32       base::WeakPtr<IndexedDBTransaction> transaction,
33       const url::Origin& origin,
34       base::WeakPtr<IndexedDBDispatcherHost> dispatcher_host,
35       scoped_refptr<base::SequencedTaskRunner> idb_runner);
36   ~TransactionImpl() override;
37 
38   // blink::mojom::IDBTransaction implementation
39   void CreateObjectStore(int64_t object_store_id,
40                          const base::string16& name,
41                          const blink::IndexedDBKeyPath& key_path,
42                          bool auto_increment) override;
43   void DeleteObjectStore(int64_t object_store_id) override;
44   void Put(int64_t object_store_id,
45            blink::mojom::IDBValuePtr value,
46            const blink::IndexedDBKey& key,
47            blink::mojom::IDBPutMode mode,
48            const std::vector<blink::IndexedDBIndexKeys>& index_keys,
49            blink::mojom::IDBTransaction::PutCallback callback) override;
50   void Commit(int64_t num_errors_handled) override;
51 
52   void OnGotUsageAndQuotaForCommit(blink::mojom::QuotaStatusCode status,
53                                    int64_t usage,
54                                    int64_t quota);
55 
56  private:
57   // Turns an IDBValue into a set of IndexedDBExternalObjects in
58   // |external_objects|.
59   void CreateExternalObjects(
60       blink::mojom::IDBValuePtr& value,
61       std::vector<IndexedDBExternalObject>* external_objects);
62 
63   base::WeakPtr<IndexedDBDispatcherHost> dispatcher_host_;
64   scoped_refptr<IndexedDBContextImpl> indexed_db_context_;
65   base::WeakPtr<IndexedDBTransaction> transaction_;
66   const url::Origin origin_;
67   scoped_refptr<base::SequencedTaskRunner> idb_runner_;
68 
69   SEQUENCE_CHECKER(sequence_checker_);
70 
71   base::WeakPtrFactory<TransactionImpl> weak_factory_{this};
72 
73   DISALLOW_COPY_AND_ASSIGN(TransactionImpl);
74 };
75 
76 }  // namespace content
77 
78 #endif  // CONTENT_BROWSER_INDEXED_DB_TRANSACTION_IMPL_H_
79