Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/init/AsyncInitTaskRunner.java

Issue 2627093009: Fetch Finch seed during restore (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.chrome.browser.init;
6
7 import android.content.BroadcastReceiver;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.IntentFilter;
11 import android.content.SharedPreferences;
12 import android.content.SharedPreferences.Editor;
13 import android.os.StrictMode;
14 import android.support.v4.content.LocalBroadcastManager;
15
16 import org.chromium.base.ContextUtils;
17 import org.chromium.base.Log;
18 import org.chromium.base.ThreadUtils;
19 import org.chromium.base.library_loader.LibraryLoader;
20 import org.chromium.base.library_loader.LibraryProcessType;
21 import org.chromium.base.library_loader.ProcessInitException;
22 import org.chromium.chrome.browser.ChromeVersionInfo;
23 import org.chromium.components.variations.firstrun.VariationsSeedService;
24 import org.chromium.content.browser.ChildProcessLauncher;
25
26 /**
27 * Class for running the Asynchronous startup tasks that need to be run before t he native side is
28 * started. Currently it runs two tasks:
29 * - Native library loading
30 * - Fetching the variations seed on first run.
31 */
32 public abstract class AsyncInitTaskRunner {
33
Alexei Svitkine (slow) 2017/01/12 16:06:28 Nit: Remove empty line. You can add it after TAG i
aberent 2017/01/12 17:31:50 Done.
34 private static final String TAG = "AsyncTaskRunner";
35 private static final String VARIATIONS_INITIALIZED = "variations_initialized ";
36 private boolean mWaitingForVariationsFetch;
37 private boolean mLibraryLoaded;
38
39 private static boolean shouldFetchVariationsSeedDuringFirstRun() {
Alexei Svitkine (slow) 2017/01/12 16:06:28 This logic changed on TOT in the meantime - this f
aberent 2017/01/12 17:31:50 Done.
40 // For now, only do the fetching on official canary and dev builds, as t here is a concern
41 // about the extra latency this adds.
42 // TODO(asvitkine): Revise this logic based on histogram data.
43 return ChromeVersionInfo.isOfficialBuild()
44 && (ChromeVersionInfo.isCanaryBuild() || ChromeVersionInfo.isDev Build());
45 }
46
47 /**
48 * Start the background tasks.
Alexei Svitkine (slow) 2017/01/12 16:06:28 Nit: Javadoc for params.
aberent 2017/01/12 17:31:50 Done.
49 */
50 public void startBackgroundTasks(final boolean allocateChildConnection,
51 final boolean initVariationSeed) {
Alexei Svitkine (slow) 2017/01/12 16:06:28 Nit: No need for final on the second var.
aberent 2017/01/12 17:31:50 Done.
52
53 if (initVariationSeed && shouldFetchVariationsSeedDuringFirstRun()) {
54 StrictMode.ThreadPolicy policy = StrictMode.allowThreadDiskReads();
55 SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
56 boolean variationsInitialized = prefs.getBoolean(VARIATIONS_INITIALI ZED, false);
Alexei Svitkine (slow) 2017/01/12 16:06:28 I don't understand how this will work. If this Ja
aberent 2017/01/12 17:31:50 Backup only saves and restores a very limited subs
Alexei Svitkine (slow) 2017/01/12 18:55:43 Ah, I did not realise this! It means that my fix f
57 // Only attempt to get the initial seed once, even if the attempt fa ils.
58 Editor prefsEditor = prefs.edit();
59 prefsEditor.putBoolean(VARIATIONS_INITIALIZED, true);
60 prefsEditor.apply();
61 StrictMode.setThreadPolicy(policy);
62
63 if (!variationsInitialized) {
64
Alexei Svitkine (slow) 2017/01/12 16:06:28 Nit: Remove empty line.
aberent 2017/01/12 17:31:50 Done.
65 Context context = ContextUtils.getApplicationContext();
66 mWaitingForVariationsFetch = true;
67 IntentFilter filter = new IntentFilter(VariationsSeedService.COM PLETE_BROADCAST);
68 final LocalBroadcastManager manager = LocalBroadcastManager.getI nstance(context);
69 manager.registerReceiver(
70 new BroadcastReceiver() {
71 @Override
72 public void onReceive(Context context, Intent intent ) {
73 // This check is needed because onReceive() can be called multiple
74 // times even after having unregistered below if two broadcasts
75 // arrive in rapid succession.
76 if (!mWaitingForVariationsFetch) return;
77 mWaitingForVariationsFetch = false;
78 manager.unregisterReceiver(this);
79 Log.i(TAG, "Variations fetched");
80 tasksPossiblyComplete();
81 }
82 },
83 filter);
84 Log.i(TAG, "Fetching variations");
85 context.startService(new Intent(context, VariationsSeedService.c lass));
86 }
87 }
88
89 // TODO(yusufo) : Investigate using an AsyncTask for this.
90 new Thread() {
91 @Override
92 public void run() {
93 try {
94 LibraryLoader libraryLoader =
95 LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER );
96 libraryLoader.ensureInitialized();
97 // The prefetch is done after the library load for two reaso ns:
98 // - It is easier to know the library location after it has
99 // been loaded.
100 // - Testing has shown that this gives the best compromise,
101 // by avoiding performance regression on any tested
102 // device, and providing performance improvement on
103 // some. Doing it earlier delays UI inflation and more
104 // generally startup on some devices, most likely by
105 // competing for IO.
106 // For experimental results, see http://crbug.com/460438.
107 libraryLoader.asyncPrefetchLibrariesToMemory();
108 } catch (ProcessInitException e) {
109 Log.e(TAG, "Unable to load native library.", e);
110 onFailure();
111 return;
112 }
113 if (allocateChildConnection) {
114 ChildProcessLauncher.warmUp(ContextUtils.getApplicationConte xt());
115 }
116 ThreadUtils.runOnUiThread(new Runnable() {
117 @Override
118 public void run() {
119 mLibraryLoaded = true;
120 tasksPossiblyComplete();
121 }
122 });
123 }
124 }.start();
125
126 }
127
128 private void tasksPossiblyComplete() {
129 ThreadUtils.assertOnUiThread();
130
131 if (mLibraryLoaded && !mWaitingForVariationsFetch) {
132 onSuccess();
133 }
134 }
135
136 public abstract void onSuccess();
137 public abstract void onFailure();
138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698