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