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,
void Element::setAttribute(const QualifiedName& name,
const USVStringOrTrustedURL& stringOrURL,
ExceptionState& exception_state) {
DCHECK(stringOrURL.IsUSVString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
if (stringOrURL.IsUSVString() && GetDocument().RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return;
String url =
TrustedURL::GetString(stringOrURL, &GetDocument(), exception_state);
if (!exception_state.HadException()) {
setAttribute(name, AtomicString(url));
}
String valueString = stringOrURL.IsUSVString()
? stringOrURL.GetAsUSVString()
: stringOrURL.GetAsTrustedURL()->toString();
setAttribute(name, AtomicString(valueString));
}
ALWAYS_INLINE void Element::SetAttributeInternal(
......
......@@ -1550,21 +1550,12 @@ DOMWindow* LocalDOMWindow::open(ExecutionContext* executionContext,
const AtomicString& target,
const String& features,
ExceptionState& exception_state) {
DCHECK(stringOrUrl.IsUSVString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
if (!stringOrUrl.IsTrustedURL() && document_->RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return nullptr;
String url = TrustedURL::GetString(stringOrUrl, document_, exception_state);
if (!exception_state.HadException()) {
return openFromString(executionContext, current_window, entered_window, url,
target, features, exception_state);
}
String url = stringOrUrl.IsUSVString()
? stringOrUrl.GetAsUSVString()
: stringOrUrl.GetAsTrustedURL()->toString();
return openFromString(executionContext, current_window, entered_window, url,
target, features, exception_state);
return nullptr;
}
DOMWindow* LocalDOMWindow::openFromString(ExecutionContext* executionContext,
......@@ -1609,21 +1600,12 @@ DOMWindow* LocalDOMWindow::open(const USVStringOrTrustedURL& stringOrUrl,
LocalDOMWindow* calling_window,
LocalDOMWindow* entered_window,
ExceptionState& exception_state) {
DCHECK(stringOrUrl.IsUSVString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
if (!stringOrUrl.IsTrustedURL() && document_->RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return nullptr;
String url = TrustedURL::GetString(stringOrUrl, document_, exception_state);
if (!exception_state.HadException()) {
return openFromString(url, frame_name, window_features_string,
calling_window, entered_window, exception_state);
}
String url = stringOrUrl.IsUSVString()
? stringOrUrl.GetAsUSVString()
: stringOrUrl.GetAsTrustedURL()->toString();
return openFromString(url, frame_name, window_features_string, calling_window,
entered_window, exception_state);
return nullptr;
}
DOMWindow* LocalDOMWindow::openFromString(const String& url_string,
......
......@@ -121,20 +121,11 @@ void Location::setHref(LocalDOMWindow* current_window,
LocalDOMWindow* entered_window,
const USVStringOrTrustedURL& stringOrUrl,
ExceptionState& exception_state) {
DCHECK(stringOrUrl.IsUSVString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
if (stringOrUrl.IsUSVString() &&
current_window->document()->RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return;
String url = TrustedURL::GetString(stringOrUrl, current_window->document(),
exception_state);
if (!exception_state.HadException()) {
SetLocation(url, current_window, entered_window, &exception_state);
}
String url = stringOrUrl.IsUSVString()
? stringOrUrl.GetAsUSVString()
: stringOrUrl.GetAsTrustedURL()->toString();
SetLocation(url, current_window, entered_window, &exception_state);
}
void Location::setProtocol(LocalDOMWindow* current_window,
......@@ -233,45 +224,23 @@ void Location::assign(LocalDOMWindow* current_window,
return;
}
DCHECK(stringOrUrl.IsUSVString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
DCHECK(!stringOrUrl.IsNull());
if (stringOrUrl.IsUSVString() &&
current_window->document()->RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return;
String url = TrustedURL::GetString(stringOrUrl, current_window->document(),
exception_state);
if (!exception_state.HadException()) {
SetLocation(url, current_window, entered_window, &exception_state);
}
String url = stringOrUrl.IsUSVString()
? stringOrUrl.GetAsUSVString()
: stringOrUrl.GetAsTrustedURL()->toString();
SetLocation(url, current_window, entered_window, &exception_state);
}
void Location::replace(LocalDOMWindow* current_window,
LocalDOMWindow* entered_window,
const USVStringOrTrustedURL& stringOrUrl,
ExceptionState& exception_state) {
DCHECK(stringOrUrl.IsUSVString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
DCHECK(!stringOrUrl.IsNull());
if (stringOrUrl.IsUSVString() &&
current_window->document()->RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedURL` assignment.");
return;
String url = TrustedURL::GetString(stringOrUrl, current_window->document(),
exception_state);
if (!exception_state.HadException()) {
SetLocation(url, current_window, entered_window, &exception_state,
SetLocationPolicy::kReplaceThisFrame);
}
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) {
......
......@@ -96,21 +96,7 @@ KURL HTMLBaseElement::href() const {
void HTMLBaseElement::setHref(const USVStringOrTrustedURL& stringOrUrl,
ExceptionState& exception_state) {
DCHECK(stringOrUrl.IsUSVString() ||
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);
setAttribute(hrefAttr, stringOrUrl, exception_state);
}
} // namespace blink
......@@ -4,8 +4,11 @@
#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/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
namespace blink {
......@@ -26,6 +29,25 @@ TrustedURL* TrustedURL::unsafelyCreate(ScriptState* script_state,
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 {
return url_.GetString();
}
......
......@@ -13,7 +13,10 @@
namespace blink {
class Document;
class ExceptionState;
class ScriptState;
class USVStringOrTrustedURL;
class CORE_EXPORT TrustedURL final : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO();
......@@ -25,6 +28,9 @@ class CORE_EXPORT TrustedURL final : public ScriptWrappable {
String toString() const;
static TrustedURL* create(ScriptState*, const String& url);
static TrustedURL* unsafelyCreate(ScriptState*, const String& url);
static String GetString(USVStringOrTrustedURL,
const Document*,
ExceptionState&);
private:
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