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.webapk.shell_apk; | 5 package org.chromium.webapk.shell_apk; |
6 | 6 |
7 import android.app.Service; | 7 import android.app.Service; |
| 8 import android.content.Context; |
8 import android.content.Intent; | 9 import android.content.Intent; |
9 import android.os.IBinder; | 10 import android.os.IBinder; |
| 11 import android.util.Log; |
10 | 12 |
11 import org.chromium.webapk.lib.runtime_library.WebApkServiceImpl; | 13 import org.chromium.webapk.lib.common.WebApkUtils; |
| 14 |
| 15 import java.io.File; |
| 16 import java.lang.reflect.Constructor; |
12 | 17 |
13 /** | 18 /** |
14 * Implements services offered by the WebAPK to Chrome. | 19 * Shell class for services provided by WebAPK to Chrome. Extracts code with imp
lementation of |
| 20 * services from .dex file in Chrome APK. |
15 */ | 21 */ |
16 public class WebApkServiceFactory extends Service { | 22 public class WebApkServiceFactory extends Service { |
| 23 private static final String TAG = "cr_WebApkServiceFactory"; |
| 24 |
| 25 /** |
| 26 * Name of the class with IBinder API implementation. |
| 27 */ |
| 28 private static final String WEBAPK_SERVICE_IMPL_CLASS_NAME = |
| 29 "org.chromium.webapk.lib.runtime_library.WebApkServiceImpl"; |
| 30 |
| 31 /* |
| 32 * ClassLoader for loading {@link WEBAPK_SERVICE_IMPL_CLASS_NAME}. Static so
that all |
| 33 * {@link WebApkServiceFactory} service instatiations use the same ClassLoad
er during the app's |
| 34 * lifetime. |
| 35 */ |
| 36 private static ClassLoader sClassLoader; |
17 | 37 |
18 @Override | 38 @Override |
19 public IBinder onBind(Intent intent) { | 39 public IBinder onBind(Intent intent) { |
20 return new WebApkServiceImpl(this, R.drawable.app_icon); | 40 ClassLoader webApkClassLoader = getClassLoaderInstance(this); |
| 41 if (webApkClassLoader == null) { |
| 42 Log.w(TAG, "Unable to create ClassLoader."); |
| 43 return null; |
| 44 } |
| 45 |
| 46 try { |
| 47 Class<?> webApkServiceImplClass = |
| 48 webApkClassLoader.loadClass(WEBAPK_SERVICE_IMPL_CLASS_NAME); |
| 49 Constructor<?> webApkServiceImplConstructor = |
| 50 webApkServiceImplClass.getConstructor(Context.class, int.cla
ss); |
| 51 return (IBinder) webApkServiceImplConstructor.newInstance( |
| 52 new Object[] {this, R.drawable.app_icon}); |
| 53 } catch (Exception e) { |
| 54 Log.w(TAG, "Unable to create WebApkServiceImpl."); |
| 55 e.printStackTrace(); |
| 56 return null; |
| 57 } |
| 58 } |
| 59 |
| 60 /** |
| 61 * Gets / creates ClassLaoder for loading {@link WEBAPK_SERVICE_IMPL_CLASS_N
AME}. |
| 62 * @param context WebAPK's context. |
| 63 * @return The ClassLoader. |
| 64 */ |
| 65 private static ClassLoader getClassLoaderInstance(Context context) { |
| 66 if (sClassLoader == null) { |
| 67 sClassLoader = createClassLoader(context); |
| 68 } |
| 69 return sClassLoader; |
| 70 } |
| 71 |
| 72 /** |
| 73 * Creates ClassLoader for loading {@link WEBAPK_SERVICE_IMPL_CLASS_NAME}. |
| 74 * @param context WebAPK's context. |
| 75 * @return The ClassLoader. |
| 76 */ |
| 77 private static ClassLoader createClassLoader(Context context) { |
| 78 Context remoteContext = WebApkUtils.getHostBrowserContext(context); |
| 79 if (remoteContext == null) { |
| 80 Log.w(TAG, "Failed to get remote context."); |
| 81 return null; |
| 82 } |
| 83 |
| 84 File localDexDir = context.getDir("dex", Context.MODE_PRIVATE); |
| 85 File remoteDexFile = |
| 86 new File(remoteContext.getDir("dex", Context.MODE_PRIVATE), "web
_apk.dex"); |
| 87 return DexLoader.load(remoteContext, "web_apk.dex", WEBAPK_SERVICE_IMPL_
CLASS_NAME, |
| 88 remoteDexFile, localDexDir); |
21 } | 89 } |
22 } | 90 } |
OLD | NEW |