OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.components.variations.firstrun; | 5 package org.chromium.components.variations.firstrun; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.SharedPreferences; | 8 import android.content.SharedPreferences; |
9 import android.os.SystemClock; | 9 import android.os.SystemClock; |
10 | 10 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 // Prevent multiple simultaneous fetches | 96 // Prevent multiple simultaneous fetches |
97 synchronized (sLock) { | 97 synchronized (sLock) { |
98 Context context = ContextUtils.getApplicationContext(); | 98 Context context = ContextUtils.getApplicationContext(); |
99 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); | 99 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); |
100 // Early return if an attempt has already been made to fetch the see
d, even if it | 100 // Early return if an attempt has already been made to fetch the see
d, even if it |
101 // failed. Only attempt to get the initial Java seed once, since a f
ailure probably | 101 // failed. Only attempt to get the initial Java seed once, since a f
ailure probably |
102 // indicates a network problem that is unlikely to be resolved by a
second attempt. | 102 // indicates a network problem that is unlikely to be resolved by a
second attempt. |
103 // Note that VariationsSeedBridge.hasNativePref() is a pure Java fun
ction, reading an | 103 // Note that VariationsSeedBridge.hasNativePref() is a pure Java fun
ction, reading an |
104 // Android preference that is set when the seed is fetched by the na
tive code. | 104 // Android preference that is set when the seed is fetched by the na
tive code. |
105 if (prefs.getBoolean(VARIATIONS_INITIALIZED_PREF, false) | 105 if (prefs.getBoolean(VARIATIONS_INITIALIZED_PREF, false) |
106 || VariationsSeedBridge.hasNativePref(context)) { | 106 || VariationsSeedBridge.hasNativePref()) { |
107 return; | 107 return; |
108 } | 108 } |
109 downloadContent(context, restrictMode); | 109 downloadContent(context, restrictMode); |
110 prefs.edit().putBoolean(VARIATIONS_INITIALIZED_PREF, true).apply(); | 110 prefs.edit().putBoolean(VARIATIONS_INITIALIZED_PREF, true).apply(); |
111 } | 111 } |
112 } | 112 } |
113 | 113 |
114 private void recordFetchResultOrCode(int resultOrCode) { | 114 private void recordFetchResultOrCode(int resultOrCode) { |
115 SparseHistogramSample histogram = | 115 SparseHistogramSample histogram = |
116 new SparseHistogramSample("Variations.FirstRun.SeedFetchResult")
; | 116 new SparseHistogramSample("Variations.FirstRun.SeedFetchResult")
; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 } | 148 } |
149 | 149 |
150 recordSeedConnectTime(SystemClock.elapsedRealtime() - startTimeMilli
s); | 150 recordSeedConnectTime(SystemClock.elapsedRealtime() - startTimeMilli
s); |
151 // Convert the InputStream into a byte array. | 151 // Convert the InputStream into a byte array. |
152 byte[] rawSeed = getRawSeed(connection); | 152 byte[] rawSeed = getRawSeed(connection); |
153 String signature = getHeaderFieldOrEmpty(connection, "X-Seed-Signatu
re"); | 153 String signature = getHeaderFieldOrEmpty(connection, "X-Seed-Signatu
re"); |
154 String country = getHeaderFieldOrEmpty(connection, "X-Country"); | 154 String country = getHeaderFieldOrEmpty(connection, "X-Country"); |
155 String date = getHeaderFieldOrEmpty(connection, "Date"); | 155 String date = getHeaderFieldOrEmpty(connection, "Date"); |
156 boolean isGzipCompressed = getHeaderFieldOrEmpty(connection, "IM").e
quals("gzip"); | 156 boolean isGzipCompressed = getHeaderFieldOrEmpty(connection, "IM").e
quals("gzip"); |
157 VariationsSeedBridge.setVariationsFirstRunSeed( | 157 VariationsSeedBridge.setVariationsFirstRunSeed( |
158 context, rawSeed, signature, country, date, isGzipCompressed
); | 158 rawSeed, signature, country, date, isGzipCompressed); |
159 recordSeedFetchTime(SystemClock.elapsedRealtime() - startTimeMillis)
; | 159 recordSeedFetchTime(SystemClock.elapsedRealtime() - startTimeMillis)
; |
160 } catch (SocketTimeoutException e) { | 160 } catch (SocketTimeoutException e) { |
161 recordFetchResultOrCode(SEED_FETCH_RESULT_TIMEOUT); | 161 recordFetchResultOrCode(SEED_FETCH_RESULT_TIMEOUT); |
162 Log.w(TAG, "SocketTimeoutException fetching first run seed: ", e); | 162 Log.w(TAG, "SocketTimeoutException fetching first run seed: ", e); |
163 } catch (UnknownHostException e) { | 163 } catch (UnknownHostException e) { |
164 recordFetchResultOrCode(SEED_FETCH_RESULT_UNKNOWN_HOST_EXCEPTION); | 164 recordFetchResultOrCode(SEED_FETCH_RESULT_UNKNOWN_HOST_EXCEPTION); |
165 Log.w(TAG, "UnknownHostException fetching first run seed: ", e); | 165 Log.w(TAG, "UnknownHostException fetching first run seed: ", e); |
166 } catch (IOException e) { | 166 } catch (IOException e) { |
167 recordFetchResultOrCode(SEED_FETCH_RESULT_IOEXCEPTION); | 167 recordFetchResultOrCode(SEED_FETCH_RESULT_IOEXCEPTION); |
168 Log.w(TAG, "IOException fetching first run seed: ", e); | 168 Log.w(TAG, "IOException fetching first run seed: ", e); |
(...skipping 27 matching lines...) Expand all Loading... |
196 private byte[] convertInputStreamToByteArray(InputStream inputStream) throws
IOException { | 196 private byte[] convertInputStreamToByteArray(InputStream inputStream) throws
IOException { |
197 ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); | 197 ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); |
198 byte[] buffer = new byte[BUFFER_SIZE]; | 198 byte[] buffer = new byte[BUFFER_SIZE]; |
199 int charactersReadCount = 0; | 199 int charactersReadCount = 0; |
200 while ((charactersReadCount = inputStream.read(buffer)) != -1) { | 200 while ((charactersReadCount = inputStream.read(buffer)) != -1) { |
201 byteBuffer.write(buffer, 0, charactersReadCount); | 201 byteBuffer.write(buffer, 0, charactersReadCount); |
202 } | 202 } |
203 return byteBuffer.toByteArray(); | 203 return byteBuffer.toByteArray(); |
204 } | 204 } |
205 } | 205 } |
OLD | NEW |