summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/indexeddb/idb_object_store.h
blob: 79819338c464913b6c1aaa7afdfc3e8c1a08ddea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * Copyright (C) 2010 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_INDEXEDDB_IDB_OBJECT_STORE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_INDEXEDDB_IDB_OBJECT_STORE_H_

#include "base/memory/scoped_refptr.h"
#include "third_party/blink/public/common/indexeddb/web_idb_types.h"
#include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_idb_index_parameters.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_cursor.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_index.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_key.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_key_range.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_metadata.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_request.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_transaction.h"
#include "third_party/blink/renderer/modules/indexeddb/web_idb_cursor.h"
#include "third_party/blink/renderer/modules/indexeddb/web_idb_database.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

class DOMStringList;
class ExceptionState;

class MODULES_EXPORT IDBObjectStore final : public ScriptWrappable {
  DEFINE_WRAPPERTYPEINFO();

 public:
  IDBObjectStore(scoped_refptr<IDBObjectStoreMetadata>, IDBTransaction*);
  ~IDBObjectStore() override = default;

  void Trace(Visitor*) override;

  const IDBObjectStoreMetadata& Metadata() const { return *metadata_; }
  const IDBKeyPath& IdbKeyPath() const { return Metadata().key_path; }

  // Per spec prose, keyPath attribute should return the same object each time
  // (if it is not just a primitive type). The IDL cannot use [SameObject]
  // because the key path may not be an 'object'. So use [CachedAttribute],
  // but never dirty the cache.
  bool IsKeyPathDirty() const { return false; }

  // Implement the IDBObjectStore IDL
  int64_t Id() const { return Metadata().id; }
  const String& name() const { return Metadata().name; }
  void setName(const String& name, ExceptionState&);
  ScriptValue keyPath(ScriptState*) const;
  DOMStringList* indexNames() const;
  IDBTransaction* transaction() const { return transaction_.Get(); }
  bool autoIncrement() const { return Metadata().auto_increment; }

  IDBRequest* openCursor(ScriptState*,
                         const ScriptValue& range,
                         const String& direction,
                         ExceptionState&);
  IDBRequest* openKeyCursor(ScriptState*,
                            const ScriptValue& range,
                            const String& direction,
                            ExceptionState&);
  IDBRequest* get(ScriptState*, const ScriptValue& key, ExceptionState&);
  IDBRequest* getKey(ScriptState*, const ScriptValue& key, ExceptionState&);
  IDBRequest* getAll(ScriptState*,
                     const ScriptValue& range,
                     uint32_t max_count,
                     ExceptionState&);
  IDBRequest* getAll(ScriptState*, const ScriptValue& range, ExceptionState&);
  IDBRequest* getAllKeys(ScriptState*,
                         const ScriptValue& range,
                         uint32_t max_count,
                         ExceptionState&);
  IDBRequest* getAllKeys(ScriptState*,
                         const ScriptValue& range,
                         ExceptionState&);
  IDBRequest* add(ScriptState*, const ScriptValue& value, ExceptionState&);
  IDBRequest* add(ScriptState*,
                  const ScriptValue& value,
                  const ScriptValue& key,
                  ExceptionState&);
  IDBRequest* put(ScriptState*, const ScriptValue& value, ExceptionState&);
  IDBRequest* put(ScriptState*,
                  const ScriptValue& value,
                  const ScriptValue& key,
                  ExceptionState&);
  IDBRequest* Delete(ScriptState*, const ScriptValue& key, ExceptionState&);
  IDBRequest* clear(ScriptState*, ExceptionState&);

  IDBIndex* createIndex(ScriptState* script_state,
                        const String& name,
                        const StringOrStringSequence& key_path,
                        const IDBIndexParameters* options,
                        ExceptionState& exception_state) {
    return createIndex(script_state, name, IDBKeyPath(key_path), options,
                       exception_state);
  }
  IDBIndex* index(const String& name, ExceptionState&);
  void deleteIndex(const String& name, ExceptionState&);

  IDBRequest* count(ScriptState*, const ScriptValue& range, ExceptionState&);

