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

Unified Diff: components/variations/android/java/src/org/chromium/components/variations/firstrun/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: Moved exception catch closer to place of possible raise 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: components/variations/android/java/src/org/chromium/components/variations/firstrun/VariationsSeedService.java
diff --git a/components/variations/android/java/src/org/chromium/components/variations/firstrun/VariationsSeedService.java b/components/variations/android/java/src/org/chromium/components/variations/firstrun/VariationsSeedService.java
new file mode 100644
index 0000000000000000000000000000000000000000..24bb3ceb0cfac75330561285ad51563a9e6bb2cc
--- /dev/null
+++ b/components/variations/android/java/src/org/chromium/components/variations/firstrun/VariationsSeedService.java
@@ -0,0 +1,97 @@
+// 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.components.variations.firstrun;
+
+import android.app.IntentService;
+import android.content.Intent;
+
+import org.chromium.base.Log;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+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 = "VariationsSeedServ";
+ 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) {
+ try {
+ downloadContent(new URL(VARIATIONS_SERVER_URL));
+ } catch (MalformedURLException e) {
+ Log.w(TAG, "Variations server URL is malformed.", e);
+ }
+ }
+
+ private boolean downloadContent(URL variationsServerUrl) throws MalformedURLException {
+ try {
+ return doActualFetch(variationsServerUrl);
+ } catch (IOException e) {
+ Log.w(TAG, "Variations service has thrown IO Exception.", e);
+ return false;
+ }
+ }
+
+ private boolean doActualFetch(URL variationsServerUrl)
+ throws IOException, MalformedURLException {
newt (away) 2015/11/10 06:20:15 Remove the "throws" clause; this shouldn't throw I
Alexander Agulenko 2015/11/10 06:40:12 See my comment below.
+ InputStream inputStream = null;
+ HttpURLConnection connection = null;
+ try {
+ connection = (HttpURLConnection) variationsServerUrl.openConnection();
+ connection.setReadTimeout(READ_TIMEOUT);
+ connection.setConnectTimeout(REQUEST_TIMEOUT);
+ connection.setDoInput(true);
+ // TODO(agulenko): add gzip compression support.
+ // connection.setRequestProperty("A-IM", "gzip");
+ connection.connect();
+ int responseCode = connection.getResponseCode();
+ if (responseCode != HttpURLConnection.HTTP_OK) {
+ Log.d(TAG, "Response code = %d", responseCode);
Alexei Svitkine (slow) 2015/11/10 16:23:24 Nit: Move this above outside the if, or changing t
Alexander Agulenko 2015/11/10 22:44:13 Done.
+ return false;
+ }
+ 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 {
newt (away) 2015/11/10 06:20:15 just add the catch statement here (and delete the
Alexander Agulenko 2015/11/10 06:40:12 I tried to make exactly as you suggest, but inputS
Alexander Agulenko 2015/11/10 06:41:55 Of course, I can resolve it by making nested try/c
newt (away) 2015/11/10 17:17:51 Actually, you should wrap inputStream.close() in a
Steven Holte 2015/11/10 20:51:10 For tidiness, split into two funcions: private by
Alexander Agulenko 2015/11/10 22:44:13 Done.
Alexander Agulenko 2015/11/10 22:44:13 Check the comment below by Steven, it seems to be
+ if (inputStream != null) {
+ inputStream.close();
+ }
+ if (connection != null) {
+ connection.disconnect();
+ }
+ }
+ }
+
+ private byte[] convertInputStreamToByteArray(InputStream inputStream) throws IOException {
+ ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int charactersReadCount = 0;
+ while ((charactersReadCount = inputStream.read(buffer)) != -1) {
+ byteBuffer.write(buffer, 0, charactersReadCount);
+ }
+ return byteBuffer.toByteArray();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698