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

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

Issue 2970993002: Add AwVariationsSeedFetcher and refactory VariationsSeedFetcher (Closed)
Patch Set: Update unittest 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..66987f7daa263300dc128974a155d44db6f797ec 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;
@@ -23,6 +22,8 @@ import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
@@ -30,8 +31,15 @@ import java.util.concurrent.TimeUnit;
*/
public class VariationsSeedFetcher {
private static final String TAG = "VariationsSeedFetch";
- private static final String VARIATIONS_SERVER_URL =
- "https://clientservices.googleapis.com/chrome-variations/seed?osname=android";
+
+ public static final String VARIATIONS_PLATFORM = "android";
Alexei Svitkine (slow) 2017/07/06 22:01:10 Nit: VARIATIONS_PLATFORM_ANDROID Also, add a blan
+ public static final String VARIATIONS_SERVER_URL =
+ "https://clientservices.googleapis.com/chrome-variations/seed?osname=";
+
+ public static final String SIGNATURE_HEADERFIELD = "X-Seed-Signature";
+ public static final String COUNTRY_HEADERFIELD = "X-Country";
+ public static final String DATE_HEADERFIELD = "Date";
+ public static final String IM_HEADERFIELD = "IM";
private static final int BUFFER_SIZE = 4096;
private static final int READ_TIMEOUT = 3000; // time in ms
@@ -77,9 +85,8 @@ public class VariationsSeedFetcher {
}
@VisibleForTesting
- protected HttpURLConnection getServerConnection(String restrictMode)
+ protected HttpURLConnection getServerConnection(String urlString, String restrictMode)
throws MalformedURLException, IOException {
- String urlString = VARIATIONS_SERVER_URL;
if (restrictMode != null && !restrictMode.isEmpty()) {
urlString += "&restrict=" + restrictMode;
}
@@ -95,7 +102,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 +112,18 @@ public class VariationsSeedFetcher {
|| VariationsSeedBridge.hasNativePref()) {
return;
}
- downloadContent(context, restrictMode);
+
+ Map<String, String> headerFields = new HashMap<String, String>();
+ byte[] rawSeed = downloadContent(
+ VARIATIONS_SERVER_URL + VARIATIONS_PLATFORM, restrictMode, headerFields);
+ if (rawSeed != null) {
+ String signature = headerFields.get(SIGNATURE_HEADERFIELD);
+ String country = headerFields.get(COUNTRY_HEADERFIELD);
+ String date = headerFields.get(DATE_HEADERFIELD);
+ boolean isGzipCompressed = headerFields.get(IM_HEADERFIELD).equals("gzip");
+ VariationsSeedBridge.setVariationsFirstRunSeed(
+ rawSeed, signature, country, date, isGzipCompressed);
+ }
prefs.edit().putBoolean(VARIATIONS_INITIALIZED_PREF, true).apply();
}
}
@@ -130,11 +147,13 @@ public class VariationsSeedFetcher {
histogram.record(timeDeltaMillis);
}
- private void downloadContent(Context context, String restrictMode) {
+ public byte[] downloadContent(
+ String urlString, String restrictMode, Map<String, String> headFields) {
HttpURLConnection connection = null;
+ byte[] rawSeed = null;
try {
long startTimeMillis = SystemClock.elapsedRealtime();
- connection = getServerConnection(restrictMode);
+ connection = getServerConnection(urlString, restrictMode);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(REQUEST_TIMEOUT);
connection.setDoInput(true);
@@ -144,18 +163,18 @@ public class VariationsSeedFetcher {
recordFetchResultOrCode(responseCode);
if (responseCode != HttpURLConnection.HTTP_OK) {
Log.w(TAG, "Non-OK response code = %d", responseCode);
- return;
+ return null;
}
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);
+ rawSeed = getRawSeed(connection);
+ headFields.put(SIGNATURE_HEADERFIELD,
+ getHeaderFieldOrEmpty(connection, SIGNATURE_HEADERFIELD));
+ headFields.put(
+ COUNTRY_HEADERFIELD, getHeaderFieldOrEmpty(connection, COUNTRY_HEADERFIELD));
+ headFields.put(DATE_HEADERFIELD, getHeaderFieldOrEmpty(connection, DATE_HEADERFIELD));
+ headFields.put(IM_HEADERFIELD, getHeaderFieldOrEmpty(connection, IM_HEADERFIELD));
Alexei Svitkine (slow) 2017/07/06 22:01:10 Suggest defining a simple class with these fields
recordSeedFetchTime(SystemClock.elapsedRealtime() - startTimeMillis);
} catch (SocketTimeoutException e) {
recordFetchResultOrCode(SEED_FETCH_RESULT_TIMEOUT);
@@ -171,6 +190,7 @@ public class VariationsSeedFetcher {
connection.disconnect();
}
}
+ return rawSeed;
paulmiller 2017/07/06 23:09:05 It looks like this can be null, but AwVariationsSe
}
private String getHeaderFieldOrEmpty(HttpURLConnection connection, String name) {

Powered by Google App Engine
This is Rietveld 408576698