Commit d3839beb authored by Hiroshige Hayashizaki's avatar Hiroshige Hayashizaki Committed by Commit Bot

[Import Maps] Clean up after built-in module removal

Follow-up of
https://chromium-review.googlesource.com/c/chromium/src/+/2096165.

This CL simplifies the code by removing Vector and base::Optional
that are no longer needed.

This CL doesn't change the behavior.

Bug: 848607
Change-Id: I6a76b240a02c01b712702a594d403547d40b9dbd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2108206Reviewed-by: default avatarKouhei Ueno <kouhei@chromium.org>
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751632}
parent d2517e92
...@@ -119,6 +119,7 @@ KURL NormalizeValue(const String& key, ...@@ -119,6 +119,7 @@ KURL NormalizeValue(const String& key,
return NullURL(); return NullURL();
} }
DCHECK(value.GetUrl().IsValid());
return value.GetUrl(); return value.GetUrl();
} }
} }
...@@ -295,7 +296,6 @@ ImportMap::SpecifierMap ImportMap::SortAndNormalizeSpecifierMap( ...@@ -295,7 +296,6 @@ ImportMap::SpecifierMap ImportMap::SortAndNormalizeSpecifierMap(
if (normalized_specifier_key.IsEmpty()) if (normalized_specifier_key.IsEmpty())
continue; continue;
Vector<KURL> values;
switch (entry.second->GetType()) { switch (entry.second->GetType()) {
case JSONValue::ValueType::kTypeString: { case JSONValue::ValueType::kTypeString: {
// Steps 2.4-2.6 are implemented in NormalizeValue(). // Steps 2.4-2.6 are implemented in NormalizeValue().
...@@ -303,14 +303,13 @@ ImportMap::SpecifierMap ImportMap::SortAndNormalizeSpecifierMap( ...@@ -303,14 +303,13 @@ ImportMap::SpecifierMap ImportMap::SortAndNormalizeSpecifierMap(
if (!imports->GetString(entry.first, &value_string)) { if (!imports->GetString(entry.first, &value_string)) {
AddIgnoredValueMessage(logger, entry.first, AddIgnoredValueMessage(logger, entry.first,
"Internal error in GetString()."); "Internal error in GetString().");
normalized.Set(normalized_specifier_key, NullURL());
break; break;
} }
KURL value =
NormalizeValue(entry.first, value_string, base_url, logger);
// <spec step="2.7">Set normalized[specifierKey] to addressURL.</spec> normalized.Set(
if (value.IsValid()) normalized_specifier_key,
values.push_back(value); NormalizeValue(entry.first, value_string, base_url, logger));
break; break;
} }
...@@ -327,15 +326,12 @@ ImportMap::SpecifierMap ImportMap::SortAndNormalizeSpecifierMap( ...@@ -327,15 +326,12 @@ ImportMap::SpecifierMap ImportMap::SortAndNormalizeSpecifierMap(
AddIgnoredValueMessage(logger, entry.first, "Invalid value type."); AddIgnoredValueMessage(logger, entry.first, "Invalid value type.");
// <spec step="2.3.2">Set normalized[specifierKey] to null.</spec> // <spec step="2.3.2">Set normalized[specifierKey] to null.</spec>
// normalized.Set(normalized_specifier_key, NullURL());
// <spec step="2.3.3">Continue.</spec> // <spec step="2.3.3">Continue.</spec>
break; break;
} }
CHECK_LE(values.size(), 1u);
// <spec step="2.7">Set normalized[specifierKey] to addressURL.</spec>
normalized.Set(normalized_specifier_key, values);
} }
return normalized; return normalized;
...@@ -447,36 +443,50 @@ base::Optional<KURL> ImportMap::ResolveImportsMatch( ...@@ -447,36 +443,50 @@ base::Optional<KURL> ImportMap::ResolveImportsMatch(
} }
// <specdef href="https://wicg.github.io/import-maps/#resolve-an-imports-match"> // <specdef href="https://wicg.github.io/import-maps/#resolve-an-imports-match">
base::Optional<KURL> ImportMap::ResolveImportsMatchInternal( KURL ImportMap::ResolveImportsMatchInternal(const String& key,
const String& key, const MatchResult& matched,
const MatchResult& matched, String* debug_message) const {
String* debug_message) const {
// <spec step="1.2.3">Let afterPrefix be the portion of normalizedSpecifier // <spec step="1.2.3">Let afterPrefix be the portion of normalizedSpecifier
// after the initial specifierKey prefix.</spec> // after the initial specifierKey prefix.</spec>
const String after_prefix = key.Substring(matched->key.length()); const String after_prefix = key.Substring(matched->key.length());
for (const KURL& value : matched->value) { // <spec step="1.1.1">If resolutionResult is null, then throw a TypeError
// <spec step="1.1">If specifierKey is normalizedSpecifier, then:</spec> // indicating that resolution of specifierKey was blocked by a null
// // entry.</spec>
// <spec step="1.2">If specifierKey ends with U+002F (/) and //
// normalizedSpecifier starts with specifierKey, then:</spec> // <spec step="1.2.1">If resolutionResult is null, then throw a TypeError
// // indicating that resolution of specifierKey was blocked by a null
// <spec step="1.2.5">Let url be the result of parsing afterPrefix relative // entry.</spec>
// to the base URL resolutionResult.</spec> if (!matched->value.IsValid()) {
const KURL url = after_prefix.IsEmpty() ? value : KURL(value, after_prefix);
*debug_message = "Import Map: \"" + key + "\" matches with \"" + *debug_message = "Import Map: \"" + key + "\" matches with \"" +
matched->key + "\" and is mapped to " + url.ElidedString(); matched->key + "\" but is blocked by a null value";
return NullURL();
}
// <spec step="1.2.6">If url is failure, then throw ...</spec> // <spec step="1.1">If specifierKey is normalizedSpecifier, then:</spec>
// //
// <spec step="1.2.8">Return url.</spec> // <spec step="1.2">If specifierKey ends with U+002F (/) and
return url; // normalizedSpecifier starts with specifierKey, then:</spec>
//
// <spec step="1.2.5">Let url be the result of parsing afterPrefix relative
// to the base URL resolutionResult.</spec>
const KURL url = after_prefix.IsEmpty() ? matched->value
: KURL(matched->value, after_prefix);
// <spec step="1.2.6">If url is failure, then throw a TypeError indicating
// that resolution of specifierKey was blocked due to a URL parse
// failure.</spec>
if (!url.IsValid()) {
*debug_message = "Import Map: \"" + key + "\" matches with \"" +
matched->key +
"\" but is blocked due to relative URL parse failure";
return NullURL();
} }
// <spec step="1.2.8">Return url.</spec>
*debug_message = "Import Map: \"" + key + "\" matches with \"" + *debug_message = "Import Map: \"" + key + "\" matches with \"" +
matched->key + "\" but fails to be mapped (no viable URLs)"; matched->key + "\" and is mapped to " + url.ElidedString();
return NullURL(); return url;
} }
static void SpecifierMapToString(StringBuilder& builder, static void SpecifierMapToString(StringBuilder& builder,
...@@ -489,12 +499,10 @@ static void SpecifierMapToString(StringBuilder& builder, ...@@ -489,12 +499,10 @@ static void SpecifierMapToString(StringBuilder& builder,
is_first_key = false; is_first_key = false;
builder.Append(it.key.EncodeForDebugging()); builder.Append(it.key.EncodeForDebugging());
builder.Append(":"); builder.Append(":");
if (it.value.size() == 0) { if (it.value.IsValid())
builder.Append(it.value.GetString().EncodeForDebugging());
else
builder.Append("null"); builder.Append("null");
} else {
DCHECK_EQ(it.value.size(), 1u);
builder.Append(it.value[0].GetString().EncodeForDebugging());
}
} }
builder.Append("}"); builder.Append("}");
} }
......
...@@ -32,11 +32,13 @@ class CORE_EXPORT ImportMap final : public GarbageCollected<ImportMap> { ...@@ -32,11 +32,13 @@ class CORE_EXPORT ImportMap final : public GarbageCollected<ImportMap> {
ScriptValue* error_to_rethrow); ScriptValue* error_to_rethrow);
// <spec href="https://wicg.github.io/import-maps/#specifier-map">A specifier // <spec href="https://wicg.github.io/import-maps/#specifier-map">A specifier
// map is an ordered map from strings to lists of URLs.</spec> // map is an ordered map from strings to resolution results.</spec>
//
// An invalid KURL corresponds to a null resolution result in the spec.
// //
// In Blink, we actually use an unordered map here, and related algorithms // In Blink, we actually use an unordered map here, and related algorithms
// are implemented differently from the spec. // are implemented differently from the spec.
using SpecifierMap = HashMap<String, Vector<KURL>>; using SpecifierMap = HashMap<String, KURL>;
// <spec href="https://wicg.github.io/import-maps/#import-map-scopes">an // <spec href="https://wicg.github.io/import-maps/#import-map-scopes">an
// ordered map of URLs to specifier maps.</spec> // ordered map of URLs to specifier maps.</spec>
...@@ -48,6 +50,12 @@ class CORE_EXPORT ImportMap final : public GarbageCollected<ImportMap> { ...@@ -48,6 +50,12 @@ class CORE_EXPORT ImportMap final : public GarbageCollected<ImportMap> {
ImportMap(SpecifierMap&& imports, ScopeType&& scopes); ImportMap(SpecifierMap&& imports, ScopeType&& scopes);
// Return values of Resolve(), ResolveImportsMatch() and
// ResolveImportsMatchInternal():
// - base::nullopt: corresponds to returning a null in the spec,
// i.e. allowing fallback to a less specific scope etc.
// - An invalid KURL: corresponds to throwing an error in the spec.
// - A valid KURL: corresponds to returning a valid URL in the spec.
base::Optional<KURL> Resolve(const ParsedSpecifier&, base::Optional<KURL> Resolve(const ParsedSpecifier&,
const KURL& base_url, const KURL& base_url,
String* debug_message) const; String* debug_message) const;
...@@ -60,9 +68,6 @@ class CORE_EXPORT ImportMap final : public GarbageCollected<ImportMap> { ...@@ -60,9 +68,6 @@ class CORE_EXPORT ImportMap final : public GarbageCollected<ImportMap> {
using MatchResult = SpecifierMap::const_iterator; using MatchResult = SpecifierMap::const_iterator;
// https://wicg.github.io/import-maps/#resolve-an-imports-match // https://wicg.github.io/import-maps/#resolve-an-imports-match
// Returns nullopt when not mapped by |this| import map (i.e. the import map
// doesn't have corresponding keys).
// Returns a null URL when resolution fails.
base::Optional<KURL> ResolveImportsMatch(const ParsedSpecifier&, base::Optional<KURL> ResolveImportsMatch(const ParsedSpecifier&,
const SpecifierMap&, const SpecifierMap&,
String* debug_message) const; String* debug_message) const;
...@@ -72,10 +77,9 @@ class CORE_EXPORT ImportMap final : public GarbageCollected<ImportMap> { ...@@ -72,10 +77,9 @@ class CORE_EXPORT ImportMap final : public GarbageCollected<ImportMap> {
const KURL& base_url, const KURL& base_url,
ConsoleLogger&); ConsoleLogger&);
base::Optional<KURL> ResolveImportsMatchInternal( KURL ResolveImportsMatchInternal(const String& normalizedSpecifier,
const String& normalizedSpecifier, const MatchResult&,
const MatchResult&, String* debug_message) const;
String* debug_message) const;
// https://wicg.github.io/import-maps/#import-map-imports // https://wicg.github.io/import-maps/#import-map-imports
SpecifierMap imports_; SpecifierMap imports_;
......
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