Commit 86c9de0f authored by Orsi Batiz's avatar Orsi Batiz Committed by Commit Bot

GetString added in TrustedURL

Function String GetString(USVStringOrTrustedURL, const Document*, ExceptionState&)
has been added to trusted_url.cc
All other callsites (setAttribute in element.cc, open in local_dom_window.cc,
assign, replace and setHref in location.cc) have been updated to call this function
setHref in html_base_element.cc has been changed to call a different setAttribute
from element.cc (the one mentioned above), this way there was no need to call
GetString in there as well

Bug: 739170
Change-Id: I07b9fee53ea62aa706bb59814f147a394f214549
Reviewed-on: https://chromium-review.googlesource.com/1144927
Commit-Queue: Orsolya Bernadett Batiz <orsibatiz@google.com>
Reviewed-by: default avatarMike West <mkwst@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577449}
parent 9ed8cdeb
...@@ -1617,19 +1617,11 @@ void Element::setAttribute(const QualifiedName& name, ...@@ -1617,19 +1617,11 @@ void Element::setAttribute(const QualifiedName& name,
void Element::setAttribute(const QualifiedName& name, void Element::setAttribute(const QualifiedName& name,
const USVStringOrTrustedURL& stringOrURL, const USVStringOrTrustedURL& stringOrURL,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(stringOrURL.IsUSVString() || String url =
RuntimeEnabledFeatures::TrustedDOMTypesEnabled()); TrustedURL::GetString(stringOrURL, &GetDocument(), exception_state);
if (stringOrURL.IsUSVString() && GetDocument().RequireTrustedTypes()) { if (!exception_state.HadException()) {
exception_state.ThrowTypeError( setAttribute(name, AtomicString(url));
"This document requires `TrustedURL` assignment.");
return;
} }
String valueString = stringOrURL.IsUSVString()
? stringOrURL.GetAsUSVString()
: stringOrURL.GetAsTrustedURL()->toString();
setAttribute(name, AtomicString(valueString));
} }
ALWAYS_INLINE void Element::SetAttributeInternal( ALWAYS_INLINE void Element::SetAttributeInternal(
......
...@@ -1550,21 +1550,12 @@ DOMWindow* LocalDOMWindow::open(ExecutionContext* executionContext, ...@@ -1550,21 +1550,12 @@ DOMWindow* LocalDOMWindow::open(ExecutionContext* executionContext,
const AtomicString& target, const AtomicString& target,
const String& features, const String& features,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(stringOrUrl.IsUSVString() || String url = TrustedURL::GetString(stringOrUrl, document_, exception_state);
RuntimeEnabledFeatures::TrustedDOMTypesEnabled()); if (!exception_state.HadException()) {
return openFromString(executionContext, current_window, entered_window, url,
if (!stringOrUrl.IsTrustedURL() && document_->RequireTrustedTypes()) { target, features, exception_state);
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return nullptr;
} }
return nullptr;
String url = stringOrUrl.IsUSVString()
? stringOrUrl.GetAsUSVString()
: stringOrUrl.GetAsTrustedURL()->toString();
return openFromString(executionContext, current_window, entered_window, url,
target, features, exception_state);
} }
DOMWindow* LocalDOMWindow::openFromString(ExecutionContext* executionContext, DOMWindow* LocalDOMWindow::openFromString(ExecutionContext* executionContext,
...@@ -1609,21 +1600,12 @@ DOMWindow* LocalDOMWindow::open(const USVStringOrTrustedURL& stringOrUrl, ...@@ -1609,21 +1600,12 @@ DOMWindow* LocalDOMWindow::open(const USVStringOrTrustedURL& stringOrUrl,
LocalDOMWindow* calling_window, LocalDOMWindow* calling_window,
LocalDOMWindow* entered_window, LocalDOMWindow* entered_window,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(stringOrUrl.IsUSVString() || String url = TrustedURL::GetString(stringOrUrl, document_, exception_state);
RuntimeEnabledFeatures::TrustedDOMTypesEnabled()); if (!exception_state.HadException()) {
return openFromString(url, frame_name, window_features_string,
if (!stringOrUrl.IsTrustedURL() && document_->RequireTrustedTypes()) { calling_window, entered_window, exception_state);
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return nullptr;
} }
return nullptr;
String url = stringOrUrl.IsUSVString()
? stringOrUrl.GetAsUSVString()
: stringOrUrl.GetAsTrustedURL()->toString();
return openFromString(url, frame_name, window_features_string, calling_window,
entered_window, exception_state);
} }
DOMWindow* LocalDOMWindow::openFromString(const String& url_string, DOMWindow* LocalDOMWindow::openFromString(const String& url_string,
......
...@@ -121,20 +121,11 @@ void Location::setHref(LocalDOMWindow* current_window, ...@@ -121,20 +121,11 @@ void Location::setHref(LocalDOMWindow* current_window,
LocalDOMWindow* entered_window, LocalDOMWindow* entered_window,
const USVStringOrTrustedURL& stringOrUrl, const USVStringOrTrustedURL& stringOrUrl,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(stringOrUrl.IsUSVString() || String url = TrustedURL::GetString(stringOrUrl, current_window->document(),
RuntimeEnabledFeatures::TrustedDOMTypesEnabled()); exception_state);
if (!exception_state.HadException()) {
if (stringOrUrl.IsUSVString() && SetLocation(url, current_window, entered_window, &exception_state);
current_window->document()->RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return;
} }
String url = stringOrUrl.IsUSVString()
? stringOrUrl.GetAsUSVString()
: stringOrUrl.GetAsTrustedURL()->toString();
SetLocation(url, current_window, entered_window, &exception_state);
} }
void Location::setProtocol(LocalDOMWindow* current_window, void Location::setProtocol(LocalDOMWindow* current_window,
...@@ -233,45 +224,23 @@ void Location::assign(LocalDOMWindow* current_window, ...@@ -233,45 +224,23 @@ void Location::assign(LocalDOMWindow* current_window,
return; return;
} }
DCHECK(stringOrUrl.IsUSVString() || String url = TrustedURL::GetString(stringOrUrl, current_window->document(),
RuntimeEnabledFeatures::TrustedDOMTypesEnabled()); exception_state);
DCHECK(!stringOrUrl.IsNull()); if (!exception_state.HadException()) {
SetLocation(url, current_window, entered_window, &exception_state);
if (stringOrUrl.IsUSVString() &&
current_window->document()->RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return;
} }
String url = stringOrUrl.IsUSVString()
? stringOrUrl.GetAsUSVString()
: stringOrUrl.GetAsTrustedURL()->toString();
SetLocation(url, current_window, entered_window, &exception_state);
} }
void Location::replace(LocalDOMWindow* current_window, void Location::replace(LocalDOMWindow* current_window,
LocalDOMWindow* entered_window, LocalDOMWindow* entered_window,
const USVStringOrTrustedURL& stringOrUrl, const USVStringOrTrustedURL& stringOrUrl,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(stringOrUrl.IsUSVString() || String url = TrustedURL::GetString(stringOrUrl, current_window->document(),
RuntimeEnabledFeatures::TrustedDOMTypesEnabled()); exception_state);
DCHECK(!stringOrUrl.IsNull()); if (!exception_state.HadException()) {
SetLocation(url, current_window, entered_window, &exception_state,
if (stringOrUrl.IsUSVString() && SetLocationPolicy::kReplaceThisFrame);
current_window->document()->RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return;
} }
String url = stringOrUrl.IsUSVString()
? stringOrUrl.GetAsUSVString()
: stringOrUrl.GetAsTrustedURL()->toString();
SetLocation(url, current_window, entered_window, &exception_state,
SetLocationPolicy::kReplaceThisFrame);
} }
void Location::reload(LocalDOMWindow* current_window) { void Location::reload(LocalDOMWindow* current_window) {
......
...@@ -96,21 +96,7 @@ KURL HTMLBaseElement::href() const { ...@@ -96,21 +96,7 @@ KURL HTMLBaseElement::href() const {
void HTMLBaseElement::setHref(const USVStringOrTrustedURL& stringOrUrl, void HTMLBaseElement::setHref(const USVStringOrTrustedURL& stringOrUrl,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(stringOrUrl.IsUSVString() || setAttribute(hrefAttr, stringOrUrl, exception_state);
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
DCHECK(!stringOrUrl.IsNull());
if (stringOrUrl.IsUSVString() && GetDocument().RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return;
}
AtomicString value(stringOrUrl.IsUSVString()
? stringOrUrl.GetAsUSVString()
: stringOrUrl.GetAsTrustedURL()->toString());
setAttribute(hrefAttr, value);
} }
} // namespace blink } // namespace blink
...@@ -4,8 +4,11 @@ ...@@ -4,8 +4,11 @@
#include "third_party/blink/renderer/core/trustedtypes/trusted_url.h" #include "third_party/blink/renderer/core/trustedtypes/trusted_url.h"
#include "third_party/blink/renderer/bindings/core/v8/usv_string_or_trusted_url.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h" #include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
namespace blink { namespace blink {
...@@ -26,6 +29,25 @@ TrustedURL* TrustedURL::unsafelyCreate(ScriptState* script_state, ...@@ -26,6 +29,25 @@ TrustedURL* TrustedURL::unsafelyCreate(ScriptState* script_state,
ExecutionContext::From(script_state)->CompleteURL(url)); ExecutionContext::From(script_state)->CompleteURL(url));
} }
String TrustedURL::GetString(USVStringOrTrustedURL stringOrURL,
const Document* doc,
ExceptionState& exception_state) {
DCHECK(stringOrURL.IsUSVString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
DCHECK(!stringOrURL.IsNull());
if (!stringOrURL.IsTrustedURL() && doc && doc->RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return g_empty_string;
}
String markup = stringOrURL.IsUSVString()
? stringOrURL.GetAsUSVString()
: stringOrURL.GetAsTrustedURL()->toString();
return markup;
}
String TrustedURL::toString() const { String TrustedURL::toString() const {
return url_.GetString(); return url_.GetString();
} }
......
...@@ -13,7 +13,10 @@ ...@@ -13,7 +13,10 @@
namespace blink { namespace blink {
class Document;
class ExceptionState;
class ScriptState; class ScriptState;
class USVStringOrTrustedURL;
class CORE_EXPORT TrustedURL final : public ScriptWrappable { class CORE_EXPORT TrustedURL final : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
...@@ -25,6 +28,9 @@ class CORE_EXPORT TrustedURL final : public ScriptWrappable { ...@@ -25,6 +28,9 @@ class CORE_EXPORT TrustedURL final : public ScriptWrappable {
String toString() const; String toString() const;
static TrustedURL* create(ScriptState*, const String& url); static TrustedURL* create(ScriptState*, const String& url);
static TrustedURL* unsafelyCreate(ScriptState*, const String& url); static TrustedURL* unsafelyCreate(ScriptState*, const String& url);
static String GetString(USVStringOrTrustedURL,
const Document*,
ExceptionState&);
private: private:
TrustedURL(const KURL&); TrustedURL(const KURL&);
......
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