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 | 4 |
5 package org.chromium.android_webview.crash; | 5 package org.chromium.android_webview.crash; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.webkit.ValueCallback; | |
8 | 9 |
10 import org.chromium.android_webview.UserConsentInterface; | |
9 import org.chromium.base.Log; | 11 import org.chromium.base.Log; |
10 import org.chromium.base.VisibleForTesting; | 12 import org.chromium.base.VisibleForTesting; |
11 import org.chromium.components.minidump_uploader.CrashFileManager; | 13 import org.chromium.components.minidump_uploader.CrashFileManager; |
12 import org.chromium.components.minidump_uploader.MinidumpUploadCallable; | 14 import org.chromium.components.minidump_uploader.MinidumpUploadCallable; |
13 import org.chromium.components.minidump_uploader.util.CrashReportingPermissionMa nager; | 15 import org.chromium.components.minidump_uploader.util.CrashReportingPermissionMa nager; |
14 | 16 |
15 import java.io.File; | 17 import java.io.File; |
16 | 18 |
17 /** | 19 /** |
18 * Class in charge of uploading Minidumps from WebView's data directory. | 20 * Class in charge of uploading Minidumps from WebView's data directory. |
19 * This class gets invoked from a JobScheduler job and posts the operation of up loading minidumps to | 21 * This class gets invoked from a JobScheduler job and posts the operation of up loading minidumps to |
20 * a privately defined worker thread. | 22 * a privately defined worker thread. |
21 * Note that this implementation is state-less in the sense that it doesn't keep track of whether it | 23 * Note that this implementation is state-less in the sense that it doesn't keep track of whether it |
22 * successfully uploaded any minidumps. At the end of a job it simply checks whe ther there are any | 24 * successfully uploaded any minidumps. At the end of a job it simply checks whe ther there are any |
23 * minidumps left to upload, and if so, the job is rescheduled. | 25 * minidumps left to upload, and if so, the job is rescheduled. |
24 */ | 26 */ |
25 public class MinidumpUploaderImpl implements MinidumpUploader { | 27 public class MinidumpUploaderImpl implements MinidumpUploader { |
26 private static final String TAG = "MinidumpUploaderImpl"; | 28 private static final String TAG = "MinidumpUploaderImpl"; |
27 private Thread mWorkerThread; | 29 private Thread mWorkerThread; |
28 private final CrashFileManager mFileManager; | 30 private final CrashFileManager mFileManager; |
29 | 31 |
30 private Object mCancelLock = new Object(); | 32 private Object mCancelLock = new Object(); |
31 private boolean mCancelUpload = false; | 33 private boolean mCancelUpload = false; |
32 | 34 |
33 private final boolean mCleanOutMinidumps; | 35 private final boolean mCleanOutMinidumps; |
34 | 36 |
37 private final UserConsentInterface mUserConsentInterface; | |
38 | |
35 @VisibleForTesting | 39 @VisibleForTesting |
36 public static final int MAX_UPLOAD_TRIES_ALLOWED = 3; | 40 public static final int MAX_UPLOAD_TRIES_ALLOWED = 3; |
37 | 41 |
38 /** | 42 /** |
39 * Notify the worker thread that the current job has been canceled - so we s houldn't upload any | 43 * Notify the worker thread that the current job has been canceled - so we s houldn't upload any |
40 * more minidumps. | 44 * more minidumps. |
41 */ | 45 */ |
42 private void setCancelUpload(boolean cancel) { | 46 private void setCancelUpload(boolean cancel) { |
43 synchronized (mCancelLock) { | 47 synchronized (mCancelLock) { |
44 mCancelUpload = cancel; | 48 mCancelUpload = cancel; |
45 } | 49 } |
46 } | 50 } |
47 | 51 |
48 /** | 52 /** |
49 * Check whether the current job has been canceled. | 53 * Check whether the current job has been canceled. |
50 */ | 54 */ |
51 private boolean getCancelUpload() { | 55 private boolean getCancelUpload() { |
52 synchronized (mCancelLock) { | 56 synchronized (mCancelLock) { |
53 return mCancelUpload; | 57 return mCancelUpload; |
54 } | 58 } |
55 } | 59 } |
56 | 60 |
57 @VisibleForTesting | 61 @VisibleForTesting |
58 public MinidumpUploaderImpl(Context context, boolean cleanOutMinidumps) { | 62 public MinidumpUploaderImpl( |
63 Context context, boolean cleanOutMinidumps, UserConsentInterface use rConsentInterface) { | |
59 File webviewCrashDir = CrashReceiverService.createWebViewCrashDir(contex t); | 64 File webviewCrashDir = CrashReceiverService.createWebViewCrashDir(contex t); |
60 mFileManager = new CrashFileManager(webviewCrashDir); | 65 mFileManager = new CrashFileManager(webviewCrashDir); |
61 if (!mFileManager.ensureCrashDirExists()) { | 66 if (!mFileManager.ensureCrashDirExists()) { |
62 Log.e(TAG, "Crash directory doesn't exist!"); | 67 Log.e(TAG, "Crash directory doesn't exist!"); |
63 } | 68 } |
64 mCleanOutMinidumps = cleanOutMinidumps; | 69 mCleanOutMinidumps = cleanOutMinidumps; |
70 mUserConsentInterface = userConsentInterface; | |
65 } | 71 } |
66 | 72 |
67 /** | 73 /** |
68 * Utility method to allow us to test the logic of this class by injecting | 74 * Utility method to allow us to test the logic of this class by injecting |
69 * test-MinidumpUploadCallables. | 75 * test-MinidumpUploadCallables. |
70 */ | 76 */ |
71 @VisibleForTesting | 77 @VisibleForTesting |
72 public MinidumpUploadCallable createMinidumpUploadCallable(File minidumpFile , File logfile) { | 78 public MinidumpUploadCallable createMinidumpUploadCallable(File minidumpFile , File logfile) { |
73 return new MinidumpUploadCallable( | 79 return new MinidumpUploadCallable( |
74 minidumpFile, logfile, createWebViewCrashReportingManager()); | 80 minidumpFile, logfile, createWebViewCrashReportingManager()); |
(...skipping 23 matching lines...) Expand all Loading... | |
98 /** | 104 /** |
99 * This method is already represented by isClientInMetricsSample() a nd | 105 * This method is already represented by isClientInMetricsSample() a nd |
100 * isNetworkAvailableForCrashUploads(). | 106 * isNetworkAvailableForCrashUploads(). |
101 */ | 107 */ |
102 @Override | 108 @Override |
103 public boolean isMetricsUploadPermitted() { | 109 public boolean isMetricsUploadPermitted() { |
104 return true; | 110 return true; |
105 } | 111 } |
106 @Override | 112 @Override |
107 public boolean isUsageAndCrashReportingPermittedByUser() { | 113 public boolean isUsageAndCrashReportingPermittedByUser() { |
108 // TODO(gsennton): make this depend on Android Checkbox when we can read that | 114 // We ensure we have user permission before starting to upload m inidumps - so always |
109 // through GmsCore. | 115 // return true here. |
110 return false; | 116 return true; |
111 } | 117 } |
112 @Override | 118 @Override |
113 public boolean isUploadEnabledForTests() { | 119 public boolean isUploadEnabledForTests() { |
114 return SynchronizedWebViewCommandLine.hasSwitch( | 120 return SynchronizedWebViewCommandLine.hasSwitch( |
115 CrashReceiverService.CRASH_UPLOADS_ENABLED_FOR_TESTING_S WITCH); | 121 CrashReceiverService.CRASH_UPLOADS_ENABLED_FOR_TESTING_S WITCH); |
116 } | 122 } |
117 }; | 123 }; |
118 } | 124 } |
119 | 125 |
120 /** | 126 /** |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 | 164 |
159 // Reschedule if there are still minidumps to upload. | 165 // Reschedule if there are still minidumps to upload. |
160 boolean reschedule = | 166 boolean reschedule = |
161 mFileManager.getAllMinidumpFiles(MAX_UPLOAD_TRIES_ALLOWED).l ength > 0; | 167 mFileManager.getAllMinidumpFiles(MAX_UPLOAD_TRIES_ALLOWED).l ength > 0; |
162 mUploadsFinishedCallback.uploadsFinished(reschedule); | 168 mUploadsFinishedCallback.uploadsFinished(reschedule); |
163 } | 169 } |
164 } | 170 } |
165 | 171 |
166 @Override | 172 @Override |
167 public void uploadAllMinidumps( | 173 public void uploadAllMinidumps( |
168 MinidumpUploader.UploadsFinishedCallback uploadsFinishedCallback) { | 174 final MinidumpUploader.UploadsFinishedCallback uploadsFinishedCallba ck) { |
169 if (mWorkerThread != null) { | 175 if (mWorkerThread != null) { |
170 throw new RuntimeException("Only one upload-job should be active at a time"); | 176 throw new RuntimeException("Only one upload-job should be active at a time"); |
171 } | 177 } |
172 mWorkerThread = new Thread(new UploadRunnable(uploadsFinishedCallback), "mWorkerThread"); | |
173 setCancelUpload(false); | 178 setCancelUpload(false); |
174 mWorkerThread.start(); | 179 |
180 // Abort if we cannot get the user-consent value. | |
181 if (!mUserConsentInterface.userConsentInterfaceAvailable()) { | |
182 uploadsFinishedCallback.uploadsFinished(false /* reschedule */); | |
183 return; | |
184 } | |
185 | |
186 mUserConsentInterface.userConsents(new ValueCallback<Boolean>() { | |
187 @Override | |
188 public void onReceiveValue(Boolean enabled) { | |
189 // TODO if (mWorkerThread != null) return; ? | |
190 // This callback is posted to the UI thread when we know whether the U&D flag is | |
191 // toggled on or off. We need to check that the flag is on and t hat our job hasn't | |
192 // been cancelled already. | |
193 if (getCancelUpload()) { | |
194 return; | |
195 } | |
196 if (enabled) { | |
gsennton
2017/01/13 19:01:55
TODO also read enabledForTesting flag and have it
gsennton
2017/01/23 17:50:40
Done (in CrashReceiverService).
| |
197 mWorkerThread = new Thread( | |
198 new UploadRunnable(uploadsFinishedCallback), "mWorke rThread"); | |
199 mWorkerThread.start(); | |
200 } else { | |
201 uploadsFinishedCallback.uploadsFinished(false /* reschedule */); | |
202 } | |
203 } | |
204 }); | |
175 } | 205 } |
176 | 206 |
177 /** | 207 /** |
178 * @return whether to reschedule the uploads. | 208 * @return whether to reschedule the uploads. |
179 */ | 209 */ |
180 @Override | 210 @Override |
181 public boolean cancelUploads() { | 211 public boolean cancelUploads() { |
182 setCancelUpload(true); | 212 setCancelUpload(true); |
183 | 213 |
184 // Reschedule if there are still minidumps to upload. | 214 // Reschedule if there are still minidumps to upload. |
185 return mFileManager.getAllMinidumpFiles(MAX_UPLOAD_TRIES_ALLOWED).length > 0; | 215 return mFileManager.getAllMinidumpFiles(MAX_UPLOAD_TRIES_ALLOWED).length > 0; |
186 } | 216 } |
187 } | 217 } |
OLD | NEW |