OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser; | |
6 | |
7 import android.content.Context; | |
8 | |
9 import com.google.android.gms.gcm.GcmNetworkManager; | |
10 import com.google.android.gms.gcm.GcmTaskService; | |
11 import com.google.android.gms.gcm.TaskParams; | |
12 | |
13 import org.chromium.base.Log; | |
14 import org.chromium.base.ThreadUtils; | |
15 import org.chromium.base.VisibleForTesting; | |
16 import org.chromium.base.annotations.SuppressFBWarnings; | |
17 import org.chromium.base.library_loader.LibraryProcessType; | |
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; | |
21 import org.chromium.content.app.ContentApplication; | |
22 import org.chromium.content.browser.BrowserStartupController; | |
23 | |
24 /** | |
25 * {@link GcmTaskService} is scheduled through the {@link GcmNetworkManager} whe n the browser needs | |
26 * to be launched for scheduled tasks, or in response to changing network or pow er conditions. | |
27 */ | |
28 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.
| |
29 private static final String TAG = "ChromeGcmTaskService"; | |
30 | |
31 @Override | |
32 @VisibleForTesting | |
33 public int onRunTask(final TaskParams params) { | |
34 Log.i(TAG, "Woken up at " + new java.util.Date().toString()); | |
35 final Context context = this; | |
36 ThreadUtils.runOnUiThread(new Runnable() { | |
37 @Override | |
38 public void run() { | |
39 switch(params.getTag()) { | |
40 case BackgroundSyncLauncher.TASK_TAG: | |
41 handleBackgroundSyncEvent(context); | |
42 break; | |
43 | |
44 case SnippetsLauncher.TASK_TAG: | |
45 handleFetchSnippets(context); | |
46 break; | |
47 } | |
48 } | |
49 }); | |
50 return GcmNetworkManager.RESULT_SUCCESS; | |
51 } | |
52 | |
53 private void handleBackgroundSyncEvent(Context context) { | |
54 if (!BackgroundSyncLauncher.hasInstance()) { | |
55 // Start the browser. The browser's BackgroundSyncManager (for the a ctive profile) will | |
56 // start, check the network, and run any necessary sync events. This task runs with a | |
57 // wake lock, but has a three minute timeout, so we need to start th e browser in its | |
58 // own task. | |
59 // TODO(jkarlin): Protect the browser sync event with a wake lock. | |
60 // See crbug.com/486020. | |
61 launchBrowser(context); | |
62 } | |
63 } | |
64 | |
65 private void handleFetchSnippets(Context context) { | |
66 if (!SnippetsLauncher.hasInstance()) { | |
67 launchBrowser(context); | |
68 } | |
69 SnippetsController.get(context).fetchSnippets(true); | |
70 } | |
71 | |
72 @VisibleForTesting | |
73 @SuppressFBWarnings("DM_EXIT") | |
74 protected void launchBrowser(Context context) { | |
75 Log.i(TAG, "Launching browser"); | |
76 ContentApplication.initCommandLine(context); | |
77 try { | |
78 BrowserStartupController.get(context, LibraryProcessType.PROCESS_BRO WSER) | |
79 .startBrowserProcessesSync(false); | |
80 } catch (ProcessInitException e) { | |
81 Log.e(TAG, "ProcessInitException while starting the browser process" ); | |
82 // Since the library failed to initialize nothing in the application | |
83 // can work, so kill the whole application not just the activity. | |
84 System.exit(-1); | |
85 } | |
86 } | |
87 | |
88 @Override | |
89 public void onInitializeTasks() { | |
90 BackgroundSyncLauncher.rescheduleTasksOnUpgrade(this); | |
91 } | |
92 } | |
OLD | NEW |