1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_SyncedContextInlines_h
#define mozilla_dom_SyncedContextInlines_h
#include "mozilla/dom/SyncedContext.h"
#include "mozilla/dom/BrowsingContextGroup.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/ContentChild.h"
#include "nsReadableUtils.h"
namespace mozilla {
namespace dom {
namespace syncedcontext {
template <typename Context>
nsCString FormatValidationError(IndexSet aFailedFields, const char* prefix) {
MOZ_ASSERT(!aFailedFields.isEmpty());
return nsDependentCString{prefix} +
StringJoin(", "_ns, aFailedFields,
[](nsACString& dest, const auto& idx) {
dest.Append(Context::FieldIndexToName(idx));
});
}
template <typename Context>
nsresult Transaction<Context>::Commit(Context* aOwner) {
if (NS_WARN_IF(aOwner->IsDiscarded())) {
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
IndexSet failedFields = Validate(aOwner, nullptr);
if (!failedFields.isEmpty()) {
nsCString error = FormatValidationError<Context>(
failedFields, "CanSet failed for field(s): ");
MOZ_CRASH_UNSAFE_PRINTF("%s", error.get());
}
if (XRE_IsContentProcess()) {
ContentChild* cc = ContentChild::GetSingleton();
// Increment the field epoch for fields affected by this transaction.
uint64_t epoch = cc->NextBrowsingContextFieldEpoch();
EachIndex([&](auto idx) {
if (mModified.contains(idx)) {
FieldEpoch(idx, aOwner) = epoch;
}
});
// Tell our derived class to send the correct "Commit" IPC message.
aOwner->SendCommitTransaction(cc, *this, epoch);
} else {
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
// Tell our derived class to send the correct "Commit" IPC messages.
BrowsingContextGroup* group = aOwner->Group();
group->EachParent([&](ContentParent* aParent) {
aOwner->SendCommitTransaction(aParent, *this,
aParent->GetBrowsingContextFieldEpoch());
});
}
Apply(aOwner);
return NS_OK;
}
template <typename Context>
mozilla::ipc::IPCResult Transaction<Context>::CommitFromIPC(
const MaybeDiscarded<Context>& aOwner, ContentParent* aSource) {
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
if (aOwner.IsNullOrDiscarded()) {
MOZ_LOG(Context::GetLog(), LogLevel::Debug,
("IPC: Trying to send a message to dead or detached context"));
return IPC_OK();
}
Context* owner = aOwner.get();
// Validate that the set from content is allowed before continuing.
IndexSet failedFields = Validate(owner, aSource);
if (!failedFields.isEmpty()) {
nsCString error = FormatValidationError<Context>(
failedFields,
"Invalid Transaction from Child - CanSet failed for field(s): ");
return IPC_FAIL(aSource, error.get());
}
BrowsingContextGroup* group = owner->Group();
group->EachOtherParent(aSource, [&](ContentParent* aParent) {
owner->SendCommitTransaction(aParent, *this,
aParent->GetBrowsingContextFieldEpoch());
});
Apply(owner);
return IPC_OK();
}
template <typename Context>
mozilla::ipc::IPCResult Transaction<Context>::CommitFromIPC(
const MaybeDiscarded<Context>& aOwner, uint64_t aEpoch,
ContentChild* aSource) {
MOZ_DIAGNOSTIC_ASSERT(XRE_IsContentProcess());
if (aOwner.IsNullOrDiscarded()) {
MOZ_LOG(Context::GetLog(), LogLevel::Debug,
("ChildIPC: Trying to send a message to dead or detached context"));
return IPC_OK();
}
Context* owner = aOwner.get();
// Clear any fields which have been obsoleted by the epoch.
EachIndex([&](auto idx) {
if (mModified.contains(idx) && FieldEpoch(idx, owner) > aEpoch) {
mModified -= idx;
}
});
Apply(owner);
return IPC_OK();
}
template <typename Context>
void Transaction<Context>::Apply(Context* aOwner) {
EachIndex([&](auto idx) {
if (mModified.contains(idx)) {
auto& txnField = mValues.Get(idx);
auto& ownerField = aOwner->mFields.mValues.Get(idx);
std::swap(ownerField, txnField);
aOwner->DidSet(idx);
aOwner->DidSet(idx, std::move(txnField));
}
});
mModified.clear();
}
template <typename Context>
IndexSet Transaction<Context>::Validate(Context* aOwner,
ContentParent* aSource) {
IndexSet failedFields;
// Validate that the set from content is allowed before continuing.
EachIndex([&](auto idx) {
if (mModified.contains(idx) &&
NS_WARN_IF(!aOwner->CanSet(idx, mValues.Get(idx), aSource))) {
failedFields += idx;
}
});
return failedFields;
}
template <typename Context>
void Transaction<Context>::Write(IPC::Message* aMsg,
mozilla::ipc::IProtocol* aActor) const {
// Record which field indices will be included, and then write those fields
// out.
uint64_t modified = mModified.serialize();
WriteIPDLParam(aMsg, aActor, modified);
EachIndex([&](auto idx) {
if (mModified.contains(idx)) {
WriteIPDLParam(aMsg, aActor, mValues.Get(idx));
}
});
}
template <typename Context>
bool Transaction<Context>::Read(const IPC::Message* aMsg, PickleIterator* aIter,
mozilla::ipc::IProtocol* aActor) {
// Read in which field indices were sent by the remote, followed by the fields
// identified by those indices.
uint64_t modified = 0;
if (!ReadIPDLParam(aMsg, aIter, aActor, &modified)) {
return false;
}
mModified.deserialize(modified);
bool ok = true;
EachIndex([&](auto idx) {
if (ok && mModified.contains(idx)) {
ok = ReadIPDLParam(aMsg, aIter, aActor, &mValues.Get(idx));
}
});
return ok;
}
template <typename Base, size_t Count>
void FieldValues<Base, Count>::Write(IPC::Message* aMsg,
mozilla::ipc::IProtocol* aActor) const {
// XXX The this-> qualification is necessary to work around a bug in older gcc
// versions causing an ICE.
EachIndex([&](auto idx) { WriteIPDLParam(aMsg, aActor, this->Get(idx)); });
}
template <typename Base, size_t Count>
bool FieldValues<Base, Count>::Read(const IPC::Message* aMsg,
PickleIterator* aIter,
mozilla::ipc::IProtocol* aActor) {
bool ok = true;
EachIndex([&](auto idx) {
if (ok) {
// XXX The this-> qualification is necessary to work around a bug in older
// gcc versions causing an ICE.
ok = ReadIPDLParam(aMsg, aIter, aActor, &this->Get(idx));
}
});
return ok;
}
} // namespace syncedcontext
} // namespace dom
} // namespace mozilla
#endif // !defined(mozilla_dom_SyncedContextInlines_h)
|