OLD | NEW |
---|---|
1 // Copyright 2016 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 package org.chromium.components.minidump_uploader; | 4 package org.chromium.components.minidump_uploader; |
5 | 5 |
6 import android.annotation.TargetApi; | 6 import android.annotation.TargetApi; |
7 import android.app.job.JobInfo; | |
7 import android.app.job.JobParameters; | 8 import android.app.job.JobParameters; |
9 import android.app.job.JobScheduler; | |
8 import android.app.job.JobService; | 10 import android.app.job.JobService; |
11 import android.content.ComponentName; | |
12 import android.content.Context; | |
9 import android.os.Build; | 13 import android.os.Build; |
14 import android.os.PersistableBundle; | |
15 | |
16 import org.chromium.base.Log; | |
10 | 17 |
11 /** | 18 /** |
12 * Class that interacts with the Android JobScheduler to upload Minidumps at app ropriate times. | 19 * Class that interacts with the Android JobScheduler to upload Minidumps at app ropriate times. |
13 */ | 20 */ |
14 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 21 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
15 public abstract class MinidumpUploadJobService extends JobService { | 22 public abstract class MinidumpUploadJobService extends JobService { |
16 Object mRunningLock = new Object(); | 23 private static final String TAG = "MinidumpJobService"; |
17 boolean mRunningJob = false; | 24 |
18 MinidumpUploader mMinidumpUploader; | 25 // The job id for uploading minidumps. For more info on this constant, see |
26 // https://developer.android.com/reference/android/app/job/JobInfo.Builder.h tml#JobInfo.Builder(int, | |
27 // android.content.ComponentName) | |
28 private static final int MINIDUMP_UPLOADING_JOB_ID = 42; | |
gsennton
2017/03/13 17:57:17
I think we want different numbers for Chrome and W
Ilya Sherman
2017/03/14 02:18:55
Done. Good point -- I completely forgot about Mon
| |
29 | |
30 // Initial back-off time for upload-job. This is set to a fairly high number (30 minutes) to | |
31 // increase the chance of performing uploads in batches if the initial uploa d fails. | |
32 private static final int JOB_BACKOFF_TIME_IN_MS = 1000 * 60 * 30; | |
33 | |
34 // Back-off policy for upload-job. | |
35 private static final int JOB_BACKOFF_POLICY = JobInfo.BACKOFF_POLICY_EXPONEN TIAL; | |
36 | |
37 private final Object mRunningLock = new Object(); | |
38 private boolean mRunningJob = false; | |
39 private MinidumpUploader mMinidumpUploader; | |
40 | |
41 /** | |
42 * Schedules uploading of all pending minidumps. | |
43 * @param context The application context, in which to schedule the crash re port uploads. | |
44 * @param jobServiceClass The concrete class to use as the job service for u ploading minidumps. | |
45 * Should be a subclass of this class. | |
46 * @param requiredNetworkType The network type required for upload. Should b e set to one of the | |
47 * NETWORK_TYPE constants defined in the Android JobInfo class. | |
48 * @param extras Any serialized data that should be passed to the upload job , in case the job | |
49 * runs after the calling process is destroyed. | |
50 */ | |
51 public static void scheduleUpload(Context context, | |
52 Class<? extends MinidumpUploadJobService> jobServiceClass, int requi redNetworkType, | |
53 PersistableBundle extras) { | |
54 Log.i(TAG, "Scheduling upload of all pending minidumps."); | |
55 JobScheduler scheduler = | |
56 (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SE RVICE); | |
57 JobInfo uploadJob = | |
58 new JobInfo | |
59 .Builder(MINIDUMP_UPLOADING_JOB_ID, | |
60 new ComponentName(context, jobServiceClass)) | |
61 .setRequiredNetworkType(requiredNetworkType) | |
62 // Minimum delay when a job is retried (a retry will hap pen when | |
63 // there are minidumps left after trying to upload all m inidumps - | |
64 // this could e.g. happen if we add more minidumps at th e same time | |
65 // as uploading old ones). | |
66 .setBackoffCriteria(JOB_BACKOFF_TIME_IN_MS, JOB_BACKOFF_ POLICY) | |
67 .setExtras(extras) | |
68 .build(); | |
69 if (scheduler.schedule(uploadJob) == JobScheduler.RESULT_FAILURE) { | |
70 throw new RuntimeException("couldn't schedule " + uploadJob); | |
71 } | |
72 } | |
19 | 73 |
20 @Override | 74 @Override |
21 public boolean onStartJob(JobParameters params) { | 75 public boolean onStartJob(JobParameters params) { |
22 // Ensure we only run one job at a time. | 76 // Ensure we only run one job at a time. |
23 synchronized (mRunningLock) { | 77 synchronized (mRunningLock) { |
78 // TODO(isherman): We never read mRunningJob -- is it obsolete? | |
Ilya Sherman
2017/03/13 03:12:09
Gustav, I forget whether we've discussed this befo
gsennton
2017/03/13 17:57:17
It's asserting that we aren't running several jobs
Ilya Sherman
2017/03/14 02:18:55
Okay, as long as it's intended just a sanity-check
gsennton
2017/03/14 18:17:28
Yeah, it's only enabled for WebView with a certain
| |
24 assert !mRunningJob; | 79 assert !mRunningJob; |
25 mRunningJob = true; | 80 mRunningJob = true; |
26 } | 81 } |
27 mMinidumpUploader = createMinidumpUploader(); | 82 mMinidumpUploader = createMinidumpUploader(params.getExtras()); |
28 mMinidumpUploader.uploadAllMinidumps(createJobFinishedCallback(params)); | 83 mMinidumpUploader.uploadAllMinidumps(createJobFinishedCallback(params)); |
29 return true; // true = processing work on a separate thread, false = don e already. | 84 return true; // true = processing work on a separate thread, false = don e already. |
30 } | 85 } |
31 | 86 |
32 @Override | 87 @Override |
33 public boolean onStopJob(JobParameters params) { | 88 public boolean onStopJob(JobParameters params) { |
89 Log.i(TAG, "Canceling pending uploads due to change in networking status ."); | |
34 boolean reschedule = mMinidumpUploader.cancelUploads(); | 90 boolean reschedule = mMinidumpUploader.cancelUploads(); |
35 synchronized (mRunningLock) { | 91 synchronized (mRunningLock) { |
36 mRunningJob = false; | 92 mRunningJob = false; |
37 } | 93 } |
38 return reschedule; | 94 return reschedule; |
39 } | 95 } |
40 | 96 |
41 @Override | 97 @Override |
42 public void onDestroy() { | 98 public void onDestroy() { |
43 mMinidumpUploader = null; | 99 mMinidumpUploader = null; |
44 super.onDestroy(); | 100 super.onDestroy(); |
45 } | 101 } |
46 | 102 |
47 private MinidumpUploader.UploadsFinishedCallback createJobFinishedCallback( | 103 private MinidumpUploader.UploadsFinishedCallback createJobFinishedCallback( |
48 final JobParameters params) { | 104 final JobParameters params) { |
49 return new MinidumpUploader.UploadsFinishedCallback() { | 105 return new MinidumpUploader.UploadsFinishedCallback() { |
50 @Override | 106 @Override |
51 public void uploadsFinished(boolean reschedule) { | 107 public void uploadsFinished(boolean reschedule) { |
108 if (reschedule) { | |
109 Log.i(TAG, "Some minidumps remain un-uploaded; rescheduling. "); | |
110 } | |
52 synchronized (mRunningLock) { | 111 synchronized (mRunningLock) { |
53 mRunningJob = false; | 112 mRunningJob = false; |
54 } | 113 } |
55 MinidumpUploadJobService.this.jobFinished(params, reschedule); | 114 MinidumpUploadJobService.this.jobFinished(params, reschedule); |
56 } | 115 } |
57 }; | 116 }; |
58 } | 117 } |
59 | 118 |
60 /** | 119 /** |
120 * @param extras Any extra data persisted for this job. | |
61 * @return The minidump uploader that jobs should use. | 121 * @return The minidump uploader that jobs should use. |
62 */ | 122 */ |
63 protected abstract MinidumpUploader createMinidumpUploader(); | 123 protected abstract MinidumpUploader createMinidumpUploader(PersistableBundle extras); |
64 } | 124 } |
OLD | NEW |