summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/portal/portal_activate_event.cc
blob: 3aebb0c255efd0c6e8f0fc6f52458c5f26a37f1c (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// 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/core/html/portal/portal_activate_event.h"

#include <utility>
#include "third_party/blink/public/mojom/portal/portal.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_value.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_portal_activate_event_init.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/event_type_names.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/html/portal/html_portal_element.h"
#include "third_party/blink/renderer/core/messaging/message_port.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h"

namespace blink {

PortalActivateEvent* PortalActivateEvent::Create(
    LocalFrame* frame,
    const base::UnguessableToken& predecessor_portal_token,
    mojo::PendingAssociatedRemote<mojom::blink::Portal> predecessor_portal,
    mojo::PendingAssociatedReceiver<mojom::blink::PortalClient>
        predecessor_portal_client_receiver,
    scoped_refptr<SerializedScriptValue> data,
    MessagePortArray* ports,
    OnPortalActivatedCallback callback) {
  return MakeGarbageCollected<PortalActivateEvent>(
      frame->GetDocument(), predecessor_portal_token,
      std::move(predecessor_portal),
      std::move(predecessor_portal_client_receiver),
      SerializedScriptValue::Unpack(std::move(data)), ports,
      std::move(callback));
}

PortalActivateEvent* PortalActivateEvent::Create(
    const AtomicString& type,
    const PortalActivateEventInit* init) {
  return MakeGarbageCollected<PortalActivateEvent>(type, init);
}

PortalActivateEvent::PortalActivateEvent(
    Document* document,
    const base::UnguessableToken& predecessor_portal_token,
    mojo::PendingAssociatedRemote<mojom::blink::Portal> predecessor_portal,
    mojo::PendingAssociatedReceiver<mojom::blink::PortalClient>
        predecessor_portal_client_receiver,
    UnpackedSerializedScriptValue* data,
    MessagePortArray* ports,
    OnPortalActivatedCallback callback)
    : Event(event_type_names::kPortalactivate,
            Bubbles::kNo,
            Cancelable::kNo,
            base::TimeTicks::Now()),
      document_(document),
      predecessor_portal_token_(predecessor_portal_token),
      predecessor_portal_(std::move(predecessor_portal)),
      predecessor_portal_client_receiver_(
          std::move(predecessor_portal_client_receiver)),
      data_(data),
      ports_(ports),
      on_portal_activated_callback_(std::move(callback)) {}

PortalActivateEvent::PortalActivateEvent(const AtomicString& type,
                                         const PortalActivateEventInit* init)
    : Event(type, init) {
  if (init->hasData()) {
    data_from_init_.Set(V8PerIsolateData::MainThreadIsolate(),
                        init->data().V8Value());
  }

  // Remaining fields, such as |document_|, are left null.
  // All accessors and operations must handle this case.
}

PortalActivateEvent::~PortalActivateEvent() = default;

ScriptValue PortalActivateEvent::data(ScriptState* script_state) {
  v8::Isolate* isolate = script_state->GetIsolate();
  v8::HandleScope handle_scope(isolate);
  if (!data_ && data_from_init_.IsEmpty())
    return ScriptValue(isolate, v8::Null(isolate));

  auto result =
      v8_data_.insert(script_state, TraceWrapperV8Reference<v8::Value>());
  TraceWrapperV8Reference<v8::Value>& relevant_data =
      result.stored_value->value;

  if (!result.is_new_entry)
    return ScriptValue(isolate, relevant_data.NewLocal(isolate));

  v8::Local<v8::Value> value;
  if (data_) {
    SerializedScriptValue::DeserializeOptions options;
    options.message_ports = ports_.Get();
    value = data_->Deserialize(isolate, options);
  } else {
    DCHECK(!data_from_init_.IsEmpty());
    value = data_from_init_.GetAcrossWorld(script_state);
  }

  relevant_data.Set(isolate, value);
  return ScriptValue(isolate, value);
}

void PortalActivateEvent::Trace(Visitor* visitor) const {
  Event::Trace(visitor);
  visitor->Trace(document_);
  visitor->Trace(adopted_portal_);
  visitor->Trace(data_);
  visitor->Trace(ports_);
  visitor->Trace(data_from_init_);
  visitor->Trace(v8_data_);
}

const AtomicString& PortalActivateEvent::InterfaceName() const {
  return event_type_names::kPortalactivate;
}

HTMLPortalElement* PortalActivateEvent::adoptPredecessor(
    ExceptionState& exception_state) {
  if (!RuntimeEnabledFeatures::PortalsEnabled(
          document_ ? document_->GetExecutionContext() : nullptr)) {
    exception_state.ThrowDOMException(
        DOMExceptionCode::kNotSupportedError,
        "Portals is not enabled in this document.");
    return nullptr;
  }

  if (!predecessor_portal_) {
    exception_state.ThrowDOMException(
        DOMExceptionCode::kInvalidStateError,
        "The PortalActivateEvent is not associated with a predecessor browsing "
        "context");
    return nullptr;
  }

  DCHECK(!adopted_portal_);
  adopted_portal_ = MakeGarbageCollected<HTMLPortalElement>(
      *document_, predecessor_portal_token_, std::move(predecessor_portal_),
      std::move(predecessor_portal_client_receiver_));
  std::move(on_portal_activated_callback_)
      .Run(mojom::blink::PortalActivateResult::kPredecessorWasAdopted);
  return adopted_portal_;
}

void PortalActivateEvent::ExpireAdoptionLifetime() {
  // End the special privilege associated with any adopted portals.
  // This may destroy the guest contents.
  if (adopted_portal_) {
    adopted_portal_->ExpireAdoptionLifetime();

    // We no longer need to hold the adopted portal, so stop drop the GC
    // reference.
    adopted_portal_ = nullptr;
  }

  // End the special privilege associated with the predecessor contents if it
  // was not adopted. This may destroy the guest contents.
  if (predecessor_portal_) {
    std::move(on_portal_activated_callback_)
        .Run(mojom::blink::PortalActivateResult::kPredecessorWillUnload);
    predecessor_portal_.reset();
  }
}

}  // namespace blink