Shaka Player Embedded
transaction.cc
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/js/idb/transaction.h"
16 
19 #include "src/js/idb/database.h"
21 #include "src/js/idb/request.h"
22 #include "src/js/js_error.h"
23 #include "src/memory/heap_tracer.h"
24 
25 namespace shaka {
26 namespace js {
27 namespace idb {
28 
30  const std::vector<std::string>& scope)
31  : db(db),
32  error(nullptr),
33  mode(mode),
34  aborted(false),
35  active(true),
36  done(false),
37  sqlite_transaction(nullptr) {
38  AddListenerField(EventType::Abort, &on_abort);
39  AddListenerField(EventType::Complete, &on_complete);
40  AddListenerField(EventType::Error, &on_error);
41 
42  for (const std::string& name : scope) {
43  scope_.emplace(name, new IDBObjectStore(this, name));
44  }
45 }
46 
47 // \cond Doxygen_Skip
48 IDBTransaction::~IDBTransaction() {}
49 // \endcond Doxygen_Skip
50 
53  tracer->Trace(&db);
54  tracer->Trace(&error);
55  tracer->Trace(&requests_);
56  for (const auto& pair : scope_)
57  tracer->Trace(&pair.second);
58 }
59 
61  const std::string name) {
62  // 1. If transaction’s state is finished, then throw an "InvalidStateError"
63  // DOMException.
64  if (done)
66  // 2. Let store be the object store named name in this transaction's scope, or
67  // throw a "NotFoundError" DOMException if none.
68  if (scope_.count(name) == 0)
70 
71  // 3. Return an object store handle associated with store and this
72  // transaction.
73  return scope_.at(name);
74 }
75 
77  // 1. If this transaction's state is committing or finished, then throw an
78  // "InvalidStateError" DOMException.
79  if (done)
81  aborted = true;
82  active = false;
83  return {};
84 }
85 
87  requests_.emplace_back(request);
88  return request;
89 }
90 
92  DCHECK(JsManagerImpl::Instance()->MainThread()->BelongsToCurrentThread());
93  DCHECK(!done);
94 
95  SqliteTransaction transaction;
96  DatabaseStatus status = connection->BeginTransaction(&transaction);
97  if (status != DatabaseStatus::Success) {
99  aborted = true;
100  active = false;
101  RaiseEvent<events::Event>(EventType::Error);
102  }
103  DoCommit(&transaction);
104 }
105 
107  sqlite_transaction = transaction;
108 
109  for (auto it = requests_.begin(); it != requests_.end(); it++) {
110  // These methods will call synchronously into JavaScript, which can add more
111  // requests at the end.
112  if (aborted)
113  (*it)->OnAbort();
114  else
115  (*it)->PerformOperation(transaction);
116  DCHECK((*it)->ready_state == IDBRequestReadyState::DONE);
117  }
118 
119  sqlite_transaction = nullptr;
120  active = false;
121  done = true;
122 
123  const DatabaseStatus status =
124  aborted ? transaction->Rollback() : transaction->Commit();
125  if (status != DatabaseStatus::Success) {
127  aborted = true;
128  RaiseEvent<events::Event>(EventType::Error);
129  }
130 
131  if (aborted)
132  RaiseEvent<events::Event>(EventType::Abort);
133  else
134  RaiseEvent<events::Event>(EventType::Complete);
135 }
136 
137 void IDBTransaction::AddObjectStore(const std::string& name) {
138  DCHECK_EQ(scope_.count(name), 0u);
139  scope_[name] = new IDBObjectStore(this, name);
140 }
141 
142 void IDBTransaction::DeleteObjectStore(const std::string& name) {
143  DCHECK_EQ(scope_.count(name), 1u);
144  scope_.erase(name);
145 }
146 
147 
149  AddReadOnlyProperty("mode", &IDBTransaction::mode);
150  AddReadOnlyProperty("db", &IDBTransaction::db);
151  AddReadOnlyProperty("error", &IDBTransaction::error);
152 
153  AddListenerField(EventType::Abort, &IDBTransaction::on_abort);
154  AddListenerField(EventType::Complete, &IDBTransaction::on_complete);
155  AddListenerField(EventType::Error, &IDBTransaction::on_error);
156 
157  AddMemberFunction("objectStore", &IDBTransaction::ObjectStore);
158  AddMemberFunction("abort", &IDBTransaction::Abort);
159 
160  NotImplemented("commit");
161 }
162 
163 } // namespace idb
164 } // namespace js
165 } // namespace shaka
ExceptionOr< void > Abort()
Definition: transaction.cc:76
void Trace(memory::HeapTracer *tracer) const override
Definition: transaction.cc:51
SqliteTransaction * sqlite_transaction
Definition: transaction.h:88
void DeleteObjectStore(const std::string &name)
Definition: transaction.cc:142
void Trace(const Traceable *ptr)
Definition: heap_tracer.cc:43
Member< dom::DOMException > error
Definition: transaction.h:80
void AddObjectStore(const std::string &name)
Definition: transaction.cc:137
void DoCommit(SqliteConnection *connection)
Definition: transaction.cc:91
ExceptionOr< RefPtr< IDBObjectStore > > ObjectStore(const std::string name)
Definition: transaction.cc:60
void Trace(memory::HeapTracer *tracer) const override
Definition: event_target.cc:31
RefPtr< IDBRequest > AddRequest(RefPtr< IDBRequest > request)
Definition: transaction.cc:86
IDBTransaction(RefPtr< IDBDatabase > db, IDBTransactionMode mode, const std::vector< std::string > &scope)
Definition: transaction.cc:29
static JsError DOMException(ExceptionCode code)
Definition: js_error.cc:115
const Member< IDBDatabase > db
Definition: transaction.h:79
const IDBTransactionMode mode
Definition: transaction.h:81
void AddListenerField(EventType type, Listener *on_field)
Definition: event_target.h:138
DatabaseStatus BeginTransaction(SqliteTransaction *transaction)
Definition: sqlite.cc:495
std::string name() const