Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4086)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/download/DownloadSharedPreferenceEntry.java

Issue 1809203006: Switch to use download GUID to indentify download items (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix merge error Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/download/DownloadSharedPreferenceEntry.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadSharedPreferenceEntry.java b/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadSharedPreferenceEntry.java
new file mode 100644
index 0000000000000000000000000000000000000000..a23149629849b266aa46ce6264ed6691caf2ac8e
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadSharedPreferenceEntry.java
@@ -0,0 +1,96 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.download;
+
+import org.chromium.base.Log;
+import org.chromium.base.VisibleForTesting;
+
+import java.util.UUID;
+
+/**
+ * Class representing the download information stored in SharedPreferences to construct a
+ * download notification.
+ */
+public class DownloadSharedPreferenceEntry {
+ private static final String TAG = "DownloadEntry";
+ // Current version of the DownloadSharedPreferenceEntry. When changing the SharedPreference,
+ // we need to change the version number too.
+ @VisibleForTesting static final int VERSION = 1;
+ public final int notificationId;
+ public final boolean isResumable;
+ // This field is not yet used, but will soon be used. We add it here to avoid changing the
+ // format of the SharedPreference string again.
+ public final boolean isStartedOnMeteredNetwork;
+ public final String fileName;
+ public final String downloadGuid;
+
+ DownloadSharedPreferenceEntry(int notificationId, boolean isResumable,
+ boolean isStartedOnMeteredNetwork, String guid, String fileName) {
+ this.notificationId = notificationId;
+ this.isResumable = isResumable;
+ this.isStartedOnMeteredNetwork = isStartedOnMeteredNetwork;
+ this.downloadGuid = guid;
+ this.fileName = fileName;
+ }
+
+ /**
+ * Parse the pending notification from a String object in SharedPrefs.
+ *
+ * @param sharedPrefString String from SharedPreference, containing the notification ID, GUID,
+ * file name, whether it is resumable and whether download started on a metered network.
+ * @return a DownloadSharedPreferenceEntry object.
+ */
+ static DownloadSharedPreferenceEntry parseFromString(String sharedPrefString) {
+ String[] values = sharedPrefString.split(",", 6);
+ if (values.length == 6) {
+ try {
+ int version = Integer.parseInt(values[0]);
+ // Ignore all SharedPreference entries that has an invalid version for now.
+ if (version != VERSION) {
+ return new DownloadSharedPreferenceEntry(-1, false, false, null, "");
+ }
+ int id = Integer.parseInt(values[1]);
+ boolean isResumable = "1".equals(values[2]);
+ boolean isStartedOnMeteredNetwork = "1".equals(values[3]);
+ if (!isValidGUID(values[4])) {
+ return new DownloadSharedPreferenceEntry(-1, false, false, null, "");
+ }
+ return new DownloadSharedPreferenceEntry(
+ id, isResumable, isStartedOnMeteredNetwork, values[4], values[5]);
+ } catch (NumberFormatException nfe) {
+ Log.w(TAG, "Exception while parsing pending download:" + sharedPrefString);
+ }
+ }
+ return new DownloadSharedPreferenceEntry(-1, false, false, null, "");
+ }
+
+ /**
+ * @return a string for the DownloadSharedPreferenceEntry instance to be inserted into
+ * SharedPrefs.
+ */
+ String getSharedPreferenceString() {
+ return VERSION + "," + notificationId + "," + (isResumable ? "1" : "0") + ","
+ + (isStartedOnMeteredNetwork ? "1" : "0") + "," + downloadGuid + "," + fileName;
+ }
+
+ /**
+ * Check if a string is a valid GUID. GUID is RFC 4122 compliant, it should have format
+ * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
+ * TODO(qinmin): move this to base/.
+ * @return true if the string is a valid GUID, or false otherwise.
+ */
+ static boolean isValidGUID(String guid) {
+ if (guid == null) return false;
+ try {
+ // Java UUID class doesn't check the length of the string. Need to convert it back to
+ // string so that we can validate the length of the original string.
+ UUID uuid = UUID.fromString(guid);
+ String uuidString = uuid.toString();
+ return guid.equalsIgnoreCase(uuidString);
+ } catch (IllegalArgumentException e) {
+ return false;
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698