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 "BroadcastChannelParent.h"
8 #include "BroadcastChannelService.h"
9 #include "mozilla/dom/File.h"
10 #include "mozilla/dom/IPCBlobUtils.h"
11 #include "mozilla/ipc/BackgroundParent.h"
12 #include "mozilla/ipc/IPCStreamUtils.h"
13 #include "mozilla/Unused.h"
14 
15 namespace mozilla {
16 
17 using namespace ipc;
18 
19 namespace dom {
20 
BroadcastChannelParent(const nsAString & aOriginChannelKey)21 BroadcastChannelParent::BroadcastChannelParent(
22     const nsAString& aOriginChannelKey)
23     : mService(BroadcastChannelService::GetOrCreate()),
24       mOriginChannelKey(aOriginChannelKey) {
25   AssertIsOnBackgroundThread();
26   mService->RegisterActor(this, mOriginChannelKey);
27 }
28 
~BroadcastChannelParent()29 BroadcastChannelParent::~BroadcastChannelParent() {
30   AssertIsOnBackgroundThread();
31 }
32 
RecvPostMessage(const MessageData & aData)33 mozilla::ipc::IPCResult BroadcastChannelParent::RecvPostMessage(
34     const MessageData& aData) {
35   AssertIsOnBackgroundThread();
36 
37   if (NS_WARN_IF(!mService)) {
38     return IPC_FAIL_NO_REASON(this);
39   }
40 
41   mService->PostMessage(this, aData, mOriginChannelKey);
42   return IPC_OK();
43 }
44 
RecvClose()45 mozilla::ipc::IPCResult BroadcastChannelParent::RecvClose() {
46   AssertIsOnBackgroundThread();
47 
48   if (NS_WARN_IF(!mService)) {
49     return IPC_FAIL_NO_REASON(this);
50   }
51 
52   mService->UnregisterActor(this, mOriginChannelKey);
53   mService = nullptr;
54 
55   Unused << Send__delete__(this);
56 
57   return IPC_OK();
58 }
59 
ActorDestroy(ActorDestroyReason aWhy)60 void BroadcastChannelParent::ActorDestroy(ActorDestroyReason aWhy) {
61   AssertIsOnBackgroundThread();
62 
63   if (mService) {
64     // This object is about to be released and with it, also mService will be
65     // released too.
66     mService->UnregisterActor(this, mOriginChannelKey);
67   }
68 }
69 
70 }  // namespace dom
71 }  // namespace mozilla
72