OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.crash; | 5 package org.chromium.chrome.browser.crash; |
6 | 6 |
7 import org.chromium.base.Log; | 7 import org.chromium.base.Log; |
8 import org.chromium.base.VisibleForTesting; | 8 import org.chromium.base.VisibleForTesting; |
9 | 9 |
10 import java.io.File; | 10 import java.io.File; |
(...skipping 25 matching lines...) Expand all Loading... |
36 private static final Pattern MINIDUMP_MIME_FIRST_TRY_PATTERN = | 36 private static final Pattern MINIDUMP_MIME_FIRST_TRY_PATTERN = |
37 Pattern.compile("\\.dmp([0-9]+)$\\z"); | 37 Pattern.compile("\\.dmp([0-9]+)$\\z"); |
38 | 38 |
39 private static final Pattern MINIDUMP_PATTERN = | 39 private static final Pattern MINIDUMP_PATTERN = |
40 Pattern.compile("\\.dmp([0-9]*)(\\.try[0-9])?\\z"); | 40 Pattern.compile("\\.dmp([0-9]*)(\\.try[0-9])?\\z"); |
41 | 41 |
42 private static final Pattern UPLOADED_MINIDUMP_PATTERN = Pattern.compile("\\
.up([0-9]*)\\z"); | 42 private static final Pattern UPLOADED_MINIDUMP_PATTERN = Pattern.compile("\\
.up([0-9]*)\\z"); |
43 | 43 |
44 private static final String UPLOADED_MINIDUMP_SUFFIX = ".up"; | 44 private static final String UPLOADED_MINIDUMP_SUFFIX = ".up"; |
45 | 45 |
| 46 private static final String UPLOAD_SKIPPED_MINIDUMP_SUFFIX = ".skipped"; |
| 47 |
46 private static final String UPLOAD_ATTEMPT_DELIMITER = ".try"; | 48 private static final String UPLOAD_ATTEMPT_DELIMITER = ".try"; |
47 | 49 |
48 @VisibleForTesting | 50 @VisibleForTesting |
49 protected static final String TMP_SUFFIX = ".tmp"; | 51 protected static final String TMP_SUFFIX = ".tmp"; |
50 | 52 |
51 private static final Pattern TMP_PATTERN = Pattern.compile("\\.tmp\\z"); | 53 private static final Pattern TMP_PATTERN = Pattern.compile("\\.tmp\\z"); |
52 | 54 |
53 /** | 55 /** |
54 * Comparator used for sorting files by modification | 56 * Comparator used for sorting files by modification |
55 * Note that the behavior is undecided if the files are created at the same
time | 57 * Note that the behavior is undecided if the files are created at the same
time |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 try { | 121 try { |
120 return Integer.parseInt(numTriesString); | 122 return Integer.parseInt(numTriesString); |
121 } catch (NumberFormatException ignored) { | 123 } catch (NumberFormatException ignored) { |
122 return 0; | 124 return 0; |
123 } | 125 } |
124 } | 126 } |
125 } | 127 } |
126 return 0; | 128 return 0; |
127 } | 129 } |
128 | 130 |
129 public static boolean tryMarkAsUploaded(File mFileToUpload) { | 131 /** |
130 return mFileToUpload.renameTo( | 132 * Marks a crash dump file as successfully uploaded, by renaming the file. |
131 new File(mFileToUpload.getPath().replaceAll( | 133 * |
132 "\\.dmp", UPLOADED_MINIDUMP_SUFFIX))); | 134 * Does not immediately delete the file, for testing reasons. However, if re
naming fails, |
| 135 * attempts to delete the file immediately. |
| 136 */ |
| 137 public static void markUploadSuccess(File crashDumpFile) { |
| 138 CrashFileManager.renameCrashDumpFollowingUpload(crashDumpFile, UPLOADED_
MINIDUMP_SUFFIX); |
| 139 } |
| 140 |
| 141 /** |
| 142 * Marks a crash dump file's upload being skipped. An upload might be skippe
d due to lack of |
| 143 * user consent, or due to this client being excluded from the sample of cli
ents reporting |
| 144 * crashes. |
| 145 * |
| 146 * Renames the file rather than deleting it, so that the user can manually u
pload the file later |
| 147 * (via chrome://crashes). However, if renaming fails, attempts to delete th
e file immediately. |
| 148 */ |
| 149 public static void markUploadSkipped(File crashDumpFile) { |
| 150 CrashFileManager.renameCrashDumpFollowingUpload( |
| 151 crashDumpFile, UPLOAD_SKIPPED_MINIDUMP_SUFFIX); |
| 152 } |
| 153 |
| 154 /** |
| 155 * Renames a crash dump file. However, if renaming fails, attempts to delete
the file |
| 156 * immediately. |
| 157 */ |
| 158 private static void renameCrashDumpFollowingUpload(File crashDumpFile, Strin
g suffix) { |
| 159 boolean renamed = crashDumpFile.renameTo( |
| 160 new File(crashDumpFile.getPath().replaceAll("\\.dmp", suffix))); |
| 161 if (!renamed) { |
| 162 Log.w(TAG, "Failed to rename " + crashDumpFile); |
| 163 if (!crashDumpFile.delete()) { |
| 164 Log.w(TAG, "Failed to delete " + crashDumpFile); |
| 165 } |
| 166 } |
133 } | 167 } |
134 | 168 |
135 private final File mCacheDir; | 169 private final File mCacheDir; |
136 | 170 |
137 public CrashFileManager(File cacheDir) { | 171 public CrashFileManager(File cacheDir) { |
138 if (cacheDir == null) { | 172 if (cacheDir == null) { |
139 throw new NullPointerException("Specified context cannot be null."); | 173 throw new NullPointerException("Specified context cannot be null."); |
140 } else if (!cacheDir.isDirectory()) { | 174 } else if (!cacheDir.isDirectory()) { |
141 throw new IllegalArgumentException(cacheDir.getAbsolutePath() | 175 throw new IllegalArgumentException(cacheDir.getAbsolutePath() |
142 + " is not a directory."); | 176 + " is not a directory."); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 } | 250 } |
217 | 251 |
218 File getCrashUploadLogFile() { | 252 File getCrashUploadLogFile() { |
219 return new File(mCacheDir, CRASH_DUMP_LOGFILE); | 253 return new File(mCacheDir, CRASH_DUMP_LOGFILE); |
220 } | 254 } |
221 | 255 |
222 private File[] getAllTempFiles() { | 256 private File[] getAllTempFiles() { |
223 return getMatchingFiles(TMP_PATTERN); | 257 return getMatchingFiles(TMP_PATTERN); |
224 } | 258 } |
225 } | 259 } |
OLD | NEW |