  // Exposed for the use of IDBCursor::update().
  IDBRequest* DoPut(ScriptState*,
                    mojom::IDBPutMode,
                    const IDBRequest::Source&,
                    const ScriptValue&,
                    const IDBKey*,
                    ExceptionState&);

  // Used internally and by InspectorIndexedDBAgent:
  IDBRequest* openCursor(
      ScriptState*,
      IDBKeyRange*,
      mojom::IDBCursorDirection,
      mojom::IDBTaskType = mojom::IDBTaskType::Normal,
      IDBRequest::AsyncTraceState = IDBRequest::AsyncTraceState());
  IDBRequest* deleteFunction(
      ScriptState*,
      IDBKeyRange*,
      IDBRequest::AsyncTraceState = IDBRequest::AsyncTraceState());
  IDBRequest* getKeyGeneratorCurrentNumber(
      ScriptState*,
      IDBRequest::AsyncTraceState = IDBRequest::AsyncTraceState());

  void MarkDeleted();
  bool IsDeleted() const { return deleted_; }

  // True if this object store was created in its associated transaction.
  // Only valid if the store's associated transaction is a versionchange.
  bool IsNewlyCreated() const {
    DCHECK(transaction_->IsVersionChange());
    // Object store IDs are allocated sequentially, so we can tell if an object
    // store was created in this transaction by comparing its ID against the
    // database's maximum object store ID at the time when the transaction was
    // started.
    return Id() > transaction_->OldMaxObjectStoreId();
  }

  // Clears the cache used to implement the index() method.
  //
  // This should be called when the store's transaction clears its reference
  // to this IDBObjectStore instance, so the store can clear its references to
  // IDBIndex instances. This way, Oilpan can garbage-collect the instances
  // that are not referenced in JavaScript.
  //
  // For most stores, the condition above is met when the transaction
  // finishes. The exception is stores that are created and deleted in the
  // same transaction. Those stores will remain marked for deletion even if
  // the transaction aborts, so the transaction can forget about them (and
  // clear their index caches) right when they are deleted.
  void ClearIndexCache();

  // Sets the object store's metadata to a previous version.
  //
  // The reverting process includes reverting the metadata for the IDBIndex
  // instances that are still tracked by the store. It does not revert the
  // IDBIndex metadata for indexes that were deleted in this transaction.
  //
  // Used when a versionchange transaction is aborted.
  void RevertMetadata(scoped_refptr<IDBObjectStoreMetadata> previous_metadata);
  // This relies on the changes made by RevertMetadata().
  void RevertDeletedIndexMetadata(IDBIndex& deleted_index);

  // Used by IDBIndex::setName:
  bool ContainsIndex(const String& name) const {
    return FindIndexId(name) != IDBIndexMetadata::kInvalidId;
  }
  void RenameIndex(int64_t index_id, const String& new_name);

  WebIDBDatabase* BackendDB() const;

 private:
  using IDBIndexMap = HeapHashMap<String, Member<IDBIndex>>;

  IDBIndex* createIndex(ScriptState*,
                        const String& name,
                        const IDBKeyPath&,
                        const IDBIndexParameters*,
                        ExceptionState&);
  IDBRequest* DoPut(ScriptState*,
                    mojom::IDBPutMode,
                    const ScriptValue&,
                    const ScriptValue& key_value,
                    ExceptionState&);

  int64_t FindIndexId(const String& name) const;

  // The IDBObjectStoreMetadata is shared with the object store map in the
  // database's metadata.
  scoped_refptr<IDBObjectStoreMetadata> metadata_;
  Member<IDBTransaction> transaction_;
  bool deleted_ = false;

  // Caches the IDBIndex instances returned by the index() method.
  //
  // The spec requires that an object store's index() returns the same
  // IDBIndex instance for a specific index, so this cache is necessary
  // for correctness.
  //
  // index() throws for completed/aborted transactions, so this is not used
  // after a transaction is finished, and can be cleared.
  IDBIndexMap index_map_;

#if DCHECK_IS_ON()
  bool clear_index_cache_called_ = false;
#endif  // DCHECK_IS_ON()
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_INDEXEDDB_IDB_OBJECT_STORE_H_