1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/presentation/presentation_request.h"
#include <memory>
#include "base/containers/to_vector.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/loader/mixed_content_checker.h"
#include "third_party/blink/renderer/modules/event_target_modules.h"
#include "third_party/blink/renderer/modules/presentation/presentation_availability_state.h"
#include "third_party/blink/renderer/modules/presentation/presentation_connection.h"
#include "third_party/blink/renderer/modules/presentation/presentation_connection_callbacks.h"
#include "third_party/blink/renderer/modules/presentation/presentation_controller.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
namespace blink {
namespace {
bool IsKnownProtocolForPresentationUrl(const KURL& url) {
return url.ProtocolIsInHTTPFamily() || url.ProtocolIs("cast") ||
url.ProtocolIs("cast-dial");
}
} // anonymous namespace
// static
PresentationRequest* PresentationRequest::Create(
ExecutionContext* execution_context,
const String& url,
ExceptionState& exception_state) {
Vector<String> urls(1);
urls[0] = url;
return Create(execution_context, urls, exception_state);
}
// static
PresentationRequest* PresentationRequest::Create(
ExecutionContext* execution_context,
const Vector<String>& urls,
ExceptionState& exception_state) {
if (execution_context->IsSandboxed(
network::mojom::blink::WebSandboxFlags::kPresentationController)) {
exception_state.ThrowSecurityError(
DynamicTo<LocalDOMWindow>(execution_context)
->GetFrame()
->IsInFencedFrameTree()
? "PresentationRequest is not supported in a fenced frame tree."
: "The document is sandboxed and lacks the 'allow-presentation' "
"flag.");
return nullptr;
}
Vector<KURL> parsed_urls;
for (const auto& url : urls) {
const KURL& parsed_url = KURL(execution_context->Url(), url);
if (!parsed_url.IsValid()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kSyntaxError,
"'" + url + "' can't be resolved to a valid URL.");
return nullptr;
}
if (parsed_url.ProtocolIsInHTTPFamily() &&
MixedContentChecker::IsMixedContent(
execution_context->GetSecurityOrigin(), parsed_url)) {
exception_state.ThrowSecurityError(
"Presentation of an insecure document [" + url +
"] is prohibited from a secure context.");
return nullptr;
}
if (IsKnownProtocolForPresentationUrl(parsed_url)) {
parsed_urls.push_back(parsed_url);
}
}
if (parsed_urls.empty()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kNotSupportedError,
"An empty sequence of URLs is not supported.");
return nullptr;
}
return MakeGarbageCollected<PresentationRequest>(execution_context,
parsed_urls);
}
const AtomicString& PresentationRequest::InterfaceName() const {
return event_target_names::kPresentationRequest;
}
ExecutionContext* PresentationRequest::GetExecutionContext() const {
return ExecutionContextClient::GetExecutionContext();
}
void PresentationRequest::AddedEventListener(
const AtomicString& event_type,
RegisteredEventListener& registered_listener) {
EventTarget::AddedEventListener(event_type, registered_listener);
if (event_type == event_type_names::kConnectionavailable) {
UseCounter::Count(
GetExecutionContext(),
WebFeature::kPresentationRequestConnectionAvailableEventListener);
}
}
bool PresentationRequest::HasPendingActivity() const {
// Prevents garbage collecting of this object when not hold by another
// object but still has listeners registered.
if (!GetExecutionContext()) {
return false;
}
return HasEventListeners();
}
ScriptPromise<PresentationConnection> PresentationRequest::start(
ScriptState* script_state,
ExceptionState& exception_state) {
if (!script_state->ContextIsValid()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kInvalidStateError,
"The PresentationRequest is no longer associated to a frame.");
return EmptyPromise();
}
LocalDOMWindow* window = LocalDOMWindow::From(script_state);
if (window->GetFrame()->GetSettings()->GetPresentationRequiresUserGesture() &&
!LocalFrame::HasTransientUserActivation(window->GetFrame())) {
exception_state.ThrowDOMException(
DOMExceptionCode::kInvalidAccessError,
"PresentationRequest::start() requires user gesture.");
return EmptyPromise();
}
PresentationController* controller = PresentationController::From(*window);
auto* resolver =
MakeGarbageCollected<ScriptPromiseResolver<PresentationConnection>>(
script_state, exception_state.GetContext());
controller->GetPresentationService()->StartPresentation(
urls_,
WTF::BindOnce(
&PresentationConnectionCallbacks::HandlePresentationResponse,
std::make_unique<PresentationConnectionCallbacks>(resolver, this)));
return resolver->Promise();
}
ScriptPromise<PresentationConnection> PresentationRequest::reconnect(
ScriptState* script_state,
const String& id,
ExceptionState& exception_state) {
PresentationController* controller =
PresentationController::FromContext(GetExecutionContext());
if (!controller) {
exception_state.ThrowDOMException(
DOMExceptionCode::kInvalidStateError,
"The PresentationRequest is no longer associated to a frame.");
return EmptyPromise();
}
auto* resolver =
MakeGarbageCollected<ScriptPromiseResolver<PresentationConnection>>(
script_state, exception_state.GetContext());
ControllerPresentationConnection* existing_connection =
controller->FindExistingConnection(base::ToVector(urls_, ToWebURL), id);
if (existing_connection) {
controller->GetPresentationService()->ReconnectPresentation(
urls_, id,
WTF::BindOnce(
&PresentationConnectionCallbacks::HandlePresentationResponse,
std::make_unique<PresentationConnectionCallbacks>(
resolver, existing_connection)));
} else {
controller->GetPresentationService()->ReconnectPresentation(
urls_, id,
WTF::BindOnce(
&PresentationConnectionCallbacks::HandlePresentationResponse,
std::make_unique<PresentationConnectionCallbacks>(resolver, this)));
}
return resolver->Promise();
}
ScriptPromise<PresentationAvailability> PresentationRequest::getAvailability(
ScriptState* script_state,
ExceptionState& exception_state) {
PresentationController* controller =
PresentationController::FromContext(GetExecutionContext());
if (!controller) {
exception_state.ThrowDOMException(
DOMExceptionCode::kInvalidStateError,
"The PresentationRequest is no longer associated to a frame.");
return EmptyPromise();
}
auto* resolver =
MakeGarbageCollected<ScriptPromiseResolver<PresentationAvailability>>(
script_state, exception_state.GetContext());
auto screen_availability =
controller->GetAvailabilityState()->GetScreenAvailability(urls_);
// Reject Promise if screen availability is unsupported for all URLs.
if (screen_availability == mojom::blink::ScreenAvailability::DISABLED) {
resolver->RejectWithDOMException(
DOMExceptionCode::kNotSupportedError,
PresentationAvailability::kNotSupportedErrorInfo);
return resolver->Promise();
}
// Create availability object the first time getAvailability() is called.
if (!availability_) {
availability_ = PresentationAvailability::Take(
resolver->GetExecutionContext(), urls_,
screen_availability == mojom::blink::ScreenAvailability::AVAILABLE);
}
if (screen_availability != mojom::blink::ScreenAvailability::UNKNOWN) {
// Resolve Promise with availability object if screen availability is known.
resolver->Resolve(availability_);
} else {
// Start request for screen availability if it is unknown.
controller->GetAvailabilityState()->RequestAvailability(availability_);
availability_->AddResolver(resolver);
}
return resolver->Promise();
}
const Vector<KURL>& PresentationRequest::Urls() const {
return urls_;
}
void PresentationRequest::Trace(Visitor* visitor) const {
EventTarget::Trace(visitor);
ExecutionContextClient::Trace(visitor);
visitor->Trace(availability_);
}
PresentationRequest::PresentationRequest(ExecutionContext* execution_context,
const Vector<KURL>& urls)
: ActiveScriptWrappable<PresentationRequest>({}),
ExecutionContextClient(execution_context),
urls_(urls) {}
} // namespace blink
|