OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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.offlinepages; | 5 package org.chromium.chrome.browser.offlinepages; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 | 8 |
9 import org.chromium.base.Callback; | 9 import org.chromium.base.Callback; |
| 10 import org.chromium.base.Log; |
| 11 import org.chromium.base.library_loader.LibraryLoader; |
| 12 import org.chromium.base.library_loader.ProcessInitException; |
| 13 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; |
10 import org.chromium.chrome.browser.offlinepages.interfaces.BackgroundSchedulerPr
ocessor; | 14 import org.chromium.chrome.browser.offlinepages.interfaces.BackgroundSchedulerPr
ocessor; |
11 import org.chromium.components.background_task_scheduler.BackgroundTask; | 15 import org.chromium.components.background_task_scheduler.BackgroundTask; |
12 import org.chromium.components.background_task_scheduler.BackgroundTask.TaskFini
shedCallback; | 16 import org.chromium.components.background_task_scheduler.BackgroundTask.TaskFini
shedCallback; |
13 import org.chromium.components.background_task_scheduler.TaskIds; | 17 import org.chromium.components.background_task_scheduler.TaskIds; |
14 import org.chromium.components.background_task_scheduler.TaskParameters; | 18 import org.chromium.components.background_task_scheduler.TaskParameters; |
15 | 19 |
16 /** | 20 /** |
17 * Handles servicing background offlining requests coming via background_task_sc
heduler component. | 21 * Handles servicing background offlining requests coming via background_task_sc
heduler component. |
18 */ | 22 */ |
19 public class OfflineBackgroundTask implements BackgroundTask { | 23 public class OfflineBackgroundTask implements BackgroundTask { |
| 24 private static final String TAG = "OPBackgroundTask"; |
| 25 |
20 BackgroundSchedulerProcessor mBackgroundProcessor; | 26 BackgroundSchedulerProcessor mBackgroundProcessor; |
21 | 27 |
22 public OfflineBackgroundTask() { | 28 public OfflineBackgroundTask() { |
23 mBackgroundProcessor = new BackgroundSchedulerProcessorImpl(); | 29 mBackgroundProcessor = new BackgroundSchedulerProcessorImpl(); |
24 } | 30 } |
25 | 31 |
26 @Override | 32 @Override |
27 public boolean onStartTask( | 33 public boolean onStartTask( |
28 Context context, TaskParameters taskParameters, TaskFinishedCallback
callback) { | 34 Context context, TaskParameters taskParameters, TaskFinishedCallback
callback) { |
29 assert taskParameters.getTaskId() == TaskIds.OFFLINE_PAGES_BACKGROUND_JO
B_ID; | 35 assert taskParameters.getTaskId() == TaskIds.OFFLINE_PAGES_BACKGROUND_JO
B_ID; |
| 36 |
| 37 // Ensuring that native potion of the browser is launched. |
| 38 launchBrowserIfNecessary(context); |
| 39 |
30 return BackgroundOfflinerTask.startBackgroundRequestsImpl( | 40 return BackgroundOfflinerTask.startBackgroundRequestsImpl( |
31 mBackgroundProcessor, context, taskParameters.getExtras(), wrapC
allback(callback)); | 41 mBackgroundProcessor, context, taskParameters.getExtras(), wrapC
allback(callback)); |
32 } | 42 } |
33 | 43 |
34 @Override | 44 @Override |
35 public boolean onStopTask(Context context, TaskParameters taskParameters) { | 45 public boolean onStopTask(Context context, TaskParameters taskParameters) { |
36 return mBackgroundProcessor.stopScheduledProcessing(); | 46 return mBackgroundProcessor.stopScheduledProcessing(); |
37 } | 47 } |
38 | 48 |
39 /** Wraps the callback for code reuse */ | 49 /** Wraps the callback for code reuse */ |
40 private Callback<Boolean> wrapCallback(final TaskFinishedCallback callback)
{ | 50 private Callback<Boolean> wrapCallback(final TaskFinishedCallback callback)
{ |
41 return new Callback<Boolean>() { | 51 return new Callback<Boolean>() { |
42 @Override | 52 @Override |
43 public void onResult(Boolean result) { | 53 public void onResult(Boolean result) { |
44 callback.taskFinished(result); | 54 callback.taskFinished(result); |
45 } | 55 } |
46 }; | 56 }; |
47 } | 57 } |
| 58 |
| 59 private static void launchBrowserIfNecessary(Context context) { |
| 60 if (LibraryLoader.isInitialized()) return; |
| 61 |
| 62 // TODO(fgorski): This method is taken from ChromeBackgroundService as a
local fix and will |
| 63 // be removed with BackgroundTaskScheduler supporting GcmNetworkManager
scheduling. |
| 64 try { |
| 65 ChromeBrowserInitializer.getInstance(context).handleSynchronousStart
up(); |
| 66 } catch (ProcessInitException e) { |
| 67 Log.e(TAG, "ProcessInitException while starting the browser process.
"); |
| 68 // Since the library failed to initialize nothing in the application
can work, so kill |
| 69 // the whole application not just the activity. |
| 70 System.exit(-1); |
| 71 } |
| 72 } |
48 } | 73 } |
OLD | NEW |