summaryrefslogtreecommitdiffstats
path: root/dom/workers/MessageEventRunnable.cpp
blob: 9ead17b4707fffc8e353f2772675dc30d4e4310c (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "MessageEventRunnable.h"

#include "mozilla/dom/MessageEvent.h"
#include "mozilla/dom/MessageEventBinding.h"
#include "mozilla/dom/RootedDictionary.h"
#include "mozilla/TimelineConsumers.h"
#include "mozilla/WorkerTimelineMarker.h"
#include "nsQueryObject.h"
#include "WorkerScope.h"

namespace mozilla::dom {

MessageEventRunnable::MessageEventRunnable(WorkerPrivate* aWorkerPrivate,
                                           TargetAndBusyBehavior aBehavior)
    : WorkerDebuggeeRunnable(aWorkerPrivate, aBehavior),
      StructuredCloneHolder(CloningSupported, TransferringSupported,
                            StructuredCloneScope::SameProcess) {}

bool MessageEventRunnable::DispatchDOMEvent(JSContext* aCx,
                                            WorkerPrivate* aWorkerPrivate,
                                            DOMEventTargetHelper* aTarget,
                                            bool aIsMainThread) {
  nsCOMPtr<nsIGlobalObject> parent = aTarget->GetParentObject();

  // For some workers without window, parent is null and we try to find it
  // from the JS Context.
  if (!parent) {
    JS::Rooted<JSObject*> globalObject(aCx, JS::CurrentGlobalOrNull(aCx));
    if (NS_WARN_IF(!globalObject)) {
      return false;
    }

    parent = xpc::NativeGlobal(globalObject);
    if (NS_WARN_IF(!parent)) {
      return false;
    }
  }

  MOZ_ASSERT(parent);

  JS::Rooted<JS::Value> messageData(aCx);
  IgnoredErrorResult rv;

  UniquePtr<AbstractTimelineMarker> start;
  UniquePtr<AbstractTimelineMarker> end;
  bool isTimelineRecording = !TimelineConsumers::IsEmpty();

  if (isTimelineRecording) {
    start = MakeUnique<WorkerTimelineMarker>(
        aIsMainThread
            ? ProfileTimelineWorkerOperationType::DeserializeDataOnMainThread
            : ProfileTimelineWorkerOperationType::DeserializeDataOffMainThread,
        MarkerTracingType::START);
  }

  JS::CloneDataPolicy cloneDataPolicy;
  if (parent->GetClientInfo().isSome() &&
      parent->GetClientInfo()->AgentClusterId().isSome() &&
      parent->GetClientInfo()->AgentClusterId()->Equals(
          aWorkerPrivate->AgentClusterId())) {
    cloneDataPolicy.allowIntraClusterClonableSharedObjects();
  }

  if (aWorkerPrivate->IsSharedMemoryAllowed()) {
    cloneDataPolicy.allowSharedMemoryObjects();
  }

  Read(parent, aCx, &messageData, cloneDataPolicy, rv);

  if (isTimelineRecording) {
    end = MakeUnique<WorkerTimelineMarker>(
        aIsMainThread
            ? ProfileTimelineWorkerOperationType::DeserializeDataOnMainThread
            : ProfileTimelineWorkerOperationType::DeserializeDataOffMainThread,
        MarkerTracingType::END);
    TimelineConsumers::AddMarkerForAllObservedDocShells(start);
    TimelineConsumers::AddMarkerForAllObservedDocShells(end);
  }

  if (NS_WARN_IF(rv.Failed())) {
    DispatchError(aCx, aTarget);
    return false;
  }

  Sequence<OwningNonNull<MessagePort>> ports;
  if (!TakeTransferredPortsAsSequence(ports)) {
    DispatchError(aCx, aTarget);
    return false;
  }

  RefPtr<MessageEvent> event = new MessageEvent(aTarget, nullptr, nullptr);
  event->InitMessageEvent(nullptr, u"message"_ns, CanBubble::eNo,
                          Cancelable::eNo, messageData, u""_ns, u""_ns, nullptr,
                          ports);

  event->SetTrusted(true);

  aTarget->DispatchEvent(*event);

  return true;
}

bool MessageEventRunnable::WorkerRun(JSContext* aCx,
                                     WorkerPrivate* aWorkerPrivate) {
  if (mBehavior == ParentThreadUnchangedBusyCount) {
    // Don't fire this event if the JS object has been disconnected from the
    // private object.
    if (!aWorkerPrivate->IsAcceptingEvents()) {
      return true;
    }

    // Once a window has frozen its workers, their
    // mMainThreadDebuggeeEventTargets should be paused, and their
    // WorkerDebuggeeRunnables should not be being executed. The same goes for
    // WorkerDebuggeeRunnables sent from child to parent workers, but since a
    // frozen parent worker runs only control runnables anyway, that is taken
    // care of naturally.
    MOZ_ASSERT(!aWorkerPrivate->IsFrozen());

    // Similarly for paused windows; all its workers should have been informed.
    // (Subworkers are unaffected by paused windows.)
    MOZ_ASSERT(!aWorkerPrivate->IsParentWindowPaused());

    aWorkerPrivate->AssertInnerWindowIsCorrect();

    return DispatchDOMEvent(aCx, aWorkerPrivate,
                            aWorkerPrivate->ParentEventTargetRef(),
                            !aWorkerPrivate->GetParent());
  }

  MOZ_ASSERT(aWorkerPrivate == GetWorkerPrivateFromContext(aCx));

  return DispatchDOMEvent(aCx, aWorkerPrivate, aWorkerPrivate->GlobalScope(),
                          false);
}

void MessageEventRunnable::DispatchError(JSContext* aCx,
                                         DOMEventTargetHelper* aTarget) {
  RootedDictionary<MessageEventInit> init(aCx);
  init.mBubbles = false;
  init.mCancelable = false;

  RefPtr<Event> event =
      MessageEvent::Constructor(aTarget, u"messageerror"_ns, init);
  event->SetTrusted(true);

  aTarget->DispatchEvent(*event);
}

}  // namespace mozilla::dom