From 444e01deae219666643e78867a6be69afc23beab Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Tue, 2 Oct 2018 12:31:12 -0700 Subject: [PATCH] Improve key presence checking in SafeData. This isn't a perfect check either, but it should be safer and more consistent than using static "invalid values". --- .../securesms/jobmanager/SafeData.java | 67 ++++++------------- .../securesms/jobs/AttachmentDownloadJob.java | 2 +- .../securesms/jobs/DirectoryRefreshJob.java | 4 +- .../securesms/jobs/MmsDownloadJob.java | 2 +- .../jobs/MultiDeviceContactUpdateJob.java | 4 +- .../jobs/MultiDeviceReadReceiptUpdateJob.java | 2 +- .../securesms/jobs/PushContentReceiveJob.java | 2 +- .../securesms/jobs/PushGroupSendJob.java | 2 +- .../jobs/RetrieveProfileAvatarJob.java | 2 +- 9 files changed, 31 insertions(+), 56 deletions(-) diff --git a/src/org/thoughtcrime/securesms/jobmanager/SafeData.java b/src/org/thoughtcrime/securesms/jobmanager/SafeData.java index 642ab84e0e..2d7fba3e2d 100644 --- a/src/org/thoughtcrime/securesms/jobmanager/SafeData.java +++ b/src/org/thoughtcrime/securesms/jobmanager/SafeData.java @@ -11,9 +11,6 @@ import androidx.work.Data; */ public class SafeData { - private final static int INVALID_INT = Integer.MIN_VALUE; - private final static long INVALID_LONG = Long.MIN_VALUE; - private final Data data; public SafeData(@NonNull Data data) { @@ -21,60 +18,38 @@ public class SafeData { } public int getInt(@NonNull String key) { - int value = data.getInt(key, INVALID_INT); - - if (value == INVALID_INT) { - throw new IllegalStateException("Missing key: " + key); - } - - return value; + assertKeyPresence(key); + return data.getInt(key, -1); } public long getLong(@NonNull String key) { - long value = data.getLong(key, INVALID_LONG); - - if (value == INVALID_LONG) { - throw new IllegalStateException("Missing key: " + key); - } - - return value; + assertKeyPresence(key); + return data.getLong(key, -1); } - public @NonNull String getString(@NonNull String key) { - String value = data.getString(key); - - if (value == null) { - throw new IllegalStateException("Missing key: " + key); - } - - return value; - } - - public @Nullable String getNullableString(@NonNull String key) { + public String getString(@NonNull String key) { + assertKeyPresence(key); return data.getString(key); } - public @NonNull String[] getStringArray(@NonNull String key) { - String[] value = data.getStringArray(key); - - if (value == null) { - throw new IllegalStateException("Missing key: " + key); - } - - return value; + public String[] getStringArray(@NonNull String key) { + assertKeyPresence(key); + return data.getStringArray(key); } - public @NonNull long[] getLongArray(@NonNull String key) { - long[] value = data.getLongArray(key); - - if (value == null) { - throw new IllegalStateException("Missing key: " + key); - } - - return value; + public long[] getLongArray(@NonNull String key) { + assertKeyPresence(key); + return data.getLongArray(key); } - public boolean getBoolean(@NonNull String key, boolean defaultValue) { - return data.getBoolean(key, defaultValue); + public boolean getBoolean(@NonNull String key) { + assertKeyPresence(key); + return data.getBoolean(key, false); + } + + private void assertKeyPresence(@NonNull String key) { + if (!data.getKeyValueMap().containsKey(key)) { + throw new IllegalStateException("Missing key: " + key); + } } } diff --git a/src/org/thoughtcrime/securesms/jobs/AttachmentDownloadJob.java b/src/org/thoughtcrime/securesms/jobs/AttachmentDownloadJob.java index deebfc252d..b801da4077 100644 --- a/src/org/thoughtcrime/securesms/jobs/AttachmentDownloadJob.java +++ b/src/org/thoughtcrime/securesms/jobs/AttachmentDownloadJob.java @@ -78,7 +78,7 @@ public class AttachmentDownloadJob extends MasterSecretJob implements Injectable messageId = data.getLong(KEY_MESSAGE_ID); partRowId = data.getLong(KEY_PART_ROW_ID); partUniqueId = data.getLong(KEY_PAR_UNIQUE_ID); - manual = data.getBoolean(KEY_MANUAL, false); + manual = data.getBoolean(KEY_MANUAL); } @Override diff --git a/src/org/thoughtcrime/securesms/jobs/DirectoryRefreshJob.java b/src/org/thoughtcrime/securesms/jobs/DirectoryRefreshJob.java index 20409eaa01..789167d09a 100644 --- a/src/org/thoughtcrime/securesms/jobs/DirectoryRefreshJob.java +++ b/src/org/thoughtcrime/securesms/jobs/DirectoryRefreshJob.java @@ -50,11 +50,11 @@ public class DirectoryRefreshJob extends ContextJob { @Override protected void initialize(@NonNull SafeData data) { - String serializedAddress = data.getNullableString(KEY_ADDRESS); + String serializedAddress = data.getString(KEY_ADDRESS); Address address = serializedAddress != null ? Address.fromSerialized(serializedAddress) : null; recipient = address != null ? Recipient.from(context, address, true) : null; - notifyOfNewUsers = data.getBoolean(KEY_NOTIFY_OF_NEW_USERS, false); + notifyOfNewUsers = data.getBoolean(KEY_NOTIFY_OF_NEW_USERS); } @Override diff --git a/src/org/thoughtcrime/securesms/jobs/MmsDownloadJob.java b/src/org/thoughtcrime/securesms/jobs/MmsDownloadJob.java index fa50125167..b9773865d6 100644 --- a/src/org/thoughtcrime/securesms/jobs/MmsDownloadJob.java +++ b/src/org/thoughtcrime/securesms/jobs/MmsDownloadJob.java @@ -83,7 +83,7 @@ public class MmsDownloadJob extends MasterSecretJob { protected void initialize(@NonNull SafeData data) { messageId = data.getLong(KEY_MESSAGE_ID); threadId = data.getLong(KEY_THREAD_ID); - automatic = data.getBoolean(KEY_AUTOMATIC, false); + automatic = data.getBoolean(KEY_AUTOMATIC); } @Override diff --git a/src/org/thoughtcrime/securesms/jobs/MultiDeviceContactUpdateJob.java b/src/org/thoughtcrime/securesms/jobs/MultiDeviceContactUpdateJob.java index bef0c9ac65..7b56daa856 100644 --- a/src/org/thoughtcrime/securesms/jobs/MultiDeviceContactUpdateJob.java +++ b/src/org/thoughtcrime/securesms/jobs/MultiDeviceContactUpdateJob.java @@ -99,8 +99,8 @@ public class MultiDeviceContactUpdateJob extends MasterSecretJob implements Inje @Override protected void initialize(@NonNull SafeData data) { - address = data.getNullableString(KEY_ADDRESS); - forceSync = data.getBoolean(KEY_FORCE_SYNC, false); + address = data.getString(KEY_ADDRESS); + forceSync = data.getBoolean(KEY_FORCE_SYNC); } @Override diff --git a/src/org/thoughtcrime/securesms/jobs/MultiDeviceReadReceiptUpdateJob.java b/src/org/thoughtcrime/securesms/jobs/MultiDeviceReadReceiptUpdateJob.java index f0ae033109..9aafd3a1b3 100644 --- a/src/org/thoughtcrime/securesms/jobs/MultiDeviceReadReceiptUpdateJob.java +++ b/src/org/thoughtcrime/securesms/jobs/MultiDeviceReadReceiptUpdateJob.java @@ -48,7 +48,7 @@ public class MultiDeviceReadReceiptUpdateJob extends ContextJob implements Injec @Override protected void initialize(@NonNull SafeData data) { - enabled = data.getBoolean(KEY_ENABLED, false); + enabled = data.getBoolean(KEY_ENABLED); } @Override diff --git a/src/org/thoughtcrime/securesms/jobs/PushContentReceiveJob.java b/src/org/thoughtcrime/securesms/jobs/PushContentReceiveJob.java index bd9aa10d58..4787d74c60 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushContentReceiveJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushContentReceiveJob.java @@ -40,7 +40,7 @@ public class PushContentReceiveJob extends PushReceivedJob { @Override protected void initialize(@NonNull SafeData data) { - this.data = data.getNullableString(KEY_DATA); + this.data = data.getString(KEY_DATA); } @Override diff --git a/src/org/thoughtcrime/securesms/jobs/PushGroupSendJob.java b/src/org/thoughtcrime/securesms/jobs/PushGroupSendJob.java index 45f6841bc2..d4c97f2d21 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushGroupSendJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushGroupSendJob.java @@ -84,7 +84,7 @@ public class PushGroupSendJob extends PushSendJob implements InjectableType { @Override protected void initialize(@NonNull SafeData data) { messageId = data.getLong(KEY_MESSAGE_ID); - filterAddress = data.getNullableString(KEY_FILTER_ADDRESS); + filterAddress = data.getString(KEY_FILTER_ADDRESS); } @Override diff --git a/src/org/thoughtcrime/securesms/jobs/RetrieveProfileAvatarJob.java b/src/org/thoughtcrime/securesms/jobs/RetrieveProfileAvatarJob.java index d98e0af1c3..7d8ee23711 100644 --- a/src/org/thoughtcrime/securesms/jobs/RetrieveProfileAvatarJob.java +++ b/src/org/thoughtcrime/securesms/jobs/RetrieveProfileAvatarJob.java @@ -59,7 +59,7 @@ public class RetrieveProfileAvatarJob extends ContextJob implements InjectableTy @Override protected void initialize(@NonNull SafeData data) { - profileAvatar = data.getNullableString(KEY_PROFILE_AVATAR); + profileAvatar = data.getString(KEY_PROFILE_AVATAR); recipient = Recipient.from(context, Address.fromSerialized(data.getString(KEY_ADDRESS)), true); }