OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.android_webview; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.os.SystemClock; |
| 9 |
| 10 import org.chromium.base.ContextUtils; |
| 11 import org.chromium.base.Log; |
| 12 import org.chromium.base.ThreadUtils; |
| 13 import org.chromium.components.variations.firstrun.VariationsSeedFetcher; |
| 14 |
| 15 import java.io.FileNotFoundException; |
| 16 import java.io.FileOutputStream; |
| 17 import java.io.IOException; |
| 18 import java.io.ObjectOutputStream; |
| 19 import java.net.SocketTimeoutException; |
| 20 import java.net.UnknownHostException; |
| 21 |
| 22 /** |
| 23 * Fetches the variations seed in Java layer before Android WebView's native cod
e runs. |
| 24 */ |
| 25 public class AwVariationsSeedFetcher { |
| 26 private static final String TAG = "AwVariatnsSeedFetch"; |
| 27 |
| 28 private static final String SEED_DATA_FILENAME = "variations_seed"; |
| 29 private static final String SEED_DATA_PREF_FILENAME = "variations_seed_pref"
; |
| 30 |
| 31 // Synchronization lock to prevent simultaneous local seed file writing |
| 32 private static final Object sLock = new Object(); |
| 33 |
| 34 private AwVariationsSeedFetcher() {} |
| 35 |
| 36 /** |
| 37 * Store the variations seed preference. |
| 38 */ |
| 39 public static class SeedPreference { |
| 40 public boolean isGzipCompressed; |
| 41 public long lastFetchTime; |
| 42 public String signature; |
| 43 public String country; |
| 44 public String date; |
| 45 |
| 46 public SeedPreference(VariationsSeedFetcher.SeedInfo info) { |
| 47 isGzipCompressed = info.isGzipCompressed; |
| 48 signature = info.signature; |
| 49 country = info.country; |
| 50 date = info.date; |
| 51 } |
| 52 } |
| 53 |
| 54 public static void fetchSeed(String restrictMode) { |
| 55 assert !ThreadUtils.runningOnUiThread(); |
| 56 // Prevent multiple simultaneous file writing |
| 57 synchronized (sLock) { |
| 58 try { |
| 59 VariationsSeedFetcher.SeedInfo si = VariationsSeedFetcher.get().
downloadContent( |
| 60 VariationsSeedFetcher.VariationsPlatform.ANDROID_WEBVIEW
, restrictMode); |
| 61 if (si != null) { |
| 62 SeedPreference sp = new SeedPreference(si); |
| 63 sp.lastFetchTime = SystemClock.elapsedRealtime(); |
| 64 storeSeed(si.rawSeed, sp); |
| 65 } |
| 66 } catch (SocketTimeoutException e) { |
| 67 } catch (UnknownHostException e) { |
| 68 } catch (IOException e) { |
| 69 } |
| 70 } |
| 71 } |
| 72 |
| 73 private static void storeSeed(final byte[] rawSeed, final SeedPreference sp)
{ |
| 74 FileOutputStream fosSeed = null; |
| 75 ObjectOutputStream oosSeedPref = null; |
| 76 final Context appContext = ContextUtils.getApplicationContext(); |
| 77 try { |
| 78 fosSeed = appContext.openFileOutput(SEED_DATA_FILENAME, Context.MODE
_PRIVATE); |
| 79 fosSeed.write(rawSeed, 0, rawSeed.length); |
| 80 oosSeedPref = new ObjectOutputStream( |
| 81 appContext.openFileOutput(SEED_DATA_PREF_FILENAME, Context.M
ODE_PRIVATE)); |
| 82 oosSeedPref.writeObject(sp); |
| 83 } catch (FileNotFoundException e) { |
| 84 Log.e(TAG, "FileNotFoundException store seed."); |
| 85 } catch (IOException e) { |
| 86 Log.e(TAG, "IOException store seed"); |
| 87 } finally { |
| 88 if (fosSeed != null) { |
| 89 try { |
| 90 fosSeed.close(); |
| 91 } catch (IOException e) { |
| 92 Log.e(TAG, "IOException file close"); |
| 93 } |
| 94 } |
| 95 if (oosSeedPref != null) { |
| 96 try { |
| 97 oosSeedPref.close(); |
| 98 } catch (IOException e) { |
| 99 Log.e(TAG, "IOException file close"); |
| 100 } |
| 101 } |
| 102 } |
| 103 } |
| 104 } |
OLD | NEW |