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.chrome.browser.background_task_scheduler; | |
6 | |
7 import android.content.Context; | |
8 | |
9 import org.chromium.base.Log; | |
10 import org.chromium.base.library_loader.LibraryProcessType; | |
11 import org.chromium.base.library_loader.ProcessInitException; | |
12 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; | |
13 import org.chromium.components.background_task_scheduler.BackgroundTask; | |
14 import org.chromium.components.background_task_scheduler.TaskParameters; | |
15 import org.chromium.content.browser.BrowserStartupController; | |
16 | |
17 /** | |
18 * Base class implementing {@link BackgroundTask} that adds native initializatio n, ensuring that | |
19 * tasks are run after Chrome is successfully started. | |
20 * Background tasks that require native parts of browser to be started should ex tend this class | |
21 * instead of implementing the interface directly. {@link #onStartTaskWithNative (Context, | |
22 * TaskParameters, TaskFinishedCallback)} method should be implemented instead o f {@link | |
23 * #onStartTask(Context, TaskParameters, TaskFinishedCallback)}. | |
24 */ | |
25 public abstract class NativeBackgroundTask implements BackgroundTask { | |
nyquist
2017/04/18 22:48:43
How would you feel about writing a test to ensure
fgorski
2017/04/21 18:45:43
Here is what I thought 3 hours ago: hold my beer..
| |
26 private static final String TAG = "BTS_NativeBkgrdTask"; | |
27 | |
28 protected NativeBackgroundTask() {} | |
29 | |
30 @Override | |
31 public final boolean onStartTask( | |
32 Context context, TaskParameters taskParameters, TaskFinishedCallback callback) { | |
33 ensureNativeInitialized(context); | |
34 return onStartTaskWithNative(context, taskParameters, callback); | |
35 } | |
36 | |
37 /** | |
38 * Method that should be implemented in derived classes to provide implement ation of {@link | |
39 * BackgroundTask#onStartTask(Context, TaskParameters, TaskFinishedCallback) }. | |
40 */ | |
41 protected abstract boolean onStartTaskWithNative( | |
42 Context context, TaskParameters taskParameters, TaskFinishedCallback callback); | |
43 | |
44 /** | |
45 * Method that ensure that native parts of Chrome are initialized, before fu rther work can be | |
46 * done. If native part is initialized this method does nothing. | |
47 */ | |
48 protected void ensureNativeInitialized(Context context) { | |
nyquist
2017/04/18 22:48:44
Nit: I think you want @SuppressFBWarnings("DM_EXI
fgorski
2017/04/21 18:45:43
Actually I thought of a different approach, in whi
| |
49 if (BrowserStartupController.get(LibraryProcessType.PROCESS_BROWSER) | |
50 .isStartupSuccessfullyCompleted()) { | |
51 return; | |
52 } | |
53 | |
54 try { | |
55 ChromeBrowserInitializer.getInstance(context).handleSynchronousStart up(); | |
56 } catch (ProcessInitException e) { | |
57 Log.e(TAG, "ProcessInitException while starting the browser process. "); | |
58 // Since the library failed to initialize nothing in the application can work, so kill | |
59 // the whole application not just the activity. | |
60 System.exit(-1); | |
61 } | |
62 } | |
63 } | |
OLD | NEW |