 Chromium Code Reviews
 Chromium Code Reviews Issue 2975693002:
  Add AwVariationsSeedFetchService and refactory VariationsSeedFetcher  (Closed)
    
  
    Issue 2975693002:
  Add AwVariationsSeedFetchService and refactory VariationsSeedFetcher  (Closed) 
  | 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.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; | |
| 
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
 | |
| 16 import org.chromium.components.variations.firstrun.VariationsSeedFetcher.SeedInf o; | |
| 17 | |
| 18 import java.io.Closeable; | |
| 19 import java.io.File; | |
| 20 import java.io.FileNotFoundException; | |
| 21 import java.io.FileOutputStream; | |
| 22 import java.io.IOException; | |
| 23 import java.io.ObjectOutputStream; | |
| 24 import java.io.Serializable; | |
| 25 import java.util.concurrent.locks.Lock; | |
| 26 import java.util.concurrent.locks.ReentrantLock; | |
| 27 | |
| 28 /** | |
| 29 * AwVariationsSeedFetchService is a Job Service to fetch test seed data which i s used by Finch | |
| 30 * to enable AB testing experiments in the native code. The fetched data is stor ed in the local | |
| 31 * directory which belongs to the Service process. | |
| 32 */ | |
| 33 @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.
 | |
| 34 public class AwVariationsSeedFetchService extends JobService { | |
| 35 private static final String TAG = "AwVartnsSeedFetchSvc"; | |
| 36 | |
| 37 public static final String WEBVIEW_VARIATIONS_DIR = "WebView_Variations/"; | |
| 38 public static final String SEED_DATA_FILENAME = "variations_seed_data"; | |
| 39 public static final String SEED_PREF_FILENAME = "variations_seed_pref"; | |
| 40 | |
| 41 // Synchronization lock to prevent simultaneous local seed file writing. | |
| 42 private static final Lock sLock = new ReentrantLock(); | |
| 43 | |
| 44 @Override | |
| 45 public boolean onStartJob(JobParameters params) { | |
| 46 new FetchFinchSeedDataTask(params).execute(); | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 @Override | |
| 51 public boolean onStopJob(JobParameters params) { | |
| 52 // When job parameter is not satisfied any more, the job should be resch eduled. | |
| 
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.
 | |
| 53 return true; | |
| 54 } | |
| 55 | |
| 56 private class FetchFinchSeedDataTask extends AsyncTask<Void, Void, Void> { | |
| 57 private JobParameters mJobParams; | |
| 58 | |
| 59 FetchFinchSeedDataTask(JobParameters params) { | |
| 60 mJobParams = params; | |
| 61 } | |
| 62 | |
| 63 @Override | |
| 64 protected Void doInBackground(Void... params) { | |
| 65 AwVariationsSeedFetchService.fetchSeed(); | |
| 66 return null; | |
| 67 } | |
| 68 | |
| 69 @Override | |
| 70 protected void onPostExecute(Void success) { | |
| 71 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.
 | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 private static void fetchSeed() { | |
| 76 assert !ThreadUtils.runningOnUiThread(); | |
| 77 // TryLock will drop calls from other threads when one threads are execu ting the function. | |
| 78 // TODO(yiyuny): Add explicitly control to to ensure there's only one th reading 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
 | |
| 79 // a time and that the seed doesn't get fetched too frequently | |
| 80 if (sLock.tryLock()) { | |
| 81 try { | |
| 82 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
 | |
| 83 VariationsSeedFetcher.VariationsPlatform.ANDROID_WEBVIEW , ""); | |
| 84 storeVariationsSeed(seedInfo); | |
| 85 } catch (IOException e) { | |
| 86 // Exceptions are handled in the downloadContent function and re throwing the | |
| 
gsennton
2017/07/13 13:31:59
Maybe note that exceptions are also logged within
 
yiyuny
2017/07/14 01:33:57
Done.
 | |
| 87 // exception is to stop the normal logic flow after it, so no er ror-handling here. | |
| 88 // Not explicitly handing SocketTimeoutException and UnknownHost Exception for they | |
| 89 // are both subclasses of IOException. | |
| 90 } finally { | |
| 91 sLock.unlock(); | |
| 92 } | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 /** | |
| 97 * SeedPreference is used to serialize/deserialize related fields of seed da ta when reading or | |
| 98 * writing them to the internal storage. | |
| 99 */ | |
| 100 public static class SeedPreference implements Serializable { | |
| 101 /** | |
| 102 * Let the program deserialize the data when the fields are changed. | |
| 103 */ | |
| 104 private static final long serialVersionUID = 0L; | |
| 105 | |
| 106 public final String signature; | |
| 107 public final String country; | |
| 108 public final String date; | |
| 109 public final boolean isGzipCompressed; | |
| 110 | |
| 111 public SeedPreference(SeedInfo seedInfo) { | |
| 112 signature = seedInfo.signature; | |
| 113 country = seedInfo.country; | |
| 114 date = seedInfo.date; | |
| 115 isGzipCompressed = seedInfo.isGzipCompressed; | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 private static File getOrCreateWebViewVariationsDir() { | |
| 120 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
 | |
| 121 File dir = new File(webViewFileDir, WEBVIEW_VARIATIONS_DIR); | |
| 122 if (dir.mkdir() || dir.isDirectory()) { | |
| 123 return dir; | |
| 124 } | |
| 125 return null; | |
| 126 } | |
| 127 | |
| 128 private static void storeVariationsSeed(SeedInfo seedInfo) { | |
| 129 File webViewVariationsDir = getOrCreateWebViewVariationsDir(); | |
| 130 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.
 | |
| 131 return; | |
| 132 } | |
| 133 | |
| 134 FileOutputStream fosSeed = null; | |
| 135 ObjectOutputStream oosSeedPref = null; | |
| 136 try { | |
| 137 fosSeed = new FileOutputStream(new File(webViewVariationsDir, SEED_D ATA_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
 | |
| 138 fosSeed.write(seedInfo.seedData, 0, seedInfo.seedData.length); | |
| 139 // Store separately so that reading large seed data (which is expens ive) 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
 | |
| 140 // prevented when checking the last seed fetch time. | |
| 141 oosSeedPref = new ObjectOutputStream( | |
| 142 new FileOutputStream(new File(webViewVariationsDir, SEED_PRE F_FILENAME))); | |
| 143 oosSeedPref.writeObject(new SeedPreference(seedInfo)); | |
| 144 } catch (FileNotFoundException e) { | |
| 145 Log.e(TAG, | |
| 146 "FileNotFoundException failed to open file to write seed dat a 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
 | |
| 147 } catch (IOException e) { | |
| 148 Log.e(TAG, | |
| 149 "IOException failed to write variations seed data or prefere nce to the file."); | |
| 150 } finally { | |
| 151 closeStream(fosSeed, SEED_DATA_FILENAME); | |
| 152 closeStream(oosSeedPref, SEED_PREF_FILENAME); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 private static void closeStream(Closeable stream, String filename) { | |
| 157 if (stream != null) { | |
| 158 try { | |
| 159 stream.close(); | |
| 160 } catch (IOException e) { | |
| 161 Log.e(TAG, "IOException failed to close " + filename + " file.") ; | |
| 162 } | |
| 163 } | |
| 164 } | |
| 165 } | |
| OLD | NEW |