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

Unified Diff: android_webview/java/src/org/chromium/android_webview/AwVariationsSeedFetcher.java

Issue 2970993002: Add AwVariationsSeedFetcher and refactory VariationsSeedFetcher (Closed)
Patch Set: Update logic in handling Exceptions 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: android_webview/java/src/org/chromium/android_webview/AwVariationsSeedFetcher.java
diff --git a/android_webview/java/src/org/chromium/android_webview/AwVariationsSeedFetcher.java b/android_webview/java/src/org/chromium/android_webview/AwVariationsSeedFetcher.java
new file mode 100644
index 0000000000000000000000000000000000000000..90c9a331f93c952098ffd8f30e6cd513e82e2af4
--- /dev/null
+++ b/android_webview/java/src/org/chromium/android_webview/AwVariationsSeedFetcher.java
@@ -0,0 +1,104 @@
+// Copyright 2017 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.android_webview;
+
+import android.content.Context;
+import android.os.SystemClock;
+
+import org.chromium.base.ContextUtils;
+import org.chromium.base.Log;
+import org.chromium.base.ThreadUtils;
+import org.chromium.components.variations.firstrun.VariationsSeedFetcher;
+
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.net.SocketTimeoutException;
+import java.net.UnknownHostException;
+
+/**
+ * Fetches the variations seed in Java layer before Android WebView's native code runs.
+ */
+public class AwVariationsSeedFetcher {
+ private static final String TAG = "AwVariatnsSeedFetch";
+
+ private static final String SEED_DATA_FILENAME = "variations_seed";
+ private static final String SEED_DATA_PREF_FILENAME = "variations_seed_pref";
+
+ // Synchronization lock to prevent simultaneous local seed file writing
+ private static final Object sLock = new Object();
+
+ private AwVariationsSeedFetcher() {}
+
+ /**
+ * Store the variations seed preference.
+ */
+ public static class SeedPreference {
+ public boolean isGzipCompressed;
+ public long lastFetchTime;
+ public String signature;
+ public String country;
+ public String date;
+
+ public SeedPreference(VariationsSeedFetcher.SeedInfo info) {
+ isGzipCompressed = info.isGzipCompressed;
+ signature = info.signature;
+ country = info.country;
+ date = info.date;
+ }
+ }
+
+ public static void fetchSeed(String restrictMode) {
+ assert !ThreadUtils.runningOnUiThread();
+ // Prevent multiple simultaneous file writing
+ synchronized (sLock) {
+ try {
+ VariationsSeedFetcher.SeedInfo si = VariationsSeedFetcher.get().downloadContent(
+ VariationsSeedFetcher.VariationsPlatform.ANDROID_WEBVIEW, restrictMode);
+ if (si != null) {
+ SeedPreference sp = new SeedPreference(si);
+ sp.lastFetchTime = SystemClock.elapsedRealtime();
+ storeSeed(si.rawSeed, sp);
+ }
+ } catch (SocketTimeoutException e) {
+ } catch (UnknownHostException e) {
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ private static void storeSeed(final byte[] rawSeed, final SeedPreference sp) {
+ FileOutputStream fosSeed = null;
+ ObjectOutputStream oosSeedPref = null;
+ final Context appContext = ContextUtils.getApplicationContext();
+ try {
+ fosSeed = appContext.openFileOutput(SEED_DATA_FILENAME, Context.MODE_PRIVATE);
+ fosSeed.write(rawSeed, 0, rawSeed.length);
+ oosSeedPref = new ObjectOutputStream(
+ appContext.openFileOutput(SEED_DATA_PREF_FILENAME, Context.MODE_PRIVATE));
+ oosSeedPref.writeObject(sp);
+ } catch (FileNotFoundException e) {
+ Log.e(TAG, "FileNotFoundException store seed.");
+ } catch (IOException e) {
+ Log.e(TAG, "IOException store seed");
+ } finally {
+ if (fosSeed != null) {
+ try {
+ fosSeed.close();
+ } catch (IOException e) {
+ Log.e(TAG, "IOException file close");
+ }
+ }
+ if (oosSeedPref != null) {
+ try {
+ oosSeedPref.close();
+ } catch (IOException e) {
+ Log.e(TAG, "IOException file close");
+ }
+ }
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698