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 "MessageChannel.h"
8 :
9 : #include "mozilla/dom/MessageChannelBinding.h"
10 : #include "mozilla/dom/MessagePort.h"
11 : #include "mozilla/dom/Navigator.h"
12 : #include "mozilla/dom/WorkerPrivate.h"
13 : #include "mozilla/dom/WorkerRunnable.h"
14 : #include "nsContentUtils.h"
15 : #include "nsIDocument.h"
16 : #include "nsIGlobalObject.h"
17 : #include "nsIPrincipal.h"
18 : #include "nsServiceManagerUtils.h"
19 :
20 : namespace mozilla {
21 : namespace dom {
22 :
23 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MessageChannel, mGlobal, mPort1, mPort2)
24 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(MessageChannel)
25 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(MessageChannel)
26 :
27 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MessageChannel)
28 0 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
29 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
30 0 : NS_INTERFACE_MAP_END
31 :
32 0 : MessageChannel::MessageChannel(nsIGlobalObject* aGlobal)
33 0 : : mGlobal(aGlobal)
34 : {
35 0 : MOZ_ASSERT(aGlobal);
36 0 : }
37 :
38 0 : MessageChannel::~MessageChannel()
39 : {
40 0 : }
41 :
42 : JSObject*
43 0 : MessageChannel::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
44 : {
45 0 : return MessageChannelBinding::Wrap(aCx, this, aGivenProto);
46 : }
47 :
48 : /* static */ already_AddRefed<MessageChannel>
49 0 : MessageChannel::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
50 : {
51 0 : nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
52 0 : return Constructor(global, aRv);
53 : }
54 :
55 : /* static */ already_AddRefed<MessageChannel>
56 0 : MessageChannel::Constructor(nsIGlobalObject* aGlobal, ErrorResult& aRv)
57 : {
58 0 : MOZ_ASSERT(aGlobal);
59 :
60 : nsID portUUID1;
61 0 : aRv = nsContentUtils::GenerateUUIDInPlace(portUUID1);
62 0 : if (aRv.Failed()) {
63 0 : return nullptr;
64 : }
65 :
66 : nsID portUUID2;
67 0 : aRv = nsContentUtils::GenerateUUIDInPlace(portUUID2);
68 0 : if (aRv.Failed()) {
69 0 : return nullptr;
70 : }
71 :
72 0 : RefPtr<MessageChannel> channel = new MessageChannel(aGlobal);
73 :
74 0 : channel->mPort1 = MessagePort::Create(aGlobal, portUUID1, portUUID2, aRv);
75 0 : if (NS_WARN_IF(aRv.Failed())) {
76 0 : return nullptr;
77 : }
78 :
79 0 : channel->mPort2 = MessagePort::Create(aGlobal, portUUID2, portUUID1, aRv);
80 0 : if (NS_WARN_IF(aRv.Failed())) {
81 0 : return nullptr;
82 : }
83 :
84 0 : channel->mPort1->UnshippedEntangle(channel->mPort2);
85 0 : channel->mPort2->UnshippedEntangle(channel->mPort1);
86 :
87 0 : return channel.forget();
88 : }
89 :
90 : } // namespace dom
91 : } // namespace mozilla
|