2019-01-11 21:57:23 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "SocketProcessHost.h"
|
2019-01-11 23:54:06 +03:00
|
|
|
|
2019-01-11 21:57:23 +03:00
|
|
|
#include "nsAppRunner.h"
|
2019-01-11 23:54:06 +03:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "SocketProcessParent.h"
|
2019-01-11 21:57:23 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2019-01-11 23:54:06 +03:00
|
|
|
#define NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC "ipc:network:set-offline"
|
|
|
|
|
|
|
|
class OfflineObserver final : public nsIObserver {
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
explicit OfflineObserver(SocketProcessHost* aProcessHost)
|
|
|
|
: mProcessHost(aProcessHost) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(this, NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Destroy() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(this, NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC);
|
|
|
|
}
|
|
|
|
mProcessHost = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// nsIObserver implementation.
|
|
|
|
NS_IMETHOD
|
|
|
|
Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) override {
|
|
|
|
if (!mProcessHost) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC)) {
|
|
|
|
NS_ConvertUTF16toUTF8 dataStr(aData);
|
|
|
|
const char* offline = dataStr.get();
|
|
|
|
if (!mProcessHost->IsConnected() ||
|
|
|
|
mProcessHost->GetActor()->SendSetOffline(
|
|
|
|
!strcmp(offline, "true") ? true : false)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
virtual ~OfflineObserver() = default;
|
|
|
|
|
|
|
|
SocketProcessHost* mProcessHost;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(OfflineObserver, nsIObserver)
|
|
|
|
|
2019-01-11 21:57:23 +03:00
|
|
|
SocketProcessHost::SocketProcessHost(Listener* aListener)
|
|
|
|
: GeckoChildProcessHost(GeckoProcessType_Socket),
|
|
|
|
mListener(aListener),
|
|
|
|
mTaskFactory(this),
|
|
|
|
mLaunchPhase(LaunchPhase::Unlaunched),
|
|
|
|
mShutdownRequested(false),
|
|
|
|
mChannelClosed(false) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_COUNT_CTOR(SocketProcessHost);
|
|
|
|
}
|
|
|
|
|
2019-01-11 23:54:06 +03:00
|
|
|
SocketProcessHost::~SocketProcessHost() {
|
|
|
|
MOZ_COUNT_DTOR(SocketProcessHost);
|
|
|
|
if (mOfflineObserver) {
|
|
|
|
RefPtr<OfflineObserver> observer = mOfflineObserver;
|
|
|
|
NS_DispatchToMainThread(
|
|
|
|
NS_NewRunnableFunction("SocketProcessHost::DestroyOfflineObserver",
|
|
|
|
[observer]() { observer->Destroy(); }));
|
|
|
|
}
|
|
|
|
}
|
2019-01-11 21:57:23 +03:00
|
|
|
|
|
|
|
bool SocketProcessHost::Launch() {
|
|
|
|
MOZ_ASSERT(mLaunchPhase == LaunchPhase::Unlaunched);
|
|
|
|
MOZ_ASSERT(!mSocketProcessParent);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
std::vector<std::string> extraArgs;
|
|
|
|
|
2019-01-11 21:39:22 +03:00
|
|
|
nsAutoCString parentBuildID(mozilla::PlatformBuildID());
|
|
|
|
extraArgs.push_back("-parentBuildID");
|
|
|
|
extraArgs.push_back(parentBuildID.get());
|
2019-01-11 21:57:23 +03:00
|
|
|
|
2019-01-11 21:39:22 +03:00
|
|
|
SharedPreferenceSerializer prefSerializer;
|
|
|
|
if (!prefSerializer.SerializeToSharedMemory()) {
|
2019-01-11 21:57:23 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Formats a pointer or pointer-sized-integer as a string suitable for passing
|
|
|
|
// in an arguments list.
|
|
|
|
auto formatPtrArg = [](auto arg) {
|
|
|
|
return nsPrintfCString("%zu", uintptr_t(arg));
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
// Record the handle as to-be-shared, and pass it via a command flag. This
|
|
|
|
// works because Windows handles are system-wide.
|
2019-01-11 21:39:22 +03:00
|
|
|
HANDLE prefsHandle = prefSerializer.GetSharedMemoryHandle();
|
2019-01-11 21:57:23 +03:00
|
|
|
AddHandleToShare(prefsHandle);
|
2019-01-11 21:39:22 +03:00
|
|
|
AddHandleToShare(prefSerializer.GetPrefMapHandle().get());
|
2019-01-11 21:57:23 +03:00
|
|
|
extraArgs.push_back("-prefsHandle");
|
|
|
|
extraArgs.push_back(formatPtrArg(prefsHandle).get());
|
|
|
|
extraArgs.push_back("-prefMapHandle");
|
2019-01-11 21:39:22 +03:00
|
|
|
extraArgs.push_back(
|
|
|
|
formatPtrArg(prefSerializer.GetPrefMapHandle().get()).get());
|
2019-01-11 21:57:23 +03:00
|
|
|
#else
|
|
|
|
// In contrast, Unix fds are per-process. So remap the fd to a fixed one that
|
|
|
|
// will be used in the child.
|
|
|
|
// XXX: bug 1440207 is about improving how fixed fds are used.
|
|
|
|
//
|
|
|
|
// Note: on Android, AddFdToRemap() sets up the fd to be passed via a Parcel,
|
|
|
|
// and the fixed fd isn't used. However, we still need to mark it for
|
|
|
|
// remapping so it doesn't get closed in the child.
|
2019-01-11 21:39:22 +03:00
|
|
|
AddFdToRemap(prefSerializer.GetSharedMemoryHandle().fd, kPrefsFileDescriptor);
|
|
|
|
AddFdToRemap(prefSerializer.GetPrefMapHandle().get(), kPrefMapFileDescriptor);
|
2019-01-11 21:57:23 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Pass the lengths via command line flags.
|
|
|
|
extraArgs.push_back("-prefsLen");
|
2019-01-11 21:39:22 +03:00
|
|
|
extraArgs.push_back(formatPtrArg(prefSerializer.GetPrefLength()).get());
|
2019-01-11 21:57:23 +03:00
|
|
|
extraArgs.push_back("-prefMapSize");
|
2019-01-11 21:39:22 +03:00
|
|
|
extraArgs.push_back(formatPtrArg(prefSerializer.GetPrefMapSize()).get());
|
2019-01-11 21:57:23 +03:00
|
|
|
|
|
|
|
mLaunchPhase = LaunchPhase::Waiting;
|
|
|
|
if (!GeckoChildProcessHost::LaunchAndWaitForProcessHandle(extraArgs)) {
|
|
|
|
mLaunchPhase = LaunchPhase::Complete;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketProcessHost::OnChannelConnected(int32_t peer_pid) {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
GeckoChildProcessHost::OnChannelConnected(peer_pid);
|
|
|
|
|
|
|
|
// Post a task to the main thread. Take the lock because mTaskFactory is not
|
|
|
|
// thread-safe.
|
|
|
|
RefPtr<Runnable> runnable;
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
runnable = mTaskFactory.NewRunnableMethod(
|
|
|
|
&SocketProcessHost::OnChannelConnectedTask);
|
|
|
|
}
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketProcessHost::OnChannelError() {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
GeckoChildProcessHost::OnChannelError();
|
|
|
|
|
|
|
|
// Post a task to the main thread. Take the lock because mTaskFactory is not
|
|
|
|
// thread-safe.
|
|
|
|
RefPtr<Runnable> runnable;
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
runnable =
|
|
|
|
mTaskFactory.NewRunnableMethod(&SocketProcessHost::OnChannelErrorTask);
|
|
|
|
}
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketProcessHost::OnChannelConnectedTask() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mLaunchPhase == LaunchPhase::Waiting) {
|
|
|
|
InitAfterConnect(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketProcessHost::OnChannelErrorTask() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mLaunchPhase == LaunchPhase::Waiting) {
|
|
|
|
InitAfterConnect(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketProcessHost::InitAfterConnect(bool aSucceeded) {
|
|
|
|
MOZ_ASSERT(mLaunchPhase == LaunchPhase::Waiting);
|
|
|
|
MOZ_ASSERT(!mSocketProcessParent);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mLaunchPhase = LaunchPhase::Complete;
|
|
|
|
|
|
|
|
if (aSucceeded) {
|
|
|
|
mSocketProcessParent = MakeUnique<SocketProcessParent>(this);
|
|
|
|
DebugOnly<bool> rv = mSocketProcessParent->Open(
|
|
|
|
GetChannel(), base::GetProcId(GetChildProcessHandle()));
|
|
|
|
MOZ_ASSERT(rv);
|
2019-01-11 23:54:06 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> ioService(do_GetIOService());
|
|
|
|
MOZ_ASSERT(ioService, "No IO service?");
|
|
|
|
bool offline = false;
|
|
|
|
DebugOnly<nsresult> result = ioService->GetOffline(&offline);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(result), "Failed getting offline?");
|
|
|
|
|
|
|
|
Unused << GetActor()->SendSetOffline(offline);
|
|
|
|
|
|
|
|
mOfflineObserver = new OfflineObserver(this);
|
2019-01-11 21:57:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mListener) {
|
|
|
|
mListener->OnProcessLaunchComplete(this, aSucceeded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketProcessHost::Shutdown() {
|
|
|
|
MOZ_ASSERT(!mShutdownRequested);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mListener = nullptr;
|
2019-01-11 23:54:06 +03:00
|
|
|
if (mOfflineObserver) {
|
|
|
|
mOfflineObserver->Destroy();
|
|
|
|
mOfflineObserver = nullptr;
|
|
|
|
}
|
2019-01-11 21:57:23 +03:00
|
|
|
|
|
|
|
if (mSocketProcessParent) {
|
|
|
|
// OnChannelClosed uses this to check if the shutdown was expected or
|
|
|
|
// unexpected.
|
|
|
|
mShutdownRequested = true;
|
|
|
|
|
|
|
|
// The channel might already be closed if we got here unexpectedly.
|
|
|
|
if (!mChannelClosed) {
|
|
|
|
mSocketProcessParent->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DestroyProcess();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketProcessHost::OnChannelClosed() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mChannelClosed = true;
|
|
|
|
|
|
|
|
if (!mShutdownRequested && mListener) {
|
|
|
|
// This is an unclean shutdown. Notify our listener that we're going away.
|
|
|
|
mListener->OnProcessUnexpectedShutdown(this);
|
|
|
|
} else {
|
|
|
|
DestroyProcess();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the actor.
|
|
|
|
SocketProcessParent::Destroy(std::move(mSocketProcessParent));
|
|
|
|
MOZ_ASSERT(!mSocketProcessParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DelayedDeleteSubprocess(GeckoChildProcessHost* aSubprocess) {
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(
|
|
|
|
MakeAndAddRef<DeleteTask<GeckoChildProcessHost>>(aSubprocess));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketProcessHost::DestroyProcess() {
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mTaskFactory.RevokeAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageLoop::current()->PostTask(NewRunnableFunction(
|
|
|
|
"DestroySocketProcessRunnable", DelayedDeleteSubprocess, this));
|
|
|
|
}
|
|
|
|
|
2019-01-11 23:52:39 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// SocketProcessMemoryReporter
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool SocketProcessMemoryReporter::IsAlive() const {
|
|
|
|
MOZ_ASSERT(gIOService);
|
|
|
|
|
|
|
|
if (!gIOService->mSocketProcess) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gIOService->mSocketProcess->IsConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SocketProcessMemoryReporter::SendRequestMemoryReport(
|
|
|
|
const uint32_t& aGeneration, const bool& aAnonymize,
|
|
|
|
const bool& aMinimizeMemoryUsage, const dom::MaybeFileDesc& aDMDFile) {
|
|
|
|
MOZ_ASSERT(gIOService);
|
|
|
|
|
|
|
|
if (!gIOService->mSocketProcess) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketProcessParent* actor = gIOService->mSocketProcess->GetActor();
|
|
|
|
if (!actor) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return actor->SendRequestMemoryReport(aGeneration, aAnonymize,
|
|
|
|
aMinimizeMemoryUsage, aDMDFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t SocketProcessMemoryReporter::Pid() const {
|
|
|
|
MOZ_ASSERT(gIOService);
|
|
|
|
|
|
|
|
if (!gIOService->mSocketProcess) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SocketProcessParent* actor = gIOService->mSocketProcess->GetActor()) {
|
|
|
|
return (int32_t)actor->OtherPid();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-11 21:57:23 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|