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

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

Issue 1699143002: [NTP Snippets] Schedule periodic fetching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_feature
Patch Set: merge GcmTaskService implementations Created 4 years, 10 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
Index: chrome/android/java/src/org/chromium/chrome/browser/ChromeGcmTaskService.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeGcmTaskService.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeGcmTaskService.java
new file mode 100644
index 0000000000000000000000000000000000000000..2e31b0c1a9f2832479f0b9803233a91531b97c62
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeGcmTaskService.java
@@ -0,0 +1,92 @@
+// Copyright 2016 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;
+
+import android.content.Context;
+
+import com.google.android.gms.gcm.GcmNetworkManager;
+import com.google.android.gms.gcm.GcmTaskService;
+import com.google.android.gms.gcm.TaskParams;
+
+import org.chromium.base.Log;
+import org.chromium.base.ThreadUtils;
+import org.chromium.base.VisibleForTesting;
+import org.chromium.base.annotations.SuppressFBWarnings;
+import org.chromium.base.library_loader.LibraryProcessType;
+import org.chromium.base.library_loader.ProcessInitException;
+import org.chromium.chrome.browser.ntp.snippets.SnippetsController;
+import org.chromium.chrome.browser.ntp.snippets.SnippetsLauncher;
+import org.chromium.content.app.ContentApplication;
+import org.chromium.content.browser.BrowserStartupController;
+
+/**
+ * {@link GcmTaskService} is scheduled through the {@link GcmNetworkManager} when the browser needs
+ * to be launched for scheduled tasks, or in response to changing network or power conditions.
+ */
+public class ChromeGcmTaskService extends GcmTaskService {
Bernhard Bauer 2016/02/18 10:32:40 Should we just name this ChromeBackgroundService?
Marc Treib 2016/02/18 10:52:40 Done.
+ private static final String TAG = "ChromeGcmTaskService";
+
+ @Override
+ @VisibleForTesting
+ public int onRunTask(final TaskParams params) {
+ Log.i(TAG, "Woken up at " + new java.util.Date().toString());
+ final Context context = this;
+ ThreadUtils.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ switch(params.getTag()) {
+ case BackgroundSyncLauncher.TASK_TAG:
+ handleBackgroundSyncEvent(context);
+ break;
+
+ case SnippetsLauncher.TASK_TAG:
+ handleFetchSnippets(context);
+ break;
+ }
+ }
+ });
+ return GcmNetworkManager.RESULT_SUCCESS;
+ }
+
+ private void handleBackgroundSyncEvent(Context context) {
+ if (!BackgroundSyncLauncher.hasInstance()) {
+ // Start the browser. The browser's BackgroundSyncManager (for the active profile) will
+ // start, check the network, and run any necessary sync events. This task runs with a
+ // wake lock, but has a three minute timeout, so we need to start the browser in its
+ // own task.
+ // TODO(jkarlin): Protect the browser sync event with a wake lock.
+ // See crbug.com/486020.
+ launchBrowser(context);
+ }
+ }
+
+ private void handleFetchSnippets(Context context) {
+ if (!SnippetsLauncher.hasInstance()) {
+ launchBrowser(context);
+ }
+ SnippetsController.get(context).fetchSnippets(true);
+ }
+
+ @VisibleForTesting
+ @SuppressFBWarnings("DM_EXIT")
+ protected void launchBrowser(Context context) {
+ Log.i(TAG, "Launching browser");
+ ContentApplication.initCommandLine(context);
+ try {
+ BrowserStartupController.get(context, LibraryProcessType.PROCESS_BROWSER)
+ .startBrowserProcessesSync(false);
+ } 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);
+ }
+ }
+
+ @Override
+ public void onInitializeTasks() {
+ BackgroundSyncLauncher.rescheduleTasksOnUpgrade(this);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698