Source code

Revision control

Other Tools

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 "SharedMessagePortMessage.h"
8
#include "MessagePort.h"
9
#include "MessagePortChild.h"
10
#include "MessagePortParent.h"
11
#include "mozilla/dom/File.h"
12
#include "mozilla/dom/PMessagePort.h"
13
#include "mozilla/ipc/BackgroundChild.h"
14
#include "mozilla/ipc/BackgroundParent.h"
15
16
namespace mozilla {
17
18
using namespace ipc;
19
20
namespace dom {
21
22
/* static */
23
void SharedMessagePortMessage::FromSharedToMessagesChild(
24
MessagePortChild* aActor,
25
const nsTArray<RefPtr<SharedMessagePortMessage>>& aData,
26
nsTArray<ClonedMessageData>& aArray) {
27
MOZ_ASSERT(aActor);
28
MOZ_ASSERT(aArray.IsEmpty());
29
aArray.SetCapacity(aData.Length());
30
31
PBackgroundChild* backgroundManager = aActor->Manager();
32
MOZ_ASSERT(backgroundManager);
33
34
for (auto& data : aData) {
35
ClonedMessageData* message = aArray.AppendElement();
36
data->BuildClonedMessageDataForBackgroundChild(backgroundManager, *message);
37
}
38
}
39
40
/* static */
41
bool SharedMessagePortMessage::FromMessagesToSharedChild(
42
nsTArray<ClonedMessageData>& aArray,
43
FallibleTArray<RefPtr<SharedMessagePortMessage>>& aData) {
44
MOZ_ASSERT(aData.IsEmpty());
45
46
if (NS_WARN_IF(!aData.SetCapacity(aArray.Length(), mozilla::fallible))) {
47
return false;
48
}
49
50
for (auto& message : aArray) {
51
RefPtr<SharedMessagePortMessage> data = new SharedMessagePortMessage();
52
data->StealFromClonedMessageDataForBackgroundChild(message);
53
54
if (!aData.AppendElement(data, mozilla::fallible)) {
55
return false;
56
}
57
}
58
59
return true;
60
}
61
62
/* static */
63
bool SharedMessagePortMessage::FromSharedToMessagesParent(
64
MessagePortParent* aActor,
65
const nsTArray<RefPtr<SharedMessagePortMessage>>& aData,
66
FallibleTArray<ClonedMessageData>& aArray) {
67
MOZ_ASSERT(aArray.IsEmpty());
68
69
if (NS_WARN_IF(!aArray.SetCapacity(aData.Length(), mozilla::fallible))) {
70
return false;
71
}
72
73
PBackgroundParent* backgroundManager = aActor->Manager();
74
MOZ_ASSERT(backgroundManager);
75
76
for (auto& data : aData) {
77
ClonedMessageData* message = aArray.AppendElement(mozilla::fallible);
78
data->BuildClonedMessageDataForBackgroundParent(backgroundManager,
79
*message);
80
}
81
82
return true;
83
}
84
85
/* static */
86
bool SharedMessagePortMessage::FromMessagesToSharedParent(
87
nsTArray<ClonedMessageData>& aArray,
88
FallibleTArray<RefPtr<SharedMessagePortMessage>>& aData) {
89
MOZ_ASSERT(aData.IsEmpty());
90
91
if (NS_WARN_IF(!aData.SetCapacity(aArray.Length(), mozilla::fallible))) {
92
return false;
93
}
94
95
for (auto& message : aArray) {
96
RefPtr<SharedMessagePortMessage> data = new SharedMessagePortMessage();
97
data->StealFromClonedMessageDataForBackgroundParent(message);
98
99
if (!aData.AppendElement(data, mozilla::fallible)) {
100
return false;
101
}
102
}
103
104
return true;
105
}
106
107
} // namespace dom
108
} // namespace mozilla