Bumping manifests a=b2g-bump
[gecko.git] / ipc / glue / GeckoChildProcessHost.h
blob3442320fa43d338405eb5b6d4e3794a5ac239487
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef __IPC_GLUE_GECKOCHILDPROCESSHOST_H__
6 #define __IPC_GLUE_GECKOCHILDPROCESSHOST_H__
8 #include "base/file_path.h"
9 #include "base/process_util.h"
10 #include "base/scoped_ptr.h"
11 #include "base/waitable_event.h"
12 #include "chrome/common/child_process_host.h"
14 #include "mozilla/DebugOnly.h"
15 #include "mozilla/ipc/FileDescriptor.h"
16 #include "mozilla/Monitor.h"
17 #include "mozilla/StaticPtr.h"
19 #include "nsCOMPtr.h"
20 #include "nsXULAppAPI.h" // for GeckoProcessType
21 #include "nsString.h"
23 #if defined(XP_WIN) && defined(MOZ_SANDBOX)
24 #include "sandboxBroker.h"
25 #endif
27 class nsIFile;
29 namespace mozilla {
30 namespace ipc {
32 class GeckoChildProcessHost : public ChildProcessHost
34 protected:
35 typedef mozilla::Monitor Monitor;
36 typedef std::vector<std::string> StringVector;
38 public:
39 typedef base::ChildPrivileges ChildPrivileges;
40 typedef base::ProcessHandle ProcessHandle;
42 static ChildPrivileges DefaultChildPrivileges();
44 explicit GeckoChildProcessHost(GeckoProcessType aProcessType,
45 ChildPrivileges aPrivileges=base::PRIVILEGES_DEFAULT);
47 ~GeckoChildProcessHost();
49 static nsresult GetArchitecturesForBinary(const char *path, uint32_t *result);
51 static uint32_t GetSupportedArchitecturesForProcessType(GeckoProcessType type);
53 // Block until the IPC channel for our subprocess is initialized,
54 // but no longer. The child process may or may not have been
55 // created when this method returns.
56 bool AsyncLaunch(StringVector aExtraOpts=StringVector());
58 // Block until the IPC channel for our subprocess is initialized and
59 // the OS process is created. The subprocess may or may not have
60 // connected back to us when this method returns.
62 // NB: on POSIX, this method is relatively cheap, and doesn't
63 // require disk IO. On win32 however, it requires at least the
64 // analogue of stat(). This difference induces a semantic
65 // difference in this method: on POSIX, when we return, we know the
66 // subprocess has been created, but we don't know whether its
67 // executable image can be loaded. On win32, we do know that when
68 // we return. But we don't know if dynamic linking succeeded on
69 // either platform.
70 bool LaunchAndWaitForProcessHandle(StringVector aExtraOpts=StringVector());
72 // Block until the child process has been created and it connects to
73 // the IPC channel, meaning it's fully initialized. (Or until an
74 // error occurs.)
75 bool SyncLaunch(StringVector aExtraOpts=StringVector(),
76 int32_t timeoutMs=0,
77 base::ProcessArchitecture arch=base::GetCurrentProcessArchitecture());
79 virtual bool PerformAsyncLaunch(StringVector aExtraOpts=StringVector(),
80 base::ProcessArchitecture aArch=base::GetCurrentProcessArchitecture());
82 virtual void OnChannelConnected(int32_t peer_pid);
83 virtual void OnMessageReceived(const IPC::Message& aMsg);
84 virtual void OnChannelError();
85 virtual void GetQueuedMessages(std::queue<IPC::Message>& queue);
87 virtual void InitializeChannel();
89 virtual bool CanShutdown() { return true; }
91 virtual void OnWaitableEventSignaled(base::WaitableEvent *event);
93 IPC::Channel* GetChannel() {
94 return channelp();
97 base::WaitableEvent* GetShutDownEvent() {
98 return GetProcessEvent();
101 // Returns a "borrowed" handle to the child process - the handle returned
102 // by this function must not be closed by the caller.
103 ProcessHandle GetChildProcessHandle() {
104 return mChildProcessHandle;
107 // Returns an "owned" handle to the child process - the handle returned
108 // by this function must be closed by the caller.
109 ProcessHandle GetOwnedChildProcessHandle() {
110 ProcessHandle handle;
111 // We use OpenPrivilegedProcessHandle as that is where our
112 // mChildProcessHandle initially came from.
113 bool ok = base::OpenPrivilegedProcessHandle(base::GetProcId(mChildProcessHandle),
114 &handle);
115 NS_ASSERTION(ok, "Failed to get owned process handle");
116 return ok ? handle : 0;
119 GeckoProcessType GetProcessType() {
120 return mProcessType;
123 #ifdef XP_MACOSX
124 task_t GetChildTask() {
125 return mChildTask;
127 #endif
130 * Must run on the IO thread. Cause the OS process to exit and
131 * ensure its OS resources are cleaned up.
133 void Join();
135 // For bug 943174: Skip the EnsureProcessTerminated call in the destructor.
136 void SetAlreadyDead();
138 protected:
139 GeckoProcessType mProcessType;
140 ChildPrivileges mPrivileges;
141 Monitor mMonitor;
142 FilePath mProcessPath;
144 // This value must be accessed while holding mMonitor.
145 enum {
146 // This object has been constructed, but the OS process has not
147 // yet.
148 CREATING_CHANNEL = 0,
149 // The IPC channel for our subprocess has been created, but the OS
150 // process has still not been created.
151 CHANNEL_INITIALIZED,
152 // The OS process has been created, but it hasn't yet connected to
153 // our IPC channel.
154 PROCESS_CREATED,
155 // The process is launched and connected to our IPC channel. All
156 // is well.
157 PROCESS_CONNECTED,
158 PROCESS_ERROR
159 } mProcessState;
161 static int32_t mChildCounter;
163 void PrepareLaunch();
165 #ifdef XP_WIN
166 void InitWindowsGroupID();
167 nsString mGroupId;
169 #ifdef MOZ_SANDBOX
170 SandboxBroker mSandboxBroker;
171 std::vector<std::wstring> mAllowedFilesRead;
172 #endif
173 #endif // XP_WIN
175 #if defined(OS_POSIX)
176 base::file_handle_mapping_vector mFileMap;
177 #endif
179 base::WaitableEventWatcher::Delegate* mDelegate;
181 ProcessHandle mChildProcessHandle;
182 #if defined(OS_MACOSX)
183 task_t mChildTask;
184 #endif
186 void OpenPrivilegedHandle(base::ProcessId aPid);
188 private:
189 DISALLOW_EVIL_CONSTRUCTORS(GeckoChildProcessHost);
191 // Does the actual work for AsyncLaunch, on the IO thread.
192 bool PerformAsyncLaunchInternal(std::vector<std::string>& aExtraOpts,
193 base::ProcessArchitecture arch);
195 bool RunPerformAsyncLaunch(StringVector aExtraOpts=StringVector(),
196 base::ProcessArchitecture aArch=base::GetCurrentProcessArchitecture());
198 static void GetPathToBinary(FilePath& exePath);
200 // In between launching the subprocess and handing off its IPC
201 // channel, there's a small window of time in which *we* might still
202 // be the channel listener, and receive messages. That's bad
203 // because we have no idea what to do with those messages. So queue
204 // them here until we hand off the eventual listener.
206 // FIXME/cjones: this strongly indicates bad design. Shame on us.
207 std::queue<IPC::Message> mQueue;
210 #ifdef MOZ_NUWA_PROCESS
211 class GeckoExistingProcessHost MOZ_FINAL : public GeckoChildProcessHost
213 public:
214 GeckoExistingProcessHost(GeckoProcessType aProcessType,
215 base::ProcessHandle aProcess,
216 const FileDescriptor& aFileDescriptor,
217 ChildPrivileges aPrivileges=base::PRIVILEGES_DEFAULT);
219 ~GeckoExistingProcessHost();
221 virtual bool PerformAsyncLaunch(StringVector aExtraOpts=StringVector(),
222 base::ProcessArchitecture aArch=base::GetCurrentProcessArchitecture()) MOZ_OVERRIDE;
224 virtual void InitializeChannel() MOZ_OVERRIDE;
226 private:
227 base::ProcessHandle mExistingProcessHandle;
228 mozilla::ipc::FileDescriptor mExistingFileDescriptor;
230 #endif /* MOZ_NUWA_PROCESS */
232 } /* namespace ipc */
233 } /* namespace mozilla */
235 #endif /* __IPC_GLUE_GECKOCHILDPROCESSHOST_H__ */