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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/firstrun/variations/VariationsSeedService.java

Issue 1417733008: Java code for fetching variations first run seed after receiving chrome.TOS_ACKED broadcast. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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: chrome/android/java/src/org/chromium/chrome/browser/firstrun/variations/VariationsSeedService.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/firstrun/variations/VariationsSeedService.java b/chrome/android/java/src/org/chromium/chrome/browser/firstrun/variations/VariationsSeedService.java
new file mode 100644
index 0000000000000000000000000000000000000000..b889099d88d968c5042b83b56449de50f9dbacca
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/firstrun/variations/VariationsSeedService.java
@@ -0,0 +1,106 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.firstrun.variations;
+
+import android.app.IntentService;
+import android.content.Intent;
+import android.os.AsyncTask;
+import android.os.IBinder;
+import android.util.Log;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+/**
+ * Background service that fetches the variations seed before the actual first run of Chrome.
+ */
+public class VariationsSeedService extends IntentService {
+ private static final String TAG = "VariationsSeedService";
+ private static final String VARIATIONS_SERVER_URL =
+ "https://clients4.google.com/chrome-variations/seed?osname=android";
+ private static final int BUFFER_SIZE = 4096;
+ private static final int READ_TIMEOUT = 10000; // time in ms
+ private static final int REQUEST_TIMEOUT = 15000; // time in ms
+
+ public VariationsSeedService() {
+ super(TAG);
+ }
+
+ @Override
+ public void onHandleIntent(Intent intent) {
+ Log.d(TAG, "Variations service started!");
+ try {
+ new DownloadTask().execute(new URL(VARIATIONS_SERVER_URL));
+ } catch (MalformedURLException e) {
+ Log.w(TAG, "Variations server URL is malformed.");
+ }
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return null;
+ }
+
+ private class DownloadTask extends AsyncTask<URL, Void, Boolean> {
+ @Override
+ protected Boolean doInBackground(URL... params) {
+ try {
+ return downloadContent(params[0]);
+ } catch (IOException e) {
+ return false;
+ }
+ }
+ }
+
+ private Boolean downloadContent(URL variationsServerUrl)
+ throws IOException, MalformedURLException {
+ InputStream inputStream = null;
+ try {
+ HttpURLConnection connection = (HttpURLConnection) variationsServerUrl.openConnection();
+ connection.setReadTimeout(READ_TIMEOUT);
+ connection.setConnectTimeout(REQUEST_TIMEOUT);
+ connection.setRequestMethod("GET");
+ connection.setDoInput(true);
+ // TODO(agulenko): add gzip compression support.
+ // connection.setRequestProperty("A-IM", "gzip");
+ connection.connect();
+ int responseCode = connection.getResponseCode();
+
+ Log.d(TAG, "variationsTracker: Response code = " + Integer.toString(responseCode));
+ inputStream = connection.getInputStream();
+
+ // Convert the InputStream into a byte array.
+ byte[] rawSeed = convertInputStreamToByteArray(inputStream);
+ String signature = connection.getHeaderField("X-Seed-Signature");
+ String country = connection.getHeaderField("X-Country");
+ VariationsSeedBridge.setVariationsFirstRunSeed(
+ getApplicationContext(), rawSeed, signature, country);
+ return true;
+ } finally {
+ if (inputStream != null) {
+ inputStream.close();
+ }
+ }
+ }
+
+ public byte[] convertInputStreamToByteArray(InputStream inputStream)
+ throws IOException, UnsupportedEncodingException {
+ ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int charactersReadCount = 0;
+ int totalCharactersCount = 0;
+ while ((charactersReadCount = inputStream.read(buffer)) != -1) {
+ byteBuffer.write(buffer, 0, charactersReadCount);
+ totalCharactersCount += charactersReadCount;
+ }
+ Log.w(TAG, "variationsTracker: seed length = " + totalCharactersCount);
+ return byteBuffer.toByteArray();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698