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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/background_task_scheduler/NativeBackgroundTask.java

Issue 2825783003: [Android] Native initialization task for BackgroundTaskScheduler (Closed)
Patch Set: Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/android/java_sources.gni » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/android/java/src/org/chromium/chrome/browser/background_task_scheduler/NativeBackgroundTask.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/background_task_scheduler/NativeBackgroundTask.java b/chrome/android/java/src/org/chromium/chrome/browser/background_task_scheduler/NativeBackgroundTask.java
new file mode 100644
index 0000000000000000000000000000000000000000..d16ce638e018448fe7dcfd9ca9169a181962fc2e
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/background_task_scheduler/NativeBackgroundTask.java
@@ -0,0 +1,63 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.background_task_scheduler;
+
+import android.content.Context;
+
+import org.chromium.base.Log;
+import org.chromium.base.library_loader.LibraryProcessType;
+import org.chromium.base.library_loader.ProcessInitException;
+import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
+import org.chromium.components.background_task_scheduler.BackgroundTask;
+import org.chromium.components.background_task_scheduler.TaskParameters;
+import org.chromium.content.browser.BrowserStartupController;
+
+/**
+ * Base class implementing {@link BackgroundTask} that adds native initialization, ensuring that
+ * tasks are run after Chrome is successfully started.
+ * Background tasks that require native parts of browser to be started should extend this class
+ * instead of implementing the interface directly. {@link #onStartTaskWithNative(Context,
+ * TaskParameters, TaskFinishedCallback)} method should be implemented instead of {@link
+ * #onStartTask(Context, TaskParameters, TaskFinishedCallback)}.
+ */
+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..
+ private static final String TAG = "BTS_NativeBkgrdTask";
+
+ protected NativeBackgroundTask() {}
+
+ @Override
+ public final boolean onStartTask(
+ Context context, TaskParameters taskParameters, TaskFinishedCallback callback) {
+ ensureNativeInitialized(context);
+ return onStartTaskWithNative(context, taskParameters, callback);
+ }
+
+ /**
+ * Method that should be implemented in derived classes to provide implementation of {@link
+ * BackgroundTask#onStartTask(Context, TaskParameters, TaskFinishedCallback)}.
+ */
+ protected abstract boolean onStartTaskWithNative(
+ Context context, TaskParameters taskParameters, TaskFinishedCallback callback);
+
+ /**
+ * Method that ensure that native parts of Chrome are initialized, before further work can be
+ * done. If native part is initialized this method does nothing.
+ */
+ 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
+ if (BrowserStartupController.get(LibraryProcessType.PROCESS_BROWSER)
+ .isStartupSuccessfullyCompleted()) {
+ return;
+ }
+
+ try {
+ ChromeBrowserInitializer.getInstance(context).handleSynchronousStartup();
+ } catch (ProcessInitException e) {
+ Log.e(TAG, "ProcessInitException while starting the browser process.");
+ // Since the library failed to initialize nothing in the application can work, so kill
+ // the whole application not just the activity.
+ System.exit(-1);
+ }
+ }
+}
« no previous file with comments | « no previous file | chrome/android/java_sources.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698