Skip to content

Commit

Permalink
Migrate LoadFaviconTask to StrongHttpsClient
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Jul 16, 2015
1 parent 0b4a571 commit 50807f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 35 deletions.
2 changes: 0 additions & 2 deletions mobile/android/base/favicons/Favicons.java
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,6 @@ public static void close() {
}
loadTasks.clear();
}

LoadFaviconTask.closeHTTPClient();
}

/**
Expand Down
47 changes: 14 additions & 33 deletions mobile/android/base/favicons/LoadFaviconTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.favicons.decoders.FaviconDecoder;
import org.mozilla.gecko.favicons.decoders.LoadFaviconResult;
import org.mozilla.gecko.util.ProxyRoutePlanner;
import org.mozilla.gecko.util.GeckoJarReader;
import org.mozilla.gecko.util.IOUtils;
import org.mozilla.gecko.util.ProxySettings;
import org.mozilla.gecko.util.ThreadUtils;

import java.io.IOException;
Expand All @@ -33,9 +33,9 @@
import ch.boye.httpclientandroidlib.Header;
import ch.boye.httpclientandroidlib.HttpEntity;
import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.client.HttpClient;
import ch.boye.httpclientandroidlib.client.methods.HttpGet;
import ch.boye.httpclientandroidlib.impl.client.CloseableHttpClient;
import ch.boye.httpclientandroidlib.impl.client.HttpClients;
import info.guardianproject.netcipher.client.StrongHttpsClient;

import static org.mozilla.gecko.util.IOUtils.ConsumedInputStream;

Expand Down Expand Up @@ -75,11 +75,6 @@ public class LoadFaviconTask {
private LinkedList<LoadFaviconTask> chainees;
private boolean isChaining;

static CloseableHttpClient httpClient = HttpClients.custom()
.setUserAgent(GeckoAppShell.getGeckoInterface().getDefaultUAString())
.setRoutePlanner(new ProxyRoutePlanner())
.build();

public LoadFaviconTask(Context context, String pageURL, String faviconURL, int flags, OnFaviconLoadedListener listener) {
this(context, pageURL, faviconURL, flags, listener, -1, false);
}
Expand Down Expand Up @@ -126,14 +121,22 @@ private void saveFaviconToDb(final BrowserDB db, final byte[] encodedFavicon) {
private HttpResponse tryDownload(URI faviconURI) throws URISyntaxException, IOException {
HashSet<String> visitedLinkSet = new HashSet<>();
visitedLinkSet.add(faviconURI.toString());
return tryDownloadRecurse(faviconURI, visitedLinkSet);

StrongHttpsClient client = new StrongHttpsClient(context);
ProxySettings.setProxy(client);
try {
return tryDownloadRecurse(client, faviconURI, visitedLinkSet);
} finally {
client.close();
}
}
private HttpResponse tryDownloadRecurse(URI faviconURI, HashSet<String> visited) throws URISyntaxException, IOException {
private HttpResponse tryDownloadRecurse(HttpClient httpClient, URI faviconURI, HashSet<String> visited) throws URISyntaxException, IOException {
if (visited.size() == MAX_REDIRECTS_TO_FOLLOW) {
return null;
}

HttpGet request = new HttpGet(faviconURI);
request.setHeader("User-Agent", GeckoAppShell.getGeckoInterface().getDefaultUAString());

HttpResponse response = httpClient.execute(request);
if (response == null) {
Expand Down Expand Up @@ -176,7 +179,7 @@ private HttpResponse tryDownloadRecurse(URI faviconURI, HashSet<String> visited)
}
}

return tryDownloadRecurse(new URI(newURI), visited);
return tryDownloadRecurse(httpClient, new URI(newURI), visited);
}

if (status >= 400) {
Expand Down Expand Up @@ -604,26 +607,4 @@ private void chainTasks(LoadFaviconTask aChainee) {
int getId() {
return id;
}

static void closeHTTPClient() {
// 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()) {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
}
}
return;
}

ThreadUtils.postToBackgroundThread(new Runnable() {
@Override
public void run() {
LoadFaviconTask.closeHTTPClient();
}
});
}
}

0 comments on commit 50807f5

Please sign in to comment.