Shaka Player Embedded
object_store.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 
16 
17 #include <utility>
18 
19 #include "src/js/idb/database.h"
20 #include "src/js/idb/database.pb.h"
21 #include "src/js/idb/idb_utils.h"
22 #include "src/js/idb/request.h"
24 #include "src/js/idb/transaction.h"
25 #include "src/memory/heap_tracer.h"
26 
27 namespace shaka {
28 namespace js {
29 namespace idb {
30 
32  const std::string& name)
33  : store_name(name), transaction(transaction) {}
34 // \cond Doxygen_Skip
35 IDBObjectStore::~IDBObjectStore() {}
36 // \endcond Doxygen_Skip
37 
39  BackingObject::Trace(tracer);
40  tracer->Trace(&transaction);
41 }
42 
45  return AddOrPut(value, key, /* overwrite */ false);
46 }
47 
50  return AddOrPut(value, key, /* overwrite */ false);
51 }
52 
54  Any value, optional<IdbKeyType> key, bool no_overwrite) {
55  // 1-5
56  RETURN_IF_ERROR(CheckState(/* need_write */ true));
57  // 6. If store uses in-line keys and key was given, throw a "DataError"
58  // DOMException.
59  if (key_path.has_value() && key.has_value())
61  // 7. If store uses out-of-line keys and has no key generator and key was not
62  // given, throw a "DataError" DOMException.
63  if (!key_path.has_value() && !auto_increment && !key.has_value())
65  // 8. If key was given, then:
66  // NA, already converted.
67 
68  // 9. Let targetRealm be a user-agent defined Realm.
69  // 10. Let clone be a clone of value in targetRealm. Rethrow any exceptions.
70  proto::Value clone;
71  RETURN_IF_ERROR(StoreInProto(value, &clone));
72 
73  // 11. If store uses in-line keys, then:
74  DCHECK(!key_path.has_value());
75 
76  // 12. Return the result (an IDBRequest) of running asynchronously execute a
77  // request with handle as source and store a record into an object store
78  // as operation, using store, the clone as value, key, and no-overwrite
79  // flag.
80  return transaction->AddRequest(new IDBStoreRequest(
81  this, transaction, std::move(clone), key, no_overwrite));
82 }
83 
85  // 1-5
86  RETURN_IF_ERROR(CheckState(/* need_write */ true));
87  // 6. Let range be the result of running convert a value to a key range with
88  // query and null disallowed flag true. Rethrow any exceptions.
89  // NA, already converted.
90 
91  // 7. Return the result (an IDBRequest) of running asynchronously execute a
92  // request with this object store handle as source and delete records from
93  // an object store as operation, using store and range.
94  return transaction->AddRequest(new IDBDeleteRequest(this, transaction, key));
95 }
96 
98  // 1-4
99  RETURN_IF_ERROR(CheckState(/* need_write */ false));
100  // 5. Let range be the result of running convert a value to a key range with
101  // query and null disallowed flag true. Rethrow any exceptions.
102  // NA, already converted.
103 
104  // 6. Return the result (an IDBRequest) of running asynchronously execute a
105  // request with this object store handle as source and retrieve a value
106  // from an object store as operation, using the current Realm as
107  // targetRealm, store and range.
108  return transaction->AddRequest(new IDBGetRequest(this, transaction, key));
109 }
110 
113  if (range)
115  // 1-4
116  RETURN_IF_ERROR(CheckState(/* need_write */ false));
117  // 5. Let range be the result of running convert a value to a key range with
118  // query and null disallowed flag true. Rethrow any exceptions.
119  // NA, already converted.
120  // 6. Let cursor be a new cursor with its transaction set to transaction,
121  // undefined position, direction set to direction, got value flag set to
122  // false, undefined key and value, source set to store, range set to range,
123  // and key only flag set to false.
124  const auto dir = direction.value_or(IDBCursorDirection::NEXT);
125  RefPtr<IDBCursor> cursor = new IDBCursor(this, dir);
126  // 7. Let request be the result of running asynchronously execute a request
127  // with this object store handle as source and iterate a cursor as
128  // operation, using the current Realm as targetRealm, and cursor.
130  new IDBIterateCursorRequest(this, transaction, cursor, 1);
131  // 8. Set cursor’s request to request.
132  cursor->request = request;
133  // 9. Return request.
134  return transaction->AddRequest(request);
135 }
136 
137 ExceptionOr<void> IDBObjectStore::CheckState(bool need_write) const {
138  // 1. Let transaction be this object store handle's transaction.
139  // 2. Let store be this object store handle's object store.
140  // 3. If store has been deleted, throw an "InvalidStateError" DOMException.
141  if (!transaction->db->object_store_names->contains(store_name))
143  // 4. If transaction’s state is not active, then throw a
144  // "TransactionInactiveError" DOMException.
145  if (!transaction->active)
147  // 5. If transaction is a read-only transaction, throw a "ReadOnlyError"
148  // DOMException.
149  if (need_write && transaction->mode == IDBTransactionMode::READ_ONLY)
151  return {};
152 }
153 
154 
156  AddReadOnlyProperty("autoIncrement", &IDBObjectStore::auto_increment);
157  AddReadOnlyProperty("keyPath", &IDBObjectStore::key_path);
158  AddReadOnlyProperty("name", &IDBObjectStore::store_name);
159  AddReadOnlyProperty("transaction", &IDBObjectStore::transaction);
160 
161  AddMemberFunction("add", &IDBObjectStore::Add);
162  AddMemberFunction("delete", &IDBObjectStore::Delete);
163  AddMemberFunction("get", &IDBObjectStore::Get);
164  AddMemberFunction("openCursor", &IDBObjectStore::OpenCursor);
165  AddMemberFunction("put", &IDBObjectStore::Put);
166 
167  NotImplemented("clear");
168  NotImplemented("count");
169  NotImplemented("createIndex");
170  NotImplemented("deleteIndex");
171  NotImplemented("index");
172  NotImplemented("indexNames");
173 }
174 
175 } // namespace idb
176 } // namespace js
177 } // namespace shaka
Definition: any.h:31
ExceptionOr< void > StoreInProto(Any input, proto::Value *result)
Definition: idb_utils.cc:233
const Member< IDBTransaction > transaction
Definition: object_store.h:48
void Trace(memory::HeapTracer *tracer) const override
ExceptionOr< RefPtr< IDBRequest > > Put(Any value, optional< IdbKeyType > key)
Definition: object_store.cc:48
ExceptionOr< RefPtr< IDBRequest > > Get(IdbKeyType key)
Definition: object_store.cc:97
const char * name
const std::string store_name
Definition: object_store.h:47
IDBObjectStore(RefPtr< IDBTransaction > transaction, const std::string &name)
Definition: object_store.cc:31
int64_t IdbKeyType
Definition: idb_utils.h:29
ExceptionOr< RefPtr< IDBRequest > > AddOrPut(Any value, optional< IdbKeyType > key, bool no_overwrite)
Definition: object_store.cc:53
void Trace(const Traceable *ptr)
Definition: heap_tracer.cc:43
#define RETURN_IF_ERROR(code)
Definition: sqlite.cc:31
const optional< std::string > key_path
Definition: object_store.h:46
ExceptionOr< RefPtr< IDBRequest > > Delete(IdbKeyType key)
Definition: object_store.cc:84
T value_or(U &&default_value) const &
Definition: optional.h:165
ExceptionOr< RefPtr< IDBRequest > > Add(Any value, optional< IdbKeyType > key)
Definition: object_store.cc:43
ExceptionOr< RefPtr< IDBRequest > > OpenCursor(optional< IdbKeyType > range, optional< IDBCursorDirection > direction)
void Trace(memory::HeapTracer *tracer) const override
Definition: object_store.cc:38
static JsError DOMException(ExceptionCode code)
Definition: js_error.cc:115
bool has_value() const
Definition: optional.h:143