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

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

Issue 2975693002: Add AwVariationsSeedFetchService and refactory VariationsSeedFetcher (Closed)
Patch Set: Update for comments of Patch 5 and fix test failure 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.annotation.SuppressLint;
8 import android.app.job.JobParameters;
9 import android.app.job.JobService;
10 import android.os.AsyncTask;
11
12 import org.chromium.base.ContextUtils;
13 import org.chromium.base.Log;
14 import org.chromium.base.ThreadUtils;
15 import org.chromium.components.variations.firstrun.VariationsSeedFetcher;
16
17 import java.io.Closeable;
18 import java.io.File;
19 import java.io.FileNotFoundException;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.ObjectOutputStream;
23 import java.io.Serializable;
24 import java.util.concurrent.locks.Lock;
25 import java.util.concurrent.locks.ReentrantLock;
26
27 /**
28 * AwVariationsSeedFetchService is a Job Service to fetch test seed data which i s used by Finch
29 * to enable AB tesing experiments in the native code. The fetched data is store d in the local
30 * directory which belongs to the Service process.
31 */
32 @SuppressLint("NewApi") // JobService requires API level 21.
33 public class AwVariationsSeedFetchService extends JobService {
34 private static final String TAG = "AwVartnsSeedFetchSvc";
35
36 public static final String WEBVIEW_VARIATIONS_DIR = "WebView_Variations/";
37 public static final String SEED_DATA_FILENAME = "variations_seed_data";
38 public static final String SEED_PREF_FILENAME = "variations_seed_pref";
39
40 // Synchronization lock to prevent simultaneous local seed file writing.
41 private static final Lock sLock = new ReentrantLock();
42
43 @Override
44 public boolean onStartJob(final JobParameters params) {
45 new FetchFinchSeedDataTask(params).execute();
46 return true;
47 }
48
49 @Override
50 public boolean onStopJob(JobParameters params) {
51 // When job parameter is not satisfied any more, the job should be resch eduled.
52 return true;
53 }
54
55 private class FetchFinchSeedDataTask extends AsyncTask<Void, Void, Void> {
Alexei Svitkine (slow) 2017/07/12 15:52:50 Can this class be static?
yiyuny 2017/07/12 18:31:30 I prefer to make it non-static because function jo
Alexei Svitkine (slow) 2017/07/12 19:01:21 That makes sense.
56 private JobParameters mJobParams;
57
58 FetchFinchSeedDataTask(JobParameters params) {
59 mJobParams = params;
60 }
61
62 @Override
63 protected Void doInBackground(Void... params) {
64 AwVariationsSeedFetchService.fetchSeed();
65 return null;
66 }
67
68 @Override
69 protected void onPostExecute(Void success) {
70 jobFinished(mJobParams, false);
71 }
72 }
73
74 private static void fetchSeed() {
75 assert !ThreadUtils.runningOnUiThread();
76 // TryLock will drop calls from other threads when one threads are execu ting the function.
77 // TODO(yiyuny): Add explicitly control to make sure there is only one t hread fetching the
78 // seed in a while before the seed is expired.
Alexei Svitkine (slow) 2017/07/12 15:52:50 Nit: "before the seed is expired" isn't too meanin
yiyuny 2017/07/12 18:31:30 Done.
79 if (sLock.tryLock()) {
80 try {
81 VariationsSeedFetcher.SeedInfo si = VariationsSeedFetcher.get(). downloadContent(
Alexei Svitkine (slow) 2017/07/12 15:52:50 Nit: seedInfo
yiyuny 2017/07/12 18:31:30 Done.
82 VariationsSeedFetcher.VariationsPlatform.ANDROID_WEBVIEW , "");
83 storeVariationsSeed(si);
84 } catch (IOException e) {
85 // Exceptions are handled in the downloadContent function and re throwing the
86 // exception is to stop the normal logic flow after it, so no er ror-handling here.
87 // Not explicitly handing SocketTimeoutException and UnknownHost Exception for they
88 // are both subclasses of IOException.
89 } finally {
90 sLock.unlock();
91 }
92 }
93 }
94
95 /**
96 * Store seed preference independently from Seed Info.
Alexei Svitkine (slow) 2017/07/12 15:52:49 This comment reads like a method comment, since it
yiyuny 2017/07/12 18:31:30 Done.
97 */
98 public static class SeedPreference implements Serializable {
99 public String signature;
100 public String country;
101 public String date;
102 public boolean isGzipCompressed;
Alexei Svitkine (slow) 2017/07/12 15:52:49 Can these be final - or is that something that's n
yiyuny 2017/07/12 18:31:30 Done.
103
104 public SeedPreference(final VariationsSeedFetcher.SeedInfo si) {
Alexei Svitkine (slow) 2017/07/12 15:52:49 Nit: seedInfo. Acronyms are discouraged, especiall
yiyuny 2017/07/12 18:31:30 Done.
105 signature = si.signature;
106 country = si.country;
107 date = si.date;
108 isGzipCompressed = si.isGzipCompressed;
109 }
110 }
111
112 private static File getOrCreateWebViewVariationsDir() {
113 File dir = new File(
114 ContextUtils.getApplicationContext().getFilesDir(), WEBVIEW_VARI ATIONS_DIR);
Alexei Svitkine (slow) 2017/07/12 15:52:50 Nit: If this is wrapping already, suggest extracti
yiyuny 2017/07/12 18:31:30 Done.
115 if (dir.mkdir() || dir.isDirectory()) {
116 return dir;
117 }
118 return null;
119 }
120
121 private static void storeVariationsSeed(final VariationsSeedFetcher.SeedInfo si) {
Alexei Svitkine (slow) 2017/07/12 15:52:49 seedInfo
yiyuny 2017/07/12 18:31:30 Done.
122 FileOutputStream fosSeed = null;
123 ObjectOutputStream oosSeedPref = null;
Alexei Svitkine (slow) 2017/07/12 15:52:49 Nit: Move these to right above the try.
yiyuny 2017/07/12 18:31:30 Done.
124 File webViewVariationsDir = getOrCreateWebViewVariationsDir();
125 if (webViewVariationsDir == null) {
126 return;
127 }
128 try {
129 fosSeed = new FileOutputStream(new File(webViewVariationsDir, SEED_D ATA_FILENAME));
130 fosSeed.write(si.rawSeed, 0, si.rawSeed.length);
131 // Store separately so that reading large seed data (which is expens ive) can be
132 // prevented when checking the last seed fetch time.
133 oosSeedPref = new ObjectOutputStream(
134 new FileOutputStream(new File(webViewVariationsDir, SEED_PRE F_FILENAME)));
135 oosSeedPref.writeObject(new SeedPreference(si));
136 } catch (FileNotFoundException e) {
137 Log.e(TAG,
138 "FileNotFoundException failed to open file to write seed dat a or preference.");
139 } catch (IOException e) {
140 Log.e(TAG,
141 "IOException failed to write variations seed data or prefere nce to the file.");
142 } finally {
143 closeStream(fosSeed, SEED_DATA_FILENAME);
144 closeStream(oosSeedPref, SEED_PREF_FILENAME);
145 }
146 }
147
148 private static void closeStream(Closeable stream, String filename) {
149 if (stream != null) {
150 try {
151 stream.close();
152 } catch (IOException e) {
153 Log.e(TAG, "IOException failed to close " + filename + " file.") ;
154 }
155 }
156 }
157 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698