| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.minting; | 5 package org.chromium.minting; |
| 6 | 6 |
| 7 import android.app.Service; | 7 import android.app.Service; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.os.IBinder; | 10 import android.os.IBinder; |
| 11 import android.util.Log; | 11 import android.util.Log; |
| 12 | 12 |
| 13 import org.chromium.minting.lib.common.WebAPKUtils; |
| 14 |
| 13 import java.io.File; | 15 import java.io.File; |
| 14 import java.lang.reflect.Constructor; | 16 import java.lang.reflect.Constructor; |
| 15 | 17 |
| 16 /** | 18 /** |
| 17 * Shell class for services provided by Web APK to Chrome. Extracts code with im
plementation of | 19 * Shell class for services provided by Web APK to Chrome. Extracts code with im
plementation of |
| 18 * services from .dex file in Chrome APK. | 20 * services from .dex file in Chrome APK. |
| 19 */ | 21 */ |
| 20 public class MintingServiceFactory extends Service { | 22 public class MintingServiceFactory extends Service { |
| 21 private static final String TAG = "cr.MintingServiceFactory"; | 23 private static final String TAG = "cr.MintingServiceFactory"; |
| 22 | 24 |
| 23 /** | 25 /** |
| 24 * Name of the class with IBinder API implementation. | 26 * Name of the class with IBinder API implementation. |
| 25 */ | 27 */ |
| 26 private static final String MINTED_SERVICE_IMPL_CLASS_NAME = | 28 private static final String MINTED_SERVICE_IMPL_CLASS_NAME = |
| 27 "org.chromium.minting.MintedServiceImpl"; | 29 "org.chromium.minting.MintedServiceImpl"; |
| 28 | 30 |
| 29 /** | 31 /** |
| 30 * ClassLoader for loading {@link MINTED_SERVICE_IMPL_CLASS_NAME}. Static so
that all | 32 * ClassLoader for loading {@link MINTED_SERVICE_IMPL_CLASS_NAME}. Static so
that all |
| 31 * {@link MintingServiceFactory} service instatiations use the same ClassLoa
der during the app's | 33 * {@link MintingServiceFactory} service instatiations use the same ClassLoa
der during the app's |
| 32 * lifetime. | 34 * lifetime. |
| 33 */ | 35 */ |
| 34 private static ClassLoader sClassLoader; | 36 private static ClassLoader sClassLoader; |
| 35 | 37 |
| 36 @Override | 38 @Override |
| 37 public IBinder onBind(Intent intent) { | 39 public IBinder onBind(Intent intent) { |
| 38 if (sClassLoader == null) { | 40 if (sClassLoader == null) { |
| 39 String remotePackageName = | 41 Context remoteContext = WebAPKUtils.getHostBrowserContext(this); |
| 40 getSharedPreferences(MintingApplication.MINT_PREFS, MODE_PRI
VATE) | |
| 41 .getString(MintingApplication.HOST_PACKAGE_PREF, | |
| 42 MintingApplication.DEFAULT_CHROME_PACKAGE_NA
ME); | |
| 43 Context remoteContext = DexLoader.getRemoteContext(this, remotePacka
geName); | |
| 44 if (remoteContext == null) { | 42 if (remoteContext == null) { |
| 45 Log.w(TAG, "Failed to get remote context."); | 43 Log.w(TAG, "Failed to get remote context."); |
| 46 return null; | 44 return null; |
| 47 } | 45 } |
| 48 | 46 |
| 49 File remoteDexFile = | 47 File remoteDexFile = |
| 50 new File(remoteContext.getDir("dex", Context.MODE_PRIVATE),
"web_apk.dex"); | 48 new File(remoteContext.getDir("dex", Context.MODE_PRIVATE),
"web_apk.dex"); |
| 51 File localDexDir = getDir("dex", Context.MODE_PRIVATE); | 49 File localDexDir = getDir("dex", Context.MODE_PRIVATE); |
| 52 sClassLoader = DexLoader.load(remoteContext, "web_apk.dex", | 50 sClassLoader = DexLoader.load(remoteContext, "web_apk.dex", |
| 53 MINTED_SERVICE_IMPL_CLASS_NAME, remoteDexFile, localDexDir); | 51 MINTED_SERVICE_IMPL_CLASS_NAME, remoteDexFile, localDexDir); |
| 54 if (sClassLoader == null) { | 52 if (sClassLoader == null) { |
| 55 Log.w(TAG, "Unable to load web APK dex."); | 53 Log.w(TAG, "Unable to load web APK dex."); |
| 56 return null; | 54 return null; |
| 57 } | 55 } |
| 58 } | 56 } |
| 59 | 57 |
| 60 try { | 58 try { |
| 61 Class<?> mintedServiceImplClass = | 59 Class<?> mintedServiceImplClass = |
| 62 sClassLoader.loadClass(MINTED_SERVICE_IMPL_CLASS_NAME); | 60 sClassLoader.loadClass(MINTED_SERVICE_IMPL_CLASS_NAME); |
| 63 Constructor mintedServiceImplConstructor = | 61 Constructor<?> mintedServiceImplConstructor = |
| 64 mintedServiceImplClass.getConstructor(Context.class, int.cla
ss); | 62 mintedServiceImplClass.getConstructor(Context.class, int.cla
ss); |
| 65 return (IBinder) mintedServiceImplConstructor.newInstance(new Object
[] {this, | 63 return (IBinder) mintedServiceImplConstructor.newInstance(new Object
[] {this, |
| 66 R.drawable.app_icon}); | 64 R.drawable.app_icon}); |
| 67 } catch (Exception e) { | 65 } catch (Exception e) { |
| 68 Log.w(TAG, "Unable to create MintedServiceImpl"); | 66 Log.w(TAG, "Unable to create MintedServiceImpl"); |
| 69 e.printStackTrace(); | 67 e.printStackTrace(); |
| 70 return null; | 68 return null; |
| 71 } | 69 } |
| 72 } | 70 } |
| 73 } | 71 } |
| OLD | NEW |