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.FileNotFoundException; | |
19 import java.io.FileOutputStream; | |
20 import java.io.IOException; | |
21 import java.io.ObjectOutputStream; | |
22 import java.io.Serializable; | |
23 import java.net.SocketTimeoutException; | |
24 import java.net.UnknownHostException; | |
25 | |
26 /** | |
27 * Job Service to fetch seed in the background which guarantees to get the job d one. | |
paulmiller
2017/07/10 18:23:12
This comment is confusing. Explain what "seed" mea
yiyuny
2017/07/11 19:15:19
Done.
| |
28 */ | |
29 @SuppressLint("NewApi") // JobService requires API level 21 | |
30 public class AwVariationsSeedFetchService extends JobService { | |
31 private static final String TAG = "AwVartnsSeedFetchSvc"; | |
32 | |
33 public static final String SEED_DATA_FILENAME = "variations_seed"; | |
34 public static final String SEED_DATA_PREF_FILENAME = "variations_seed_pref"; | |
paulmiller
2017/07/10 18:23:12
These files should go in the "app_webview" subdire
paulmiller
2017/07/10 23:37:40
Tony pointed out this is for the WebView/Monochrom
yiyuny
2017/07/11 19:15:19
Done.
| |
35 | |
36 // Synchronization lock to prevent simultaneous local seed file writing | |
37 private static final Object sLock = new Object(); | |
38 | |
39 @Override | |
40 public boolean onStartJob(final JobParameters params) { | |
41 new FetchFinchSeedDataTask(params, this).execute(); | |
42 return true; | |
43 } | |
44 | |
45 @Override | |
46 public boolean onStopJob(JobParameters params) { | |
47 return false; | |
paulmiller
2017/07/10 18:23:12
Shouldn't this stop the FetchFinchSeedDataTask?
paulmiller
2017/07/10 23:37:40
Tony pointed out that this is only called when the
yiyuny
2017/07/11 19:15:18
Done.
| |
48 } | |
49 | |
50 private static class FetchFinchSeedDataTask extends AsyncTask<Void, Void, Vo id> { | |
51 private JobParameters mJobParams; | |
52 private JobService mJobSvc; | |
53 | |
54 FetchFinchSeedDataTask(JobParameters params, JobService svc) { | |
paulmiller
2017/07/10 18:23:12
It seems strange to declare the subclass static, b
yiyuny
2017/07/11 19:15:18
Done.
| |
55 mJobParams = params; | |
56 mJobSvc = svc; | |
57 } | |
58 | |
59 @Override | |
60 protected Void doInBackground(Void... params) { | |
61 AwVariationsSeedFetchService.fetchSeed(""); | |
62 return null; | |
63 } | |
64 | |
65 @Override | |
66 protected void onPostExecute(Void success) { | |
67 mJobSvc.jobFinished(mJobParams, false); | |
68 } | |
69 } | |
70 | |
71 private static void fetchSeed(String restrictMode) { | |
paulmiller
2017/07/10 18:23:12
Remove the argument, since it's only ever called w
yiyuny
2017/07/11 19:15:19
Done.
| |
72 assert !ThreadUtils.runningOnUiThread(); | |
73 // Prevent multiple simultaneous file writing | |
74 synchronized (sLock) { | |
paulmiller
2017/07/10 18:23:12
If multiple jobs are scheduled at the same time, t
yiyuny
2017/07/11 19:15:19
Done.
| |
75 try { | |
76 VariationsSeedFetcher.SeedInfo si = VariationsSeedFetcher.get(). downloadContent( | |
77 VariationsSeedFetcher.VariationsPlatform.ANDROID_WEBVIEW , restrictMode); | |
78 if (si != null) { | |
79 storeSeed(si); | |
80 } | |
81 } catch (SocketTimeoutException e) { | |
82 } catch (UnknownHostException e) { | |
83 } catch (IOException e) { | |
paulmiller
2017/07/10 18:23:12
Group these exceptions together using "|" syntax:
yiyuny
2017/07/11 19:15:19
Done.
| |
84 } | |
85 } | |
86 } | |
87 | |
88 /** | |
89 * Store seed preference independently from Seed Info | |
90 */ | |
91 public static class SeedPreference implements Serializable { | |
92 public String signature; | |
93 public String country; | |
94 public String date; | |
95 public boolean isGzipCompressed; | |
96 | |
97 public SeedPreference(VariationsSeedFetcher.SeedInfo si) { | |
98 signature = si.signature; | |
99 country = si.country; | |
100 date = si.date; | |
101 isGzipCompressed = si.isGzipCompressed; | |
102 } | |
103 } | |
104 | |
105 private static void storeSeed(final VariationsSeedFetcher.SeedInfo si) { | |
106 FileOutputStream fosSeed = null; | |
107 ObjectOutputStream oosSeedPref = null; | |
108 final Context appContext = ContextUtils.getApplicationContext(); | |
109 try { | |
110 fosSeed = appContext.openFileOutput(SEED_DATA_FILENAME, Context.MODE _PRIVATE); | |
111 fosSeed.write(si.rawSeed, 0, si.rawSeed.length); | |
112 // store separately to prevent an expensive checking expiration | |
paulmiller
2017/07/10 18:23:12
This comment is confusing. What is "checking expir
yiyuny
2017/07/11 19:15:19
Done.
| |
113 SeedPreference sp = new SeedPreference(si); | |
114 oosSeedPref = new ObjectOutputStream( | |
115 appContext.openFileOutput(SEED_DATA_PREF_FILENAME, Context.M ODE_PRIVATE)); | |
116 oosSeedPref.writeObject(sp); | |
117 } catch (FileNotFoundException e) { | |
118 Log.e(TAG, "FileNotFoundException store seed."); | |
119 } catch (IOException e) { | |
120 Log.e(TAG, "IOException store seed"); | |
121 } finally { | |
122 if (fosSeed != null) { | |
123 try { | |
124 fosSeed.close(); | |
125 } catch (IOException e) { | |
126 Log.e(TAG, "IOException file close"); | |
paulmiller
2017/07/10 18:23:12
Keep in mind that when people read your error mess
yiyuny
2017/07/11 19:15:19
Done.
| |
127 } | |
128 } | |
129 if (oosSeedPref != null) { | |
130 try { | |
131 oosSeedPref.close(); | |
132 } catch (IOException e) { | |
133 Log.e(TAG, "IOException file close"); | |
134 } | |
135 } | |
136 } | |
137 } | |
138 } | |
OLD | NEW |