OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.base; | 5 package org.chromium.base; |
6 | 6 |
7 import android.content.ComponentName; | 7 import android.content.ComponentName; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.Intent; | 9 import android.content.Intent; |
10 import android.content.ServiceConnection; | 10 import android.content.ServiceConnection; |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 | 173 |
174 public int getPid() { | 174 public int getPid() { |
175 return mPid; | 175 return mPid; |
176 } | 176 } |
177 } | 177 } |
178 | 178 |
179 /** | 179 /** |
180 * Spawns and connects to a child process. | 180 * Spawns and connects to a child process. |
181 * May not be called from the main thread. | 181 * May not be called from the main thread. |
182 * | 182 * |
| 183 * @param context context used to obtain the application context. |
183 * @param commandLine the child process command line argv. | 184 * @param commandLine the child process command line argv. |
184 * @return the PID of the started process or 0 if the process could not be s
tarted. | 185 * @return the PID of the started process or 0 if the process could not be s
tarted. |
185 */ | 186 */ |
186 @CalledByNative | 187 @CalledByNative |
187 private static int launchClient( | 188 private static int launchClient(final Context context, final String[] comman
dLine, |
188 final String[] commandLine, final FileDescriptorInfo[] filesToMap) { | 189 final FileDescriptorInfo[] filesToMap) { |
189 if (ThreadUtils.runningOnUiThread()) { | 190 if (ThreadUtils.runningOnUiThread()) { |
190 // This can't be called on the main thread as the native side will b
lock until | 191 // This can't be called on the main thread as the native side will b
lock until |
191 // onServiceConnected above is called, which cannot happen if the ma
in thread is | 192 // onServiceConnected above is called, which cannot happen if the ma
in thread is |
192 // blocked. | 193 // blocked. |
193 throw new RuntimeException("launchClient cannot be called on the mai
n thread"); | 194 throw new RuntimeException("launchClient cannot be called on the mai
n thread"); |
194 } | 195 } |
195 | 196 |
196 ClientServiceConnection connection = | 197 ClientServiceConnection connection = |
197 sConnectionAllocator.allocateConnection(commandLine, filesToMap)
; | 198 sConnectionAllocator.allocateConnection(commandLine, filesToMap)
; |
198 Intent intent = new Intent(); | 199 Intent intent = new Intent(); |
199 String className = connection.getServiceClassName(); | 200 String className = connection.getServiceClassName(); |
200 String packageName = ContextUtils.getApplicationContext().getPackageName
(); | 201 intent.setComponent(new ComponentName(context.getPackageName(), classNam
e)); |
201 intent.setComponent(new ComponentName(packageName, className)); | 202 if (!context.bindService( |
202 if (!ContextUtils.getApplicationContext().bindService( | |
203 intent, connection, Context.BIND_AUTO_CREATE | Context.BIND_
IMPORTANT)) { | 203 intent, connection, Context.BIND_AUTO_CREATE | Context.BIND_
IMPORTANT)) { |
204 Log.e(TAG, "Failed to bind service: " + packageName + "." + classNam
e); | 204 Log.e(TAG, "Failed to bind service: " + context.getPackageName() + "
." + className); |
205 sConnectionAllocator.freeConnection(connection); | 205 sConnectionAllocator.freeConnection(connection); |
206 return 0; | 206 return 0; |
207 } | 207 } |
208 | 208 |
209 connection.waitForConnection(); | 209 connection.waitForConnection(); |
210 | 210 |
211 return connection.getPid(); | 211 return connection.getPid(); |
212 } | 212 } |
213 | 213 |
214 /** | 214 /** |
215 * Blocks until the main method invoked by a previous call to launchClient t
erminates or until | 215 * Blocks until the main method invoked by a previous call to launchClient t
erminates or until |
216 * the specified time-out expires. | 216 * the specified time-out expires. |
217 * Returns immediately if main has already returned. | 217 * Returns immediately if main has already returned. |
| 218 * @param context context used to obtain the application context. |
218 * @param pid the process ID that was returned by the call to launchClient | 219 * @param pid the process ID that was returned by the call to launchClient |
219 * @param timeoutMs the timeout in milliseconds after which the method retur
ns even if main has | 220 * @param timeoutMs the timeout in milliseconds after which the method retur
ns even if main has |
220 * not returned. | 221 * not returned. |
221 * @return the return code returned by the main method or whether it timed-o
ut. | 222 * @return the return code returned by the main method or whether it timed-o
ut. |
222 */ | 223 */ |
223 @CalledByNative | 224 @CalledByNative |
224 private static MainReturnCodeResult waitForMainToReturn(int pid, int timeout
Ms) { | 225 private static MainReturnCodeResult waitForMainToReturn( |
| 226 Context context, int pid, int timeoutMs) { |
225 ClientServiceConnection connection = sConnectionAllocator.getConnectionB
yPid(pid); | 227 ClientServiceConnection connection = sConnectionAllocator.getConnectionB
yPid(pid); |
226 if (connection == null) { | 228 if (connection == null) { |
227 Log.e(TAG, "waitForMainToReturn called on unknown connection for pid
" + pid); | 229 Log.e(TAG, "waitForMainToReturn called on unknown connection for pid
" + pid); |
228 return null; | 230 return null; |
229 } | 231 } |
230 try { | 232 try { |
231 return connection.getService().waitForMainToReturn(timeoutMs); | 233 return connection.getService().waitForMainToReturn(timeoutMs); |
232 } catch (RemoteException e) { | 234 } catch (RemoteException e) { |
233 Log.e(TAG, "Remote call to waitForMainToReturn failed."); | 235 Log.e(TAG, "Remote call to waitForMainToReturn failed."); |
234 return null; | 236 return null; |
235 } finally { | 237 } finally { |
236 freeConnection(connection); | 238 freeConnection(context, connection); |
237 } | 239 } |
238 } | 240 } |
239 | 241 |
240 @CalledByNative | 242 @CalledByNative |
241 private static boolean terminate(int pid, int exitCode, boolean wait) { | 243 private static boolean terminate(Context context, int pid, int exitCode, boo
lean wait) { |
242 ClientServiceConnection connection = sConnectionAllocator.getConnectionB
yPid(pid); | 244 ClientServiceConnection connection = sConnectionAllocator.getConnectionB
yPid(pid); |
243 if (connection == null) { | 245 if (connection == null) { |
244 Log.e(TAG, "terminate called on unknown connection for pid " + pid); | 246 Log.e(TAG, "terminate called on unknown connection for pid " + pid); |
245 return false; | 247 return false; |
246 } | 248 } |
247 try { | 249 try { |
248 if (wait) { | 250 if (wait) { |
249 connection.getService().forceStopSynchronous(exitCode); | 251 connection.getService().forceStopSynchronous(exitCode); |
250 } else { | 252 } else { |
251 connection.getService().forceStop(exitCode); | 253 connection.getService().forceStop(exitCode); |
252 } | 254 } |
253 } catch (RemoteException e) { | 255 } catch (RemoteException e) { |
254 // We expect this failure, since the forceStop's service implementat
ion calls | 256 // We expect this failure, since the forceStop's service implementat
ion calls |
255 // System.exit(). | 257 // System.exit(). |
256 } finally { | 258 } finally { |
257 freeConnection(connection); | 259 freeConnection(context, connection); |
258 } | 260 } |
259 return true; | 261 return true; |
260 } | 262 } |
261 | 263 |
262 private static void freeConnection(ClientServiceConnection connection) { | 264 private static void freeConnection(Context context, ClientServiceConnection
connection) { |
263 ContextUtils.getApplicationContext().unbindService(connection); | 265 context.unbindService(connection); |
264 sConnectionAllocator.freeConnection(connection); | 266 sConnectionAllocator.freeConnection(connection); |
265 } | 267 } |
266 | 268 |
267 /** Does not take ownership of of fds. */ | 269 /** Does not take ownership of of fds. */ |
268 @CalledByNative | 270 @CalledByNative |
269 private static FileDescriptorInfo[] makeFdInfoArray(int[] keys, int[] fds) { | 271 private static FileDescriptorInfo[] makeFdInfoArray(int[] keys, int[] fds) { |
270 FileDescriptorInfo[] fdInfos = new FileDescriptorInfo[keys.length]; | 272 FileDescriptorInfo[] fdInfos = new FileDescriptorInfo[keys.length]; |
271 for (int i = 0; i < keys.length; i++) { | 273 for (int i = 0; i < keys.length; i++) { |
272 FileDescriptorInfo fdInfo = makeFdInfo(keys[i], fds[i]); | 274 FileDescriptorInfo fdInfo = makeFdInfo(keys[i], fds[i]); |
273 if (fdInfo == null) { | 275 if (fdInfo == null) { |
274 Log.e(TAG, "Failed to make file descriptor (" + keys[i] + ", " +
fds[i] + ")."); | 276 Log.e(TAG, "Failed to make file descriptor (" + keys[i] + ", " +
fds[i] + ")."); |
275 return null; | 277 return null; |
276 } | 278 } |
277 fdInfos[i] = fdInfo; | 279 fdInfos[i] = fdInfo; |
278 } | 280 } |
279 return fdInfos; | 281 return fdInfos; |
280 } | 282 } |
281 | 283 |
282 private static FileDescriptorInfo makeFdInfo(int id, int fd) { | 284 private static FileDescriptorInfo makeFdInfo(int id, int fd) { |
283 ParcelFileDescriptor parcelableFd = null; | 285 ParcelFileDescriptor parcelableFd = null; |
284 try { | 286 try { |
285 parcelableFd = ParcelFileDescriptor.fromFd(fd); | 287 parcelableFd = ParcelFileDescriptor.fromFd(fd); |
286 } catch (IOException e) { | 288 } catch (IOException e) { |
287 Log.e(TAG, "Invalid FD provided for process connection, aborting con
nection.", e); | 289 Log.e(TAG, "Invalid FD provided for process connection, aborting con
nection.", e); |
288 return null; | 290 return null; |
289 } | 291 } |
290 return new FileDescriptorInfo(id, parcelableFd, 0 /* offset */, 0 /* siz
e */); | 292 return new FileDescriptorInfo(id, parcelableFd, 0 /* offset */, 0 /* siz
e */); |
291 } | 293 } |
292 } | 294 } |
OLD | NEW |