Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.webapk.lib.common; | |
| 6 | |
| 7 import android.app.Service; | |
| 8 import android.content.Context; | |
| 9 import android.content.Intent; | |
| 10 import android.os.IBinder; | |
| 11 import android.util.Log; | |
| 12 | |
| 13 import java.lang.reflect.Method; | |
| 14 | |
| 15 /** | |
| 16 * Child process service hosted by WebAPKs. This class uses BaseDexClassLoader t o load Chrome's | |
| 17 * {@link ChildProcessServiceImpl} which loads Chrome's native libraries, initia lizes JNI and | |
| 18 * creates renderer processes. | |
|
pkotwicz
2016/06/15 14:13:50
Nit: Can you please update the comment? This class
Xi Han
2016/06/15 17:10:39
Done.
| |
| 19 */ | |
| 20 public class WebApkSandboxedProcessService extends Service { | |
| 21 private static final String CHILD_PROCESS_SERVICE_IMPL = | |
|
pkotwicz
2016/06/15 14:13:50
Nit: Rename to CHILD_PROCESS_SERVICE_IMPL_CLASS_NA
Xi Han
2016/06/15 17:10:39
Done.
| |
| 22 "org.chromium.content.app.ChildProcessServiceImpl"; | |
| 23 private static final String TAG = "cr_WebApkSandboxedProcessService"; | |
| 24 | |
| 25 private Class<?> mChildProcessServiceImplClass; | |
| 26 private Object mChildProcessServiceImplInstance; | |
| 27 private static ClassLoader sClassLoader; | |
| 28 | |
| 29 /** | |
| 30 * Gets/creates ClassLoader for loading {@link CHILD_PROCESS_SERVICE_IMPL}. | |
| 31 * @param context WebAPK's context. | |
| 32 * @return The ClassLoader. | |
| 33 */ | |
| 34 private static ClassLoader getClassLoaderInstance(Context context) { | |
| 35 if (sClassLoader == null) { | |
| 36 sClassLoader = createClassLoader(context); | |
| 37 } | |
| 38 return sClassLoader; | |
| 39 } | |
| 40 | |
| 41 /** | |
| 42 * Creates ClassLoader for loading {@link CHILD_PROCESS_SERVICE_IMPL}. | |
| 43 * @param context WebAPK's context. | |
| 44 * @return The ClassLoader. | |
| 45 */ | |
| 46 private static ClassLoader createClassLoader(Context context) { | |
| 47 Context remoteContext = WebApkUtils.getHostBrowserContext(context); | |
| 48 return remoteContext.getClassLoader(); | |
| 49 } | |
| 50 | |
| 51 @Override | |
| 52 public void onCreate() { | |
| 53 super.onCreate(); | |
| 54 | |
| 55 try { | |
|
pkotwicz
2016/06/15 14:13:50
Can you just do:
Context hostBrowserContext = WebA
Xi Han
2016/06/15 17:10:39
You are right, removed since we have to call the W
pkotwicz
2016/06/15 17:51:00
Thank you for making this change!
| |
| 56 ClassLoader classLoader = getClassLoaderInstance( | |
| 57 getApplicationContext()); | |
| 58 mChildProcessServiceImplClass = | |
| 59 classLoader.loadClass(CHILD_PROCESS_SERVICE_IMPL); | |
| 60 mChildProcessServiceImplInstance = mChildProcessServiceImplClass.new Instance(); | |
| 61 | |
| 62 Method createMethod = mChildProcessServiceImplClass.getMethod("creat e", | |
| 63 Context.class, Context.class); | |
| 64 createMethod.invoke(mChildProcessServiceImplInstance, getApplication Context(), | |
| 65 WebApkUtils.getHostBrowserContext(getApplicationContext())); | |
| 66 } catch (Exception e) { | |
| 67 Log.d(TAG, "Unable to create a ChildProcessServiceImpl for the WebAP K."); | |
| 68 e.printStackTrace(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 @Override | |
| 73 public IBinder onBind(Intent intent) { | |
| 74 // We call stopSelf() to request that this service be stopped as soon as the client | |
| 75 // unbinds. Otherwise the system may keep it around and available for a reconnect. The | |
| 76 // child processes do not currently support reconnect; they must be init ialized from | |
| 77 // scratch every time. | |
| 78 stopSelf(); | |
| 79 try { | |
| 80 Method bindMethod = mChildProcessServiceImplClass.getMethod( | |
| 81 "bind", Intent.class); | |
| 82 return (IBinder) bindMethod.invoke(mChildProcessServiceImplInstance, intent); | |
| 83 } catch (Exception e) { | |
| 84 Log.d(TAG, "Unable to bind to the WebApkSandboxedProcessService."); | |
| 85 e.printStackTrace(); | |
| 86 } | |
| 87 return null; | |
| 88 } | |
| 89 | |
| 90 @Override | |
| 91 public void onDestroy() { | |
| 92 super.onDestroy(); | |
| 93 try { | |
| 94 Method destroyMethod = mChildProcessServiceImplClass.getMethod("dest roy"); | |
| 95 destroyMethod.invoke(mChildProcessServiceImplInstance); | |
| 96 } catch (Exception e) { | |
| 97 Log.d(TAG, "Unable to destroy the WebApkSandboxedProcessService."); | |
| 98 e.printStackTrace(); | |
| 99 } | |
| 100 } | |
| 101 } | |
| OLD | NEW |