Index: chrome/android/webapk/libs/common/src/org/chromium/webapk/lib/common/WebApkSandboxedProcessService.java |
diff --git a/chrome/android/webapk/libs/common/src/org/chromium/webapk/lib/common/WebApkSandboxedProcessService.java b/chrome/android/webapk/libs/common/src/org/chromium/webapk/lib/common/WebApkSandboxedProcessService.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1f733054c3b00bf1d51861450586829d9f4b9459 |
--- /dev/null |
+++ b/chrome/android/webapk/libs/common/src/org/chromium/webapk/lib/common/WebApkSandboxedProcessService.java |
@@ -0,0 +1,112 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.webapk.lib.common; |
+ |
+import android.app.Service; |
+import android.content.Context; |
+import android.content.Intent; |
+import android.os.IBinder; |
+import android.util.Log; |
+ |
+import dalvik.system.BaseDexClassLoader; |
+ |
+import java.lang.reflect.Constructor; |
+import java.lang.reflect.InvocationTargetException; |
+import java.lang.reflect.Method; |
+ |
+/** |
+ * Child process service hosted by WebAPKs. This class uses BaseDexClassLoader to load Chrome's |
+ * {@link ChildProcessServiceImpl} which loads Chrome's native libraries, initializes JNI and |
+ * creates renderer processes. |
+ */ |
+public class WebApkSandboxedProcessService extends Service { |
+ private static final String CHILD_PROCESS_SERVICE_IMPL = |
+ "org.chromium.content.app.ChildProcessServiceImpl"; |
+ private static final String TAG = "cr_WebApkSandboxedProcessService"; |
+ |
+ private Class<?> mChildProcessServiceImplClass; |
+ private Object mChildProcessServiceImplInstance; |
+ private static ClassLoader sClassLoader; |
+ |
+ /** |
+ * Gets/creates ClassLoader for loading {@link CHILD_PROCESS_SERVICE_IMPL}. |
+ * @param context WebAPK's context. |
+ * @return The ClassLoader. |
+ */ |
+ private static ClassLoader getClassLoaderInstance(Context context) { |
+ if (sClassLoader == null) { |
+ sClassLoader = createClassLoader(context); |
+ } |
+ return sClassLoader; |
+ } |
+ |
+ /** |
+ * Creates ClassLoader for loading {@link CHILD_PROCESS_SERVICE_IMPL}. |
+ * @param context WebAPK's context. |
+ * @return The ClassLoader. |
+ */ |
+ private static ClassLoader createClassLoader(Context context) { |
+ Context remoteContext = WebApkUtils.getHostBrowserContext(context); |
+ String dexPath = remoteContext.getApplicationInfo().sourceDir; |
+ String libraryPath = remoteContext.getApplicationInfo().nativeLibraryDir; |
+ return new BaseDexClassLoader(dexPath, null, libraryPath, |
+ ClassLoader.getSystemClassLoader()); |
pkotwicz
2016/06/10 21:29:47
can you instead return remoteContext.getClassLoade
Xi Han
2016/06/13 20:04:10
It works! Updated the ChildProcessServiceImpl#crea
|
+ } |
+ |
+ @Override |
+ public void onCreate() { |
+ super.onCreate(); |
+ |
+ try { |
+ BaseDexClassLoader classLoader = (BaseDexClassLoader) getClassLoaderInstance( |
+ getApplicationContext()); |
pkotwicz
2016/06/10 21:29:47
Can the |classLoader| variable be of type ClassLoa
Xi Han
2016/06/13 20:04:11
Done.
|
+ mChildProcessServiceImplClass = |
+ classLoader.loadClass(CHILD_PROCESS_SERVICE_IMPL); |
pkotwicz
2016/06/10 21:29:47
Nit: Can you call mChildProcessServiceImplClass.ne
Xi Han
2016/06/13 20:04:10
Thanks for catching this. I forgot to remove the c
|
+ Constructor<?> childProcessServiceImplConstructor = |
+ mChildProcessServiceImplClass.getConstructor(); |
+ mChildProcessServiceImplInstance = childProcessServiceImplConstructor.newInstance(); |
pkotwicz
2016/06/10 21:29:47
Nit: mChildProcessServiceImplClass.newInstance()
Xi Han
2016/06/13 20:04:10
Done.
|
+ |
+ Method createMethod = mChildProcessServiceImplClass.getMethod("create", |
+ Context.class, Context.class, ClassLoader.class); |
+ createMethod.invoke(mChildProcessServiceImplInstance, getApplicationContext(), |
+ WebApkUtils.getHostBrowserContext(getApplicationContext()), classLoader); |
+ } catch (Exception e) { |
+ Log.d(TAG, "Unable to create a ChildProcessServiceImpl for the WebAPK."); |
+ e.printStackTrace(); |
+ } |
+ } |
+ |
+ @Override |
+ public IBinder onBind(Intent intent) { |
+ // We call stopSelf() to request that this service be stopped as soon as the client |
+ // unbinds. Otherwise the system may keep it around and available for a reconnect. The |
+ // child processes do not currently support reconnect; they must be initialized from |
+ // scratch every time. |
+ stopSelf(); |
+ try { |
+ Method bindMethod = mChildProcessServiceImplClass.getMethod( |
+ "bind", Intent.class); |
+ return (IBinder) bindMethod.invoke(mChildProcessServiceImplInstance, intent); |
+ } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException |
pkotwicz
2016/06/10 21:29:47
Nit: Can you replace this with catch (Exception e)
Xi Han
2016/06/13 20:04:10
Done.
|
+ | InvocationTargetException e) { |
+ Log.d(TAG, "Unable to bind to the WebApkSandboxedProcessService."); |
+ e.printStackTrace(); |
+ } |
+ return null; |
+ } |
+ |
+ @Override |
+ public void onDestroy() { |
+ super.onDestroy(); |
+ try { |
+ Method destroyMethod = mChildProcessServiceImplClass.getMethod("destroy"); |
+ destroyMethod.invoke(mChildProcessServiceImplInstance); |
+ } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException |
+ | InvocationTargetException e) { |
pkotwicz
2016/06/10 21:29:47
Nit: Can you replace this with catch (Exception e)
Xi Han
2016/06/13 20:04:10
Done.
|
+ Log.d(TAG, "Unable to destroy the WebApkSandboxedProcessService."); |
+ e.printStackTrace(); |
+ } |
+ } |
+} |