| 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();
|
| + }
|
| +}
|
|
|