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

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

Issue 1699143002: [NTP Snippets] Schedule periodic fetching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_feature
Patch Set: fix bots Created 4 years, 9 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser; 5 package org.chromium.chrome.browser;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 8
9 import com.google.android.gms.gcm.GcmNetworkManager; 9 import com.google.android.gms.gcm.GcmNetworkManager;
10 import com.google.android.gms.gcm.GcmTaskService; 10 import com.google.android.gms.gcm.GcmTaskService;
11 import com.google.android.gms.gcm.TaskParams; 11 import com.google.android.gms.gcm.TaskParams;
12 12
13 import org.chromium.base.Log; 13 import org.chromium.base.Log;
14 import org.chromium.base.ThreadUtils; 14 import org.chromium.base.ThreadUtils;
15 import org.chromium.base.VisibleForTesting; 15 import org.chromium.base.VisibleForTesting;
16 import org.chromium.base.annotations.SuppressFBWarnings; 16 import org.chromium.base.annotations.SuppressFBWarnings;
17 import org.chromium.base.library_loader.LibraryProcessType; 17 import org.chromium.base.library_loader.LibraryProcessType;
18 import org.chromium.base.library_loader.ProcessInitException; 18 import org.chromium.base.library_loader.ProcessInitException;
19 import org.chromium.chrome.browser.ntp.snippets.SnippetsController;
20 import org.chromium.chrome.browser.ntp.snippets.SnippetsLauncher;
19 import org.chromium.content.app.ContentApplication; 21 import org.chromium.content.app.ContentApplication;
20 import org.chromium.content.browser.BrowserStartupController; 22 import org.chromium.content.browser.BrowserStartupController;
21 23
22 /** 24 /**
23 * {@link BackgroundSyncLauncherService} is scheduled through the {@link GcmNetw orkManager} 25 * {@link ChromeBackgroundService} is scheduled through the {@link GcmNetworkMan ager} when the
24 * when the browser needs to be launched in response to changing network or powe r conditions. 26 * browser needs to be launched for scheduled tasks, or in response to changing network or power
27 * conditions.
25 */ 28 */
26 public class BackgroundSyncLauncherService extends GcmTaskService { 29 public class ChromeBackgroundService extends GcmTaskService {
27 private static final String TAG = "BgSyncLauncher"; 30 private static final String TAG = "BackgroundService";
28 31
29 @Override 32 @Override
33 public int onRunTask(TaskParams params) {
34 Log.i(TAG, "Woken up at " + new java.util.Date().toString());
35 handleRunTask(params.getTag());
36 return GcmNetworkManager.RESULT_SUCCESS;
37 }
38
30 @VisibleForTesting 39 @VisibleForTesting
31 public int onRunTask(TaskParams params) { 40 public void handleRunTask(final String tag) {
32 // Start the browser. The browser's BackgroundSyncManager (for the activ e profile) will
33 // start, check the network, and run any necessary sync events. This tas k runs with a wake
34 // lock, but has a three minute timeout, so we need to start the browser in its own task.
35 // TODO(jkarlin): Protect the browser sync event with a wake lock. See c rbug.com/486020.
36 Log.v(TAG, "Starting Browser after coming online");
37 final Context context = this; 41 final Context context = this;
38 ThreadUtils.runOnUiThread(new Runnable() { 42 ThreadUtils.runOnUiThread(new Runnable() {
39 @Override 43 @Override
40 public void run() { 44 public void run() {
41 if (!BackgroundSyncLauncher.hasInstance()) { 45 switch(tag) {
42 launchBrowser(context); 46 case BackgroundSyncLauncher.TASK_TAG:
47 handleBackgroundSyncEvent(context);
48 break;
49
50 case SnippetsLauncher.TASK_TAG:
51 handleFetchSnippets(context);
52 break;
53
54 default:
55 Log.i(TAG, "Unknown task tag " + tag);
56 break;
43 } 57 }
44 } 58 }
45 }); 59 });
46 return GcmNetworkManager.RESULT_SUCCESS; 60 }
61
62 private void handleBackgroundSyncEvent(Context context) {
63 if (!BackgroundSyncLauncher.hasInstance()) {
64 // Start the browser. The browser's BackgroundSyncManager (for the a ctive profile) will
65 // start, check the network, and run any necessary sync events. This task runs with a
66 // wake lock, but has a three minute timeout, so we need to start th e browser in its
67 // own task.
68 // TODO(jkarlin): Protect the browser sync event with a wake lock.
69 // See crbug.com/486020.
70 launchBrowser(context);
71 }
72 }
73
74 private void handleFetchSnippets(Context context) {
75 if (!SnippetsLauncher.hasInstance()) {
76 launchBrowser(context);
77 }
78 SnippetsController.get(context).fetchSnippets(true);
47 } 79 }
48 80
49 @VisibleForTesting 81 @VisibleForTesting
50 @SuppressFBWarnings("DM_EXIT") 82 @SuppressFBWarnings("DM_EXIT")
51 protected void launchBrowser(Context context) { 83 protected void launchBrowser(Context context) {
84 Log.i(TAG, "Launching browser");
52 ContentApplication.initCommandLine(context); 85 ContentApplication.initCommandLine(context);
53 try { 86 try {
54 BrowserStartupController.get(context, LibraryProcessType.PROCESS_BRO WSER) 87 BrowserStartupController.get(context, LibraryProcessType.PROCESS_BRO WSER)
55 .startBrowserProcessesSync(false); 88 .startBrowserProcessesSync(false);
56 } catch (ProcessInitException e) { 89 } catch (ProcessInitException e) {
57 Log.e(TAG, "ProcessInitException while starting the browser process" ); 90 Log.e(TAG, "ProcessInitException while starting the browser process" );
58 // Since the library failed to initialize nothing in the application 91 // Since the library failed to initialize nothing in the application
59 // can work, so kill the whole application not just the activity. 92 // can work, so kill the whole application not just the activity.
60 System.exit(-1); 93 System.exit(-1);
61 } 94 }
62 } 95 }
63 96
64 @Override 97 @Override
65 @VisibleForTesting
66 public void onInitializeTasks() { 98 public void onInitializeTasks() {
67 BackgroundSyncLauncher.rescheduleTasksOnUpgrade(this); 99 BackgroundSyncLauncher.rescheduleTasksOnUpgrade(this);
68 } 100 }
69 } 101 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698