Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/broadcastchannel/BroadcastChannelChild.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "BroadcastChannelChild.h"
8
#include "BroadcastChannel.h"
9
#include "jsapi.h"
10
#include "mozilla/dom/File.h"
11
#include "mozilla/dom/MessageEvent.h"
12
#include "mozilla/dom/MessageEventBinding.h"
13
#include "mozilla/dom/WorkerPrivate.h"
14
#include "mozilla/dom/WorkerScope.h"
15
#include "mozilla/dom/ScriptSettings.h"
16
#include "mozilla/ipc/PBackgroundChild.h"
17
#include "mozilla/dom/ipc/StructuredCloneData.h"
18
19
namespace mozilla {
20
21
using namespace ipc;
22
23
namespace dom {
24
25
BroadcastChannelChild::BroadcastChannelChild(const nsACString& aOrigin)
26
  : mBC(nullptr)
27
  , mActorDestroyed(false)
28
0
{
29
0
  CopyUTF8toUTF16(aOrigin, mOrigin);
30
0
}
31
32
BroadcastChannelChild::~BroadcastChannelChild()
33
0
{
34
0
  MOZ_ASSERT(!mBC);
35
0
}
36
37
mozilla::ipc::IPCResult
38
BroadcastChannelChild::RecvNotify(const ClonedMessageData& aData)
39
0
{
40
0
  // Make sure to retrieve all blobs from the message before returning to avoid
41
0
  // leaking their actors.
42
0
  ipc::StructuredCloneDataNoTransfers cloneData;
43
0
  cloneData.BorrowFromClonedMessageDataForBackgroundChild(aData);
44
0
45
0
  nsCOMPtr<DOMEventTargetHelper> helper = mBC;
46
0
  nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(helper);
47
0
48
0
  // The object is going to be deleted soon. No notify is required.
49
0
  if (!eventTarget) {
50
0
    return IPC_OK();
51
0
  }
52
0
53
0
  // CheckInnerWindowCorrectness can be used also without a window when
54
0
  // BroadcastChannel is running in a worker. In this case, it's a NOP.
55
0
  if (NS_FAILED(mBC->CheckInnerWindowCorrectness())) {
56
0
    return IPC_OK();
57
0
  }
58
0
59
0
  mBC->RemoveDocFromBFCache();
60
0
61
0
  AutoJSAPI jsapi;
62
0
  nsCOMPtr<nsIGlobalObject> globalObject;
63
0
64
0
  if (NS_IsMainThread()) {
65
0
    globalObject = do_QueryInterface(mBC->GetParentObject());
66
0
  } else {
67
0
    WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
68
0
    MOZ_ASSERT(workerPrivate);
69
0
    globalObject = workerPrivate->GlobalScope();
70
0
  }
71
0
72
0
  if (!globalObject || !jsapi.Init(globalObject)) {
73
0
    NS_WARNING("Failed to initialize AutoJSAPI object.");
74
0
    return IPC_OK();
75
0
  }
76
0
77
0
  JSContext* cx = jsapi.cx();
78
0
  JS::Rooted<JS::Value> value(cx, JS::NullValue());
79
0
  if (cloneData.DataLength()) {
80
0
    IgnoredErrorResult rv;
81
0
    cloneData.Read(cx, &value, rv);
82
0
    if (NS_WARN_IF(rv.Failed())) {
83
0
      DispatchError(cx);
84
0
      return IPC_OK();
85
0
    }
86
0
  }
87
0
88
0
  RootedDictionary<MessageEventInit> init(cx);
89
0
  init.mBubbles = false;
90
0
  init.mCancelable = false;
91
0
  init.mOrigin = mOrigin;
92
0
  init.mData = value;
93
0
94
0
  RefPtr<MessageEvent> event =
95
0
    MessageEvent::Constructor(mBC, NS_LITERAL_STRING("message"), init);
96
0
97
0
  event->SetTrusted(true);
98
0
99
0
  mBC->DispatchEvent(*event);
100
0
101
0
  return IPC_OK();
102
0
}
103
104
void
105
BroadcastChannelChild::ActorDestroy(ActorDestroyReason aWhy)
106
0
{
107
0
  mActorDestroyed = true;
108
0
}
109
110
void
111
BroadcastChannelChild::DispatchError(JSContext* aCx)
112
0
{
113
0
  RootedDictionary<MessageEventInit> init(aCx);
114
0
  init.mBubbles = false;
115
0
  init.mCancelable = false;
116
0
  init.mOrigin = mOrigin;
117
0
118
0
  RefPtr<Event> event =
119
0
    MessageEvent::Constructor(mBC, NS_LITERAL_STRING("messageerror"), init);
120
0
  event->SetTrusted(true);
121
0
122
0
  mBC->DispatchEvent(*event);
123
0
}
124
125
} // namespace dom
126
} // namespace mozilla