1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "third_party/blink/renderer/modules/presentation/presentation_connection_callbacks.h"
6
7 #include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
8 #include "third_party/blink/renderer/core/dom/dom_exception.h"
9 #include "third_party/blink/renderer/modules/presentation/presentation_connection.h"
10 #include "third_party/blink/renderer/modules/presentation/presentation_error.h"
11 #include "third_party/blink/renderer/modules/presentation/presentation_request.h"
12
13 namespace blink {
14
PresentationConnectionCallbacks(ScriptPromiseResolver * resolver,PresentationRequest * request)15 PresentationConnectionCallbacks::PresentationConnectionCallbacks(
16 ScriptPromiseResolver* resolver,
17 PresentationRequest* request)
18 : resolver_(resolver), request_(request), connection_(nullptr) {
19 DCHECK(resolver_);
20 DCHECK(request_);
21 }
22
PresentationConnectionCallbacks(ScriptPromiseResolver * resolver,ControllerPresentationConnection * connection)23 PresentationConnectionCallbacks::PresentationConnectionCallbacks(
24 ScriptPromiseResolver* resolver,
25 ControllerPresentationConnection* connection)
26 : resolver_(resolver), request_(nullptr), connection_(connection) {
27 DCHECK(resolver_);
28 DCHECK(connection_);
29 }
30
HandlePresentationResponse(mojom::blink::PresentationConnectionResultPtr result,mojom::blink::PresentationErrorPtr error)31 void PresentationConnectionCallbacks::HandlePresentationResponse(
32 mojom::blink::PresentationConnectionResultPtr result,
33 mojom::blink::PresentationErrorPtr error) {
34 if (!resolver_->GetExecutionContext() ||
35 resolver_->GetExecutionContext()->IsContextDestroyed()) {
36 return;
37 }
38
39 if (result) {
40 DCHECK(result->connection_remote);
41 DCHECK(result->connection_receiver);
42 OnSuccess(*result->presentation_info, std::move(result->connection_remote),
43 std::move(result->connection_receiver));
44 } else {
45 OnError(*error);
46 }
47 }
48
OnSuccess(const mojom::blink::PresentationInfo & presentation_info,mojo::PendingRemote<mojom::blink::PresentationConnection> connection_remote,mojo::PendingReceiver<mojom::blink::PresentationConnection> connection_receiver)49 void PresentationConnectionCallbacks::OnSuccess(
50 const mojom::blink::PresentationInfo& presentation_info,
51 mojo::PendingRemote<mojom::blink::PresentationConnection> connection_remote,
52 mojo::PendingReceiver<mojom::blink::PresentationConnection>
53 connection_receiver) {
54 // Reconnect to existing connection.
55 if (connection_ && connection_->GetState() ==
56 mojom::blink::PresentationConnectionState::CLOSED) {
57 connection_->DidChangeState(
58 mojom::blink::PresentationConnectionState::CONNECTING);
59 }
60
61 // Create a new connection.
62 if (!connection_ && request_) {
63 connection_ = ControllerPresentationConnection::Take(
64 resolver_.Get(), presentation_info, request_);
65 }
66
67 connection_->Init(std::move(connection_remote),
68 std::move(connection_receiver));
69
70 resolver_->Resolve(connection_);
71 }
72
OnError(const mojom::blink::PresentationError & error)73 void PresentationConnectionCallbacks::OnError(
74 const mojom::blink::PresentationError& error) {
75 resolver_->Reject(CreatePresentationError(error));
76 connection_ = nullptr;
77 }
78
79 } // namespace blink
80