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 dalvik.system.BaseDexClassLoader; | |
14 | |
15 import java.lang.reflect.Constructor; | |
16 import java.lang.reflect.InvocationTargetException; | |
17 import java.lang.reflect.Method; | |
18 | |
19 /** | |
20 * Child process service hosted by WebAPKs. This class uses BaseDexClassLoader t o load Chrome's | |
21 * {@link ChildProcessServiceImpl} which loads Chrome's native libraries, initia lizes JNI and | |
22 * creates renderer processes. | |
23 */ | |
24 public class WebApkSandboxedProcessService extends Service { | |
25 private static final String CHILD_PROCESS_SERVICE_IMPL = | |
26 "org.chromium.content.app.ChildProcessServiceImpl"; | |
27 private static final String TAG = "cr_WebApkSandboxedProcessService"; | |
28 | |
29 private Class<?> mChildProcessServiceImplClass; | |
30 private Object mChildProcessServiceImplInstance; | |
31 private static ClassLoader sClassLoader; | |
32 | |
33 /** | |
34 * Gets/creates ClassLoader for loading {@link CHILD_PROCESS_SERVICE_IMPL}. | |
35 * @param context WebAPK's context. | |
36 * @return The ClassLoader. | |
37 */ | |
38 private static ClassLoader getClassLoaderInstance(Context context) { | |
39 if (sClassLoader == null) { | |
40 sClassLoader = createClassLoader(context); | |
41 } | |
42 return sClassLoader; | |
43 } | |
44 | |
45 /** | |
46 * Creates ClassLoader for loading {@link CHILD_PROCESS_SERVICE_IMPL}. | |
47 * @param context WebAPK's context. | |
48 * @return The ClassLoader. | |
49 */ | |
50 private static ClassLoader createClassLoader(Context context) { | |
51 Context remoteContext = WebApkUtils.getHostBrowserContext(context); | |
52 String dexPath = remoteContext.getApplicationInfo().sourceDir; | |
53 String libraryPath = remoteContext.getApplicationInfo().nativeLibraryDir ; | |
54 return new BaseDexClassLoader(dexPath, null, libraryPath, | |
55 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
| |
56 } | |
57 | |
58 @Override | |
59 public void onCreate() { | |
60 super.onCreate(); | |
61 | |
62 try { | |
63 BaseDexClassLoader classLoader = (BaseDexClassLoader) getClassLoader Instance( | |
64 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.
| |
65 mChildProcessServiceImplClass = | |
66 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
| |
67 Constructor<?> childProcessServiceImplConstructor = | |
68 mChildProcessServiceImplClass.getConstructor(); | |
69 mChildProcessServiceImplInstance = childProcessServiceImplConstructo r.newInstance(); | |
pkotwicz
2016/06/10 21:29:47
Nit: mChildProcessServiceImplClass.newInstance()
Xi Han
2016/06/13 20:04:10
Done.
| |
70 | |
71 Method createMethod = mChildProcessServiceImplClass.getMethod("creat e", | |
72 Context.class, Context.class, ClassLoader.class); | |
73 createMethod.invoke(mChildProcessServiceImplInstance, getApplication Context(), | |
74 WebApkUtils.getHostBrowserContext(getApplicationContext()), classLoader); | |
75 } catch (Exception e) { | |
76 Log.d(TAG, "Unable to create a ChildProcessServiceImpl for the WebAP K."); | |
77 e.printStackTrace(); | |
78 } | |
79 } | |
80 | |
81 @Override | |
82 public IBinder onBind(Intent intent) { | |
83 // We call stopSelf() to request that this service be stopped as soon as the client | |
84 // unbinds. Otherwise the system may keep it around and available for a reconnect. The | |
85 // child processes do not currently support reconnect; they must be init ialized from | |
86 // scratch every time. | |
87 stopSelf(); | |
88 try { | |
89 Method bindMethod = mChildProcessServiceImplClass.getMethod( | |
90 "bind", Intent.class); | |
91 return (IBinder) bindMethod.invoke(mChildProcessServiceImplInstance, intent); | |
92 } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumen tException | |
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.
| |
93 | InvocationTargetException e) { | |
94 Log.d(TAG, "Unable to bind to the WebApkSandboxedProcessService."); | |
95 e.printStackTrace(); | |
96 } | |
97 return null; | |
98 } | |
99 | |
100 @Override | |
101 public void onDestroy() { | |
102 super.onDestroy(); | |
103 try { | |
104 Method destroyMethod = mChildProcessServiceImplClass.getMethod("dest roy"); | |
105 destroyMethod.invoke(mChildProcessServiceImplInstance); | |
106 } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumen tException | |
107 | 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.
| |
108 Log.d(TAG, "Unable to destroy the WebApkSandboxedProcessService."); | |
109 e.printStackTrace(); | |
110 } | |
111 } | |
112 } | |
OLD | NEW |