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

Side by Side Diff: components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/GCMMessage.java

Issue 2690163008: Route through a JobService when receiving a message for the GCM Driver (Closed)
Patch Set: Route through a JobService when receiving a message for the GCM Driver Created 3 years, 10 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.components.gcm_driver;
6
7 import android.annotation.TargetApi;
8 import android.os.Build;
9 import android.os.Bundle;
10 import android.os.PersistableBundle;
11
12 import org.chromium.base.annotations.SuppressFBWarnings;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import javax.annotation.Nullable;
18
19 /**
20 * Represents the contents of a GCM Message that is to be handled by the GCM Dri ver. Can be created
21 * based on data received from GCM, or serialized and deserialized to and from a PersistableBundle.
22 */
23 public class GCMMessage {
24 /**
25 * Keys used to store information in the persistable bundle for serializatio n purposes.
26 */
27 private static final String BUNDLE_KEY_APP_ID = "appId";
28 private static final String BUNDLE_KEY_COLLAPSE_KEY = "collapseKey";
29 private static final String BUNDLE_KEY_DATA = "data";
30 private static final String BUNDLE_KEY_RAW_DATA = "rawData";
31 private static final String BUNDLE_KEY_SENDER_ID = "senderId";
32
33 private final String mSenderId;
34 private final String mAppId;
35
36 @Nullable
37 private final String mCollapseKey;
38 @Nullable
39 private final byte[] mRawData;
40
41 /**
42 * Array that contains pairs of entries in the format of {key, value}.
43 */
44 private final String[] mDataKeysAndValuesArray;
45
46 /**
47 * Validates that all required fields have been set in the given bundle.
48 */
49 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
50 public static boolean validatePersistableBundle(PersistableBundle persistabl eBundle) {
51 return persistableBundle.containsKey(BUNDLE_KEY_APP_ID)
52 && persistableBundle.containsKey(BUNDLE_KEY_COLLAPSE_KEY)
53 && persistableBundle.containsKey(BUNDLE_KEY_DATA)
54 && persistableBundle.containsKey(BUNDLE_KEY_RAW_DATA)
55 && persistableBundle.containsKey(BUNDLE_KEY_SENDER_ID);
56 }
57
58 /**
59 * Creates a GCMMessage object based on data received from GCM. The extras w ill be filtered.
60 */
61 public GCMMessage(String senderId, Bundle extras) {
62 String bundleCollapseKey = "collapse_key";
63 String bundleGcmplex = "com.google.ipc.invalidation.gcmmplex.";
64 String bundleRawData = "rawData";
65 String bundleSenderId = "from";
66 String bundleSubtype = "subtype";
67
68 if (!extras.containsKey(bundleSubtype)) {
69 throw new IllegalArgumentException("Received push message with no su btype");
70 }
71
72 mSenderId = senderId;
73 mAppId = extras.getString(bundleSubtype);
74
75 mCollapseKey = extras.getString(bundleCollapseKey); // May be null.
76 mRawData = extras.getByteArray(bundleRawData); // May be null.
77
78 List<String> dataKeysAndValues = new ArrayList<String>();
79 for (String key : extras.keySet()) {
80 if (key.equals(bundleSubtype) || key.equals(bundleSenderId)
81 || key.equals(bundleCollapseKey) || key.equals(bundleRawData )
82 || key.startsWith(bundleGcmplex)) {
83 continue;
84 }
85
86 Object value = extras.get(key);
87 if (!(value instanceof String)) {
88 continue;
89 }
90
91 dataKeysAndValues.add(key);
92 dataKeysAndValues.add((String) value);
93 }
94
95 mDataKeysAndValuesArray = dataKeysAndValues.toArray(new String[dataKeysA ndValues.size()]);
96 }
97
98 /**
99 * Creates a GCMMessage object based on the given bundle. Assumes that the b undle has previously
100 * been created through {@link #toPersistableBundle}.
101 */
102 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
103 public GCMMessage(PersistableBundle persistableBundle) {
104 mSenderId = persistableBundle.getString(BUNDLE_KEY_SENDER_ID);
105 mAppId = persistableBundle.getString(BUNDLE_KEY_APP_ID);
106 mCollapseKey = persistableBundle.getString(BUNDLE_KEY_COLLAPSE_KEY);
107 // The rawData field needs to distinguish between {not set, set but empt y, set with data}.
108 String rawDataString = persistableBundle.getString(BUNDLE_KEY_RAW_DATA);
109 if (rawDataString != null) {
110 if (rawDataString.length() > 0) {
111 mRawData = rawDataString.getBytes();
112 } else {
113 mRawData = new byte[0];
114 }
115 } else {
116 mRawData = null;
117 }
118
119 mDataKeysAndValuesArray = persistableBundle.getStringArray(BUNDLE_KEY_DA TA);
120 }
121
122 public String getSenderId() {
123 return mSenderId;
124 }
125
126 public String getAppId() {
127 return mAppId;
128 }
129
130 @Nullable
131 public String getCollapseKey() {
132 return mCollapseKey;
133 }
134
135 /**
136 * Callers are expected to not modify values in the returned byte array.
137 */
138 @Nullable
139 @SuppressFBWarnings("EI_EXPOSE_REP")
140 public byte[] getRawData() {
141 return mRawData;
142 }
143
144 /**
145 * Callers are expected to not modify values in the returned byte array.
146 */
147 @SuppressFBWarnings("EI_EXPOSE_REP")
148 public String[] getDataKeysAndValuesArray() {
149 return mDataKeysAndValuesArray;
150 }
151
152 /**
153 * Serializes the contents of this GCM Message to a new persistable bundle t hat can be stored,
154 * for example for purposes of scheduling a job.
155 */
156 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
157 public PersistableBundle toPersistableBundle() {
158 PersistableBundle persistableBundle = new PersistableBundle();
159 persistableBundle.putString(BUNDLE_KEY_SENDER_ID, mSenderId);
160 persistableBundle.putString(BUNDLE_KEY_APP_ID, mAppId);
161 persistableBundle.putString(BUNDLE_KEY_COLLAPSE_KEY, mCollapseKey);
162
163 // The rawData field needs to distinguish between {not set, set but empt y, set with data}.
164 if (mRawData != null) {
165 if (mRawData.length > 0) {
166 persistableBundle.putString(BUNDLE_KEY_RAW_DATA, new String(mRaw Data));
167 } else {
168 persistableBundle.putString(BUNDLE_KEY_RAW_DATA, "");
169 }
170 } else {
171 persistableBundle.putString(BUNDLE_KEY_RAW_DATA, null);
172 }
173
174 persistableBundle.putStringArray(BUNDLE_KEY_DATA, mDataKeysAndValuesArra y);
175 return persistableBundle;
176 }
177 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698