Line data Source code
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 : #include "WorkerPrivate.h"
19 :
20 : namespace mozilla {
21 :
22 : using namespace ipc;
23 :
24 : namespace dom {
25 :
26 : using namespace workers;
27 :
28 0 : BroadcastChannelChild::BroadcastChannelChild(const nsACString& aOrigin)
29 : : mBC(nullptr)
30 0 : , mActorDestroyed(false)
31 : {
32 0 : CopyUTF8toUTF16(aOrigin, mOrigin);
33 0 : }
34 :
35 0 : BroadcastChannelChild::~BroadcastChannelChild()
36 : {
37 0 : MOZ_ASSERT(!mBC);
38 0 : }
39 :
40 : mozilla::ipc::IPCResult
41 0 : BroadcastChannelChild::RecvNotify(const ClonedMessageData& aData)
42 : {
43 : // Make sure to retrieve all blobs from the message before returning to avoid
44 : // leaking their actors.
45 0 : ipc::StructuredCloneDataNoTransfers cloneData;
46 0 : cloneData.BorrowFromClonedMessageDataForBackgroundChild(aData);
47 :
48 0 : nsCOMPtr<DOMEventTargetHelper> helper = mBC;
49 0 : nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(helper);
50 :
51 : // The object is going to be deleted soon. No notify is required.
52 0 : if (!eventTarget) {
53 0 : return IPC_OK();
54 : }
55 :
56 : // CheckInnerWindowCorrectness can be used also without a window when
57 : // BroadcastChannel is running in a worker. In this case, it's a NOP.
58 0 : if (NS_FAILED(mBC->CheckInnerWindowCorrectness())) {
59 0 : return IPC_OK();
60 : }
61 :
62 0 : mBC->RemoveDocFromBFCache();
63 :
64 0 : AutoJSAPI jsapi;
65 0 : nsCOMPtr<nsIGlobalObject> globalObject;
66 :
67 0 : if (NS_IsMainThread()) {
68 0 : globalObject = do_QueryInterface(mBC->GetParentObject());
69 : } else {
70 0 : WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
71 0 : MOZ_ASSERT(workerPrivate);
72 0 : globalObject = workerPrivate->GlobalScope();
73 : }
74 :
75 0 : if (!globalObject || !jsapi.Init(globalObject)) {
76 0 : NS_WARNING("Failed to initialize AutoJSAPI object.");
77 0 : return IPC_OK();
78 : }
79 :
80 0 : JSContext* cx = jsapi.cx();
81 0 : JS::Rooted<JS::Value> value(cx, JS::NullValue());
82 0 : if (cloneData.DataLength()) {
83 0 : ErrorResult rv;
84 0 : cloneData.Read(cx, &value, rv);
85 0 : if (NS_WARN_IF(rv.Failed())) {
86 0 : rv.SuppressException();
87 0 : return IPC_OK();
88 : }
89 : }
90 :
91 0 : RootedDictionary<MessageEventInit> init(cx);
92 0 : init.mBubbles = false;
93 0 : init.mCancelable = false;
94 0 : init.mOrigin = mOrigin;
95 0 : init.mData = value;
96 :
97 : RefPtr<MessageEvent> event =
98 0 : MessageEvent::Constructor(mBC, NS_LITERAL_STRING("message"), init);
99 :
100 0 : event->SetTrusted(true);
101 :
102 : bool status;
103 0 : mBC->DispatchEvent(static_cast<Event*>(event.get()), &status);
104 :
105 0 : return IPC_OK();
106 : }
107 :
108 : void
109 0 : BroadcastChannelChild::ActorDestroy(ActorDestroyReason aWhy)
110 : {
111 0 : mActorDestroyed = true;
112 0 : }
113 :
114 : } // namespace dom
115 : } // namespace mozilla
|