Index: chrome/android/java/src/org/chromium/chrome/browser/webapps/ManifestUpgradeDetector.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/ManifestUpgradeDetector.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/ManifestUpgradeDetector.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7a1bd7f99052e35a62cb39872c321569f43e4daf |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/ManifestUpgradeDetector.java |
@@ -0,0 +1,178 @@ |
+// Copyright 2016 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.chrome.browser.webapps; |
+ |
+import android.content.Context; |
+ |
+import org.chromium.base.ContextUtils; |
+import org.chromium.base.annotations.CalledByNative; |
+import org.chromium.chrome.browser.tab.EmptyTabObserver; |
+import org.chromium.chrome.browser.tab.Tab; |
+import org.chromium.content_public.browser.WebContents; |
+ |
+import java.util.Arrays; |
+import java.util.HashSet; |
+import java.util.Set; |
+ |
+/** |
+ * This class interacts with c++ to detect whether resources in web manifest of a WebAPK has been |
+ * updated. |
+ */ |
+public class ManifestUpgradeDetector extends EmptyTabObserver { |
+ private long mNativePointer; |
+ private final Tab mTab; |
+ private WebappInfo mWebappInfo; |
+ private Context mContext; |
+ private boolean mIsInitialized; |
+ |
+ private static final String TAG = "cr_UpgradeDetector"; |
+ |
+ public ManifestUpgradeDetector(Context context, Tab tab, WebappInfo info) { |
+ mTab = tab; |
+ mWebappInfo = info; |
+ mContext = context; |
+ |
+ String webManifestUrl = info.webManifestUrl(); |
+ if (webManifestUrl == null || webManifestUrl.isEmpty()) return; |
+ |
+ // TODO(hanxi): use "scope" of the WebappInfo directly once it is exposed. |
+ mNativePointer = nativeInitialize(mTab.getWebContents(), info.uri().getHost().toString(), |
+ webManifestUrl); |
+ mIsInitialized = true; |
+ } |
+ |
+ /** |
+ * Updates the status of the native ManifestUpgradeDetector to start the web manifest resources |
+ * fetching pipeline. |
+ */ |
+ public void start() { |
+ if (mIsInitialized) { |
+ mTab.addObserver(this); |
+ nativeStart(mNativePointer); |
+ } |
+ } |
+ |
+ /** |
+ * Puts the object in a state where it is safe to be destroyed. |
+ */ |
+ public void destroy() { |
+ if (mIsInitialized) { |
+ nativeDestroy(mNativePointer); |
+ } |
+ mNativePointer = 0; |
+ } |
+ |
+ @Override |
+ public void onWebContentsSwapped(Tab tab, boolean didStartLoad, |
+ boolean didFinishLoad) { |
+ updatePointers(tab); |
+ } |
+ |
+ @Override |
+ public void onContentChanged(Tab tab) { |
+ updatePointers(tab); |
+ } |
+ |
+ /** |
+ * Updates which WebContents the native ManifestUpgradeDetector is monitoring. |
+ */ |
+ private void updatePointers(Tab tab) { |
+ nativeReplaceWebContents(mNativePointer, tab.getWebContents()); |
+ } |
+ |
+ /** |
+ * Checks whether the new fetched resources match the ones of the current WebAppInfo. |
+ * The changes of start Url, name, background color and so on will lead to request |
+ * updating the installed WebAPK. |
+ */ |
+ @CalledByNative |
+ private void onDataAvailable(String startUrl, String name, String shortName, |
+ String[] iconUrls, int displayMode, int orientation, long themeColor, |
+ long backgroundColor) { |
+ // TODO(hanxi): crubug.com/627824. Validates whether the new WebappInfo is |
+ // WebAPK-compatible. |
+ mTab.removeObserver(this); |
+ final WebappInfo newInfo = WebappInfo.create(mWebappInfo.id(), startUrl, |
+ null, name, shortName, displayMode, orientation, mWebappInfo.source(), |
+ themeColor, backgroundColor, false, mWebappInfo.webApkPackageName(), |
+ mWebappInfo.webManifestUrl()); |
+ final Set<String> iconUrlSet = new HashSet<String>(Arrays.asList(iconUrls)); |
+ if (requireUpgrade(startUrl, newInfo)) { |
+ updateMetadataAndRequestUpdateIfNeeded(true, iconUrlSet, newInfo); |
+ return; |
+ } |
+ WebappRegistry.getWebappDataStorage(ContextUtils.getApplicationContext(), mWebappInfo.id(), |
+ new WebappRegistry.FetchWebappDataStorageCallback() { |
+ @Override |
+ public void onWebappDataStorageRetrieved(WebappDataStorage storage) { |
+ onWebappDataRetrieved(storage, iconUrlSet, newInfo); |
+ } |
+ }); |
+ } |
+ |
+ /** |
+ * Checks whether the new fetched resources match the ones stored in the SharedPreference. |
+ */ |
+ private void onWebappDataRetrieved(WebappDataStorage storage, Set<String> iconUrlSet, |
+ WebappInfo newInfo) { |
+ Set<String> cachedIconUrlsSet = storage.getIconUrls(); |
+ // If icon urls haven't been cached, whether to re-mint WebAPK depends on |
+ // other resources. |
+ boolean isUpgraded = cachedIconUrlsSet == null ? false |
+ : !checkIconUrlSet(cachedIconUrlsSet, iconUrlSet); |
+ |
+ updateMetadataAndRequestUpdateIfNeeded(isUpgraded, iconUrlSet, newInfo); |
+ } |
+ |
+ /** |
+ * Checks whether attributes, like start Url, short name, background color, are changed. |
+ * {@link mWebappInfo} keeps the latest version of these attributes, so no metadata query is |
+ * needed. These attributes' changes require to re-mint a WebAPK. |
+ */ |
+ private boolean requireUpgrade(String startUrl, WebappInfo newInfo) { |
+ // TODO(hanxi): Checks whether the "scope" matches when "scope" is added in {@WebappInfo}. |
+ return !mWebappInfo.uri().toString().equals(startUrl) |
+ || !mWebappInfo.shortName().equals(newInfo.shortName()) |
+ || mWebappInfo.backgroundColor() != newInfo.backgroundColor(); |
+ } |
+ |
+ /** |
+ * Returns whether two icon url sets are equal. |
+ */ |
+ private boolean checkIconUrlSet(Set<String> set, Set<String> anotherSet) { |
+ if (set == null) return anotherSet == null; |
+ return anotherSet != null && set.size() == anotherSet.size() |
+ && set.containsAll(anotherSet); |
+ } |
+ |
+ /** |
+ * Updates the SharedPreference and requests to update if needed. |
+ */ |
+ public void updateMetadataAndRequestUpdateIfNeeded(boolean isUpgraded, |
+ final Set<String> iconUrls, WebappInfo newInfo) { |
+ if (isUpgraded) { |
+ // call WebApkUpdateManager to request update from WebAPK minting server. |
+ // Note: always send the WebApk minting server the old metadata for checking |
+ // updates before updating Chrome's sharedPreference. |
+ } |
+ mWebappInfo = newInfo; |
+ WebappRegistry.getWebappDataStorage(mContext, mWebappInfo.id(), |
+ new WebappRegistry.FetchWebappDataStorageCallback() { |
+ @Override |
+ public void onWebappDataStorageRetrieved( |
+ WebappDataStorage storage) { |
+ storage.updateFromWebappInfo(mWebappInfo, iconUrls); |
+ } |
+ } |
+ ); |
+ } |
+ |
+ private native long nativeInitialize(WebContents webContents, String scope, |
+ String webManifestUrl); |
+ private native void nativeReplaceWebContents(long nativeManifestUpgradeDetector, |
+ WebContents webContents); |
+ private native void nativeDestroy(long nativeManifestUpgradeDetector); |
+ private native void nativeStart(long nativeManifestUpgradeDetector); |
+} |