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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/* -*- 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"
#include "mozilla/HalIPCUtils.h"
namespace mozilla {
namespace dom {
namespace syncedcontext {
template <typename T>
struct IsMozillaMaybe : std::false_type {};
template <typename T>
struct IsMozillaMaybe<Maybe<T>> : std::true_type {};
// A super-sketchy logging only function for generating a human-readable version
// of the value of a synced context field.
template <typename T>
void FormatFieldValue(nsACString& aStr, const T& aValue) {
if constexpr (std::is_same_v<bool, T>) {
if (aValue) {
aStr.AppendLiteral("true");
} else {
aStr.AppendLiteral("false");
}
} else if constexpr (std::is_same_v<nsID, T>) {
aStr.Append(nsIDToCString(aValue).get());
} else if constexpr (std::is_integral_v<T>) {
aStr.AppendInt(aValue);
} else if constexpr (std::is_enum_v<T>) {
aStr.AppendInt(static_cast<std::underlying_type_t<T>>(aValue));
} else if constexpr (std::is_floating_point_v<T>) {
aStr.AppendFloat(aValue);
} else if constexpr (std::is_base_of_v<nsAString, T>) {
AppendUTF16toUTF8(aValue, aStr);
} else if constexpr (std::is_base_of_v<nsACString, T>) {
aStr.Append(aValue);
} else if constexpr (IsMozillaMaybe<T>::value) {
if (aValue) {
aStr.AppendLiteral("Some(");
FormatFieldValue(aStr, aValue.ref());
aStr.AppendLiteral(")");
} else {
aStr.AppendLiteral("Nothing");
}
} else {
aStr.AppendLiteral("???");
}
}
// Sketchy logging-only logic to generate a human-readable output of the actions
// a synced context transaction is going to perform.
template <typename Context>
nsAutoCString FormatTransaction(
typename Transaction<Context>::IndexSet aModified,
const typename Context::FieldValues& aOldValues,
const typename Context::FieldValues& aNewValues) {
nsAutoCString result;
Context::FieldValues::EachIndex([&](auto idx) {
if (aModified.contains(idx)) {
result.Append(Context::FieldIndexToName(idx));
result.AppendLiteral("(");
FormatFieldValue(result, aOldValues.Get(idx));
result.AppendLiteral("->");
FormatFieldValue(result, aNewValues.Get(idx));
result.AppendLiteral(") ");
}
});
return result;
}
template <typename Context>
nsCString FormatValidationError(
typename Transaction<Context>::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 (mModified.isEmpty()) {
return NS_OK;
}
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, /* aFromIPC */ false);
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::GetSyncLog(), 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): ");
// data-review+ at https://bugzilla.mozilla.org/show_bug.cgi?id=1618992#c7
return IPC_FAIL_UNSAFE_PRINTF(aSource, "%s", error.get());
}
// Validate may have dropped some fields from the transaction, check it's not
// empty before continuing.
if (mModified.isEmpty()) {
return IPC_OK();
}
BrowsingContextGroup* group = owner->Group();
group->EachOtherParent(aSource, [&](ContentParent* aParent) {
owner->SendCommitTransaction(aParent, *this,
aParent->GetBrowsingContextFieldEpoch());
});
Apply(owner, /* aFromIPC */ true);
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::GetSyncLog(), 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) {
MOZ_LOG(
Context::GetSyncLog(), LogLevel::Debug,
("Transaction::Obsoleted(#%" PRIx64 ", %" PRIu64 ">%" PRIu64 "): %s",
owner->Id(), FieldEpoch(idx, owner), aEpoch,
Context::FieldIndexToName(idx)));
mModified -= idx;
}
});
if (mModified.isEmpty()) {
return IPC_OK();
}
Apply(owner, /* aFromIPC */ true);
return IPC_OK();
}
template <typename Context>
void Transaction<Context>::Apply(Context* aOwner, bool aFromIPC) {
MOZ_ASSERT(!mModified.isEmpty());
MOZ_LOG(
Context::GetSyncLog(), LogLevel::Debug,
("Transaction::Apply(#%" PRIx64 ", %s): %s", aOwner->Id(),
aFromIPC ? "ipc" : "local",
FormatTransaction<Context>(mModified, aOwner->mFields.mValues, mValues)
.get()));
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>
void Transaction<Context>::CommitWithoutSyncing(Context* aOwner) {
MOZ_LOG(
Context::GetSyncLog(), LogLevel::Debug,
("Transaction::CommitWithoutSyncing(#%" PRIx64 "): %s", aOwner->Id(),
FormatTransaction<Context>(mModified, aOwner->mFields.mValues, mValues)
.get()));
EachIndex([&](auto idx) {
if (mModified.contains(idx)) {
aOwner->mFields.mValues.Get(idx) = std::move(mValues.Get(idx));
}
});
mModified.clear();
}
inline CanSetResult AsCanSetResult(CanSetResult aValue) { return aValue; }
inline CanSetResult AsCanSetResult(bool aValue) {
return aValue ? CanSetResult::Allow : CanSetResult::Deny;
}
template <typename Context>
typename Transaction<Context>::IndexSet Transaction<Context>::Validate(
Context* aOwner, ContentParent* aSource) {
IndexSet failedFields;
Transaction<Context> revertTxn;
EachIndex([&](auto idx) {
if (!mModified.contains(idx)) {
return;
}
switch (AsCanSetResult(aOwner->CanSet(idx, mValues.Get(idx), aSource))) {
case CanSetResult::Allow:
break;
case CanSetResult::Deny:
failedFields += idx;
break;
case CanSetResult::Revert:
revertTxn.mValues.Get(idx) = aOwner->mFields.mValues.Get(idx);
revertTxn.mModified += idx;
break;
}
});
// If any changes need to be reverted, log them, remove them from this
// transaction, and optionally send a message to the source process.
if (!revertTxn.mModified.isEmpty()) {
// NOTE: Logging with modified IndexSet from revert transaction, and values
// from this transaction, so we log the failed values we're going to revert.
MOZ_LOG(Context::GetSyncLog(), LogLevel::Debug,
("Transaction::PartialRevert(#%" PRIx64 ", childid %d, pid %" PRIPID
"): %s",
aOwner->Id(), aSource ? aSource->OtherChildID() : -1,
aSource ? aSource->OtherPid() : base::kInvalidProcessId,
FormatTransaction<Context>(revertTxn.mModified, mValues,
revertTxn.mValues)
.get()));
mModified -= revertTxn.mModified;
if (aSource) {
aOwner->SendCommitTransaction(aSource, revertTxn,
aSource->GetBrowsingContextFieldEpoch());
}
}
return failedFields;
}
template <typename Context>
void Transaction<Context>::Write(IPC::MessageWriter* aWriter) const {
// Record which field indices will be included, and then write those fields
// out.
typename IndexSet::serializedType modified = mModified.serialize();
IPC::WriteParam(aWriter, modified);
EachIndex([&](auto idx) {
if (mModified.contains(idx)) {
IPC::WriteParam(aWriter, mValues.Get(idx));
}
});
}
template <typename Context>
bool Transaction<Context>::Read(IPC::MessageReader* aReader) {
// Read in which field indices were sent by the remote, followed by the fields
// identified by those indices.
typename IndexSet::serializedType modified =
typename IndexSet::serializedType{};
if (!IPC::ReadParam(aReader, &modified)) {
return false;
}
mModified.deserialize(modified);
bool ok = true;
EachIndex([&](auto idx) {
if (ok && mModified.contains(idx)) {
ok = IPC::ReadParam(aReader, &mValues.Get(idx));
}
});
return ok;
}
template <typename Base, size_t Count>
void FieldValues<Base, Count>::Write(IPC::MessageWriter* aWriter) const {
// XXX The this-> qualification is necessary to work around a bug in older gcc
// versions causing an ICE.
EachIndex([&](auto idx) { IPC::WriteParam(aWriter, this->Get(idx)); });
}
template <typename Base, size_t Count>
bool FieldValues<Base, Count>::Read(IPC::MessageReader* aReader) {
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 = IPC::ReadParam(aReader, &this->Get(idx));
}
});
return ok;
}
} // namespace syncedcontext
} // namespace dom
} // namespace mozilla
#endif // !defined(mozilla_dom_SyncedContextInlines_h)
|