1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 package org.mozilla.gecko.push; 7 8 import android.support.annotation.NonNull; 9 import org.json.JSONException; 10 import org.json.JSONObject; 11 12 /** 13 * Represent an autopush Channel subscription. 14 * <p/> 15 * Such a subscription associates a user agent and autopush data with a channel 16 * ID, a WebPush endpoint, and some service-specific data. 17 * <p/> 18 * Cloud Messaging data, and the returned uaid and secret. 19 * <p/> 20 * Each registration is associated to a single Gecko profile, although we don't 21 * enforce that here. This class is immutable, so it is by definition 22 * thread-safe. 23 */ 24 public class PushSubscription { 25 public final @NonNull String chid; 26 public final @NonNull String profileName; 27 public final @NonNull String webpushEndpoint; 28 public final @NonNull String service; 29 public final JSONObject serviceData; 30 PushSubscription(@onNull String chid, @NonNull String profileName, @NonNull String webpushEndpoint, @NonNull String service, JSONObject serviceData)31 public PushSubscription(@NonNull String chid, @NonNull String profileName, @NonNull String webpushEndpoint, @NonNull String service, JSONObject serviceData) { 32 this.chid = chid; 33 this.profileName = profileName; 34 this.webpushEndpoint = webpushEndpoint; 35 this.service = service; 36 this.serviceData = serviceData; 37 } 38 toJSONObject()39 public JSONObject toJSONObject() throws JSONException { 40 final JSONObject jsonObject = new JSONObject(); 41 jsonObject.put("chid", chid); 42 jsonObject.put("profileName", profileName); 43 jsonObject.put("webpushEndpoint", webpushEndpoint); 44 jsonObject.put("service", service); 45 jsonObject.put("serviceData", serviceData); 46 return jsonObject; 47 } 48 fromJSONObject(@onNull JSONObject subscription)49 public static PushSubscription fromJSONObject(@NonNull JSONObject subscription) throws JSONException { 50 final String chid = subscription.getString("chid"); 51 final String profileName = subscription.getString("profileName"); 52 final String webpushEndpoint = subscription.getString("webpushEndpoint"); 53 final String service = subscription.getString("service"); 54 final JSONObject serviceData = subscription.optJSONObject("serviceData"); 55 return new PushSubscription(chid, profileName, webpushEndpoint, service, serviceData); 56 } 57 58 @Override equals(Object o)59 public boolean equals(Object o) { 60 // Auto-generated. 61 if (this == o) return true; 62 if (o == null || getClass() != o.getClass()) return false; 63 64 PushSubscription that = (PushSubscription) o; 65 66 if (!chid.equals(that.chid)) return false; 67 if (!profileName.equals(that.profileName)) return false; 68 if (!webpushEndpoint.equals(that.webpushEndpoint)) return false; 69 return service.equals(that.service); 70 } 71 72 @Override hashCode()73 public int hashCode() { 74 // Auto-generated. 75 int result = profileName.hashCode(); 76 result = 31 * result + chid.hashCode(); 77 result = 31 * result + webpushEndpoint.hashCode(); 78 result = 31 * result + service.hashCode(); 79 return result; 80 } 81 } 82