Index: components/invalidation/android/java/src/org/chromium/components/invalidation/PendingInvalidation.java |
diff --git a/components/invalidation/android/java/src/org/chromium/components/invalidation/PendingInvalidation.java b/components/invalidation/android/java/src/org/chromium/components/invalidation/PendingInvalidation.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9fa3b0c2aee45a73c47484c5cd02af3366ed13a9 |
--- /dev/null |
+++ b/components/invalidation/android/java/src/org/chromium/components/invalidation/PendingInvalidation.java |
@@ -0,0 +1,122 @@ |
+// Copyright 2015 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.components.invalidation; |
+ |
+import android.os.Bundle; |
+import android.util.JsonReader; |
+import android.util.JsonWriter; |
+ |
+import org.chromium.base.Log; |
+ |
+import java.io.IOException; |
+import java.io.StringReader; |
+import java.io.StringWriter; |
+ |
+/** |
+ * A container class that stores the received invalidations. |
+ * It plays the role of abstracting conversions to and from other storage types like String |
+ * (storing in prefStore) and Bundle (ContentProvider). |
+ */ |
+public class PendingInvalidation { |
nyquist
2015/05/22 20:21:07
Should this class have a simple test for serializa
knn
2015/06/04 18:23:29
Done.
|
+ static final String TAG = Log.makeTag("invalidation"); |
+ |
+ static final String INVALIDATION_OBJECT_SOURCE_KEY = "objectSource"; |
+ static final String INVALIDATION_OBJECT_ID_KEY = "objectId"; |
+ static final String INVALIDATION_VERSION_KEY = "version"; |
+ static final String INVALIDATION_PAYLOAD_KEY = "payload"; |
+ |
+ public final String mObjectId; |
+ public final int mObjectSource; |
+ public final long mVersion; |
+ public final String mPayload; |
+ |
+ public PendingInvalidation(String id, int source, long version, String payload) { |
+ mObjectId = id; |
+ mObjectSource = source; |
+ mVersion = version; |
+ mPayload = payload; |
+ } |
+ |
+ public PendingInvalidation(Bundle bundle) { |
+ mObjectId = bundle.getString(INVALIDATION_OBJECT_ID_KEY); |
+ mObjectSource = bundle.getInt(INVALIDATION_OBJECT_SOURCE_KEY); |
+ mVersion = bundle.getLong(INVALIDATION_VERSION_KEY); |
+ mPayload = bundle.getString(INVALIDATION_PAYLOAD_KEY); |
+ } |
+ |
+ public final boolean isInvalidateAll() { |
+ return mObjectId == null; |
+ } |
+ |
+ public static PendingInvalidation fromString(String invalidationString) { |
+ JsonReader reader = new JsonReader(new StringReader(invalidationString)); |
+ String objectId = null; |
+ // There is already some special handling for objectSource and version value 0 as unknown. |
+ int objectSource = 0; |
+ long version = 0L; |
+ String payload = null; |
+ try { |
+ reader.beginObject(); |
+ while (reader.hasNext()) { |
+ String key = reader.nextName(); |
+ switch (key) { |
+ case INVALIDATION_OBJECT_ID_KEY: |
+ objectId = reader.nextString(); |
+ break; |
+ case INVALIDATION_OBJECT_SOURCE_KEY: |
+ objectSource = reader.nextInt(); |
+ break; |
+ case INVALIDATION_VERSION_KEY: |
+ version = reader.nextLong(); |
+ break; |
+ case INVALIDATION_PAYLOAD_KEY: |
+ payload = reader.nextString(); |
+ break; |
+ default: |
+ reader.skipValue(); |
+ } |
+ } |
+ reader.endObject(); |
+ reader.close(); |
+ } catch (IOException e) { |
+ // Handled below. |
Bernhard Bauer
2015/05/21 16:21:25
We shouldn't really get an IO exception from a Str
|
+ } |
+ if (objectSource == 0) return null; |
+ if (objectId == null) { |
+ Log.e(TAG, "No objectId found while parsing cached invalidation."); |
+ Log.e(TAG, "Proceeding with invalidating all ids for source %d.", objectSource); |
+ } |
+ return new PendingInvalidation(objectId, objectSource, version, payload); |
+ } |
+ |
+ @Override |
+ public String toString() { |
+ StringWriter stringWriter = new StringWriter(); |
nyquist
2015/05/22 20:21:07
I would much rather us storing Protocol Buffers th
knn
2015/06/04 18:23:29
Serializing to file to avoid the byte -> string ug
Bernhard Bauer
2015/06/05 08:20:48
You could Base64-encode the byte array to get a st
knn
2015/06/05 15:09:52
Done.
|
+ JsonWriter jsonWriter = new JsonWriter(stringWriter); |
+ try { |
+ jsonWriter.beginObject(); |
+ jsonWriter.name(INVALIDATION_OBJECT_SOURCE_KEY).value(mObjectSource); |
+ if (mObjectId != null) jsonWriter.name(INVALIDATION_OBJECT_ID_KEY).value(mObjectId); |
+ jsonWriter.name(INVALIDATION_VERSION_KEY).value(mVersion); |
+ if (mPayload != null) jsonWriter.name(INVALIDATION_PAYLOAD_KEY).value(mPayload); |
+ jsonWriter.endObject(); |
+ jsonWriter.close(); |
+ } catch (IOException e) { |
+ // Nothing to be done now. Will handle during de-serialization. |
+ } |
+ return stringWriter.toString(); |
+ } |
+ |
+ public Bundle toBundle() { |
+ Bundle bundle = new Bundle(); |
+ if (!isInvalidateAll()) { |
+ bundle.putString(INVALIDATION_OBJECT_ID_KEY, mObjectId); |
+ bundle.putInt(INVALIDATION_OBJECT_SOURCE_KEY, mObjectSource); |
+ bundle.putLong(INVALIDATION_VERSION_KEY, mVersion); |
+ bundle.putString(INVALIDATION_PAYLOAD_KEY, mPayload); |
+ } |
+ return bundle; |
+ } |
+} |