|
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/. */ |
|
4 |
|
5 #ifndef __IPC_GLUE_GECKOCHILDPROCESSHOST_H__ |
|
6 #define __IPC_GLUE_GECKOCHILDPROCESSHOST_H__ |
|
7 |
|
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" |
|
13 |
|
14 #include "mozilla/DebugOnly.h" |
|
15 #include "mozilla/ipc/FileDescriptor.h" |
|
16 #include "mozilla/Monitor.h" |
|
17 #include "mozilla/StaticPtr.h" |
|
18 |
|
19 #include "nsCOMPtr.h" |
|
20 #include "nsXULAppAPI.h" // for GeckoProcessType |
|
21 #include "nsString.h" |
|
22 |
|
23 class nsIFile; |
|
24 |
|
25 namespace mozilla { |
|
26 namespace ipc { |
|
27 |
|
28 class GeckoChildProcessHost : public ChildProcessHost |
|
29 { |
|
30 protected: |
|
31 typedef mozilla::Monitor Monitor; |
|
32 typedef std::vector<std::string> StringVector; |
|
33 |
|
34 public: |
|
35 typedef base::ChildPrivileges ChildPrivileges; |
|
36 typedef base::ProcessHandle ProcessHandle; |
|
37 |
|
38 static ChildPrivileges DefaultChildPrivileges(); |
|
39 |
|
40 GeckoChildProcessHost(GeckoProcessType aProcessType, |
|
41 ChildPrivileges aPrivileges=base::PRIVILEGES_DEFAULT); |
|
42 |
|
43 ~GeckoChildProcessHost(); |
|
44 |
|
45 static nsresult GetArchitecturesForBinary(const char *path, uint32_t *result); |
|
46 |
|
47 static uint32_t GetSupportedArchitecturesForProcessType(GeckoProcessType type); |
|
48 |
|
49 // Block until the IPC channel for our subprocess is initialized, |
|
50 // but no longer. The child process may or may not have been |
|
51 // created when this method returns. |
|
52 bool AsyncLaunch(StringVector aExtraOpts=StringVector()); |
|
53 |
|
54 // Block until the IPC channel for our subprocess is initialized and |
|
55 // the OS process is created. The subprocess may or may not have |
|
56 // connected back to us when this method returns. |
|
57 // |
|
58 // NB: on POSIX, this method is relatively cheap, and doesn't |
|
59 // require disk IO. On win32 however, it requires at least the |
|
60 // analogue of stat(). This difference induces a semantic |
|
61 // difference in this method: on POSIX, when we return, we know the |
|
62 // subprocess has been created, but we don't know whether its |
|
63 // executable image can be loaded. On win32, we do know that when |
|
64 // we return. But we don't know if dynamic linking succeeded on |
|
65 // either platform. |
|
66 bool LaunchAndWaitForProcessHandle(StringVector aExtraOpts=StringVector()); |
|
67 |
|
68 // Block until the child process has been created and it connects to |
|
69 // the IPC channel, meaning it's fully initialized. (Or until an |
|
70 // error occurs.) |
|
71 bool SyncLaunch(StringVector aExtraOpts=StringVector(), |
|
72 int32_t timeoutMs=0, |
|
73 base::ProcessArchitecture arch=base::GetCurrentProcessArchitecture()); |
|
74 |
|
75 virtual bool PerformAsyncLaunch(StringVector aExtraOpts=StringVector(), |
|
76 base::ProcessArchitecture aArch=base::GetCurrentProcessArchitecture()); |
|
77 |
|
78 virtual void OnChannelConnected(int32_t peer_pid); |
|
79 virtual void OnMessageReceived(const IPC::Message& aMsg); |
|
80 virtual void OnChannelError(); |
|
81 virtual void GetQueuedMessages(std::queue<IPC::Message>& queue); |
|
82 |
|
83 virtual void InitializeChannel(); |
|
84 |
|
85 virtual bool CanShutdown() { return true; } |
|
86 |
|
87 virtual void OnWaitableEventSignaled(base::WaitableEvent *event); |
|
88 |
|
89 IPC::Channel* GetChannel() { |
|
90 return channelp(); |
|
91 } |
|
92 |
|
93 base::WaitableEvent* GetShutDownEvent() { |
|
94 return GetProcessEvent(); |
|
95 } |
|
96 |
|
97 // Returns a "borrowed" handle to the child process - the handle returned |
|
98 // by this function must not be closed by the caller. |
|
99 ProcessHandle GetChildProcessHandle() { |
|
100 return mChildProcessHandle; |
|
101 } |
|
102 |
|
103 // Returns an "owned" handle to the child process - the handle returned |
|
104 // by this function must be closed by the caller. |
|
105 ProcessHandle GetOwnedChildProcessHandle() { |
|
106 ProcessHandle handle; |
|
107 // We use OpenPrivilegedProcessHandle as that is where our |
|
108 // mChildProcessHandle initially came from. |
|
109 bool ok = base::OpenPrivilegedProcessHandle(base::GetProcId(mChildProcessHandle), |
|
110 &handle); |
|
111 NS_ASSERTION(ok, "Failed to get owned process handle"); |
|
112 return ok ? handle : 0; |
|
113 } |
|
114 |
|
115 GeckoProcessType GetProcessType() { |
|
116 return mProcessType; |
|
117 } |
|
118 |
|
119 #ifdef XP_MACOSX |
|
120 task_t GetChildTask() { |
|
121 return mChildTask; |
|
122 } |
|
123 #endif |
|
124 |
|
125 /** |
|
126 * Must run on the IO thread. Cause the OS process to exit and |
|
127 * ensure its OS resources are cleaned up. |
|
128 */ |
|
129 void Join(); |
|
130 |
|
131 // For bug 943174: Skip the EnsureProcessTerminated call in the destructor. |
|
132 void SetAlreadyDead(); |
|
133 |
|
134 void SetSandboxEnabled(bool aSandboxEnabled) { |
|
135 mSandboxEnabled = aSandboxEnabled; |
|
136 } |
|
137 |
|
138 protected: |
|
139 GeckoProcessType mProcessType; |
|
140 bool mSandboxEnabled; |
|
141 ChildPrivileges mPrivileges; |
|
142 Monitor mMonitor; |
|
143 FilePath mProcessPath; |
|
144 |
|
145 // This value must be accessed while holding mMonitor. |
|
146 enum { |
|
147 // This object has been constructed, but the OS process has not |
|
148 // yet. |
|
149 CREATING_CHANNEL = 0, |
|
150 // The IPC channel for our subprocess has been created, but the OS |
|
151 // process has still not been created. |
|
152 CHANNEL_INITIALIZED, |
|
153 // The OS process has been created, but it hasn't yet connected to |
|
154 // our IPC channel. |
|
155 PROCESS_CREATED, |
|
156 // The process is launched and connected to our IPC channel. All |
|
157 // is well. |
|
158 PROCESS_CONNECTED, |
|
159 PROCESS_ERROR |
|
160 } mProcessState; |
|
161 |
|
162 static int32_t mChildCounter; |
|
163 |
|
164 void PrepareLaunch(); |
|
165 |
|
166 #ifdef XP_WIN |
|
167 void InitWindowsGroupID(); |
|
168 nsString mGroupId; |
|
169 #endif |
|
170 |
|
171 #if defined(OS_POSIX) |
|
172 base::file_handle_mapping_vector mFileMap; |
|
173 #endif |
|
174 |
|
175 base::WaitableEventWatcher::Delegate* mDelegate; |
|
176 |
|
177 ProcessHandle mChildProcessHandle; |
|
178 #if defined(OS_MACOSX) |
|
179 task_t mChildTask; |
|
180 #endif |
|
181 |
|
182 void OpenPrivilegedHandle(base::ProcessId aPid); |
|
183 |
|
184 private: |
|
185 DISALLOW_EVIL_CONSTRUCTORS(GeckoChildProcessHost); |
|
186 |
|
187 // Does the actual work for AsyncLaunch, on the IO thread. |
|
188 bool PerformAsyncLaunchInternal(std::vector<std::string>& aExtraOpts, |
|
189 base::ProcessArchitecture arch); |
|
190 |
|
191 bool RunPerformAsyncLaunch(StringVector aExtraOpts=StringVector(), |
|
192 base::ProcessArchitecture aArch=base::GetCurrentProcessArchitecture()); |
|
193 |
|
194 static void GetPathToBinary(FilePath& exePath); |
|
195 static void CacheGreDir(); |
|
196 |
|
197 // In between launching the subprocess and handing off its IPC |
|
198 // channel, there's a small window of time in which *we* might still |
|
199 // be the channel listener, and receive messages. That's bad |
|
200 // because we have no idea what to do with those messages. So queue |
|
201 // them here until we hand off the eventual listener. |
|
202 // |
|
203 // FIXME/cjones: this strongly indicates bad design. Shame on us. |
|
204 std::queue<IPC::Message> mQueue; |
|
205 |
|
206 static StaticRefPtr<nsIFile> sGreDir; |
|
207 static DebugOnly<bool> sGreDirCached; |
|
208 }; |
|
209 |
|
210 #ifdef MOZ_NUWA_PROCESS |
|
211 class GeckoExistingProcessHost MOZ_FINAL : public GeckoChildProcessHost |
|
212 { |
|
213 public: |
|
214 GeckoExistingProcessHost(GeckoProcessType aProcessType, |
|
215 base::ProcessHandle aProcess, |
|
216 const FileDescriptor& aFileDescriptor, |
|
217 ChildPrivileges aPrivileges=base::PRIVILEGES_DEFAULT); |
|
218 |
|
219 ~GeckoExistingProcessHost(); |
|
220 |
|
221 virtual bool PerformAsyncLaunch(StringVector aExtraOpts=StringVector(), |
|
222 base::ProcessArchitecture aArch=base::GetCurrentProcessArchitecture()) MOZ_OVERRIDE; |
|
223 |
|
224 virtual void InitializeChannel() MOZ_OVERRIDE; |
|
225 |
|
226 private: |
|
227 base::ProcessHandle mExistingProcessHandle; |
|
228 mozilla::ipc::FileDescriptor mExistingFileDescriptor; |
|
229 }; |
|
230 #endif /* MOZ_NUWA_PROCESS */ |
|
231 |
|
232 } /* namespace ipc */ |
|
233 } /* namespace mozilla */ |
|
234 |
|
235 #endif /* __IPC_GLUE_GECKOCHILDPROCESSHOST_H__ */ |