Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Unified Diff: webapk/shell_apk/src/org/chromium/webapk/shell_apk/WebApkServiceFactory.java

Issue 1965583002: Move //webapk to //chrome/android/webapk (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webapk/shell_apk/src/org/chromium/webapk/shell_apk/WebApkApplication.java ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webapk/shell_apk/src/org/chromium/webapk/shell_apk/WebApkServiceFactory.java
diff --git a/webapk/shell_apk/src/org/chromium/webapk/shell_apk/WebApkServiceFactory.java b/webapk/shell_apk/src/org/chromium/webapk/shell_apk/WebApkServiceFactory.java
deleted file mode 100644
index 4fdc9a2cd00965b93a10948e1d8fb9db991433fd..0000000000000000000000000000000000000000
--- a/webapk/shell_apk/src/org/chromium/webapk/shell_apk/WebApkServiceFactory.java
+++ /dev/null
@@ -1,168 +0,0 @@
-// Copyright 2015 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-package org.chromium.webapk.shell_apk;
-
-import android.app.Service;
-import android.content.Context;
-import android.content.Intent;
-import android.content.SharedPreferences;
-import android.content.pm.PackageInfo;
-import android.content.pm.PackageManager;
-import android.os.Bundle;
-import android.os.IBinder;
-import android.util.Log;
-
-import org.chromium.webapk.lib.common.WebApkUtils;
-
-import java.io.File;
-import java.lang.reflect.Constructor;
-import java.util.Scanner;
-
-/**
- * Shell class for services provided by WebAPK to Chrome. Extracts code with implementation of
- * services from .dex file in Chrome APK.
- */
-public class WebApkServiceFactory extends Service {
- private static final String TAG = "cr.WebApkServiceFactory";
-
- /**
- * Name of the class with IBinder API implementation.
- */
- private static final String MINTED_SERVICE_IMPL_CLASS_NAME =
- "org.chromium.webapk.lib.runtime_library.WebApkServiceImpl";
-
- private static final String KEY_APP_ICON_ID = "app_icon_id";
- private static final String KEY_EXPECTED_HOST_BROWSER = "expected_host_browser";
-
- /**
- * Name of the shared preferences file.
- */
- private static final String PREF_FILE_NAME = "MINT_PREFS";
-
- /**
- * Name of the shared preference for Chrome's version code.
- */
- private static final String REMOTE_VERSION_CODE_PREF =
- "webapk_service_factory_remote_version_code";
-
- /**
- * Name of the shared preference for the version number of the dynamically loaded dex.
- */
- private static final String RUNTIME_DEX_VERSION_PREF =
- "webapk_service_factory_runtime_dex_version";
-
- /*
- * ClassLoader for loading {@link MINTED_SERVICE_IMPL_CLASS_NAME}. Static so that all
- * {@link WebApkServiceFactory} service instatiations use the same ClassLoader during the app's
- * lifetime.
- */
- private static ClassLoader sClassLoader;
-
- @Override
- public IBinder onBind(Intent intent) {
- if (sClassLoader == null) {
- sClassLoader = createClassLoader(this);
- if (sClassLoader == null) {
- Log.w(TAG, "Unable to create ClassLoader.");
- return null;
- }
- }
-
- try {
- Class<?> mintedServiceImplClass =
- sClassLoader.loadClass(MINTED_SERVICE_IMPL_CLASS_NAME);
- Constructor<?> mintedServiceImplConstructor =
- mintedServiceImplClass.getConstructor(Context.class, Bundle.class);
- String expectedHostPackageName = WebApkUtils.getHostBrowserPackageName(this);
- Bundle bundle = new Bundle();
- bundle.putInt(KEY_APP_ICON_ID, R.drawable.app_icon);
- bundle.putString(KEY_EXPECTED_HOST_BROWSER, expectedHostPackageName);
- return (IBinder) mintedServiceImplConstructor.newInstance(new Object[] {this, bundle});
- } catch (Exception e) {
- Log.w(TAG, "Unable to create WebApkServiceImpl.");
- e.printStackTrace();
- return null;
- }
- }
-
- /**
- * Creates ClassLoader for loading {@link MINTED_SERVICE_IMPL_CLASS_NAME}.
- * @param context WebAPK's context.
- * @return The ClassLoader.
- */
- private static ClassLoader createClassLoader(Context context) {
- Context remoteContext = WebApkUtils.getHostBrowserContext(context);
- if (remoteContext == null) {
- Log.w(TAG, "Failed to get remote context.");
- return null;
- }
-
- SharedPreferences preferences = context.getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
-
- int runtimeDexVersion = preferences.getInt(RUNTIME_DEX_VERSION_PREF, -1);
- int newRuntimeDexVersion = checkForNewRuntimeDexVersion(preferences, remoteContext);
- if (newRuntimeDexVersion == -1) {
- newRuntimeDexVersion = runtimeDexVersion;
- }
- File localDexDir = context.getDir("dex", Context.MODE_PRIVATE);
- if (newRuntimeDexVersion != runtimeDexVersion) {
- Log.w(TAG, "Delete cached dex files.");
- DexLoader.deleteCachedDexes(localDexDir);
- }
-
- String dexAssetName = WebApkUtils.getRuntimeDexName(newRuntimeDexVersion);
- File remoteDexFile =
- new File(remoteContext.getDir("dex", Context.MODE_PRIVATE), dexAssetName);
- return DexLoader.load(remoteContext, dexAssetName, MINTED_SERVICE_IMPL_CLASS_NAME,
- remoteDexFile, localDexDir);
- }
-
- /**
- * Checks if there is a new "runtime dex" version number. If there is a new version number,
- * updates SharedPreferences.
- * @param preferences WebAPK's SharedPreferences.
- * @param remoteContext
- * @return The new "runtime dex" version number. -1 if there is no new version number.
- */
- private static int checkForNewRuntimeDexVersion(
- SharedPreferences preferences, Context remoteContext) {
- // The "runtime dex" version only changes when {@link remoteContext}'s APK version code
- // changes. Checking the APK's version code is less expensive than reading from the APK's
- // assets.
- PackageInfo remotePackageInfo = null;
- try {
- remotePackageInfo = remoteContext.getPackageManager().getPackageInfo(
- remoteContext.getPackageName(), 0);
- } catch (PackageManager.NameNotFoundException e) {
- Log.e(TAG, "Failed to get remote package info.");
- return -1;
- }
-
- int cachedRemoteVersionCode = preferences.getInt(REMOTE_VERSION_CODE_PREF, -1);
- if (cachedRemoteVersionCode == remotePackageInfo.versionCode) {
- return -1;
- }
-
- int runtimeDexVersion = readAssetContentsToInt(remoteContext, "web_apk_dex_version.txt");
- SharedPreferences.Editor editor = preferences.edit();
- editor.putInt(REMOTE_VERSION_CODE_PREF, remotePackageInfo.versionCode);
- editor.putInt(RUNTIME_DEX_VERSION_PREF, runtimeDexVersion);
- editor.apply();
- return runtimeDexVersion;
- }
-
- /**
- * Returns the first integer in an asset file's contents.
- * @param context
- * @param assetName The name of the asset.
- */
- private static int readAssetContentsToInt(Context context, String assetName) {
- try (Scanner scanner = new Scanner(context.getAssets().open(assetName))) {
- return scanner.nextInt();
- } catch (Exception e) {
- return -1;
- }
- }
-}
« no previous file with comments | « webapk/shell_apk/src/org/chromium/webapk/shell_apk/WebApkApplication.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698