Chromium Code Reviews| 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; | |
| 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. | |
| 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. | |
| 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); | |
| 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 | |
| 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( | |
| 83 VariationsSeedFetcher.VariationsPlatform.ANDROID_WEBVIEW , ""); | |
| 84 storeVariationsSeed(seedInfo); | |
| 85 } catch (IOException e) { | |
| 86 // Exceptions are handled in the downloadContent function and re throwing the | |
| 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 serialize related fields of seed data and is used when rea ding or writing | |
|
Alexei Svitkine (slow)
2017/07/12 19:01:21
Nit: "serializes" or "is used to serialize"
yiyuny
2017/07/12 22:22:05
Done.
| |
| 98 * the fields to the internal storage. serialVersionUID can let the program deserialize the data | |
| 99 * when the fields are changed. | |
|
Alexei Svitkine (slow)
2017/07/12 19:01:21
Nit: Move the last sentence to be about serialVers
yiyuny
2017/07/12 22:22:06
Done.
| |
| 100 */ | |
| 101 public static class SeedPreference implements Serializable { | |
| 102 private static final long serialVersionUID = 0L; | |
|
Alexei Svitkine (slow)
2017/07/12 19:01:21
Nit: Add an empty line after this.
yiyuny
2017/07/12 22:22:05
Done.
| |
| 103 public final String signature; | |
| 104 public final String country; | |
| 105 public final String date; | |
| 106 public final boolean isGzipCompressed; | |
| 107 | |
| 108 public SeedPreference(SeedInfo seedInfo) { | |
| 109 signature = seedInfo.signature; | |
| 110 country = seedInfo.country; | |
| 111 date = seedInfo.date; | |
| 112 isGzipCompressed = seedInfo.isGzipCompressed; | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 private static File getOrCreateWebViewVariationsDir() { | |
| 117 File webViewFileDir = ContextUtils.getApplicationContext().getFilesDir() ; | |
| 118 File dir = new File(webViewFileDir, WEBVIEW_VARIATIONS_DIR); | |
| 119 if (dir.mkdir() || dir.isDirectory()) { | |
| 120 return dir; | |
| 121 } | |
| 122 return null; | |
| 123 } | |
| 124 | |
| 125 private static void storeVariationsSeed(SeedInfo seedInfo) { | |
| 126 File webViewVariationsDir = getOrCreateWebViewVariationsDir(); | |
| 127 if (webViewVariationsDir == null) { | |
| 128 return; | |
| 129 } | |
| 130 | |
| 131 FileOutputStream fosSeed = null; | |
| 132 ObjectOutputStream oosSeedPref = null; | |
| 133 try { | |
| 134 fosSeed = new FileOutputStream(new File(webViewVariationsDir, SEED_D ATA_FILENAME)); | |
| 135 fosSeed.write(seedInfo.seedData, 0, seedInfo.seedData.length); | |
|
Alexei Svitkine (slow)
2017/07/12 19:01:21
How will you read this?
I think you need to also
yiyuny
2017/07/12 22:22:06
I choose to store the seed data and preference sep
Alexei Svitkine (slow)
2017/07/13 20:36:25
Ah, sorry I had missed that you're writing to diff
| |
| 136 // Store separately so that reading large seed data (which is expens ive) can be | |
| 137 // prevented when checking the last seed fetch time. | |
| 138 oosSeedPref = new ObjectOutputStream( | |
| 139 new FileOutputStream(new File(webViewVariationsDir, SEED_PRE F_FILENAME))); | |
| 140 oosSeedPref.writeObject(new SeedPreference(seedInfo)); | |
| 141 } catch (FileNotFoundException e) { | |
| 142 Log.e(TAG, | |
| 143 "FileNotFoundException failed to open file to write seed dat a or preference."); | |
|
Alexei Svitkine (slow)
2017/07/12 19:01:21
Nit: If you pass e as a last param, you don't need
paulmiller
2017/07/12 20:21:59
I recommended not passing e, because I thought a s
Alexei Svitkine (slow)
2017/07/12 20:32:31
Ah, fair enough. Maybe log e.getMessage() then, at
yiyuny
2017/07/12 22:22:06
I don't know the default value of the Exception's
| |
| 144 } catch (IOException e) { | |
| 145 Log.e(TAG, | |
| 146 "IOException failed to write variations seed data or prefere nce to the file."); | |
| 147 } finally { | |
| 148 closeStream(fosSeed, SEED_DATA_FILENAME); | |
| 149 closeStream(oosSeedPref, SEED_PREF_FILENAME); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 private static void closeStream(Closeable stream, String filename) { | |
| 154 if (stream != null) { | |
| 155 try { | |
| 156 stream.close(); | |
| 157 } catch (IOException e) { | |
| 158 Log.e(TAG, "IOException failed to close " + filename + " file.") ; | |
| 159 } | |
| 160 } | |
| 161 } | |
| 162 } | |
| OLD | NEW |