1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_INDEXEDDB_IDB_INDEX_H_
27 #define THIRD_PARTY_BLINK_RENDERER_MODULES_INDEXEDDB_IDB_INDEX_H_
28 
29 #include "third_party/blink/public/common/indexeddb/web_idb_types.h"
30 #include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom-blink-forward.h"
31 #include "third_party/blink/renderer/modules/indexeddb/idb_cursor.h"
32 #include "third_party/blink/renderer/modules/indexeddb/idb_key_path.h"
33 #include "third_party/blink/renderer/modules/indexeddb/idb_key_range.h"
34 #include "third_party/blink/renderer/modules/indexeddb/idb_metadata.h"
35 #include "third_party/blink/renderer/modules/indexeddb/idb_request.h"
36 #include "third_party/blink/renderer/modules/indexeddb/web_idb_cursor.h"
37 #include "third_party/blink/renderer/modules/indexeddb/web_idb_database.h"
38 #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
39 #include "third_party/blink/renderer/platform/wtf/forward.h"
40 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
41 
42 namespace blink {
43 
44 class ExceptionState;
45 class IDBObjectStore;
46 
47 class IDBIndex final : public ScriptWrappable {
48   DEFINE_WRAPPERTYPEINFO();
49 
50  public:
51   IDBIndex(scoped_refptr<IDBIndexMetadata>, IDBObjectStore*, IDBTransaction*);
52   ~IDBIndex() override;
53 
54   void Trace(Visitor*) const override;
55 
56   // Implement the IDL
name()57   const String& name() const { return Metadata().name; }
58   void setName(const String& name, ExceptionState&);
objectStore()59   IDBObjectStore* objectStore() const { return object_store_.Get(); }
60   ScriptValue keyPath(ScriptState*) const;
61 
62   // Per spec prose, keyPath attribute should return the same object each time
63   // (if it is not just a primitive type). The IDL cannot use [SameObject]
64   // because the key path may not be an 'object'. So use [CachedAttribute],
65   // but never dirty the cache.
IsKeyPathDirty()66   bool IsKeyPathDirty() const { return false; }
67 
unique()68   bool unique() const { return Metadata().unique; }
multiEntry()69   bool multiEntry() const { return Metadata().multi_entry; }
70 
71   IDBRequest* openCursor(ScriptState*,
72                          const ScriptValue& key,
73                          const String& direction,
74                          ExceptionState&);
75   IDBRequest* openKeyCursor(ScriptState*,
76                             const ScriptValue& range,
77                             const String& direction,
78                             ExceptionState&);
79   IDBRequest* count(ScriptState*, const ScriptValue& range, ExceptionState&);
80   IDBRequest* get(ScriptState*, const ScriptValue& key, ExceptionState&);
81   IDBRequest* getAll(ScriptState*, const ScriptValue& range, ExceptionState&);
82   IDBRequest* getAll(ScriptState*,
83                      const ScriptValue& range,
84                      uint32_t max_count,
85                      ExceptionState&);
86   IDBRequest* getKey(ScriptState*, const ScriptValue& key, ExceptionState&);
87   IDBRequest* getAllKeys(ScriptState*,
88                          const ScriptValue& range,
89                          ExceptionState&);
90   IDBRequest* getAllKeys(ScriptState*,
91                          const ScriptValue& range,
92                          uint32_t max_count,
93                          ExceptionState&);
94 
MarkDeleted()95   void MarkDeleted() {
96     DCHECK(transaction_->IsVersionChange())
97         << "Index deleted outside versionchange transaction.";
98     deleted_ = true;
99   }
IsDeleted()100   bool IsDeleted() const { return deleted_; }
Id()101   int64_t Id() const { return Metadata().id; }
102 
103   // True if this index was created in its associated transaction.
104   // Only valid if the index's associated transaction is a versionchange.
IsNewlyCreated(const IDBObjectStoreMetadata & old_object_store_metadata)105   bool IsNewlyCreated(
106       const IDBObjectStoreMetadata& old_object_store_metadata) const {
107     DCHECK(transaction_->IsVersionChange());
108 
109     // Index IDs are allocated sequentially, so we can tell if an index was
110     // created in this transaction by comparing its ID against the object
111     // store's maximum index ID at the time when the transaction was started.
112     return Id() > old_object_store_metadata.max_index_id;
113   }
114 
115   void RevertMetadata(scoped_refptr<IDBIndexMetadata> old_metadata);
116 
117   // Used internally and by InspectorIndexedDBAgent:
118   IDBRequest* openCursor(
119       ScriptState*,
120       IDBKeyRange*,
121       mojom::IDBCursorDirection,
122       IDBRequest::AsyncTraceState = IDBRequest::AsyncTraceState());
123 
124   WebIDBDatabase* BackendDB() const;
125 
126  private:
Metadata()127   const IDBIndexMetadata& Metadata() const { return *metadata_; }
128 
129   IDBRequest* GetInternal(ScriptState*,
130                           const ScriptValue& key,
131                           ExceptionState&,
132                           bool key_only,
133                           IDBRequest::AsyncTraceState metrics);
134   IDBRequest* GetAllInternal(ScriptState*,
135                              const ScriptValue& range,
136                              uint32_t max_count,
137                              ExceptionState&,
138                              bool key_only,
139                              IDBRequest::AsyncTraceState metrics);
140 
141   scoped_refptr<IDBIndexMetadata> metadata_;
142   Member<IDBObjectStore> object_store_;
143   Member<IDBTransaction> transaction_;
144   bool deleted_ = false;
145 };
146 
147 }  // namespace blink
148 
149 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_INDEXEDDB_IDB_INDEX_H_
150