OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.incrementalinstall; |
| 6 |
| 7 import android.app.Application; |
| 8 import android.content.Context; |
| 9 import android.content.pm.ApplicationInfo; |
| 10 import android.content.pm.PackageManager; |
| 11 import android.content.pm.PackageManager.NameNotFoundException; |
| 12 import android.util.Log; |
| 13 |
| 14 import java.io.File; |
| 15 import java.lang.ref.WeakReference; |
| 16 import java.util.List; |
| 17 import java.util.Map; |
| 18 |
| 19 /** |
| 20 * An Application that replaces itself with another Application (as defined in |
| 21 * the "incremental-install-real-app" meta-data tag within the |
| 22 * AndroidManifest.xml). It loads the other application only after side-loading |
| 23 * its .so and .dex files from /data/local/tmp. |
| 24 */ |
| 25 public final class BootstrapApplication extends Application { |
| 26 private static final String TAG = "cr.incrementalinstall"; |
| 27 private static final String MANAGED_DIR_PREFIX = "/data/local/tmp/incrementa
l-app-"; |
| 28 private static final String REAL_APP_META_DATA_NAME = "incremental-install-r
eal-app"; |
| 29 |
| 30 private ClassLoaderPatcher mClassLoaderPatcher; |
| 31 private Application mRealApplication; |
| 32 private Object mStashedProviderList; |
| 33 private Object mActivityThread; |
| 34 |
| 35 @Override |
| 36 protected void attachBaseContext(Context context) { |
| 37 super.attachBaseContext(context); |
| 38 File incrementalRootDir = new File(MANAGED_DIR_PREFIX + context.getPacka
geName()); |
| 39 File libDir = new File(incrementalRootDir, "lib"); |
| 40 File dexDir = new File(incrementalRootDir, "dex"); |
| 41 File installLockFile = new File(incrementalRootDir, "install.lock"); |
| 42 File firstRunLockFile = new File(incrementalRootDir, "firstrun.lock"); |
| 43 |
| 44 try { |
| 45 mActivityThread = Reflect.invokeMethod(Class.forName("android.app.Ac
tivityThread"), |
| 46 "currentActivityThread"); |
| 47 mClassLoaderPatcher = new ClassLoaderPatcher(context); |
| 48 |
| 49 boolean isFirstRun = LockFile.installerLockExists(firstRunLockFile); |
| 50 if (isFirstRun) { |
| 51 if (mClassLoaderPatcher.mIsPrimaryProcess) { |
| 52 // Wait for incremental_install.py to finish. |
| 53 LockFile.waitForInstallerLock(installLockFile, 20 * 1000); |
| 54 } else { |
| 55 // Wait for the browser process to create the optimized dex
files |
| 56 // (and for M+, copy the library files). |
| 57 LockFile.waitForInstallerLock(firstRunLockFile, 30 * 1000); |
| 58 } |
| 59 } |
| 60 |
| 61 mClassLoaderPatcher.importNativeLibs(libDir); |
| 62 mClassLoaderPatcher.loadDexFiles(dexDir); |
| 63 |
| 64 if (isFirstRun && mClassLoaderPatcher.mIsPrimaryProcess) { |
| 65 LockFile.clearInstallerLock(firstRunLockFile); |
| 66 } |
| 67 |
| 68 // attachBaseContext() is called from ActivityThread#handleBindAppli
cation() and |
| 69 // Application#mApplication is changed right after we return. Thus,
we cannot swap |
| 70 // the Application instances until onCreate() is called. |
| 71 String realApplicationName = getRealApplicationName(); |
| 72 Log.i(TAG, "Instantiating " + realApplicationName); |
| 73 mRealApplication = |
| 74 (Application) Reflect.newInstance(Class.forName(realApplicat
ionName)); |
| 75 Reflect.invokeMethod(mRealApplication, "attachBaseContext", context)
; |
| 76 |
| 77 // Between attachBaseContext() and onCreate(), ActivityThread tries
to instantiate |
| 78 // all ContentProviders. The ContentProviders break without the corr
ect Application |
| 79 // class being installed, so temporarily pretend there are no provid
ers, and then |
| 80 // instantiate them explicitly within onCreate(). |
| 81 disableContentProviders(); |
| 82 Log.i(TAG, "Waiting for onCreate"); |
| 83 } catch (Exception e) { |
| 84 throw new RuntimeException("Incremental install failed.", e); |
| 85 } |
| 86 } |
| 87 |
| 88 @Override |
| 89 public void onCreate() { |
| 90 super.onCreate(); |
| 91 try { |
| 92 Log.i(TAG, "onCreate() called. Swapping Application references"); |
| 93 swapApplicationReferences(); |
| 94 enableContentProviders(); |
| 95 Log.i(TAG, "Calling onCreate"); |
| 96 mRealApplication.onCreate(); |
| 97 } catch (Exception e) { |
| 98 throw new RuntimeException("Incremental install failed.", e); |
| 99 } |
| 100 } |
| 101 |
| 102 /** |
| 103 * Returns the class name of the real Application class (recorded in the |
| 104 * AndroidManifest.xml) |
| 105 */ |
| 106 private String getRealApplicationName() throws NameNotFoundException { |
| 107 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPack
ageName(), |
| 108 PackageManager.GET_META_DATA); |
| 109 return appInfo.metaData.getString(REAL_APP_META_DATA_NAME); |
| 110 } |
| 111 |
| 112 /** |
| 113 * Nulls out ActivityThread.mBoundApplication.providers. |
| 114 */ |
| 115 private void disableContentProviders() throws ReflectiveOperationException { |
| 116 Object data = Reflect.getField(mActivityThread, "mBoundApplication"); |
| 117 mStashedProviderList = Reflect.getField(data, "providers"); |
| 118 Reflect.setField(data, "providers", null); |
| 119 } |
| 120 |
| 121 /** |
| 122 * Restores the value of ActivityThread.mBoundApplication.providers, and inv
okes |
| 123 * ActivityThread#installContentProviders(). |
| 124 */ |
| 125 private void enableContentProviders() throws ReflectiveOperationException { |
| 126 Object data = Reflect.getField(mActivityThread, "mBoundApplication"); |
| 127 Reflect.setField(data, "providers", mStashedProviderList); |
| 128 if (mStashedProviderList != null && mClassLoaderPatcher.mIsPrimaryProces
s) { |
| 129 Log.i(TAG, "Instantiating content providers"); |
| 130 Reflect.invokeMethod(mActivityThread, "installContentProviders", mRe
alApplication, |
| 131 mStashedProviderList); |
| 132 } |
| 133 mStashedProviderList = null; |
| 134 } |
| 135 |
| 136 /** |
| 137 * Changes all fields within framework classes that have stored an reference
to this |
| 138 * BootstrapApplication to instead store references to mRealApplication. |
| 139 * @throws NoSuchFieldException |
| 140 */ |
| 141 @SuppressWarnings("unchecked") |
| 142 private void swapApplicationReferences() throws ReflectiveOperationException
{ |
| 143 if (Reflect.getField(mActivityThread, "mInitialApplication") == this) { |
| 144 Reflect.setField(mActivityThread, "mInitialApplication", mRealApplic
ation); |
| 145 } |
| 146 |
| 147 List<Application> allApplications = |
| 148 (List<Application>) Reflect.getField(mActivityThread, "mAllAppli
cations"); |
| 149 for (int i = 0; i < allApplications.size(); i++) { |
| 150 if (allApplications.get(i) == this) { |
| 151 allApplications.set(i, mRealApplication); |
| 152 } |
| 153 } |
| 154 |
| 155 for (String fieldName : new String[] { "mPackages", "mResourcePackages"
}) { |
| 156 Map<String, WeakReference<?>> packageMap = |
| 157 (Map<String, WeakReference<?>>) Reflect.getField(mActivityTh
read, fieldName); |
| 158 for (Map.Entry<String, WeakReference<?>> entry : packageMap.entrySet
()) { |
| 159 Object loadedApk = entry.getValue().get(); |
| 160 if (loadedApk != null && Reflect.getField(loadedApk, "mApplicati
on") == this) { |
| 161 Reflect.setField(loadedApk, "mApplication", mRealApplication
); |
| 162 Reflect.setField(mRealApplication, "mLoadedApk", loadedApk); |
| 163 } |
| 164 } |
| 165 } |
| 166 } |
| 167 } |
OLD | NEW |