Commit a788bcd8 authored by Kouhei Ueno's avatar Kouhei Ueno Committed by Commit Bot

[ES6 modules] Introduce ReferrerScriptInfo

This CL implements ReferrerScriptInfo, which holds a copy of "referencing
script" info, referenced in the "HostImportModuleDynamically" algorithm:
https://github.com/tc39/proposal-dynamic-import/blob/master/HTML%20Integration.md#hostimportmoduledynamicallyreferencingscriptormodule-specifier-promisecapability

This info can be encoded into v8::Local<v8::PrimitiveArray> and will be
attached to v8::ScriptOrigin in future CLs. Dynamic import resolver will
extract this from the ScriptOrigin of the referencing script in its
implementation of "HostImportModuleDynamically" algorithm.

Bug: 711706
Change-Id: I0649b6f0334154cdde910a657eee449b39f5f9c7
Reviewed-on: https://chromium-review.googlesource.com/668257Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Kouhei Ueno <kouhei@chromium.org>
Cr-Commit-Position: refs/heads/master@{#502176}
parent b10f7eda
......@@ -29,8 +29,6 @@ bindings_core_v8_files =
"core/v8/BindingSecurity.cpp",
"core/v8/BindingSecurity.h",
"core/v8/CallbackPromiseAdapter.h",
"core/v8/V0CustomElementConstructorBuilder.cpp",
"core/v8/V0CustomElementConstructorBuilder.h",
"core/v8/Dictionary.cpp",
"core/v8/Dictionary.h",
"core/v8/DictionaryHelperForBindings.h",
......@@ -54,6 +52,8 @@ bindings_core_v8_files =
"core/v8/NativeValueTraits.h",
"core/v8/NativeValueTraitsImpl.h",
"core/v8/Nullable.h",
"core/v8/ReferrerScriptInfo.cpp",
"core/v8/ReferrerScriptInfo.h",
"core/v8/RejectedPromises.cpp",
"core/v8/RejectedPromises.h",
"core/v8/RemoteWindowProxy.cpp",
......@@ -100,6 +100,8 @@ bindings_core_v8_files =
"core/v8/ToV8ForCore.h",
"core/v8/UseCounterCallback.cpp",
"core/v8/UseCounterCallback.h",
"core/v8/V0CustomElementConstructorBuilder.cpp",
"core/v8/V0CustomElementConstructorBuilder.h",
"core/v8/V8AbstractEventListener.cpp",
"core/v8/V8AbstractEventListener.h",
"core/v8/V8BindingForCore.cpp",
......@@ -111,17 +113,15 @@ bindings_core_v8_files =
"core/v8/V8DOMActivityLogger.h",
"core/v8/V8DOMConfiguration.cpp",
"core/v8/V8DOMConfiguration.h",
"core/v8/V8ThrowDOMException.cpp",
"core/v8/V8ThrowDOMException.h",
"core/v8/V8ErrorHandler.cpp",
"core/v8/V8ErrorHandler.h",
"core/v8/V8EventListener.cpp",
"core/v8/V8EventListener.h",
"core/v8/V8EventListenerHelper.cpp",
"core/v8/V8EventListenerHelper.h",
"core/v8/V8EventListenerInfo.h",
"core/v8/V8GCController.cpp",
"core/v8/V8GCController.h",
"core/v8/V8EventListenerHelper.cpp",
"core/v8/V8EventListenerHelper.h",
"core/v8/V8GCForContextDispose.cpp",
"core/v8/V8GCForContextDispose.h",
"core/v8/V8HTMLConstructor.cpp",
......@@ -131,6 +131,8 @@ bindings_core_v8_files =
"core/v8/V8Initializer.h",
"core/v8/V8IntersectionObserverDelegate.cpp",
"core/v8/V8IntersectionObserverDelegate.h",
"core/v8/V8ThrowDOMException.cpp",
"core/v8/V8ThrowDOMException.h",
"core/v8/V8IteratorResultValue.cpp",
"core/v8/V8IteratorResultValue.h",
"core/v8/V8LazyEventListener.cpp",
......@@ -188,17 +190,18 @@ bindings_unittest_files =
"core/v8/IDLTypesTest.cpp",
"core/v8/NativeValueTraitsImplTest.cpp",
"core/v8/NativeValueTraitsTest.cpp",
"core/v8/ReferrerScriptInfoTest.cpp",
"core/v8/ScriptModuleTest.cpp",
"core/v8/ScriptPromisePropertyTest.cpp",
"core/v8/ScriptPromiseResolverTest.cpp",
"core/v8/ScriptPromiseTest.cpp",
"core/v8/WindowProxyTest.cpp",
"core/v8/ScriptStreamerTest.cpp",
"core/v8/ScriptWrappableVisitorTest.cpp",
"core/v8/ToV8Test.cpp",
"core/v8/TraceWrapperMemberTest.cpp",
"core/v8/V8BindingForTesting.cpp",
"core/v8/V8BindingForTesting.h",
"core/v8/WindowProxyTest.cpp",
"core/v8/V8BindingTest.cpp",
"core/v8/V8ObjectBuilderTest.cpp",
"core/v8/V8ScriptRunnerTest.cpp",
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "bindings/core/v8/ReferrerScriptInfo.h"
#include "bindings/core/v8/V8BindingForCore.h"
#include "v8/include/v8.h"
namespace blink {
namespace {
enum HostDefinedOptionsIndex : size_t {
kCredentialsMode,
kNonce,
kParserState,
kLength
};
} // namespace
ReferrerScriptInfo ReferrerScriptInfo::FromV8HostDefinedOptions(
v8::Local<v8::Context> context,
v8::Local<v8::PrimitiveArray> host_defined_options) {
if (host_defined_options.IsEmpty() || !host_defined_options->Length()) {
String empty_nonce;
return ReferrerScriptInfo(WebURLRequest::kFetchCredentialsModeOmit,
empty_nonce, kNotParserInserted);
}
v8::Local<v8::Primitive> credentials_mode_value =
host_defined_options->Get(kCredentialsMode);
WebURLRequest::FetchCredentialsMode credentials_mode =
static_cast<WebURLRequest::FetchCredentialsMode>(
credentials_mode_value->IntegerValue(context).ToChecked());
v8::Local<v8::Primitive> nonce_value = host_defined_options->Get(kNonce);
String nonce =
ToCoreStringWithNullCheck(v8::Local<v8::String>::Cast(nonce_value));
v8::Local<v8::Primitive> parser_state_value =
host_defined_options->Get(kParserState);
ParserDisposition parser_state = static_cast<ParserDisposition>(
parser_state_value->IntegerValue(context).ToChecked());
return ReferrerScriptInfo(credentials_mode, nonce, parser_state);
}
v8::Local<v8::PrimitiveArray> ReferrerScriptInfo::ToV8HostDefinedOptions(
v8::Isolate* isolate) const {
v8::Local<v8::PrimitiveArray> host_defined_options =
v8::PrimitiveArray::New(isolate, HostDefinedOptionsIndex::kLength);
v8::Local<v8::Primitive> credentials_mode_value =
v8::Integer::NewFromUnsigned(isolate,
static_cast<uint32_t>(credentials_mode_));
host_defined_options->Set(HostDefinedOptionsIndex::kCredentialsMode,
credentials_mode_value);
v8::Local<v8::Primitive> nonce_value = V8String(isolate, nonce_);
host_defined_options->Set(HostDefinedOptionsIndex::kNonce, nonce_value);
v8::Local<v8::Primitive> parser_state_value = v8::Integer::NewFromUnsigned(
isolate, static_cast<uint32_t>(parser_state_));
host_defined_options->Set(HostDefinedOptionsIndex::kParserState,
parser_state_value);
return host_defined_options;
}
} // namespace blink
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ReferrerScriptInfo_h
#define ReferrerScriptInfo_h
#include "core/CoreExport.h"
#include "platform/loader/fetch/AccessControlStatus.h"
#include "platform/loader/fetch/ResourceLoaderOptions.h"
#include "platform/wtf/text/TextPosition.h"
#include "platform/wtf/text/WTFString.h"
#include "public/platform/WebURLRequest.h"
#include "v8/include/v8.h"
namespace blink {
// ReferrerScriptInfo carries a copy of "referencing script's" info referenced
// in HTML Spec: "HostImportModuleDynamically" algorithm.
// https://github.com/tc39/proposal-dynamic-import/blob/master/HTML%20Integration.md#hostimportmoduledynamicallyreferencingscriptormodule-specifier-promisecapability
class CORE_EXPORT ReferrerScriptInfo {
public:
ReferrerScriptInfo() {}
ReferrerScriptInfo(WebURLRequest::FetchCredentialsMode credentials_mode,
const String& nonce,
ParserDisposition parser_state)
: credentials_mode_(credentials_mode),
nonce_(nonce),
parser_state_(parser_state) {}
static ReferrerScriptInfo FromV8HostDefinedOptions(
v8::Local<v8::Context>,
v8::Local<v8::PrimitiveArray>);
v8::Local<v8::PrimitiveArray> ToV8HostDefinedOptions(v8::Isolate*) const;
WebURLRequest::FetchCredentialsMode CredentialsMode() const {
return credentials_mode_;
}
const String& Nonce() const { return nonce_; }
ParserDisposition ParserState() const { return parser_state_; }
private:
// Spec: "referencing script's credentials mode"
// The default value is "omit", from Step 5 of [HIMD].
// [HIMD]
// https://github.com/tc39/proposal-dynamic-import/blob/master/HTML%20Integration.md#hostimportmoduledynamicallyreferencingscriptormodule-specifier-promisecapability
WebURLRequest::FetchCredentialsMode credentials_mode_ =
WebURLRequest::kFetchCredentialsModeOmit;
// Spec: "referencing script's cryptographic nonce"
String nonce_;
// Spec: "referencing script's parser state"
ParserDisposition parser_state_ = kNotParserInserted;
};
} // namespace blink
#endif // ReferrerScriptInfo_h
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "bindings/core/v8/ReferrerScriptInfo.h"
#include "bindings/core/v8/V8BindingForTesting.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "v8/include/v8.h"
namespace blink {
TEST(ReferrerScriptInfo, ToFromV8) {
V8TestingScope scope;
ReferrerScriptInfo info(WebURLRequest::kFetchCredentialsModePassword,
"foobar", kNotParserInserted);
v8::Local<v8::PrimitiveArray> v8_info =
info.ToV8HostDefinedOptions(scope.GetIsolate());
ReferrerScriptInfo decoded =
ReferrerScriptInfo::FromV8HostDefinedOptions(scope.GetContext(), v8_info);
EXPECT_EQ(WebURLRequest::kFetchCredentialsModePassword,
decoded.CredentialsMode());
EXPECT_EQ("foobar", decoded.Nonce());
EXPECT_EQ(kNotParserInserted, decoded.ParserState());
}
} // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment