Chromium Code Reviews| 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 @Override | 31 @Override |
| 30 public IBinder onBind(Intent intent) { | 32 public IBinder onBind(Intent intent) { |
| 31 // TODO(pkotwicz): Add dex to application's ClassLoader. Only load dex t he first time that | 33 // TODO(pkotwicz): Add dex to application's ClassLoader. Only load dex t he first time that |
| 32 // {@link MintingServiceFactory#onBind()} is called. | 34 // {@link MintingServiceFactory#onBind()} is called. |
| 33 | 35 |
| 34 String remotePackageName = getSharedPreferences(MintingApplication.MINT_ PREFS, MODE_PRIVATE) | 36 Context remoteContext = WebAPKUtils.getHostBrowserContext(this); |
| 35 .getString(MintingApplication.HOST_PA CKAGE_PREF, | |
| 36 MintingApplication.DEFAULT_CH ROME_PACKAGE_NAME); | |
| 37 Context remoteContext = DexLoader.getRemoteContext(this, remotePackageNa me); | |
| 38 if (remoteContext == null) { | 37 if (remoteContext == null) { |
| 39 Log.w(TAG, "Failed to get remote context."); | 38 Log.w(TAG, "Failed to get remote context."); |
| 40 } | 39 } |
| 41 | 40 |
| 42 File remoteDexFile = | 41 File remoteDexFile = |
| 43 new File(remoteContext.getDir("dex", Context.MODE_PRIVATE), "web _apk.dex"); | 42 new File(remoteContext.getDir("dex", Context.MODE_PRIVATE), "web _apk.dex"); |
| 44 File localDexDir = getDir("dex", Context.MODE_PRIVATE); | 43 File localDexDir = getDir("dex", Context.MODE_PRIVATE); |
| 45 ClassLoader webApkClassLoader = DexLoader.load(remoteContext, "web_apk.d ex", | 44 ClassLoader webApkClassLoader = DexLoader.load(remoteContext, "web_apk.d ex", |
| 46 MINTED_SERVICE_IMPL_CLASS_NAME, remoteDexFile, localDexDir); | 45 MINTED_SERVICE_IMPL_CLASS_NAME, remoteDexFile, localDexDir); |
| 47 if (webApkClassLoader == null) { | 46 if (webApkClassLoader == null) { |
| 48 Log.w(TAG, "Unable to load web APK dex."); | 47 Log.w(TAG, "Unable to load web APK dex."); |
| 49 return null; | 48 return null; |
| 50 } | 49 } |
| 51 | 50 |
| 52 try { | 51 try { |
| 52 | |
|
pkotwicz
2016/04/15 17:59:43
Nit: nuke the extraneous new line
Yaron
2016/04/15 21:58:11
Done.
| |
| 53 Class<?> mintedServiceImplClass = | 53 Class<?> mintedServiceImplClass = |
| 54 webApkClassLoader.loadClass(MINTED_SERVICE_IMPL_CLASS_NAME); | 54 webApkClassLoader.loadClass(MINTED_SERVICE_IMPL_CLASS_NAME); |
| 55 Constructor mintedServiceImplConstructor = | 55 Constructor<?> mintedServiceImplConstructor = |
| 56 mintedServiceImplClass.getConstructor(Context.class); | 56 mintedServiceImplClass.getConstructor(Context.class); |
| 57 return (IBinder) mintedServiceImplConstructor.newInstance(new Object [] {this}); | 57 return (IBinder) mintedServiceImplConstructor.newInstance(new Object [] {this}); |
| 58 } catch (Exception e) { | 58 } catch (Exception e) { |
| 59 Log.w(TAG, "Unable to create MintedServiceImpl"); | 59 Log.w(TAG, "Unable to create MintedServiceImpl"); |
| 60 e.printStackTrace(); | 60 e.printStackTrace(); |
| 61 return null; | 61 return null; |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 } | 64 } |
| OLD | NEW |