OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.chrome.browser.webapps; | 5 package org.chromium.chrome.browser.webapps; |
6 | 6 |
7 import android.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
8 import android.text.TextUtils; | 8 import android.text.TextUtils; |
9 | 9 |
10 import org.chromium.base.annotations.CalledByNative; | 10 import org.chromium.base.annotations.CalledByNative; |
11 import org.chromium.chrome.browser.tab.EmptyTabObserver; | 11 import org.chromium.chrome.browser.tab.EmptyTabObserver; |
12 import org.chromium.chrome.browser.tab.Tab; | 12 import org.chromium.chrome.browser.tab.Tab; |
13 import org.chromium.content_public.browser.WebContents; | 13 import org.chromium.content_public.browser.WebContents; |
14 | 14 |
15 import java.util.HashMap; | 15 import java.util.HashMap; |
16 | 16 |
17 /** | 17 /** |
18 * Downloads the Web Manifest if the web site still uses the {@link manifestUrl} passed to the | 18 * Downloads the Web Manifest if the web site still uses the {@link manifestUrl} passed to the |
19 * constructor. | 19 * constructor. |
20 */ | 20 */ |
21 public class ManifestUpgradeDetectorFetcher extends EmptyTabObserver { | 21 public class WebApkUpdateDataFetcher extends EmptyTabObserver { |
22 /** Observes fetching of the Web Manifest. */ | |
23 public interface Observer { | |
24 /** | |
25 * Called when the Web Manifest for the initial URL load has been fetche d (successfully or | |
26 * unsuccessfully). | |
27 * TODO(pkotwicz): Add calls to {@link #onFinishedFetchingWebManifestFor InitialUrl()}. | |
28 * @param fetchedInfo The fetched Web Manifest data. Null if the initia l URL does not point | |
29 * to a Web Manifest. | |
30 * @param bestIconUrl The icon URL in {@link fetchedInfo#iconUrlToMurmu r2HashMap()} best | |
31 * suited for use as the launcher icon on this devic e. | |
32 */ | |
33 void onFinishedFetchingWebManifestForInitialUrl(WebApkInfo fetchedInfo, String bestIconUrl); | |
22 | 34 |
23 /** | |
24 * Called once the Web Manifest has been downloaded. | |
25 */ | |
26 public interface Callback { | |
27 /** | 35 /** |
28 * @param fetchedInfo The fetched Web Manifest data. | 36 * Called when the Web Manifest has been successfully fetched (including on the initial URL |
29 * @param bestIconUrl Icon URL in {@link data} which is best suited for use as the launcher | 37 * load). |
30 * icon on this device. | 38 * @param fetchedInfo The fetched Web Manifest data. |
39 * @param bestIconUrl The icon URL in {@link fetchedInfo#iconUrlToMurmu r2HashMap()} best | |
40 * suited for use as the launcher icon on this devic e. | |
31 */ | 41 */ |
32 void onGotManifestData(WebApkInfo fetchedInfo, String bestIconUrl); | 42 void onGotManifestData(WebApkInfo fetchedInfo, String bestIconUrl); |
33 } | 43 } |
34 | 44 |
35 /** | 45 /** |
36 * Pointer to the native side ManifestUpgradeDetectorFetcher. The Java side owns the native side | 46 * Pointer to the native side WebApkUpdateDataFetcher. The Java side owns th e native side |
37 * ManifestUpgradeDetectorFetcher. | 47 * WebApkUpdateDataFetcher. |
38 */ | 48 */ |
39 private long mNativePointer; | 49 private long mNativePointer; |
40 | 50 |
41 /** The tab that is being observed. */ | 51 /** The tab that is being observed. */ |
42 private Tab mTab; | 52 private Tab mTab; |
43 | 53 |
44 /** | 54 /** Web Manifest data at the time that the WebAPK was generated. */ |
45 * Web Manifest data at time that the WebAPK was generated. | |
46 */ | |
47 private WebApkInfo mOldInfo; | 55 private WebApkInfo mOldInfo; |
48 | 56 |
49 private Callback mCallback; | 57 private Observer mObserver; |
50 | 58 |
51 /** | 59 /** Starts observing page loads in order to fetch the Web Manifest after eac h page load. */ |
52 * Starts fetching the web manifest resources. | 60 public boolean start(Tab tab, WebApkInfo oldInfo, Observer observer) { |
53 * @param callback Called once the Web Manifest has been downloaded. | |
54 */ | |
55 public boolean start(Tab tab, WebApkInfo oldInfo, Callback callback) { | |
56 if (tab.getWebContents() == null || TextUtils.isEmpty(oldInfo.manifestUr l())) { | 61 if (tab.getWebContents() == null || TextUtils.isEmpty(oldInfo.manifestUr l())) { |
57 return false; | 62 return false; |
58 } | 63 } |
59 | 64 |
60 mTab = tab; | 65 mTab = tab; |
61 mOldInfo = oldInfo; | 66 mOldInfo = oldInfo; |
67 mObserver = observer; | |
68 | |
69 mTab.addObserver(this); | |
dominickn
2016/12/08 05:41:49
Out of an abundance of caution, should this observ
pkotwicz
2016/12/08 15:13:38
Things are funky when as an observer/callback can
| |
62 mNativePointer = nativeInitialize(mOldInfo.scopeUri().toString(), mOldIn fo.manifestUrl()); | 70 mNativePointer = nativeInitialize(mOldInfo.scopeUri().toString(), mOldIn fo.manifestUrl()); |
63 mCallback = callback; | |
64 mTab.addObserver(this); | |
65 nativeStart(mNativePointer, mTab.getWebContents()); | 71 nativeStart(mNativePointer, mTab.getWebContents()); |
66 return true; | 72 return true; |
67 } | 73 } |
68 | 74 |
69 /** | 75 /** |
70 * Puts the object in a state where it is safe to be destroyed. | 76 * Puts the object in a state where it is safe to be destroyed. |
71 */ | 77 */ |
72 public void destroy() { | 78 public void destroy() { |
73 mTab.removeObserver(this); | 79 mTab.removeObserver(this); |
74 nativeDestroy(mNativePointer); | 80 nativeDestroy(mNativePointer); |
75 mNativePointer = 0; | 81 mNativePointer = 0; |
76 } | 82 } |
77 | 83 |
78 @Override | 84 @Override |
79 public void onWebContentsSwapped(Tab tab, boolean didStartLoad, | 85 public void onWebContentsSwapped(Tab tab, boolean didStartLoad, |
80 boolean didFinishLoad) { | 86 boolean didFinishLoad) { |
81 updatePointers(); | 87 updatePointers(); |
82 } | 88 } |
83 | 89 |
84 @Override | 90 @Override |
85 public void onContentChanged(Tab tab) { | 91 public void onContentChanged(Tab tab) { |
86 updatePointers(); | 92 updatePointers(); |
87 } | 93 } |
88 | 94 |
89 /** | 95 /** |
90 * Updates which WebContents the native ManifestUpgradeDetectorFetcher is mo nitoring. | 96 * Updates which WebContents the native WebApkUpdateDataFetcher is monitorin g. |
91 */ | 97 */ |
92 private void updatePointers() { | 98 private void updatePointers() { |
93 nativeReplaceWebContents(mNativePointer, mTab.getWebContents()); | 99 nativeReplaceWebContents(mNativePointer, mTab.getWebContents()); |
94 } | 100 } |
95 | 101 |
96 /** | 102 /** |
97 * Called when the updated Web Manifest has been fetched. | 103 * Called when the updated Web Manifest has been fetched. |
98 */ | 104 */ |
99 @CalledByNative | 105 @CalledByNative |
100 protected void onDataAvailable(String startUrl, String scopeUrl, String name , String shortName, | 106 protected void onDataAvailable(String startUrl, String scopeUrl, String name , String shortName, |
101 String bestIconUrl, String bestIconMurmur2Hash, Bitmap bestIconBitma p, | 107 String bestIconUrl, String bestIconMurmur2Hash, Bitmap bestIconBitma p, |
102 String[] iconUrls, int displayMode, int orientation, long themeColor , | 108 String[] iconUrls, int displayMode, int orientation, long themeColor , |
103 long backgroundColor) { | 109 long backgroundColor) { |
104 HashMap<String, String> iconUrlToMurmur2HashMap = new HashMap<String, St ring>(); | 110 HashMap<String, String> iconUrlToMurmur2HashMap = new HashMap<String, St ring>(); |
105 for (String iconUrl : iconUrls) { | 111 for (String iconUrl : iconUrls) { |
106 String murmur2Hash = (iconUrl.equals(bestIconUrl)) ? bestIconMurmur2 Hash : null; | 112 String murmur2Hash = (iconUrl.equals(bestIconUrl)) ? bestIconMurmur2 Hash : null; |
107 iconUrlToMurmur2HashMap.put(iconUrl, murmur2Hash); | 113 iconUrlToMurmur2HashMap.put(iconUrl, murmur2Hash); |
108 } | 114 } |
109 | 115 |
110 WebApkInfo info = WebApkInfo.create(mOldInfo.id(), startUrl, scopeUrl, | 116 WebApkInfo info = WebApkInfo.create(mOldInfo.id(), startUrl, scopeUrl, |
111 new WebApkInfo.Icon(bestIconBitmap), name, shortName, displayMod e, orientation, | 117 new WebApkInfo.Icon(bestIconBitmap), name, shortName, displayMod e, orientation, |
112 mOldInfo.source(), themeColor, backgroundColor, mOldInfo.webApkP ackageName(), | 118 mOldInfo.source(), themeColor, backgroundColor, mOldInfo.webApkP ackageName(), |
113 mOldInfo.shellApkVersion(), mOldInfo.manifestUrl(), startUrl, | 119 mOldInfo.shellApkVersion(), mOldInfo.manifestUrl(), startUrl, |
114 iconUrlToMurmur2HashMap); | 120 iconUrlToMurmur2HashMap); |
115 mCallback.onGotManifestData(info, bestIconUrl); | 121 mObserver.onGotManifestData(info, bestIconUrl); |
116 } | 122 } |
117 | 123 |
118 private native long nativeInitialize(String scope, String webManifestUrl); | 124 private native long nativeInitialize(String scope, String webManifestUrl); |
119 private native void nativeReplaceWebContents( | 125 private native void nativeReplaceWebContents( |
120 long nativeManifestUpgradeDetectorFetcher, WebContents webContents); | 126 long nativeWebApkUpdateDataFetcher, WebContents webContents); |
121 private native void nativeDestroy(long nativeManifestUpgradeDetectorFetcher) ; | 127 private native void nativeDestroy(long nativeWebApkUpdateDataFetcher); |
122 private native void nativeStart( | 128 private native void nativeStart(long nativeWebApkUpdateDataFetcher, WebConte nts webContents); |
123 long nativeManifestUpgradeDetectorFetcher, WebContents webContents); | |
124 } | 129 } |
OLD | NEW |