2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2009-06-29 18:38:29 +00:00
|
|
|
|
|
|
|
#ifndef __IPC_GLUE_GECKOCHILDPROCESSHOST_H__
|
|
|
|
#define __IPC_GLUE_GECKOCHILDPROCESSHOST_H__
|
|
|
|
|
|
|
|
#include "base/file_path.h"
|
|
|
|
#include "base/process_util.h"
|
|
|
|
#include "base/scoped_ptr.h"
|
|
|
|
#include "base/waitable_event.h"
|
|
|
|
#include "chrome/common/child_process_host.h"
|
|
|
|
|
2014-04-04 19:16:16 +00:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-05-31 13:16:54 +00:00
|
|
|
#include "mozilla/ipc/FileDescriptor.h"
|
2009-07-20 18:14:41 +00:00
|
|
|
#include "mozilla/Monitor.h"
|
2014-04-04 19:16:16 +00:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2009-07-20 18:14:41 +00:00
|
|
|
|
2014-04-04 19:16:16 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2009-08-19 17:09:51 +00:00
|
|
|
#include "nsXULAppAPI.h" // for GeckoProcessType
|
2010-07-01 03:07:50 +00:00
|
|
|
#include "nsString.h"
|
2009-07-10 19:03:09 +00:00
|
|
|
|
2014-07-16 23:01:34 +00:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
#include "sandboxBroker.h"
|
|
|
|
#endif
|
|
|
|
|
2014-04-04 19:16:16 +00:00
|
|
|
class nsIFile;
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
class GeckoChildProcessHost : public ChildProcessHost
|
|
|
|
{
|
2009-07-20 18:14:41 +00:00
|
|
|
protected:
|
|
|
|
typedef mozilla::Monitor Monitor;
|
2012-08-07 23:29:32 +00:00
|
|
|
typedef std::vector<std::string> StringVector;
|
2009-07-20 18:14:41 +00:00
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
public:
|
2012-08-29 12:24:48 +00:00
|
|
|
typedef base::ChildPrivileges ChildPrivileges;
|
2009-10-27 21:52:37 +00:00
|
|
|
typedef base::ProcessHandle ProcessHandle;
|
|
|
|
|
2013-01-17 20:06:36 +00:00
|
|
|
static ChildPrivileges DefaultChildPrivileges();
|
|
|
|
|
2012-08-29 12:24:48 +00:00
|
|
|
GeckoChildProcessHost(GeckoProcessType aProcessType,
|
|
|
|
ChildPrivileges aPrivileges=base::PRIVILEGES_DEFAULT);
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2009-11-09 22:56:55 +00:00
|
|
|
~GeckoChildProcessHost();
|
|
|
|
|
2012-09-17 08:37:20 +00:00
|
|
|
static nsresult GetArchitecturesForBinary(const char *path, uint32_t *result);
|
2010-09-16 06:09:19 +00:00
|
|
|
|
2012-09-17 08:37:20 +00:00
|
|
|
static uint32_t GetSupportedArchitecturesForProcessType(GeckoProcessType type);
|
2010-09-16 06:09:19 +00:00
|
|
|
|
2012-08-07 23:29:32 +00:00
|
|
|
// Block until the IPC channel for our subprocess is initialized,
|
|
|
|
// but no longer. The child process may or may not have been
|
|
|
|
// created when this method returns.
|
|
|
|
bool AsyncLaunch(StringVector aExtraOpts=StringVector());
|
|
|
|
|
|
|
|
// Block until the IPC channel for our subprocess is initialized and
|
|
|
|
// the OS process is created. The subprocess may or may not have
|
|
|
|
// connected back to us when this method returns.
|
|
|
|
//
|
|
|
|
// NB: on POSIX, this method is relatively cheap, and doesn't
|
|
|
|
// require disk IO. On win32 however, it requires at least the
|
|
|
|
// analogue of stat(). This difference induces a semantic
|
|
|
|
// difference in this method: on POSIX, when we return, we know the
|
|
|
|
// subprocess has been created, but we don't know whether its
|
|
|
|
// executable image can be loaded. On win32, we do know that when
|
|
|
|
// we return. But we don't know if dynamic linking succeeded on
|
|
|
|
// either platform.
|
|
|
|
bool LaunchAndWaitForProcessHandle(StringVector aExtraOpts=StringVector());
|
|
|
|
|
|
|
|
// Block until the child process has been created and it connects to
|
|
|
|
// the IPC channel, meaning it's fully initialized. (Or until an
|
|
|
|
// error occurs.)
|
|
|
|
bool SyncLaunch(StringVector aExtraOpts=StringVector(),
|
2012-09-17 08:37:20 +00:00
|
|
|
int32_t timeoutMs=0,
|
2010-09-16 06:09:19 +00:00
|
|
|
base::ProcessArchitecture arch=base::GetCurrentProcessArchitecture());
|
2012-08-07 23:29:32 +00:00
|
|
|
|
2013-05-31 13:16:54 +00:00
|
|
|
virtual bool PerformAsyncLaunch(StringVector aExtraOpts=StringVector(),
|
|
|
|
base::ProcessArchitecture aArch=base::GetCurrentProcessArchitecture());
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2012-09-17 08:37:20 +00:00
|
|
|
virtual void OnChannelConnected(int32_t peer_pid);
|
2009-06-29 18:38:29 +00:00
|
|
|
virtual void OnMessageReceived(const IPC::Message& aMsg);
|
|
|
|
virtual void OnChannelError();
|
2012-07-14 21:21:32 +00:00
|
|
|
virtual void GetQueuedMessages(std::queue<IPC::Message>& queue);
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2013-05-31 13:16:54 +00:00
|
|
|
virtual void InitializeChannel();
|
2009-11-18 21:43:53 +00:00
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
virtual bool CanShutdown() { return true; }
|
|
|
|
|
2009-09-03 00:18:27 +00:00
|
|
|
virtual void OnWaitableEventSignaled(base::WaitableEvent *event);
|
|
|
|
|
2009-07-11 06:33:10 +00:00
|
|
|
IPC::Channel* GetChannel() {
|
|
|
|
return channelp();
|
|
|
|
}
|
|
|
|
|
2009-07-14 05:12:50 +00:00
|
|
|
base::WaitableEvent* GetShutDownEvent() {
|
|
|
|
return GetProcessEvent();
|
|
|
|
}
|
|
|
|
|
2013-10-01 00:56:16 +00:00
|
|
|
// Returns a "borrowed" handle to the child process - the handle returned
|
|
|
|
// by this function must not be closed by the caller.
|
2009-10-27 21:52:37 +00:00
|
|
|
ProcessHandle GetChildProcessHandle() {
|
|
|
|
return mChildProcessHandle;
|
|
|
|
}
|
|
|
|
|
2013-10-01 00:56:16 +00:00
|
|
|
// Returns an "owned" handle to the child process - the handle returned
|
|
|
|
// by this function must be closed by the caller.
|
|
|
|
ProcessHandle GetOwnedChildProcessHandle() {
|
|
|
|
ProcessHandle handle;
|
|
|
|
// We use OpenPrivilegedProcessHandle as that is where our
|
|
|
|
// mChildProcessHandle initially came from.
|
|
|
|
bool ok = base::OpenPrivilegedProcessHandle(base::GetProcId(mChildProcessHandle),
|
|
|
|
&handle);
|
|
|
|
NS_ASSERTION(ok, "Failed to get owned process handle");
|
|
|
|
return ok ? handle : 0;
|
|
|
|
}
|
|
|
|
|
2013-06-03 10:14:37 +00:00
|
|
|
GeckoProcessType GetProcessType() {
|
|
|
|
return mProcessType;
|
|
|
|
}
|
|
|
|
|
2010-08-27 13:32:45 +00:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
task_t GetChildTask() {
|
|
|
|
return mChildTask;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-28 09:45:16 +00:00
|
|
|
/**
|
|
|
|
* Must run on the IO thread. Cause the OS process to exit and
|
|
|
|
* ensure its OS resources are cleaned up.
|
|
|
|
*/
|
|
|
|
void Join();
|
2010-08-27 13:32:45 +00:00
|
|
|
|
2014-03-21 12:50:07 +00:00
|
|
|
// For bug 943174: Skip the EnsureProcessTerminated call in the destructor.
|
|
|
|
void SetAlreadyDead();
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
protected:
|
2009-08-19 17:09:51 +00:00
|
|
|
GeckoProcessType mProcessType;
|
2012-08-29 12:24:48 +00:00
|
|
|
ChildPrivileges mPrivileges;
|
2009-07-20 18:14:41 +00:00
|
|
|
Monitor mMonitor;
|
2009-06-29 18:38:29 +00:00
|
|
|
FilePath mProcessPath;
|
2013-11-02 02:09:45 +00:00
|
|
|
|
2012-08-07 23:29:32 +00:00
|
|
|
// This value must be accessed while holding mMonitor.
|
|
|
|
enum {
|
|
|
|
// This object has been constructed, but the OS process has not
|
|
|
|
// yet.
|
|
|
|
CREATING_CHANNEL = 0,
|
|
|
|
// The IPC channel for our subprocess has been created, but the OS
|
|
|
|
// process has still not been created.
|
|
|
|
CHANNEL_INITIALIZED,
|
|
|
|
// The OS process has been created, but it hasn't yet connected to
|
|
|
|
// our IPC channel.
|
|
|
|
PROCESS_CREATED,
|
|
|
|
// The process is launched and connected to our IPC channel. All
|
|
|
|
// is well.
|
|
|
|
PROCESS_CONNECTED,
|
|
|
|
PROCESS_ERROR
|
|
|
|
} mProcessState;
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t mChildCounter;
|
2011-03-25 23:40:50 +00:00
|
|
|
|
2012-08-07 23:29:32 +00:00
|
|
|
void PrepareLaunch();
|
|
|
|
|
2010-07-01 03:07:50 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
void InitWindowsGroupID();
|
|
|
|
nsString mGroupId;
|
2014-07-16 23:01:34 +00:00
|
|
|
SandboxBroker mSandboxBroker;
|
|
|
|
#endif // XP_WIN
|
2010-07-01 03:07:50 +00:00
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
#if defined(OS_POSIX)
|
|
|
|
base::file_handle_mapping_vector mFileMap;
|
|
|
|
#endif
|
|
|
|
|
2009-09-03 00:18:27 +00:00
|
|
|
base::WaitableEventWatcher::Delegate* mDelegate;
|
|
|
|
|
2009-10-27 21:52:37 +00:00
|
|
|
ProcessHandle mChildProcessHandle;
|
2010-08-27 13:32:45 +00:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
task_t mChildTask;
|
|
|
|
#endif
|
2009-10-27 21:52:37 +00:00
|
|
|
|
2013-05-31 13:16:54 +00:00
|
|
|
void OpenPrivilegedHandle(base::ProcessId aPid);
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
private:
|
|
|
|
DISALLOW_EVIL_CONSTRUCTORS(GeckoChildProcessHost);
|
2011-03-25 23:40:50 +00:00
|
|
|
|
|
|
|
// Does the actual work for AsyncLaunch, on the IO thread.
|
|
|
|
bool PerformAsyncLaunchInternal(std::vector<std::string>& aExtraOpts,
|
|
|
|
base::ProcessArchitecture arch);
|
2012-07-14 21:21:32 +00:00
|
|
|
|
2013-05-31 13:16:54 +00:00
|
|
|
bool RunPerformAsyncLaunch(StringVector aExtraOpts=StringVector(),
|
|
|
|
base::ProcessArchitecture aArch=base::GetCurrentProcessArchitecture());
|
2012-08-07 23:29:32 +00:00
|
|
|
|
2014-04-04 19:16:16 +00:00
|
|
|
static void GetPathToBinary(FilePath& exePath);
|
|
|
|
|
2012-07-14 21:21:32 +00:00
|
|
|
// In between launching the subprocess and handing off its IPC
|
|
|
|
// channel, there's a small window of time in which *we* might still
|
|
|
|
// be the channel listener, and receive messages. That's bad
|
|
|
|
// because we have no idea what to do with those messages. So queue
|
|
|
|
// them here until we hand off the eventual listener.
|
|
|
|
//
|
|
|
|
// FIXME/cjones: this strongly indicates bad design. Shame on us.
|
|
|
|
std::queue<IPC::Message> mQueue;
|
2009-06-29 18:38:29 +00:00
|
|
|
};
|
|
|
|
|
2013-05-31 13:16:54 +00:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
class GeckoExistingProcessHost MOZ_FINAL : public GeckoChildProcessHost
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GeckoExistingProcessHost(GeckoProcessType aProcessType,
|
|
|
|
base::ProcessHandle aProcess,
|
|
|
|
const FileDescriptor& aFileDescriptor,
|
|
|
|
ChildPrivileges aPrivileges=base::PRIVILEGES_DEFAULT);
|
|
|
|
|
|
|
|
~GeckoExistingProcessHost();
|
|
|
|
|
|
|
|
virtual bool PerformAsyncLaunch(StringVector aExtraOpts=StringVector(),
|
|
|
|
base::ProcessArchitecture aArch=base::GetCurrentProcessArchitecture()) MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
virtual void InitializeChannel() MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
private:
|
|
|
|
base::ProcessHandle mExistingProcessHandle;
|
|
|
|
mozilla::ipc::FileDescriptor mExistingFileDescriptor;
|
|
|
|
};
|
|
|
|
#endif /* MOZ_NUWA_PROCESS */
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
} /* namespace ipc */
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* __IPC_GLUE_GECKOCHILDPROCESSHOST_H__ */
|