Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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.chrome.browser.suggestions; | |
| 6 | |
| 7 import android.net.Uri; | |
| 8 import android.os.SystemClock; | |
| 9 import android.support.annotation.Nullable; | |
| 10 | |
| 11 import org.chromium.base.Callback; | |
| 12 import org.chromium.base.ThreadUtils; | |
| 13 import org.chromium.base.metrics.RecordHistogram; | |
| 14 import org.chromium.chrome.browser.ChromeFeatureList; | |
| 15 import org.chromium.chrome.browser.favicon.FaviconHelper; | |
| 16 import org.chromium.chrome.browser.favicon.FaviconHelper.FaviconImageCallback; | |
| 17 import org.chromium.chrome.browser.favicon.FaviconHelper.IconAvailabilityCallbac k; | |
| 18 import org.chromium.chrome.browser.favicon.LargeIconBridge; | |
| 19 import org.chromium.chrome.browser.favicon.LargeIconBridge.LargeIconCallback; | |
| 20 import org.chromium.chrome.browser.ntp.NewTabPage.DestructionObserver; | |
| 21 import org.chromium.chrome.browser.ntp.snippets.SuggestionsSource; | |
| 22 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; | |
| 23 import org.chromium.chrome.browser.profiles.Profile; | |
| 24 import org.chromium.chrome.browser.tab.Tab; | |
| 25 | |
| 26 import java.util.ArrayList; | |
| 27 import java.util.HashSet; | |
| 28 import java.util.List; | |
| 29 import java.util.Set; | |
| 30 import java.util.concurrent.TimeUnit; | |
| 31 | |
| 32 /** | |
| 33 * {@link SuggestionsUiDelegate} implementation. | |
| 34 */ | |
| 35 public class SuggestionsUiDelegateImpl implements SuggestionsUiDelegate { | |
| 36 private final List<DestructionObserver> mDestructionObservers = new ArrayLis t<>(); | |
| 37 private final SuggestionsSource mSuggestionsSource; | |
| 38 private final SuggestionsMetricsReporter mSuggestionsMetricsReporter; | |
| 39 private final SuggestionsNavigationDelegate mSuggestionsNavigationDelegate; | |
| 40 | |
| 41 private final Profile mProfile; | |
| 42 | |
| 43 private final Tab mTab; | |
| 44 | |
| 45 private FaviconHelper mFaviconHelper; | |
| 46 private LargeIconBridge mLargeIconBridge; | |
| 47 | |
| 48 private boolean mIsDestroyed; | |
| 49 | |
| 50 public SuggestionsUiDelegateImpl(SuggestionsSource suggestionsSource, | |
| 51 SuggestionsMetricsReporter metricsReporter, | |
| 52 SuggestionsNavigationDelegate navigationDelegate, Profile profile, T ab currentTab) { | |
| 53 mSuggestionsSource = suggestionsSource; | |
| 54 mSuggestionsMetricsReporter = metricsReporter; | |
| 55 mSuggestionsNavigationDelegate = navigationDelegate; | |
| 56 | |
| 57 mProfile = profile; | |
| 58 mTab = currentTab; | |
| 59 } | |
| 60 | |
| 61 @Override | |
| 62 public void getLocalFaviconImageForURL( | |
| 63 String url, int size, FaviconImageCallback faviconCallback) { | |
| 64 if (mIsDestroyed) return; | |
| 65 getFaviconHelper().getLocalFaviconImageForURL(mProfile, url, size, favic onCallback); | |
| 66 } | |
| 67 | |
| 68 @Override | |
| 69 public void getLargeIconForUrl(String url, int size, LargeIconCallback callb ack) { | |
| 70 if (mIsDestroyed) return; | |
| 71 getLargeIconBridge().getLargeIconForUrl(url, size, callback); | |
| 72 } | |
| 73 | |
| 74 @Override | |
| 75 public void ensureIconIsAvailable(String pageUrl, String iconUrl, boolean is LargeIcon, | |
| 76 boolean isTemporary, IconAvailabilityCallback callback) { | |
| 77 if (mIsDestroyed) return; | |
| 78 getFaviconHelper().ensureIconIsAvailable(mProfile, mTab.getWebContents() , pageUrl, iconUrl, | |
| 79 isLargeIcon, isTemporary, callback); | |
| 80 } | |
| 81 | |
| 82 @Override | |
| 83 public void getUrlsAvailableOffline( | |
| 84 Set<String> pageUrls, final Callback<Set<String>> callback) { | |
| 85 final Set<String> urlsAvailableOffline = new HashSet<>(); | |
| 86 if (mIsDestroyed || !isNtpOfflinePagesEnabled()) { | |
| 87 callback.onResult(urlsAvailableOffline); | |
| 88 return; | |
| 89 } | |
| 90 | |
| 91 HashSet<String> urlsToCheckForOfflinePage = new HashSet<>(); | |
| 92 | |
| 93 for (String pageUrl : pageUrls) { | |
| 94 if (isLocalUrl(pageUrl)) { | |
| 95 urlsAvailableOffline.add(pageUrl); | |
| 96 } else { | |
| 97 urlsToCheckForOfflinePage.add(pageUrl); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 final long offlineQueryStartTime = SystemClock.elapsedRealtime(); | |
| 102 | |
| 103 OfflinePageBridge offlinePageBridge = OfflinePageBridge.getForProfile(mP rofile); | |
| 104 | |
| 105 // TODO(dewittj): Remove this code by making the NTP badging available a fter the NTP is | |
| 106 // fully loaded. | |
| 107 if (offlinePageBridge == null || !offlinePageBridge.isOfflinePageModelLo aded()) { | |
| 108 // Posting a task to avoid potential re-entrancy issues. | |
| 109 ThreadUtils.postOnUiThread(new Runnable() { | |
| 110 @Override | |
| 111 public void run() { | |
| 112 callback.onResult(urlsAvailableOffline); | |
| 113 } | |
| 114 }); | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 offlinePageBridge.checkPagesExistOffline( | |
| 119 urlsToCheckForOfflinePage, new Callback<Set<String>>() { | |
| 120 @Override | |
| 121 public void onResult(Set<String> urlsWithOfflinePages) { | |
| 122 urlsAvailableOffline.addAll(urlsWithOfflinePages); | |
| 123 callback.onResult(urlsAvailableOffline); | |
| 124 RecordHistogram.recordTimesHistogram("NewTabPage.Offline UrlsLoadTime", | |
| 125 SystemClock.elapsedRealtime() - offlineQueryStar tTime, | |
| 126 TimeUnit.MILLISECONDS); | |
| 127 } | |
| 128 }); | |
| 129 } | |
| 130 | |
| 131 @Override | |
| 132 public SuggestionsSource getSuggestionsSource() { | |
| 133 return mSuggestionsSource; | |
| 134 } | |
| 135 | |
| 136 @Nullable | |
| 137 @Override | |
| 138 public SuggestionsMetricsReporter getMetricsReporter() { | |
| 139 return mSuggestionsMetricsReporter; | |
| 140 } | |
| 141 | |
| 142 @Nullable | |
| 143 @Override | |
| 144 public SuggestionsNavigationDelegate getNavigationDelegate() { | |
| 145 return mSuggestionsNavigationDelegate; | |
| 146 } | |
| 147 | |
| 148 @Override | |
| 149 public void addDestructionObserver(DestructionObserver destructionObserver) { | |
| 150 mDestructionObservers.add(destructionObserver); | |
| 151 } | |
| 152 | |
| 153 /** Invalidates the delegate and calls the registered destruction observers. */ | |
| 154 public void onDestroy() { | |
| 155 assert !mIsDestroyed; | |
| 156 | |
| 157 for (DestructionObserver observer : mDestructionObservers) observer.onDe stroy(); | |
| 158 | |
| 159 if (mFaviconHelper != null) { | |
| 160 mFaviconHelper.destroy(); | |
| 161 mFaviconHelper = null; | |
| 162 } | |
| 163 if (mLargeIconBridge != null) { | |
| 164 mLargeIconBridge.destroy(); | |
| 165 mLargeIconBridge = null; | |
| 166 } | |
| 167 mIsDestroyed = true; | |
| 168 } | |
| 169 | |
| 170 /** | |
| 171 * Utility method to lazily create the {@link FaviconHelper}, and avoid unne cessary native | |
| 172 * calls in tests. | |
| 173 */ | |
| 174 private FaviconHelper getFaviconHelper() { | |
| 175 assert !mIsDestroyed; | |
| 176 if (mFaviconHelper == null) mFaviconHelper = new FaviconHelper(); | |
| 177 return mFaviconHelper; | |
| 178 } | |
| 179 | |
| 180 /** | |
| 181 * Utility method to lazily create the {@link LargeIconBridge}, and avoid un necessary native | |
| 182 * calls in tests. | |
| 183 */ | |
| 184 private LargeIconBridge getLargeIconBridge() { | |
| 185 assert !mIsDestroyed; | |
| 186 if (mLargeIconBridge == null) mLargeIconBridge = new LargeIconBridge(mPr ofile); | |
| 187 return mLargeIconBridge; | |
| 188 } | |
| 189 | |
| 190 private boolean isNtpOfflinePagesEnabled() { | |
| 191 return ChromeFeatureList.isEnabled(ChromeFeatureList.NTP_OFFLINE_PAGES_F EATURE_NAME); | |
| 192 } | |
| 193 | |
| 194 private boolean isLocalUrl(String url) { | |
| 195 return "file".equals(Uri.parse(url).getScheme()); | |
| 196 } | |
| 197 | |
|
Bernhard Bauer
2017/01/18 16:58:14
Nit: these empty lines seem unnecessary.
dgn
2017/01/18 18:00:27
Done.
| |
| 198 | |
| 199 } | |
| OLD | NEW |