2013-09-12 14:49:36 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko.favicons;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
2014-09-08 13:47:49 +00:00
|
|
|
import android.content.Context;
|
2013-09-12 14:49:36 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.net.http.AndroidHttpClient;
|
2013-10-11 23:42:39 +00:00
|
|
|
import android.text.TextUtils;
|
2013-09-12 14:49:36 +00:00
|
|
|
import android.util.Log;
|
2013-10-11 23:42:39 +00:00
|
|
|
import org.apache.http.Header;
|
2013-09-12 14:49:36 +00:00
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
import org.mozilla.gecko.GeckoAppShell;
|
|
|
|
import org.mozilla.gecko.db.BrowserDB;
|
2014-01-18 03:24:28 +00:00
|
|
|
import org.mozilla.gecko.favicons.decoders.FaviconDecoder;
|
|
|
|
import org.mozilla.gecko.favicons.decoders.LoadFaviconResult;
|
2013-09-12 14:49:36 +00:00
|
|
|
import org.mozilla.gecko.util.GeckoJarReader;
|
2014-09-10 23:10:34 +00:00
|
|
|
import org.mozilla.gecko.util.IOUtils;
|
2013-10-07 19:54:41 +00:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2014-08-20 15:50:58 +00:00
|
|
|
|
2013-10-11 23:42:39 +00:00
|
|
|
import java.io.IOException;
|
2013-09-12 14:49:36 +00:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
2013-10-11 23:42:39 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
2013-10-13 04:23:10 +00:00
|
|
|
import java.util.LinkedList;
|
2014-08-12 02:50:47 +00:00
|
|
|
import java.util.concurrent.RejectedExecutionException;
|
2013-09-12 14:50:03 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2014-09-10 23:10:34 +00:00
|
|
|
import static org.mozilla.gecko.util.IOUtils.ConsumedInputStream;
|
|
|
|
|
2013-09-12 14:49:36 +00:00
|
|
|
/**
|
|
|
|
* Class representing the asynchronous task to load a Favicon which is not currently in the in-memory
|
|
|
|
* cache.
|
|
|
|
* The implementation initially tries to get the Favicon from the database. Upon failure, the icon
|
|
|
|
* is loaded from the internet.
|
|
|
|
*/
|
2014-08-12 02:50:47 +00:00
|
|
|
public class LoadFaviconTask {
|
2013-09-12 14:49:36 +00:00
|
|
|
private static final String LOGTAG = "LoadFaviconTask";
|
|
|
|
|
2013-10-11 23:42:39 +00:00
|
|
|
// Access to this map needs to be synchronized prevent multiple jobs loading the same favicon
|
|
|
|
// from executing concurrently.
|
2014-09-07 06:57:02 +00:00
|
|
|
private static final HashMap<String, LoadFaviconTask> loadsInFlight = new HashMap<>();
|
2013-10-11 23:42:39 +00:00
|
|
|
|
2013-09-12 14:49:36 +00:00
|
|
|
public static final int FLAG_PERSIST = 1;
|
2013-10-11 23:42:39 +00:00
|
|
|
private static final int MAX_REDIRECTS_TO_FOLLOW = 5;
|
2014-01-18 03:24:28 +00:00
|
|
|
// The default size of the buffer to use for downloading Favicons in the event no size is given
|
|
|
|
// by the server.
|
2014-09-10 23:10:34 +00:00
|
|
|
public static final int DEFAULT_FAVICON_BUFFER_SIZE = 25000;
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2014-09-07 06:57:02 +00:00
|
|
|
private static final AtomicInteger nextFaviconLoadId = new AtomicInteger(0);
|
|
|
|
private final Context context;
|
|
|
|
private final int id;
|
|
|
|
private final String pageUrl;
|
2014-03-12 16:20:38 +00:00
|
|
|
private String faviconURL;
|
2014-09-07 06:57:02 +00:00
|
|
|
private final OnFaviconLoadedListener listener;
|
|
|
|
private final int flags;
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2014-03-12 16:20:38 +00:00
|
|
|
private final boolean onlyFromLocal;
|
2014-09-09 01:11:51 +00:00
|
|
|
volatile boolean mCancelled;
|
2013-10-11 23:42:39 +00:00
|
|
|
|
|
|
|
// Assuming square favicons, judging by width only is acceptable.
|
2014-03-12 16:20:38 +00:00
|
|
|
protected int targetWidth;
|
|
|
|
private LinkedList<LoadFaviconTask> chainees;
|
|
|
|
private boolean isChaining;
|
2013-10-11 23:42:39 +00:00
|
|
|
|
2014-03-12 16:20:38 +00:00
|
|
|
static AndroidHttpClient httpClient = AndroidHttpClient.newInstance(GeckoAppShell.getGeckoInterface().getDefaultUAString());
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2014-09-07 06:57:02 +00:00
|
|
|
public LoadFaviconTask(Context context, String pageURL, String faviconURL, int flags, OnFaviconLoadedListener listener) {
|
|
|
|
this(context, pageURL, faviconURL, flags, listener, -1, false);
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2014-09-07 06:57:02 +00:00
|
|
|
public LoadFaviconTask(Context context, String pageURL, String faviconURL, int flags, OnFaviconLoadedListener listener,
|
2014-08-12 02:50:47 +00:00
|
|
|
int targetWidth, boolean onlyFromLocal) {
|
2014-03-12 16:20:38 +00:00
|
|
|
id = nextFaviconLoadId.incrementAndGet();
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2014-09-07 06:57:02 +00:00
|
|
|
this.context = context;
|
2014-08-12 02:50:47 +00:00
|
|
|
this.pageUrl = pageURL;
|
|
|
|
this.faviconURL = faviconURL;
|
2014-03-12 16:20:38 +00:00
|
|
|
this.listener = listener;
|
|
|
|
this.flags = flags;
|
|
|
|
this.targetWidth = targetWidth;
|
|
|
|
this.onlyFromLocal = onlyFromLocal;
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Runs in background thread
|
2014-01-18 03:24:28 +00:00
|
|
|
private LoadFaviconResult loadFaviconFromDb() {
|
2014-03-12 16:20:38 +00:00
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
return BrowserDB.getFaviconForFaviconUrl(resolver, faviconURL);
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Runs in background thread
|
2014-01-18 03:24:28 +00:00
|
|
|
private void saveFaviconToDb(final byte[] encodedFavicon) {
|
2014-05-01 21:19:34 +00:00
|
|
|
if (encodedFavicon == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-12 16:20:38 +00:00
|
|
|
if ((flags & FLAG_PERSIST) == 0) {
|
2013-09-12 14:49:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-12 16:20:38 +00:00
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
BrowserDB.updateFaviconForUrl(resolver, pageUrl, encodedFavicon, faviconURL);
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 23:42:39 +00:00
|
|
|
/**
|
|
|
|
* Helper method for trying the download request to grab a Favicon.
|
|
|
|
* @param faviconURI URL of Favicon to try and download
|
|
|
|
* @return The HttpResponse containing the downloaded Favicon if successful, null otherwise.
|
|
|
|
*/
|
|
|
|
private HttpResponse tryDownload(URI faviconURI) throws URISyntaxException, IOException {
|
2014-09-07 06:57:02 +00:00
|
|
|
HashSet<String> visitedLinkSet = new HashSet<>();
|
2013-10-11 23:42:39 +00:00
|
|
|
visitedLinkSet.add(faviconURI.toString());
|
|
|
|
return tryDownloadRecurse(faviconURI, visitedLinkSet);
|
|
|
|
}
|
|
|
|
private HttpResponse tryDownloadRecurse(URI faviconURI, HashSet<String> visited) throws URISyntaxException, IOException {
|
|
|
|
if (visited.size() == MAX_REDIRECTS_TO_FOLLOW) {
|
|
|
|
return null;
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 23:42:39 +00:00
|
|
|
HttpGet request = new HttpGet(faviconURI);
|
2014-03-12 16:20:38 +00:00
|
|
|
HttpResponse response = httpClient.execute(request);
|
2013-10-11 23:42:39 +00:00
|
|
|
if (response == null) {
|
2013-09-12 14:49:36 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:42:39 +00:00
|
|
|
if (response.getStatusLine() != null) {
|
|
|
|
|
|
|
|
// Was the response a failure?
|
|
|
|
int status = response.getStatusLine().getStatusCode();
|
|
|
|
|
|
|
|
// Handle HTTP status codes requesting a redirect.
|
|
|
|
if (status >= 300 && status < 400) {
|
|
|
|
Header header = response.getFirstHeader("Location");
|
|
|
|
|
|
|
|
// Handle mad webservers.
|
2014-08-05 12:36:50 +00:00
|
|
|
final String newURI;
|
|
|
|
try {
|
|
|
|
if (header == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
newURI = header.getValue();
|
|
|
|
if (newURI == null || newURI.equals(faviconURI.toString())) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (visited.contains(newURI)) {
|
|
|
|
// Already been redirected here - abort.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
visited.add(newURI);
|
|
|
|
} finally {
|
|
|
|
// Consume the entity before recurse or exit.
|
|
|
|
try {
|
|
|
|
response.getEntity().consumeContent();
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Doesn't matter.
|
|
|
|
}
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tryDownloadRecurse(new URI(newURI), visited);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status >= 400) {
|
2014-08-05 12:36:50 +00:00
|
|
|
// Consume the entity and exit.
|
|
|
|
try {
|
|
|
|
response.getEntity().consumeContent();
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Doesn't matter.
|
|
|
|
}
|
2013-10-11 23:42:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:35:16 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the specified favicon from the JAR, returning null if it's not
|
|
|
|
* a JAR URI.
|
|
|
|
*/
|
2014-09-07 06:57:02 +00:00
|
|
|
private Bitmap fetchJARFavicon(String uri) {
|
2013-10-31 17:35:16 +00:00
|
|
|
if (uri == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (uri.startsWith("jar:jar:")) {
|
|
|
|
Log.d(LOGTAG, "Fetching favicon from JAR.");
|
2013-10-31 17:35:17 +00:00
|
|
|
try {
|
2014-03-12 16:20:38 +00:00
|
|
|
return GeckoJarReader.getBitmap(context.getResources(), uri);
|
2013-10-31 17:35:17 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
// Just about anything could happen here.
|
|
|
|
Log.w(LOGTAG, "Error fetching favicon from JAR.", e);
|
|
|
|
return null;
|
|
|
|
}
|
2013-10-31 17:35:16 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs in background thread.
|
2013-10-31 17:35:17 +00:00
|
|
|
// Does not attempt to fetch from JARs.
|
2014-01-18 03:24:28 +00:00
|
|
|
private LoadFaviconResult downloadFavicon(URI targetFaviconURI) {
|
2013-10-31 17:35:16 +00:00
|
|
|
if (targetFaviconURI == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only get favicons for HTTP/HTTPS.
|
2013-10-11 23:42:39 +00:00
|
|
|
String scheme = targetFaviconURI.getScheme();
|
|
|
|
if (!"http".equals(scheme) && !"https".equals(scheme)) {
|
2013-09-12 14:49:36 +00:00
|
|
|
return null;
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2014-01-18 03:24:28 +00:00
|
|
|
LoadFaviconResult result = null;
|
2013-10-31 17:35:17 +00:00
|
|
|
|
2013-09-12 14:49:36 +00:00
|
|
|
try {
|
2014-01-18 03:24:28 +00:00
|
|
|
result = downloadAndDecodeImage(targetFaviconURI);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Error reading favicon", e);
|
2014-08-20 22:30:28 +00:00
|
|
|
} catch (OutOfMemoryError e) {
|
|
|
|
Log.e(LOGTAG, "Insufficient memory to process favicon");
|
2014-01-18 03:24:28 +00:00
|
|
|
}
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2014-01-18 03:24:28 +00:00
|
|
|
return result;
|
|
|
|
}
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2014-01-18 03:24:28 +00:00
|
|
|
/**
|
|
|
|
* Download the Favicon from the given URL and pass it to the decoder function.
|
|
|
|
*
|
|
|
|
* @param targetFaviconURL URL of the favicon to download.
|
|
|
|
* @return A LoadFaviconResult containing the bitmap(s) extracted from the downloaded file, or
|
|
|
|
* null if no or corrupt data ware received.
|
|
|
|
* @throws IOException If attempts to fully read the stream result in such an exception, such as
|
|
|
|
* in the event of a transient connection failure.
|
|
|
|
* @throws URISyntaxException If the underlying call to tryDownload retries and raises such an
|
|
|
|
* exception trying a fallback URL.
|
|
|
|
*/
|
|
|
|
private LoadFaviconResult downloadAndDecodeImage(URI targetFaviconURL) throws IOException, URISyntaxException {
|
|
|
|
// Try the URL we were given.
|
|
|
|
HttpResponse response = tryDownload(targetFaviconURL);
|
|
|
|
if (response == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
|
if (entity == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-08-20 22:30:28 +00:00
|
|
|
// Decode the image from the fetched response.
|
|
|
|
try {
|
|
|
|
return decodeImageFromResponse(entity);
|
|
|
|
} finally {
|
|
|
|
// Close the stream and free related resources.
|
|
|
|
entity.consumeContent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies the favicon stream to a buffer and decodes downloaded content into bitmaps using the
|
|
|
|
* FaviconDecoder.
|
|
|
|
*
|
|
|
|
* @param entity HttpEntity containing the favicon stream to decode.
|
|
|
|
* @return A LoadFaviconResult containing the bitmap(s) extracted from the downloaded file, or
|
|
|
|
* null if no or corrupt data were received.
|
|
|
|
* @throws IOException If attempts to fully read the stream result in such an exception, such as
|
|
|
|
* in the event of a transient connection failure.
|
|
|
|
*/
|
|
|
|
private LoadFaviconResult decodeImageFromResponse(HttpEntity entity) throws IOException {
|
2014-01-18 03:24:28 +00:00
|
|
|
// This may not be provided, but if it is, it's useful.
|
|
|
|
final long entityReportedLength = entity.getContentLength();
|
|
|
|
int bufferSize;
|
|
|
|
if (entityReportedLength > 0) {
|
|
|
|
// The size was reported and sane, so let's use that.
|
|
|
|
// Integer overflow should not be a problem for Favicon sizes...
|
|
|
|
bufferSize = (int) entityReportedLength + 1;
|
|
|
|
} else {
|
|
|
|
// No declared size, so guess and reallocate later if it turns out to be too small.
|
|
|
|
bufferSize = DEFAULT_FAVICON_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
2014-09-10 23:10:34 +00:00
|
|
|
// Read the InputStream into a byte[].
|
|
|
|
ConsumedInputStream result = IOUtils.readFully(entity.getContent(), bufferSize);
|
|
|
|
if (result == null) {
|
|
|
|
return null;
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
2014-01-18 03:24:28 +00:00
|
|
|
// Having downloaded the image, decode it.
|
2014-09-10 23:10:34 +00:00
|
|
|
return FaviconDecoder.decodeFavicon(result.getData(), 0, result.consumedLength);
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
2014-09-10 23:10:34 +00:00
|
|
|
// LoadFaviconTasks are performed on a unique background executor thread
|
2014-08-12 02:50:47 +00:00
|
|
|
// to avoid network blocking.
|
|
|
|
public final void execute() {
|
|
|
|
try {
|
|
|
|
Favicons.longRunningExecutor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
final Bitmap result = doInBackground();
|
|
|
|
|
|
|
|
ThreadUtils.getUiHandler().post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (mCancelled) {
|
|
|
|
onCancelled();
|
|
|
|
} else {
|
|
|
|
onPostExecute(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (RejectedExecutionException e) {
|
|
|
|
// If our executor is unavailable.
|
|
|
|
onCancelled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final boolean cancel() {
|
|
|
|
mCancelled = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final boolean isCancelled() {
|
|
|
|
return mCancelled;
|
|
|
|
}
|
|
|
|
|
2014-09-09 01:11:51 +00:00
|
|
|
Bitmap doInBackground() {
|
2013-10-11 23:42:39 +00:00
|
|
|
if (isCancelled()) {
|
2013-09-12 14:49:36 +00:00
|
|
|
return null;
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2014-09-20 00:20:45 +00:00
|
|
|
// Attempt to decode the favicon URL as a data URL. We don't bother storing such URIs in
|
|
|
|
// the database: the cost of decoding them here probably doesn't exceed the cost of mucking
|
|
|
|
// about with the DB.
|
|
|
|
LoadFaviconResult uriBitmaps = FaviconDecoder.decodeDataURI(faviconURL);
|
|
|
|
if (uriBitmaps != null) {
|
|
|
|
return pushToCacheAndGetResult(uriBitmaps);
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:42:39 +00:00
|
|
|
String storedFaviconUrl;
|
|
|
|
boolean isUsingDefaultURL = false;
|
|
|
|
|
|
|
|
// Handle the case of malformed favicon URL.
|
|
|
|
// If favicon is empty, fall back to the stored one.
|
2014-03-12 16:20:38 +00:00
|
|
|
if (TextUtils.isEmpty(faviconURL)) {
|
2013-10-11 23:42:39 +00:00
|
|
|
// Try to get the favicon URL from the memory cache.
|
2014-03-12 16:20:38 +00:00
|
|
|
storedFaviconUrl = Favicons.getFaviconURLForPageURLFromCache(pageUrl);
|
2013-10-11 23:42:39 +00:00
|
|
|
|
|
|
|
// If that failed, try to get the URL from the database.
|
|
|
|
if (storedFaviconUrl == null) {
|
2014-09-07 06:57:02 +00:00
|
|
|
storedFaviconUrl = Favicons.getFaviconURLForPageURL(context, pageUrl);
|
2013-10-11 23:42:39 +00:00
|
|
|
if (storedFaviconUrl != null) {
|
|
|
|
// If that succeeded, cache the URL loaded from the database in memory.
|
2014-03-12 16:20:38 +00:00
|
|
|
Favicons.putFaviconURLForPageURLInCache(pageUrl, storedFaviconUrl);
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2013-10-11 23:42:39 +00:00
|
|
|
// If we found a faviconURL - use it.
|
|
|
|
if (storedFaviconUrl != null) {
|
2014-03-12 16:20:38 +00:00
|
|
|
faviconURL = storedFaviconUrl;
|
2013-09-12 14:49:36 +00:00
|
|
|
} else {
|
2013-10-11 23:42:39 +00:00
|
|
|
// If we don't have a stored one, fall back to the default.
|
2014-03-12 16:20:38 +00:00
|
|
|
faviconURL = Favicons.guessDefaultFaviconURL(pageUrl);
|
2013-10-14 17:37:03 +00:00
|
|
|
|
2014-03-12 16:20:38 +00:00
|
|
|
if (TextUtils.isEmpty(faviconURL)) {
|
2013-10-14 17:37:03 +00:00
|
|
|
return null;
|
|
|
|
}
|
2013-10-11 23:42:39 +00:00
|
|
|
isUsingDefaultURL = true;
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if favicon has failed - if so, give up. We need this check because, sometimes, we
|
|
|
|
// didn't know the real Favicon URL until we asked the database.
|
2014-03-12 16:20:38 +00:00
|
|
|
if (Favicons.isFailedFavicon(faviconURL)) {
|
2013-09-12 14:49:36 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:42:39 +00:00
|
|
|
if (isCancelled()) {
|
2013-09-12 14:49:36 +00:00
|
|
|
return null;
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap image;
|
|
|
|
// Determine if there is already an ongoing task to fetch the Favicon we desire.
|
|
|
|
// If there is, just join the queue and wait for it to finish. If not, we carry on.
|
|
|
|
synchronized(loadsInFlight) {
|
|
|
|
// Another load of the current Favicon is already underway
|
2014-03-12 16:20:38 +00:00
|
|
|
LoadFaviconTask existingTask = loadsInFlight.get(faviconURL);
|
2013-10-11 23:42:39 +00:00
|
|
|
if (existingTask != null && !existingTask.isCancelled()) {
|
|
|
|
existingTask.chainTasks(this);
|
2014-03-12 16:20:38 +00:00
|
|
|
isChaining = true;
|
2013-10-13 04:23:10 +00:00
|
|
|
|
|
|
|
// If we are chaining, we want to keep the first task started to do this job as the one
|
|
|
|
// in the hashmap so subsequent tasks will add themselves to its chaining list.
|
|
|
|
return null;
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
2013-10-13 04:23:10 +00:00
|
|
|
// We do not want to update the hashmap if the task has chained - other tasks need to
|
|
|
|
// chain onto the same parent task.
|
2014-03-12 16:20:38 +00:00
|
|
|
loadsInFlight.put(faviconURL, this);
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isCancelled()) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2014-01-18 03:24:28 +00:00
|
|
|
// If there are no valid bitmaps decoded, the returned LoadFaviconResult is null.
|
|
|
|
LoadFaviconResult loadedBitmaps = loadFaviconFromDb();
|
|
|
|
if (loadedBitmaps != null) {
|
|
|
|
return pushToCacheAndGetResult(loadedBitmaps);
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 16:20:38 +00:00
|
|
|
if (onlyFromLocal || isCancelled()) {
|
2013-10-11 23:42:39 +00:00
|
|
|
return null;
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
2013-10-31 17:35:16 +00:00
|
|
|
// Let's see if it's in a JAR.
|
2014-03-12 16:20:38 +00:00
|
|
|
image = fetchJARFavicon(faviconURL);
|
2014-01-18 03:24:28 +00:00
|
|
|
if (imageIsValid(image)) {
|
2013-10-31 17:35:16 +00:00
|
|
|
// We don't want to put this into the DB.
|
2014-03-12 16:20:38 +00:00
|
|
|
Favicons.putFaviconInMemCache(faviconURL, image);
|
2013-10-31 17:35:16 +00:00
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:42:39 +00:00
|
|
|
try {
|
2014-03-12 16:20:38 +00:00
|
|
|
loadedBitmaps = downloadFavicon(new URI(faviconURL));
|
2013-10-11 23:42:39 +00:00
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
Log.e(LOGTAG, "The provided favicon URL is not valid");
|
2013-09-12 14:49:36 +00:00
|
|
|
return null;
|
2013-10-31 17:35:16 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Couldn't download favicon.", e);
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2014-01-18 03:24:28 +00:00
|
|
|
if (loadedBitmaps != null) {
|
2014-05-01 21:19:34 +00:00
|
|
|
// Fetching bytes to store can fail. saveFaviconToDb will
|
|
|
|
// do the right thing, but we still choose to cache the
|
|
|
|
// downloaded icon in memory.
|
2014-01-18 03:24:28 +00:00
|
|
|
saveFaviconToDb(loadedBitmaps.getBytesForDatabaseStorage());
|
|
|
|
return pushToCacheAndGetResult(loadedBitmaps);
|
2013-10-31 17:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isUsingDefaultURL) {
|
2014-03-12 16:20:38 +00:00
|
|
|
Favicons.putFaviconInFailedCache(faviconURL);
|
2013-10-31 17:35:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-01-18 03:24:28 +00:00
|
|
|
if (isCancelled()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:42:39 +00:00
|
|
|
// If we're not already trying the default URL, try it now.
|
2014-03-12 16:20:38 +00:00
|
|
|
final String guessed = Favicons.guessDefaultFaviconURL(pageUrl);
|
2013-10-31 17:35:17 +00:00
|
|
|
if (guessed == null) {
|
2014-03-12 16:20:38 +00:00
|
|
|
Favicons.putFaviconInFailedCache(faviconURL);
|
2013-10-31 17:35:17 +00:00
|
|
|
return null;
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2013-10-31 17:35:17 +00:00
|
|
|
image = fetchJARFavicon(guessed);
|
|
|
|
if (imageIsValid(image)) {
|
|
|
|
// We don't want to put this into the DB.
|
2014-03-12 16:20:38 +00:00
|
|
|
Favicons.putFaviconInMemCache(faviconURL, image);
|
2013-10-31 17:35:17 +00:00
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2014-01-18 03:24:28 +00:00
|
|
|
loadedBitmaps = downloadFavicon(new URI(guessed));
|
2013-10-31 17:35:17 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
// Not interesting. It was an educated guess, anyway.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-01-18 03:24:28 +00:00
|
|
|
if (loadedBitmaps != null) {
|
|
|
|
saveFaviconToDb(loadedBitmaps.getBytesForDatabaseStorage());
|
|
|
|
return pushToCacheAndGetResult(loadedBitmaps);
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
2013-10-31 17:35:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-01-18 03:24:28 +00:00
|
|
|
/**
|
|
|
|
* Helper method to put the result of a favicon load into the memory cache and then query the
|
|
|
|
* cache for the particular bitmap we want for this request.
|
|
|
|
* This call is certain to succeed, provided there was enough memory to decode this favicon.
|
|
|
|
*
|
|
|
|
* @param loadedBitmaps LoadFaviconResult to store.
|
|
|
|
* @return The optimal favicon available to satisfy this LoadFaviconTask's request, or null if
|
|
|
|
* we are under extreme memory pressure and find ourselves dropping the cache immediately.
|
|
|
|
*/
|
|
|
|
private Bitmap pushToCacheAndGetResult(LoadFaviconResult loadedBitmaps) {
|
2014-03-12 16:20:38 +00:00
|
|
|
Favicons.putFaviconsInMemCache(faviconURL, loadedBitmaps.getBitmaps());
|
|
|
|
Bitmap result = Favicons.getSizedFaviconFromCache(faviconURL, targetWidth);
|
2014-01-18 03:24:28 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:35:17 +00:00
|
|
|
private static boolean imageIsValid(final Bitmap image) {
|
|
|
|
return image != null &&
|
|
|
|
image.getWidth() > 0 &&
|
|
|
|
image.getHeight() > 0;
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 01:11:51 +00:00
|
|
|
void onPostExecute(Bitmap image) {
|
2014-03-12 16:20:38 +00:00
|
|
|
if (isChaining) {
|
2013-10-11 23:42:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-13 04:23:10 +00:00
|
|
|
// Process the result, scale for the listener, etc.
|
2013-10-11 23:42:39 +00:00
|
|
|
processResult(image);
|
2013-10-13 04:23:10 +00:00
|
|
|
|
|
|
|
synchronized (loadsInFlight) {
|
|
|
|
// Prevent any other tasks from chaining on this one.
|
2014-03-12 16:20:38 +00:00
|
|
|
loadsInFlight.remove(faviconURL);
|
2013-10-13 04:23:10 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 16:20:38 +00:00
|
|
|
// Since any update to chainees is done while holding the loadsInFlight lock, once we reach
|
2013-10-13 04:23:10 +00:00
|
|
|
// this point no further updates to that list can possibly take place (As far as other tasks
|
|
|
|
// are concerned, there is no longer a task to chain from. The above block will have waited
|
|
|
|
// for any tasks that were adding themselves to the list before reaching this point.)
|
|
|
|
|
|
|
|
// As such, I believe we're safe to do the following without holding the lock.
|
|
|
|
// This is nice - we do not want to take the lock unless we have to anyway, and chaining rarely
|
|
|
|
// actually happens outside of the strange situations unit tests create.
|
|
|
|
|
|
|
|
// Share the result with all chained tasks.
|
2014-03-12 16:20:38 +00:00
|
|
|
if (chainees != null) {
|
|
|
|
for (LoadFaviconTask t : chainees) {
|
2014-01-18 03:24:28 +00:00
|
|
|
// In the case that we just decoded multiple favicons, either we're passing the right
|
|
|
|
// image now, or the call into the cache in processResult will fetch the right one.
|
2013-10-13 04:23:10 +00:00
|
|
|
t.processResult(image);
|
|
|
|
}
|
|
|
|
}
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void processResult(Bitmap image) {
|
2014-03-12 16:20:38 +00:00
|
|
|
Favicons.removeLoadTask(id);
|
2013-10-11 23:42:39 +00:00
|
|
|
Bitmap scaled = image;
|
|
|
|
|
|
|
|
// Notify listeners, scaling if required.
|
2014-03-12 16:20:38 +00:00
|
|
|
if (targetWidth != -1 && image != null && image.getWidth() != targetWidth) {
|
|
|
|
scaled = Favicons.getSizedFaviconFromCache(faviconURL, targetWidth);
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 16:20:38 +00:00
|
|
|
Favicons.dispatchResult(pageUrl, faviconURL, scaled, listener);
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 01:11:51 +00:00
|
|
|
void onCancelled() {
|
2014-03-12 16:20:38 +00:00
|
|
|
Favicons.removeLoadTask(id);
|
2013-09-12 14:49:36 +00:00
|
|
|
|
2013-10-11 23:42:39 +00:00
|
|
|
synchronized(loadsInFlight) {
|
2013-10-13 04:23:10 +00:00
|
|
|
// Only remove from the hashmap if the task there is the one that's being canceled.
|
|
|
|
// Cancellation of a task that would have chained is not interesting to the hashmap.
|
2014-03-12 16:20:38 +00:00
|
|
|
final LoadFaviconTask primary = loadsInFlight.get(faviconURL);
|
2013-10-13 04:23:10 +00:00
|
|
|
if (primary == this) {
|
2014-03-12 16:20:38 +00:00
|
|
|
loadsInFlight.remove(faviconURL);
|
2013-10-13 04:23:10 +00:00
|
|
|
return;
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
2013-10-13 05:32:03 +00:00
|
|
|
if (primary == null) {
|
|
|
|
// This shouldn't happen.
|
|
|
|
return;
|
|
|
|
}
|
2014-03-12 16:20:38 +00:00
|
|
|
if (primary.chainees != null) {
|
|
|
|
primary.chainees.remove(this);
|
2013-10-13 05:32:03 +00:00
|
|
|
}
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 14:49:36 +00:00
|
|
|
// Note that we don't call the listener callback if the
|
|
|
|
// favicon load is cancelled.
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:42:39 +00:00
|
|
|
/**
|
|
|
|
* When the result of this job is ready, also notify the chainee of the result.
|
|
|
|
* Used for aggregating concurrent requests for the same Favicon into a single actual request.
|
|
|
|
* (Don't want to download a hundred instances of Google's Favicon at once, for example).
|
2013-10-13 04:23:10 +00:00
|
|
|
* The loadsInFlight lock must be held when calling this function.
|
2013-10-11 23:42:39 +00:00
|
|
|
*
|
|
|
|
* @param aChainee LoadFaviconTask
|
|
|
|
*/
|
|
|
|
private void chainTasks(LoadFaviconTask aChainee) {
|
2014-03-12 16:20:38 +00:00
|
|
|
if (chainees == null) {
|
2014-09-07 06:57:02 +00:00
|
|
|
chainees = new LinkedList<>();
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 16:20:38 +00:00
|
|
|
chainees.add(aChainee);
|
2013-10-11 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 14:50:03 +00:00
|
|
|
int getId() {
|
2014-03-12 16:20:38 +00:00
|
|
|
return id;
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void closeHTTPClient() {
|
2013-10-07 19:54:41 +00:00
|
|
|
// This work must be done on a background thread because it shuts down
|
|
|
|
// the connection pool, which typically involves closing a connection --
|
|
|
|
// which counts as network activity.
|
|
|
|
if (ThreadUtils.isOnBackgroundThread()) {
|
2014-03-12 16:20:38 +00:00
|
|
|
if (httpClient != null) {
|
|
|
|
httpClient.close();
|
2013-10-07 19:54:41 +00:00
|
|
|
}
|
|
|
|
return;
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
2013-10-07 19:54:41 +00:00
|
|
|
|
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
LoadFaviconTask.closeHTTPClient();
|
|
|
|
}
|
|
|
|
});
|
2013-09-12 14:49:36 +00:00
|
|
|
}
|
|
|
|
}
|