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.annotation.SuppressLint; | |
8 import android.app.job.JobParameters; | |
9 import android.app.job.JobService; | |
10 import android.content.Context; | |
11 import android.os.AsyncTask; | |
12 | |
13 import org.chromium.base.ContextUtils; | |
14 import org.chromium.base.Log; | |
15 import org.chromium.base.ThreadUtils; | |
16 import org.chromium.components.variations.firstrun.VariationsSeedFetcher; | |
17 | |
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 | |
Alexei Svitkine (slow)
2017/07/11 19:37:23
Comments should be full sentences. i.e. terminate
yiyuny
2017/07/11 22:43:37
Done.
| |
52 return true; | |
53 } | |
54 | |
55 private class FetchFinchSeedDataTask extends AsyncTask<Void, Void, Void> { | |
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 // Make sure there will only be one task fetching seed | |
77 if (sLock.tryLock()) { | |
Alexei Svitkine (slow)
2017/07/11 19:37:23
I don't think this does what you want it do. Unles
paulmiller
2017/07/11 20:51:36
It was synchronized before. I don't think we want
Alexei Svitkine (slow)
2017/07/11 20:53:47
Gotcha. It seems OK as a temporary solution with a
yiyuny
2017/07/11 22:43:37
Done.
| |
78 try { | |
79 VariationsSeedFetcher.SeedInfo si = VariationsSeedFetcher.get(). downloadContent( | |
80 VariationsSeedFetcher.VariationsPlatform.ANDROID_WEBVIEW , ""); | |
81 storeVariationsSeed(si); | |
82 } catch (IOException e) { | |
83 // Exceptions are handled in the downloadContent function and re throwing the | |
84 // exception is to stop the normal logic flow after it, so no er ror-handling here. | |
85 // Not explicitly handing SocketTimeoutException and UnknownHost Exception for they | |
86 // are both subclasses of IOException. | |
87 } finally { | |
88 sLock.unlock(); | |
89 } | |
90 } | |
91 } | |
92 | |
93 /** | |
94 * Store seed preference independently from Seed Info | |
95 */ | |
96 public static class SeedPreference implements Serializable { | |
Alexei Svitkine (slow)
2017/07/11 19:37:23
Can SeedInfo from the fetcher be re-used?
yiyuny
2017/07/11 22:43:37
I didn't re-use the SeedInfo class is because
1.
| |
97 public String signature; | |
98 public String country; | |
99 public String date; | |
100 public boolean isGzipCompressed; | |
101 | |
102 public SeedPreference(final VariationsSeedFetcher.SeedInfo si) { | |
103 signature = si.signature; | |
104 country = si.country; | |
105 date = si.date; | |
106 isGzipCompressed = si.isGzipCompressed; | |
107 } | |
108 } | |
109 | |
110 public static File getOrCreateWebViewVariationsDir() { | |
Alexei Svitkine (slow)
2017/07/11 19:37:23
Does this need to be public?
yiyuny
2017/07/11 22:43:37
The function will be used when reading the seed pr
| |
111 File dir = new File( | |
112 ContextUtils.getApplicationContext().getFilesDir(), WEBVIEW_VARI ATIONS_DIR); | |
113 if (dir.mkdir() || dir.isDirectory()) { | |
114 return dir; | |
115 } | |
116 return null; | |
117 } | |
118 | |
119 private static void storeVariationsSeed(final VariationsSeedFetcher.SeedInfo si) { | |
120 FileOutputStream fosSeed = null; | |
121 ObjectOutputStream oosSeedPref = null; | |
122 final Context appContext = ContextUtils.getApplicationContext(); | |
123 File webViewVariationsDir = getOrCreateWebViewVariationsDir(); | |
124 if (webViewVariationsDir == null) { | |
125 return; | |
126 } | |
127 try { | |
128 fosSeed = new FileOutputStream(new File(webViewVariationsDir, SEED_D ATA_FILENAME)); | |
129 fosSeed.write(si.rawSeed, 0, si.rawSeed.length); | |
130 // store separately so that reading large seed data (which is expens ive) can be | |
131 // prevented when checking the last seed fetch time | |
132 oosSeedPref = new ObjectOutputStream( | |
133 new FileOutputStream(new File(webViewVariationsDir, SEED_PRE F_FILENAME))); | |
134 oosSeedPref.writeObject(new SeedPreference(si)); | |
135 } catch (FileNotFoundException e) { | |
136 Log.e(TAG, | |
137 "FileNotFoundException failed to open file to write seed dat a or preference."); | |
138 } catch (IOException e) { | |
139 Log.e(TAG, | |
140 "IOException failed to write variations seed data or prefere nce to the file."); | |
141 } finally { | |
142 if (fosSeed != null) { | |
143 try { | |
144 fosSeed.close(); | |
145 } catch (IOException e) { | |
146 Log.e(TAG, "IOException failed to close the variations seed data file."); | |
147 } | |
148 } | |
Alexei Svitkine (slow)
2017/07/11 19:37:23
This is pretty verbose. Perhaps add a helper funct
yiyuny
2017/07/11 22:43:36
Done.
| |
149 if (oosSeedPref != null) { | |
150 try { | |
151 oosSeedPref.close(); | |
152 } catch (IOException e) { | |
153 Log.e(TAG, "IOException failed to close the variations seed preference file."); | |
154 } | |
155 } | |
156 } | |
157 } | |
158 } | |
OLD | NEW |