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

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: findbugs 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 String mSenderId;
nyquist 2017/02/22 11:38:02 I think all these members can be final?
Peter Beverloo 2017/02/23 17:26:56 Yup. Done.
34 private String mAppId;
35
36 @Nullable
37 private String mCollapseKey;
38 @Nullable
39 private byte[] mRawData;
40
41 /**
42 * Array that contains pairs of entries in the format of {key, value}.
43 */
44 private 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 final String bundleCollapseKey = "collapse_key";
nyquist 2017/02/22 11:38:02 Nit: Why are these final? I don't think we usually
Peter Beverloo 2017/02/23 17:26:55 Largely just copied, it doesn't really matter. Rem
63 final String bundleGcmplex = "com.google.ipc.invalidation.gcmmplex.";
64 final String bundleRawData = "rawData";
65 final String bundleSenderId = "from";
66 final 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.
nyquist 2017/02/22 11:38:02 The nittiest of nits: I think your formatted remov
Peter Beverloo 2017/02/23 17:26:56 `git cl format` warns if I add two spaces :/
nyquist 2017/02/24 08:22:39 I see. That's messed up. :-/
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
108 // The rawData field needs to distinguish between {not set, set but empt y, set with data}.
109 String rawDataString = persistableBundle.getString(BUNDLE_KEY_RAW_DATA);
110 if (rawDataString != null) {
111 if (rawDataString.length() > 0) {
112 mRawData = rawDataString.getBytes();
113 } else {
114 mRawData = new byte[0];
115 }
116 }
117
118 mDataKeysAndValuesArray = persistableBundle.getStringArray(BUNDLE_KEY_DA TA);
119 }
120
121 public String getSenderId() {
122 return mSenderId;
123 }
124
125 public String getAppId() {
126 return mAppId;
127 }
128
129 @Nullable
130 public String getCollapseKey() {
131 return mCollapseKey;
132 }
133
134 /**
135 * Callers are expected to not modify values in the returned byte array.
136 */
137 @Nullable
138 @SuppressFBWarnings("EI_EXPOSE_REP")
139 public byte[] getRawData() {
140 return mRawData;
141 }
142
143 /**
144 * Callers are expected to not modify values in the returned byte array.
145 */
146 @SuppressFBWarnings("EI_EXPOSE_REP")
147 public String[] getDataKeysAndValuesArray() {
148 return mDataKeysAndValuesArray;
149 }
150
151 /**
152 * Serializes the contents of this GCM Message to a new persistable bundle t hat can be stored,
153 * for example for purposes of scheduling a job.
154 */
155 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
156 public PersistableBundle toPersistableBundle() {
157 PersistableBundle persistableBundle = new PersistableBundle();
158 persistableBundle.putString(BUNDLE_KEY_SENDER_ID, mSenderId);
159 persistableBundle.putString(BUNDLE_KEY_APP_ID, mAppId);
160 persistableBundle.putString(BUNDLE_KEY_COLLAPSE_KEY, mCollapseKey);
161
162 // The rawData field needs to distinguish between {not set, set but empt y, set with data}.
163 if (mRawData != null) {
164 if (mRawData.length > 0) {
165 persistableBundle.putString(BUNDLE_KEY_RAW_DATA, new String(mRaw Data));
166 } else {
167 persistableBundle.putString(BUNDLE_KEY_RAW_DATA, "");
168 }
169 } else {
170 persistableBundle.putString(BUNDLE_KEY_RAW_DATA, null);
171 }
172
173 persistableBundle.putStringArray(BUNDLE_KEY_DATA, mDataKeysAndValuesArra y);
174 return persistableBundle;
175 }
176 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698