Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Unified Diff: components/variations/android/java/src/org/chromium/components/variations/firstrun/VariationsSeedFetcher.java

Issue 2975693002: Add AwVariationsSeedFetchService and refactory VariationsSeedFetcher (Closed)
Patch Set: Update for comments of Patch 4 Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/variations/android/java/src/org/chromium/components/variations/firstrun/VariationsSeedFetcher.java
diff --git a/components/variations/android/java/src/org/chromium/components/variations/firstrun/VariationsSeedFetcher.java b/components/variations/android/java/src/org/chromium/components/variations/firstrun/VariationsSeedFetcher.java
index b02c6f1adf8d27000ab71e6a0838fbf1c3162402..a960c55004bdca976008ce0f8b05d1bdd4970c7a 100644
--- a/components/variations/android/java/src/org/chromium/components/variations/firstrun/VariationsSeedFetcher.java
+++ b/components/variations/android/java/src/org/chromium/components/variations/firstrun/VariationsSeedFetcher.java
@@ -4,7 +4,6 @@
package org.chromium.components.variations.firstrun;
-import android.content.Context;
import android.content.SharedPreferences;
import android.os.SystemClock;
@@ -30,8 +29,11 @@ import java.util.concurrent.TimeUnit;
*/
public class VariationsSeedFetcher {
private static final String TAG = "VariationsSeedFetch";
+
+ public enum VariationsPlatform { ANDROID, ANDROID_WEBVIEW }
+
private static final String VARIATIONS_SERVER_URL =
- "https://clientservices.googleapis.com/chrome-variations/seed?osname=android";
+ "https://clientservices.googleapis.com/chrome-variations/seed?osname=";
private static final int BUFFER_SIZE = 4096;
private static final int READ_TIMEOUT = 3000; // time in ms
@@ -47,7 +49,7 @@ public class VariationsSeedFetcher {
@VisibleForTesting
static final String VARIATIONS_INITIALIZED_PREF = "variations_initialized";
- // Synchronization lock
+ // Synchronization lock to make singleton thread-safe
private static final Object sLock = new Object();
private static VariationsSeedFetcher sInstance;
@@ -77,9 +79,19 @@ public class VariationsSeedFetcher {
}
@VisibleForTesting
- protected HttpURLConnection getServerConnection(String restrictMode)
- throws MalformedURLException, IOException {
+ protected HttpURLConnection getServerConnection(final VariationsPlatform platform,
+ final String restrictMode) throws MalformedURLException, IOException {
String urlString = VARIATIONS_SERVER_URL;
+ switch (platform) {
+ case ANDROID:
+ urlString += "android";
+ break;
+ case ANDROID_WEBVIEW:
+ urlString += "android_webview";
+ break;
+ default:
+ assert false;
+ }
if (restrictMode != null && !restrictMode.isEmpty()) {
urlString += "&restrict=" + restrictMode;
}
@@ -87,6 +99,17 @@ public class VariationsSeedFetcher {
return (HttpURLConnection) url.openConnection();
}
+ /**
+ * Store the seed and its related header fields
+ */
+ public static class SeedInfo {
+ public String signature;
+ public String country;
+ public String date;
+ public boolean isGzipCompressed;
+ public byte[] rawSeed;
+ }
+
/**
* Fetch the first run variations seed.
* @param restrictMode The restrict mode parameter to pass to the server via a URL param.
@@ -95,7 +118,6 @@ public class VariationsSeedFetcher {
assert !ThreadUtils.runningOnUiThread();
// Prevent multiple simultaneous fetches
synchronized (sLock) {
- Context context = ContextUtils.getApplicationContext();
SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
// Early return if an attempt has already been made to fetch the seed, even if it
// failed. Only attempt to get the initial Java seed once, since a failure probably
@@ -106,7 +128,17 @@ public class VariationsSeedFetcher {
|| VariationsSeedBridge.hasNativePref()) {
return;
}
- downloadContent(context, restrictMode);
+
+ try {
+ SeedInfo info = downloadContent(VariationsPlatform.ANDROID, restrictMode);
+ VariationsSeedBridge.setVariationsFirstRunSeed(info.rawSeed, info.signature,
+ info.country, info.date, info.isGzipCompressed);
+ } catch (IOException e) {
+ // Exceptions are handled in the downloadContent function and rethrowing the
+ // exception is to stop the normal logic flow after it, so no error-handling here.
+ // Not explicitly handing SocketTimeoutException and UnknownHostException for they
+ // are both subclasses of IOException.
+ }
prefs.edit().putBoolean(VARIATIONS_INITIALIZED_PREF, true).apply();
}
}
@@ -130,11 +162,13 @@ public class VariationsSeedFetcher {
histogram.record(timeDeltaMillis);
}
- private void downloadContent(Context context, String restrictMode) {
+ public SeedInfo downloadContent(final VariationsPlatform platform, final String restrictMode)
Alexei Svitkine (slow) 2017/07/11 19:37:24 Please add javadoc documenting params, return valu
yiyuny 2017/07/11 22:43:37 Done.
+ throws SocketTimeoutException, UnknownHostException, IOException {
HttpURLConnection connection = null;
+ SeedInfo info = new SeedInfo();
Alexei Svitkine (slow) 2017/07/11 19:37:24 Can this be constructed right above line 187? Ide
try {
long startTimeMillis = SystemClock.elapsedRealtime();
- connection = getServerConnection(restrictMode);
+ connection = getServerConnection(platform, restrictMode);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(REQUEST_TIMEOUT);
connection.setDoInput(true);
@@ -144,33 +178,38 @@ public class VariationsSeedFetcher {
recordFetchResultOrCode(responseCode);
if (responseCode != HttpURLConnection.HTTP_OK) {
Log.w(TAG, "Non-OK response code = %d", responseCode);
- return;
+ throw new IOException(
+ "HTTP error code when fetching variations seed data: " + responseCode);
Alexei Svitkine (slow) 2017/07/11 19:37:24 Instead of crafting a whole new string, how about
yiyuny 2017/07/11 22:43:37 Done.
}
recordSeedConnectTime(SystemClock.elapsedRealtime() - startTimeMillis);
// Convert the InputStream into a byte array.
- byte[] rawSeed = getRawSeed(connection);
- String signature = getHeaderFieldOrEmpty(connection, "X-Seed-Signature");
- String country = getHeaderFieldOrEmpty(connection, "X-Country");
- String date = getHeaderFieldOrEmpty(connection, "Date");
- boolean isGzipCompressed = getHeaderFieldOrEmpty(connection, "IM").equals("gzip");
- VariationsSeedBridge.setVariationsFirstRunSeed(
- rawSeed, signature, country, date, isGzipCompressed);
+ info.rawSeed = getRawSeed(connection);
+ info.signature = getHeaderFieldOrEmpty(connection, "X-Seed-Signature");
+ info.country = getHeaderFieldOrEmpty(connection, "X-Country");
+ info.date = getHeaderFieldOrEmpty(connection, "Date");
+ info.isGzipCompressed = getHeaderFieldOrEmpty(connection, "IM").equals("gzip");
recordSeedFetchTime(SystemClock.elapsedRealtime() - startTimeMillis);
} catch (SocketTimeoutException e) {
recordFetchResultOrCode(SEED_FETCH_RESULT_TIMEOUT);
- Log.w(TAG, "SocketTimeoutException fetching first run seed: ", e);
+ Log.w(TAG, "SocketTimeoutException timeout when fetching variations seed.", e);
+ throw e;
} catch (UnknownHostException e) {
recordFetchResultOrCode(SEED_FETCH_RESULT_UNKNOWN_HOST_EXCEPTION);
- Log.w(TAG, "UnknownHostException fetching first run seed: ", e);
+ Log.w(TAG, "UnknownHostException unknown host when fetching variations seed.", e);
+ throw e;
} catch (IOException e) {
recordFetchResultOrCode(SEED_FETCH_RESULT_IOEXCEPTION);
- Log.w(TAG, "IOException fetching first run seed: ", e);
+ Log.w(TAG,
+ "IOException I/O errors while opening the connection to fetch variations seed.",
+ e);
+ throw e;
} finally {
if (connection != null) {
connection.disconnect();
}
}
+ return info;
}
private String getHeaderFieldOrEmpty(HttpURLConnection connection, String name) {

Powered by Google App Engine
This is Rietveld 408576698