Chromium Code Reviews| Index: chrome/android/webapk/libs/client/src/org/chromium/webapk/lib/client/WebApkIdentityServiceClient.java |
| diff --git a/chrome/android/webapk/libs/client/src/org/chromium/webapk/lib/client/WebApkIdentityServiceClient.java b/chrome/android/webapk/libs/client/src/org/chromium/webapk/lib/client/WebApkIdentityServiceClient.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a671ab4247c3a9f87dc7a5ede8ea1e777d3aee7e |
| --- /dev/null |
| +++ b/chrome/android/webapk/libs/client/src/org/chromium/webapk/lib/client/WebApkIdentityServiceClient.java |
| @@ -0,0 +1,146 @@ |
| +// Copyright 2017 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.lib.client; |
| + |
| +import android.content.Context; |
| +import android.content.pm.ApplicationInfo; |
| +import android.content.pm.PackageManager; |
| +import android.os.Bundle; |
| +import android.os.IBinder; |
| +import android.os.RemoteException; |
| +import android.text.TextUtils; |
| +import android.util.Log; |
| + |
| +import org.chromium.webapk.lib.common.WebApkMetaDataKeys; |
| +import org.chromium.webapk.lib.common.identity_service.IIdentityService; |
| + |
| +/** |
| + * Provides APIs that allow browsers communicating with WebAPK Identity services. Each WebAPK has |
| + * its own "WebAPK Identity service". |
| + */ |
| +public class WebApkIdentityServiceClient { |
| + /** Used to notify the consumer after checking whether the current app owns the WebAPK. */ |
| + public interface CheckBacksWebApkCallback { void onChecked(boolean isValid); } |
| + |
| + /** |
| + * Before shell APK version 6, all WebAPKs are installed from browsers, and that browser is the |
| + * runtime host and specified in the WebAPK's AndroidManifest.xml. In shell APK version 6, we |
| + * introduced a logic to allow user to choose runtime host browser for WebAPKs unbound without |
| + * any browser, and for WebAPKs installed from browsers when the browser isn't installed. |
| + * However, user can either clear WebAPK's data that may require to choose a runtime host again, |
| + * or clear a browser's data that makes the browser loses its knowledge of owned WebAPKs. |
| + * Therefore, we don't know exactly which browser is the runtime host of a WebAPK without asking |
| + * the WebAPK. An Identity service is introduced in shell APK version 16 to allow browsers |
| + * querying the runtime host of a WebAPK. |
| + */ |
| + public static final int SHELL_APK_VERSION_SUPPORTING_SWITCH_RUNTIME_HOST = 6; |
| + |
| + private static final String ACTION_WEBAPK_IDENTITY_SERVICE = "org.webapk.IDENTITY_SERVICE_API"; |
| + private static final String TAG = "cr_WebApkIdentityService"; |
| + |
| + private static WebApkIdentityServiceClient sInstance; |
| + |
| + /** Manages connections between the browser application and WebAPK Identity services. */ |
| + private WebApkServiceConnectionManager mConnectionManager; |
| + |
| + public static WebApkIdentityServiceClient getInstance() { |
| + if (sInstance == null) { |
| + sInstance = new WebApkIdentityServiceClient(); |
| + } |
| + return sInstance; |
| + } |
| + |
| + private WebApkIdentityServiceClient() { |
| + mConnectionManager = new WebApkServiceConnectionManager( |
| + null /* action */, ACTION_WEBAPK_IDENTITY_SERVICE); |
| + } |
| + |
| + /** |
| + * Checks the runtime host of a WebAPK and notifies the caller. The runtime host could be null |
| + * if the WebAPK either doesn't have an Identity service or hasn't bind to any browser yet. |
| + * @param context The app context. |
| + * @param webApkPackageName The package name of the WebAPK. |
| + * @param callback The callback to be called after querying the runtime host is done. |
| + */ |
| + public static void checkBacksWebApkAsync(final Context context, final String webApkPackageName, |
|
pkotwicz
2017/07/19 16:09:13
Nit: Rename |context| to |browserContext|
Xi Han
2017/07/21 20:36:33
Done.
|
| + final CheckBacksWebApkCallback callback) { |
| + final String packageName = context.getPackageName(); |
| + WebApkServiceConnectionManager.ConnectionCallback connectionCallback = |
| + new WebApkServiceConnectionManager.ConnectionCallback() { |
| + @Override |
| + public void onConnected(IBinder service) { |
| + if (service == null) { |
| + WebApkIdentityServiceClient.onGotWebApkRuntimeHost(packageName, |
| + maybeExtractRuntimeHostFromMetaData(context, webApkPackageName), |
| + callback); |
| + return; |
| + } |
| + |
| + IIdentityService identityService = |
| + IIdentityService.Stub.asInterface(service); |
| + String runtimeHost = null; |
| + try { |
| + runtimeHost = identityService.getRuntimeHostBrowserPackageName(); |
| + } catch (RemoteException e) { |
| + Log.w(TAG, "Failed to get runtime host from the Identity service."); |
| + } |
| + WebApkIdentityServiceClient.onGotWebApkRuntimeHost( |
| + packageName, runtimeHost, callback); |
| + } |
| + }; |
| + getInstance().mConnectionManager.connect(context, webApkPackageName, connectionCallback); |
| + } |
| + |
| + /** |
| + * Called after fetching the WebAPK's runtime host browser. |
| + * @param appPackageName The current app's package name. |
| + * @param webApkRuntimeHostPackageName The package name of the WebAPK's runtime host browser. |
| + * @param callback The callback to notify the called whether the runtime host matches the |
| + * current app. |
| + */ |
| + private static void onGotWebApkRuntimeHost(String appPackageName, |
| + String webApkRuntimeHostPackageName, CheckBacksWebApkCallback callback) { |
| + callback.onChecked(!TextUtils.isEmpty(webApkRuntimeHostPackageName) |
| + && webApkRuntimeHostPackageName.contentEquals(appPackageName)); |
| + } |
| + |
| + /** |
| + * Extracts the runtime host from metadata if |
| + * - the runtime host specified in the AndroidManifest.xml matches the current app name. |
| + * AND |
| + * - The shell APK version is less than SHELL_APK_VERSION_SUPPORTING_SWITCH_RUNTIME_HOST. |
| + * See {@link WebApkIdentityServiceClient#SHELL_APK_VERSION_SUPPORTING_SWITCH_RUNTIME_HOST} for |
| + * more details. |
| + */ |
| + public static String maybeExtractRuntimeHostFromMetaData( |
| + Context context, String webApkPackageName) { |
| + Bundle metadata = readMetaData(context, webApkPackageName); |
| + if (metadata.getInt(WebApkMetaDataKeys.SHELL_APK_VERSION) |
| + >= SHELL_APK_VERSION_SUPPORTING_SWITCH_RUNTIME_HOST) { |
| + return null; |
| + } |
| + |
| + return metadata.getString(WebApkMetaDataKeys.RUNTIME_HOST); |
| + } |
| + |
| + /** Returns the <meta-data> in the Android Manifest of the given package name. */ |
| + private static Bundle readMetaData(Context context, String packageName) { |
| + ApplicationInfo ai = null; |
| + try { |
| + ai = context.getPackageManager().getApplicationInfo( |
| + packageName, PackageManager.GET_META_DATA); |
| + } catch (PackageManager.NameNotFoundException e) { |
| + return null; |
| + } |
| + return ai.metaData; |
| + } |
| + |
| + /** Disconnects all the connections from WebAPK Identity services. */ |
| + public static void disconnectWebApkIdentityService(Context appContext) { |
| + if (sInstance == null) return; |
| + |
| + sInstance.mConnectionManager.disconnect(appContext); |
| + } |
| +} |