Shaka Player Embedded
cursor.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/cursor.h"
16 
17 #include "src/js/idb/database.h"
19 #include "src/js/idb/request.h"
21 #include "src/js/idb/transaction.h"
22 #include "src/memory/heap_tracer.h"
23 
24 namespace shaka {
25 namespace js {
26 namespace idb {
27 
29  : source(source), direction(dir) {}
30 // \cond Doxygen_Skip
31 IDBCursor::~IDBCursor() {}
32 // \endcond Doxygen_Skip
33 
34 void IDBCursor::Trace(memory::HeapTracer* tracer) const {
35  BackingObject::Trace(tracer);
36  tracer->Trace(&source);
37  tracer->Trace(&request);
38  tracer->Trace(&value);
39 }
40 
42  // 1. Let transaction be this cursor's transaction.
43  RefPtr<IDBTransaction> transaction = request->transaction;
44  // 2. If transaction’s state is not active, then throw a
45  // "TransactionInactiveError" DOMException.
46  if (!transaction->active)
48  // 3. If the cursor’s source or effective object store has been deleted, throw
49  // an "InvalidStateError" DOMException.
50  RefPtr<IDBDatabase> db = transaction->db;
51  if (!db->object_store_names->contains(source->store_name))
53  // 4. If this cursor’s got value flag is false, indicating that the cursor is
54  // being iterated or has iterated past its end, throw an
55  // "InvalidStateError" DOMException.
56  if (!got_value)
58  // 5. If key is given, then:
59  if (key.has_value())
61 
62  // 6. Set this cursor's got value flag to false.
63  got_value = false;
64  // 7. Let request be this cursor's request.
65  // 8. Set request’s processed flag to false.
66  // 9. Set request’s done flag to false.
67 
68  // 10. Run asynchronously execute a request with the cursor’s source as
69  // source, iterate a cursor as operation and request, using the current
70  // Realm as targetRealm, this cursor and key (if given).
71  // IMPL: Just add it back to the list to execute again.
72  request->count = 1;
73  transaction->AddRequest(request);
74  return {};
75 }
76 
78  // 1. Let transaction be this cursor's transaction.
79  RefPtr<IDBTransaction> transaction = request->transaction;
80  // 2. If transaction’s state is not active, then throw a
81  // "TransactionInactiveError" DOMException.
82  if (!transaction->active)
84  // 3. If transaction is a read-only transaction, throw a "ReadOnlyError"
85  // DOMException.
86  if (transaction->mode == IDBTransactionMode::READ_ONLY)
88  // 4. If the cursor’s source or effective object store has been deleted, throw
89  // an "InvalidStateError" DOMException.
90  RefPtr<IDBDatabase> db = transaction->db;
91  if (!db->object_store_names->contains(source->store_name))
93  // 5. If this cursor’s got value flag is false, indicating that the cursor is
94  // being iterated or has iterated past its end, throw an
95  // "InvalidStateError" DOMException.
96  if (!got_value)
98  DCHECK(key.has_value());
99  // 6. If this cursor’s key only flag is true, throw an "InvalidStateError"
100  // DOMException.
101  // 7. Return the result (an IDBRequest) of running asynchronously execute a
102  // request with this cursor as source and delete records from an object
103  // store as operation, using this cursor’s effective object store and
104  // effective key as store and key respectively.
105  return transaction->AddRequest(
106  new IDBDeleteRequest(source, transaction, key.value()));
107 }
108 
109 
111  AddReadOnlyProperty("direction", &IDBCursor::direction);
112  AddReadOnlyProperty("key", &IDBCursor::key);
113  AddReadOnlyProperty("primaryKey", &IDBCursor::key);
114  AddReadOnlyProperty("request", &IDBCursor::request);
115  AddReadOnlyProperty("source", &IDBCursor::source);
116  AddReadOnlyProperty("value", &IDBCursor::value);
117 
118  AddMemberFunction("continue", &IDBCursor::Continue);
119  AddMemberFunction("delete", &IDBCursor::Delete);
120 
121  NotImplemented("advance");
122  NotImplemented("update");
123 }
124 
125 } // namespace idb
126 } // namespace js
127 } // namespace shaka
ExceptionOr< void > Continue(optional< Any > key)
Definition: cursor.cc:41
void Trace(memory::HeapTracer *tracer) const override
void Trace(memory::HeapTracer *tracer) const override
Definition: cursor.cc:34
const IDBCursorDirection direction
Definition: cursor.h:52
IDBCursorDirection
Definition: cursor.h:35
const char * source
Definition: media_utils.cc:30
const Member< IDBObjectStore > source
Definition: cursor.h:50
const T & value() const &
Definition: optional.h:147
optional< IdbKeyType > key
Definition: cursor.h:56
void Trace(const Traceable *ptr)
Definition: heap_tracer.cc:43
Member< IDBIterateCursorRequest > request
Definition: cursor.h:51
ExceptionOr< RefPtr< IDBRequest > > Delete()
Definition: cursor.cc:77
IDBCursor(RefPtr< IDBObjectStore > source, IDBCursorDirection dir)
Definition: cursor.cc:28
static JsError DOMException(ExceptionCode code)
Definition: js_error.cc:115
bool has_value() const
Definition: optional.h:143