Index: android_webview/java/src/org/chromium/android_webview/AwVariationsSeedFetchService.java |
diff --git a/android_webview/java/src/org/chromium/android_webview/AwVariationsSeedFetchService.java b/android_webview/java/src/org/chromium/android_webview/AwVariationsSeedFetchService.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7370a0944be1cc70bf7126db92b28ebd5dbbc9e1 |
--- /dev/null |
+++ b/android_webview/java/src/org/chromium/android_webview/AwVariationsSeedFetchService.java |
@@ -0,0 +1,165 @@ |
+// 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.annotation.SuppressLint; |
+import android.app.job.JobParameters; |
+import android.app.job.JobService; |
+import android.os.AsyncTask; |
+ |
+import org.chromium.base.ContextUtils; |
+import org.chromium.base.Log; |
+import org.chromium.base.ThreadUtils; |
+import org.chromium.components.variations.firstrun.VariationsSeedFetcher; |
gsennton
2017/07/13 13:31:59
What's the initialization process for VariationsSe
Alexei Svitkine (slow)
2017/07/13 20:36:25
VariationsSeedFetcher does not depend on any nativ
yiyuny
2017/07/14 01:33:57
Thanks for the explanation from Alexie. Paul and I
|
+import org.chromium.components.variations.firstrun.VariationsSeedFetcher.SeedInfo; |
+ |
+import java.io.Closeable; |
+import java.io.File; |
+import java.io.FileNotFoundException; |
+import java.io.FileOutputStream; |
+import java.io.IOException; |
+import java.io.ObjectOutputStream; |
+import java.io.Serializable; |
+import java.util.concurrent.locks.Lock; |
+import java.util.concurrent.locks.ReentrantLock; |
+ |
+/** |
+ * AwVariationsSeedFetchService is a Job Service to fetch test seed data which is used by Finch |
+ * to enable AB testing experiments in the native code. The fetched data is stored in the local |
+ * directory which belongs to the Service process. |
+ */ |
+@SuppressLint("NewApi") // JobService requires API level 21. |
gsennton
2017/07/13 13:31:58
WebView doesn't support pre-L anyway:
please repla
yiyuny
2017/07/14 01:33:57
Done.
|
+public class AwVariationsSeedFetchService extends JobService { |
+ private static final String TAG = "AwVartnsSeedFetchSvc"; |
+ |
+ public static final String WEBVIEW_VARIATIONS_DIR = "WebView_Variations/"; |
+ public static final String SEED_DATA_FILENAME = "variations_seed_data"; |
+ public static final String SEED_PREF_FILENAME = "variations_seed_pref"; |
+ |
+ // Synchronization lock to prevent simultaneous local seed file writing. |
+ private static final Lock sLock = new ReentrantLock(); |
+ |
+ @Override |
+ public boolean onStartJob(JobParameters params) { |
+ new FetchFinchSeedDataTask(params).execute(); |
+ return true; |
+ } |
+ |
+ @Override |
+ public boolean onStopJob(JobParameters params) { |
+ // When job parameter is not satisfied any more, the job should be rescheduled. |
gsennton
2017/07/13 13:31:59
I have no idea what this comment means :)
yiyuny
2017/07/14 01:33:57
Oh, what I want to say is that the function will o
gsennton
2017/07/14 12:45:39
Right, I think the comment could be made more clea
yiyuny
2017/07/15 00:18:59
Done.
|
+ return true; |
+ } |
+ |
+ private class FetchFinchSeedDataTask extends AsyncTask<Void, Void, Void> { |
+ private JobParameters mJobParams; |
+ |
+ FetchFinchSeedDataTask(JobParameters params) { |
+ mJobParams = params; |
+ } |
+ |
+ @Override |
+ protected Void doInBackground(Void... params) { |
+ AwVariationsSeedFetchService.fetchSeed(); |
+ return null; |
+ } |
+ |
+ @Override |
+ protected void onPostExecute(Void success) { |
+ jobFinished(mJobParams, false); |
gsennton
2017/07/13 13:31:58
Please add an inline comment describing what false
yiyuny
2017/07/14 01:33:57
Done.
|
+ } |
+ } |
+ |
+ private static void fetchSeed() { |
+ assert !ThreadUtils.runningOnUiThread(); |
+ // TryLock will drop calls from other threads when one threads are executing the function. |
+ // TODO(yiyuny): Add explicitly control to to ensure there's only one threading fetching at |
gsennton
2017/07/13 13:31:59
Do you have a plan on how to solve this TODO?
Are
yiyuny
2017/07/14 01:33:56
Yes, I will the do the explicit control in the Fin
|
+ // a time and that the seed doesn't get fetched too frequently |
+ if (sLock.tryLock()) { |
+ try { |
+ SeedInfo seedInfo = VariationsSeedFetcher.get().downloadContent( |
gsennton
2017/07/13 13:31:58
What's the size of the data we are downloading her
Alexei Svitkine (slow)
2017/07/13 20:36:25
The chrome one is around 50k. It will be smaller f
yiyuny
2017/07/14 01:33:57
I'm using NETWORK_TYPE_ANY(data plan/wifi both wor
gsennton
2017/07/14 12:45:39
What I mean is, we should discuss whether it is OK
paulmiller
2017/07/14 17:41:05
System health will be an important consideration f
gsennton
2017/07/17 14:32:10
Ah alright, I did not realize that this is a proto
|
+ VariationsSeedFetcher.VariationsPlatform.ANDROID_WEBVIEW, ""); |
+ storeVariationsSeed(seedInfo); |
+ } catch (IOException e) { |
+ // Exceptions are handled in the downloadContent function and rethrowing the |
gsennton
2017/07/13 13:31:59
Maybe note that exceptions are also logged within
yiyuny
2017/07/14 01:33:57
Done.
|
+ // exception is to stop the normal logic flow after it, so no error-handling here. |
+ // Not explicitly handing SocketTimeoutException and UnknownHostException for they |
+ // are both subclasses of IOException. |
+ } finally { |
+ sLock.unlock(); |
+ } |
+ } |
+ } |
+ |
+ /** |
+ * SeedPreference is used to serialize/deserialize related fields of seed data when reading or |
+ * writing them to the internal storage. |
+ */ |
+ public static class SeedPreference implements Serializable { |
+ /** |
+ * Let the program deserialize the data when the fields are changed. |
+ */ |
+ private static final long serialVersionUID = 0L; |
+ |
+ public final String signature; |
+ public final String country; |
+ public final String date; |
+ public final boolean isGzipCompressed; |
+ |
+ public SeedPreference(SeedInfo seedInfo) { |
+ signature = seedInfo.signature; |
+ country = seedInfo.country; |
+ date = seedInfo.date; |
+ isGzipCompressed = seedInfo.isGzipCompressed; |
+ } |
+ } |
+ |
+ private static File getOrCreateWebViewVariationsDir() { |
+ File webViewFileDir = ContextUtils.getApplicationContext().getFilesDir(); |
gsennton
2017/07/13 13:31:59
Are we guaranteed that ContextUtils is initialized
yiyuny
2017/07/14 01:33:57
I added a initApplicationContext in onStartJob. Th
|
+ File dir = new File(webViewFileDir, WEBVIEW_VARIATIONS_DIR); |
+ if (dir.mkdir() || dir.isDirectory()) { |
+ return dir; |
+ } |
+ return null; |
+ } |
+ |
+ private static void storeVariationsSeed(SeedInfo seedInfo) { |
+ File webViewVariationsDir = getOrCreateWebViewVariationsDir(); |
+ if (webViewVariationsDir == null) { |
gsennton
2017/07/13 13:31:59
Please add a log-statement before the return-state
yiyuny
2017/07/14 01:33:57
Done.
|
+ return; |
+ } |
+ |
+ FileOutputStream fosSeed = null; |
+ ObjectOutputStream oosSeedPref = null; |
+ try { |
+ fosSeed = new FileOutputStream(new File(webViewVariationsDir, SEED_DATA_FILENAME)); |
gsennton
2017/07/13 13:31:58
One very common pattern when performing file write
yiyuny
2017/07/14 01:33:57
There should be read write lock on the seed pref f
|
+ fosSeed.write(seedInfo.seedData, 0, seedInfo.seedData.length); |
+ // Store separately so that reading large seed data (which is expensive) can be |
gsennton
2017/07/13 13:31:59
Just to clarify (to make sure I understand): we ar
yiyuny
2017/07/14 01:33:56
Yes
|
+ // prevented when checking the last seed fetch time. |
+ oosSeedPref = new ObjectOutputStream( |
+ new FileOutputStream(new File(webViewVariationsDir, SEED_PREF_FILENAME))); |
+ oosSeedPref.writeObject(new SeedPreference(seedInfo)); |
+ } catch (FileNotFoundException e) { |
+ Log.e(TAG, |
+ "FileNotFoundException failed to open file to write seed data or preference."); |
gsennton
2017/07/13 13:31:59
Include the actual exception in the logs please (t
yiyuny
2017/07/14 01:33:56
I agree with combining two exceptions together.
P
gsennton
2017/07/14 12:45:39
Well, if you're not gonna print the exception then
paulmiller
2017/07/14 23:06:24
I think these error messages strings are getting n
yiyuny
2017/07/15 00:18:59
I will add 'e' to the end of the string to log wha
|
+ } catch (IOException e) { |
+ Log.e(TAG, |
+ "IOException failed to write variations seed data or preference to the file."); |
+ } finally { |
+ closeStream(fosSeed, SEED_DATA_FILENAME); |
+ closeStream(oosSeedPref, SEED_PREF_FILENAME); |
+ } |
+ } |
+ |
+ private static void closeStream(Closeable stream, String filename) { |
+ if (stream != null) { |
+ try { |
+ stream.close(); |
+ } catch (IOException e) { |
+ Log.e(TAG, "IOException failed to close " + filename + " file."); |
+ } |
+ } |
+ } |
+} |