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
|
/* -*- 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/. */
#include "ProcessUtils.h"
#include "mozilla/Preferences.h"
#include "mozilla/GeckoArgs.h"
#include "mozilla/dom/RemoteType.h"
#include "mozilla/ipc/GeckoChildProcessHost.h"
#include "nsPrintfCString.h"
#include "XPCSelfHostedShmem.h"
namespace mozilla {
namespace ipc {
SharedPreferenceSerializer::SharedPreferenceSerializer() {
MOZ_COUNT_CTOR(SharedPreferenceSerializer);
}
SharedPreferenceSerializer::~SharedPreferenceSerializer() {
MOZ_COUNT_DTOR(SharedPreferenceSerializer);
}
SharedPreferenceSerializer::SharedPreferenceSerializer(
SharedPreferenceSerializer&& aOther)
: mPrefMapHandle(std::move(aOther.mPrefMapHandle)),
mPrefsHandle(std::move(aOther.mPrefsHandle)) {
MOZ_COUNT_CTOR(SharedPreferenceSerializer);
}
bool SharedPreferenceSerializer::SerializeToSharedMemory(
const GeckoProcessType aDestinationProcessType,
const nsACString& aDestinationRemoteType) {
mPrefMapHandle = Preferences::EnsureSnapshot();
bool destIsWebContent =
aDestinationProcessType == GeckoProcessType_Content &&
(StringBeginsWith(aDestinationRemoteType, WEB_REMOTE_TYPE) ||
StringBeginsWith(aDestinationRemoteType, PREALLOC_REMOTE_TYPE));
// Serialize the early prefs.
nsAutoCStringN<1024> prefs;
Preferences::SerializePreferences(prefs, destIsWebContent);
auto prefsLength = prefs.Length();
// Set up the shared memory.
auto handle = shared_memory::Create(prefsLength);
if (!handle) {
NS_ERROR("failed to create shared memory in the parent");
return false;
}
auto mapping = handle.Map();
if (!mapping) {
NS_ERROR("failed to map shared memory in the parent");
return false;
}
// Copy the serialized prefs into the shared memory.
memcpy(mapping.DataAs<char>(), prefs.get(), prefsLength);
mPrefsHandle = std::move(handle).ToReadOnly();
return true;
}
void SharedPreferenceSerializer::AddSharedPrefCmdLineArgs(
mozilla::ipc::GeckoChildProcessHost& procHost,
geckoargs::ChildProcessArgs& aExtraOpts) const {
auto prefsHandle = GetPrefsHandle().Clone();
MOZ_RELEASE_ASSERT(prefsHandle, "failed to clone prefs handle");
auto prefMapHandle = GetPrefMapHandle().Clone();
MOZ_RELEASE_ASSERT(prefMapHandle, "failed to clone pref map handle");
// Pass the handles via command line flags.
geckoargs::sPrefsHandle.Put(std::move(prefsHandle), aExtraOpts);
geckoargs::sPrefMapHandle.Put(std::move(prefMapHandle), aExtraOpts);
}
SharedPreferenceDeserializer::SharedPreferenceDeserializer() {
MOZ_COUNT_CTOR(SharedPreferenceDeserializer);
}
SharedPreferenceDeserializer::~SharedPreferenceDeserializer() {
MOZ_COUNT_DTOR(SharedPreferenceDeserializer);
}
bool SharedPreferenceDeserializer::DeserializeFromSharedMemory(
ReadOnlySharedMemoryHandle&& aPrefsHandle,
ReadOnlySharedMemoryHandle&& aPrefMapHandle) {
if (!aPrefsHandle || !aPrefMapHandle) {
return false;
}
mPrefMapHandle = std::move(aPrefMapHandle);
// Init the shared-memory base preference mapping first, so that only changed
// preferences wind up in heap memory.
Preferences::InitSnapshot(mPrefMapHandle);
// Set up early prefs from the shared memory.
mShmem = aPrefsHandle.Map();
if (!mShmem) {
NS_ERROR("failed to map shared memory in the child");
return false;
}
Preferences::DeserializePreferences(mShmem.DataAs<char>(), mShmem.Size());
return true;
}
void ExportSharedJSInit(mozilla::ipc::GeckoChildProcessHost& procHost,
geckoargs::ChildProcessArgs& aExtraOpts) {
auto& shmem = xpc::SelfHostedShmem::GetSingleton();
auto handle = shmem.Handle().Clone();
// If the file is not found or the content is empty, then we would start the
// content process without this optimization.
if (!handle) {
NS_ERROR("Can't use SelfHosted shared memory handle.");
return;
}
// command line: -jsInitHandle handle
geckoargs::sJsInitHandle.Put(std::move(handle), aExtraOpts);
}
bool ImportSharedJSInit(ReadOnlySharedMemoryHandle&& aJsInitHandle) {
// This is an optimization, and as such we can safely recover if the command
// line argument are not provided.
if (!aJsInitHandle) {
return true;
}
// Initialize the shared memory with the file handle and size of the content
// of the self-hosted Xdr.
auto& shmem = xpc::SelfHostedShmem::GetSingleton();
if (!shmem.InitFromChild(std::move(aJsInitHandle))) {
NS_ERROR("failed to open shared memory in the child");
return false;
}
return true;
}
} // namespace ipc
} // namespace mozilla
|