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

Side by Side Diff: android_webview/java/src/org/chromium/android_webview/AwVariationsSeedFetcher.java

Issue 2970993002: Add AwVariationsSeedFetcher and refactory VariationsSeedFetcher (Closed)
Patch Set: Update unittest 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 unified diff | Download patch
OLDNEW
(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
9 import org.chromium.base.ContextUtils;
10 import org.chromium.base.Log;
11 import org.chromium.base.ThreadUtils;
12 import org.chromium.components.variations.firstrun.VariationsSeedFetcher;
13
14 import java.io.FileNotFoundException;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17 import java.io.ObjectOutputStream;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 /**
22 * Fetches the variations seed before the actual first run of Android WebView.
paulmiller 2017/07/06 23:09:05 I don't think this is true in WebView's case.
sgurun-gerrit only 2017/07/06 23:38:10 yep not true. Please update class doc to explain i
23 */
24 public class AwVariationsSeedFetcher {
25 private static final String TAG = "AwVariatnsSeedFetch";
26
27 public static final String VARIATIONS_PLATFORM = "android_webview";
Alexei Svitkine (slow) 2017/07/06 22:01:10 I think this is fine to put in the VariationsSeedF
28 private static final String SEED_DATA_FILENAME = "seed";
29 private static final String SEED_DATA_PREF_FILENAME = "seed_pref";
30
31 // Synchronization lock
paulmiller 2017/07/06 23:09:05 Superfluous comment (not yours, but still).
32 private static final Object sLock = new Object();
33
34 private static AwVariationsSeedFetcher sInstance;
35
36 AwVariationsSeedFetcher() {}
sgurun-gerrit only 2017/07/06 23:38:11 is this class supposed to be publicly instantiatab
37
38 public static AwVariationsSeedFetcher get() {
39 // TODO(aberent) Check not running on UI thread. Doing so however makes Robolectric testing
sgurun-gerrit only 2017/07/06 23:38:11 why are we expecting aberent to fix that? is this
40 // of dependent classes difficult.
41 synchronized (sLock) {
42 if (sInstance == null) {
43 Log.d(TAG, "using android webview fetcher");
sgurun-gerrit only 2017/07/06 23:38:11 does not look necessary, remove.
44 sInstance = new AwVariationsSeedFetcher();
45 }
46 return sInstance;
47 }
48 }
49
50 public void fetchSeed(String restrictMode) {
sgurun-gerrit only 2017/07/06 23:38:11 and who is going to call that? write tests maybe?
sgurun-gerrit only 2017/07/06 23:38:11 static
51 assert !ThreadUtils.runningOnUiThread();
52 // Prevent multiple simultaneous fetches
sgurun-gerrit only 2017/07/06 23:38:11 document how multiple simultanous fetches are poss
53 synchronized (sLock) {
54 Map<String, String> headerFields = new HashMap<String, String>();
55 byte[] rawSeed = VariationsSeedFetcher.get().downloadContent(
56 VariationsSeedFetcher.VARIATIONS_SERVER_URL + VARIATIONS_PLA TFORM, restrictMode,
57 headerFields);
58 storeSeed(rawSeed, headerFields);
59 }
60 }
61
62 private void storeSeed(byte[] rawSeed, Map<String, String> headerFields) {
sgurun-gerrit only 2017/07/06 23:38:11 static
63 FileOutputStream fosSeed = null;
64 ObjectOutputStream fosSeedPref = null;
65 try {
66 fosSeed = ContextUtils.getApplicationContext().openFileOutput(
67 SEED_DATA_FILENAME, Context.MODE_PRIVATE);
68 fosSeed.write(rawSeed, 0, rawSeed.length);
69 fosSeedPref =
70 new ObjectOutputStream(ContextUtils.getApplicationContext(). openFileOutput(
71 SEED_DATA_PREF_FILENAME, Context.MODE_PRIVATE));
72 fosSeedPref.writeObject(headerFields);
73 } catch (FileNotFoundException e) {
74 Log.e(TAG, "FileNotFoundException storing seed: ", e);
paulmiller 2017/07/06 23:09:05 Mention Finch in these error messages, so people h
75 } catch (IOException e) {
76 Log.e(TAG, "IOException storing seed: ", e);
77 } finally {
78 if (fosSeed != null) {
79 try {
80 fosSeed.close();
81 } catch (IOException e) {
82 Log.e(TAG, "IOException file close: ", e);
83 }
84 }
85 if (fosSeedPref != null) {
86 try {
87 fosSeedPref.close();
88 } catch (IOException e) {
89 Log.e(TAG, "IOException file close: ", e);
90 }
91 }
92 }
93 }
94 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698