From b9868b89317a30208b3d8e1e6da0792fa863fe4c Mon Sep 17 00:00:00 2001 From: Xunran Ding Date: Fri, 12 Jan 2018 17:45:58 +0000 Subject: [PATCH] IndexedDB: Reduce IDBAny usage in IDBCursor Use IDL-generated union type 'IDBObjectStoreOrIDBIndex' instead of IDBAny. This aligns better with the spec. Bug: 798819 Change-Id: I2296cf1cb99dc4ed954e3d79dafa8f70d4796851 Reviewed-on: https://chromium-review.googlesource.com/861682 Reviewed-by: Victor Costan Reviewed-by: Kentaro Hara Reviewed-by: Jeremy Roman Commit-Queue: Victor Costan Cr-Commit-Position: refs/heads/master@{#528988} --- AUTHORS | 1 + .../Source/bindings/modules/v8/generated.gni | 2 ++ .../Source/modules/indexeddb/IDBCursor.cpp | 25 +++++++++---------- .../Source/modules/indexeddb/IDBCursor.h | 12 +++++---- .../Source/modules/indexeddb/IDBCursor.idl | 2 +- .../modules/indexeddb/IDBCursorWithValue.cpp | 4 +-- .../modules/indexeddb/IDBCursorWithValue.h | 5 ++-- .../Source/modules/indexeddb/IDBRequest.cpp | 19 +++++++++++--- 8 files changed, 42 insertions(+), 28 deletions(-) diff --git a/AUTHORS b/AUTHORS index 5a24344675a5cf..c178a9601db624 100644 --- a/AUTHORS +++ b/AUTHORS @@ -863,6 +863,7 @@ Xu Xing Xuefei Ren Xueqing Huang Xun Sun +Xunran Ding Xunran Ding Yael Aharon Yair Yogev diff --git a/third_party/WebKit/Source/bindings/modules/v8/generated.gni b/third_party/WebKit/Source/bindings/modules/v8/generated.gni index 012519664aab81..2366d399644d61 100644 --- a/third_party/WebKit/Source/bindings/modules/v8/generated.gni +++ b/third_party/WebKit/Source/bindings/modules/v8/generated.gni @@ -44,6 +44,8 @@ bindings_modules_generated_union_type_files = [ "$bindings_modules_v8_output_dir/float32_array_or_float64_array_or_dom_matrix.h", "$bindings_modules_v8_output_dir/html_canvas_element_or_offscreen_canvas.cc", "$bindings_modules_v8_output_dir/html_canvas_element_or_offscreen_canvas.h", + "$bindings_modules_v8_output_dir/idb_object_store_or_idb_index.cc", + "$bindings_modules_v8_output_dir/idb_object_store_or_idb_index.h", "$bindings_modules_v8_output_dir/long_or_constrain_long_range.cc", "$bindings_modules_v8_output_dir/long_or_constrain_long_range.h", "$bindings_modules_v8_output_dir/offscreen_rendering_context.cc", diff --git a/third_party/WebKit/Source/modules/indexeddb/IDBCursor.cpp b/third_party/WebKit/Source/modules/indexeddb/IDBCursor.cpp index b1eb16a02ade6a..59c18b64c12c0d 100644 --- a/third_party/WebKit/Source/modules/indexeddb/IDBCursor.cpp +++ b/third_party/WebKit/Source/modules/indexeddb/IDBCursor.cpp @@ -51,7 +51,7 @@ namespace blink { IDBCursor* IDBCursor::Create(std::unique_ptr backend, WebIDBCursorDirection direction, IDBRequest* request, - IDBAny* source, + const Source& source, IDBTransaction* transaction) { return new IDBCursor(std::move(backend), direction, request, source, transaction); @@ -60,7 +60,7 @@ IDBCursor* IDBCursor::Create(std::unique_ptr backend, IDBCursor::IDBCursor(std::unique_ptr backend, WebIDBCursorDirection direction, IDBRequest* request, - IDBAny* source, + const Source& source, IDBTransaction* transaction) : backend_(std::move(backend)), request_(request), @@ -69,8 +69,7 @@ IDBCursor::IDBCursor(std::unique_ptr backend, transaction_(transaction) { DCHECK(backend_); DCHECK(request_); - DCHECK(source_->GetType() == IDBAny::kIDBObjectStoreType || - source_->GetType() == IDBAny::kIDBIndexType); + DCHECK(!source_.IsNull()); DCHECK(transaction_); } @@ -223,7 +222,7 @@ void IDBCursor::continuePrimaryKey(ScriptState* script_state, return; } - if (source_->GetType() != IDBAny::kIDBIndexType) { + if (!source_.IsIDBIndex()) { exception_state.ThrowDOMException(kInvalidAccessError, "The cursor's source is not an index."); return; @@ -417,8 +416,8 @@ ScriptValue IDBCursor::value(ScriptState* script_state) { return script_value; } -ScriptValue IDBCursor::source(ScriptState* script_state) const { - return ScriptValue::From(script_state, source_); +void IDBCursor::source(Source& source) const { + source = source_; } void IDBCursor::SetValueReady(std::unique_ptr key, @@ -468,15 +467,15 @@ const IDBKey* IDBCursor::IdbPrimaryKey() const { } IDBObjectStore* IDBCursor::EffectiveObjectStore() const { - if (source_->GetType() == IDBAny::kIDBObjectStoreType) - return source_->IdbObjectStore(); - return source_->IdbIndex()->objectStore(); + if (source_.IsIDBObjectStore()) + return source_.GetAsIDBObjectStore(); + return source_.GetAsIDBIndex()->objectStore(); } bool IDBCursor::IsDeleted() const { - if (source_->GetType() == IDBAny::kIDBObjectStoreType) - return source_->IdbObjectStore()->IsDeleted(); - return source_->IdbIndex()->IsDeleted(); + if (source_.IsIDBObjectStore()) + return source_.GetAsIDBObjectStore()->IsDeleted(); + return source_.GetAsIDBIndex()->IsDeleted(); } WebIDBCursorDirection IDBCursor::StringToDirection( diff --git a/third_party/WebKit/Source/modules/indexeddb/IDBCursor.h b/third_party/WebKit/Source/modules/indexeddb/IDBCursor.h index 802d26c7f1679b..dec3b41af8bcbf 100644 --- a/third_party/WebKit/Source/modules/indexeddb/IDBCursor.h +++ b/third_party/WebKit/Source/modules/indexeddb/IDBCursor.h @@ -29,6 +29,7 @@ #include #include "base/memory/scoped_refptr.h" #include "bindings/core/v8/ScriptValue.h" +#include "bindings/modules/v8/idb_object_store_or_idb_index.h" #include "modules/indexeddb/IDBKey.h" #include "modules/indexeddb/IDBRequest.h" #include "modules/indexeddb/IndexedDB.h" @@ -40,7 +41,6 @@ namespace blink { class ExceptionState; -class IDBAny; class IDBTransaction; class IDBValue; class ScriptState; @@ -49,12 +49,14 @@ class IDBCursor : public ScriptWrappable { DEFINE_WRAPPERTYPEINFO(); public: + using Source = IDBObjectStoreOrIDBIndex; + static WebIDBCursorDirection StringToDirection(const String& mode_string); static IDBCursor* Create(std::unique_ptr, WebIDBCursorDirection, IDBRequest*, - IDBAny* source, + const Source&, IDBTransaction*); virtual ~IDBCursor(); void Trace(blink::Visitor*); @@ -70,7 +72,7 @@ class IDBCursor : public ScriptWrappable { ScriptValue key(ScriptState*); ScriptValue primaryKey(ScriptState*); ScriptValue value(ScriptState*); - ScriptValue source(ScriptState*) const; + void source(Source&) const; IDBRequest* update(ScriptState*, const ScriptValue&, ExceptionState&); void advance(unsigned, ExceptionState&); @@ -103,7 +105,7 @@ class IDBCursor : public ScriptWrappable { IDBCursor(std::unique_ptr, WebIDBCursorDirection, IDBRequest*, - IDBAny* source, + const Source&, IDBTransaction*); private: @@ -112,7 +114,7 @@ class IDBCursor : public ScriptWrappable { std::unique_ptr backend_; Member request_; const WebIDBCursorDirection direction_; - Member source_; + Source source_; Member transaction_; bool got_value_ = false; bool key_dirty_ = true; diff --git a/third_party/WebKit/Source/modules/indexeddb/IDBCursor.idl b/third_party/WebKit/Source/modules/indexeddb/IDBCursor.idl index 616ff50ac8c4af..b5d5b6383bc61f 100644 --- a/third_party/WebKit/Source/modules/indexeddb/IDBCursor.idl +++ b/third_party/WebKit/Source/modules/indexeddb/IDBCursor.idl @@ -37,7 +37,7 @@ enum IDBCursorDirection { [ Exposed=(Window,Worker) ] interface IDBCursor { - [CallWith=ScriptState] readonly attribute any source; + readonly attribute (IDBObjectStore or IDBIndex) source; readonly attribute IDBCursorDirection direction; [CallWith=ScriptState, CachedAttribute=isKeyDirty] readonly attribute any key; [CallWith=ScriptState, CachedAttribute=isPrimaryKeyDirty] readonly attribute any primaryKey; diff --git a/third_party/WebKit/Source/modules/indexeddb/IDBCursorWithValue.cpp b/third_party/WebKit/Source/modules/indexeddb/IDBCursorWithValue.cpp index 89066a90e2e87c..f8e130b5d17e58 100644 --- a/third_party/WebKit/Source/modules/indexeddb/IDBCursorWithValue.cpp +++ b/third_party/WebKit/Source/modules/indexeddb/IDBCursorWithValue.cpp @@ -36,7 +36,7 @@ IDBCursorWithValue* IDBCursorWithValue::Create( std::unique_ptr backend, WebIDBCursorDirection direction, IDBRequest* request, - IDBAny* source, + const Source& source, IDBTransaction* transaction) { return new IDBCursorWithValue(std::move(backend), direction, request, source, transaction); @@ -45,7 +45,7 @@ IDBCursorWithValue* IDBCursorWithValue::Create( IDBCursorWithValue::IDBCursorWithValue(std::unique_ptr backend, WebIDBCursorDirection direction, IDBRequest* request, - IDBAny* source, + const Source& source, IDBTransaction* transaction) : IDBCursor(std::move(backend), direction, request, source, transaction) {} diff --git a/third_party/WebKit/Source/modules/indexeddb/IDBCursorWithValue.h b/third_party/WebKit/Source/modules/indexeddb/IDBCursorWithValue.h index e3fe40b14027ae..6133a09477b77f 100644 --- a/third_party/WebKit/Source/modules/indexeddb/IDBCursorWithValue.h +++ b/third_party/WebKit/Source/modules/indexeddb/IDBCursorWithValue.h @@ -34,7 +34,6 @@ namespace blink { -class IDBAny; class IDBRequest; class IDBTransaction; @@ -45,7 +44,7 @@ class IDBCursorWithValue final : public IDBCursor { static IDBCursorWithValue* Create(std::unique_ptr, WebIDBCursorDirection, IDBRequest*, - IDBAny* source, + const Source&, IDBTransaction*); ~IDBCursorWithValue() override; @@ -60,7 +59,7 @@ class IDBCursorWithValue final : public IDBCursor { IDBCursorWithValue(std::unique_ptr, WebIDBCursorDirection, IDBRequest*, - IDBAny* source, + const Source&, IDBTransaction*); }; diff --git a/third_party/WebKit/Source/modules/indexeddb/IDBRequest.cpp b/third_party/WebKit/Source/modules/indexeddb/IDBRequest.cpp index 8ab201da477c32..d933bef535e022 100644 --- a/third_party/WebKit/Source/modules/indexeddb/IDBRequest.cpp +++ b/third_party/WebKit/Source/modules/indexeddb/IDBRequest.cpp @@ -430,6 +430,16 @@ void IDBRequest::EnqueueResponse(const Vector& string_list) { metrics_.RecordAndReset(); } +static IDBCursor::Source ToIDBCursorSource(IDBAny* any) { + if (any->GetType() == IDBAny::kIDBObjectStoreType) + return IDBCursor::Source::FromIDBObjectStore(any->IdbObjectStore()); + if (any->GetType() == IDBAny::kIDBIndexType) + return IDBCursor::Source::FromIDBIndex(any->IdbIndex()); + + NOTREACHED(); + return IDBCursor::Source(); +} + void IDBRequest::EnqueueResponse(std::unique_ptr backend, std::unique_ptr key, std::unique_ptr primary_key, @@ -446,12 +456,13 @@ void IDBRequest::EnqueueResponse(std::unique_ptr backend, switch (cursor_type_) { case IndexedDB::kCursorKeyOnly: cursor = IDBCursor::Create(std::move(backend), cursor_direction_, this, - source_.Get(), transaction_.Get()); + ToIDBCursorSource(source_.Get()), + transaction_.Get()); break; case IndexedDB::kCursorKeyAndValue: - cursor = - IDBCursorWithValue::Create(std::move(backend), cursor_direction_, - this, source_.Get(), transaction_.Get()); + cursor = IDBCursorWithValue::Create( + std::move(backend), cursor_direction_, this, + ToIDBCursorSource(source_.Get()), transaction_.Get()); break; default: NOTREACHED();