Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / gcm_driver / android / java / src / org / chromium / components / gcm_driver / GCMListener.java
1 // Copyright 2014 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.content.Intent;
8 import android.os.Bundle;
9
10 import com.google.ipc.invalidation.external.client.contrib.MultiplexingGcmListener;
11
12 import org.chromium.base.ThreadUtils;
13
14 /**
15  * Receives GCM registration events and messages rebroadcast by MultiplexingGcmListener.
16  */
17 public class GCMListener extends MultiplexingGcmListener.AbstractListener {
18     /**
19      * Receiver for broadcasts by the multiplexed GCM service. It forwards them to
20      * GCMListener.
21      *
22      * This class is public so that it can be instantiated by the Android runtime.
23      */
24     public static class Receiver extends MultiplexingGcmListener.AbstractListener.Receiver {
25         @Override
26         protected Class<?> getServiceClass() {
27             return GCMListener.class;
28         }
29     }
30
31     private static final String TAG = "GCMListener";
32
33     // Used as a fallback until GCM always gives us the subtype.
34     // TODO(johnme): Remove this once it does.
35     static final String UNKNOWN_APP_ID = "push#https://www.gcmlistenerfake.com#0";
36
37     public GCMListener() {
38         super(TAG);
39     }
40
41     @Override
42     protected void onRegistered(String registrationId) {
43         // Ignore this, since we register using GoogleCloudMessagingV2.
44     }
45
46     @Override
47     protected void onUnregistered(String registrationId) {
48         // Ignore this, since we register using GoogleCloudMessagingV2.
49     }
50
51     @Override
52     protected void onMessage(final Intent intent) {
53         final String bundleSubtype = "subtype";
54         final String bundleDataForPushApi = "data";
55         Bundle extras = intent.getExtras();
56         if (!extras.containsKey(bundleSubtype) && !extras.containsKey(bundleDataForPushApi)) {
57             // TODO(johnme): Once GCM always gives us the subtype, we'll be able to early-out if
58             // there is no subtype extra. For now we have to also allow messages without subtype
59             // if they have the data key which suggests they are for the Push API, but this is
60             // technically a layering violation as this code is for other consumers of GCM too.
61             return;
62         }
63         final String appId = extras.containsKey(bundleSubtype) ? extras.getString(bundleSubtype)
64                                                                : UNKNOWN_APP_ID;
65         ThreadUtils.runOnUiThread(new Runnable() {
66             @Override public void run() {
67                 GCMDriver.onMessageReceived(getApplicationContext(), appId,
68                         intent.getExtras());
69             }
70         });
71     }
72
73     @Override
74     protected void onDeletedMessages(int total) {
75         ThreadUtils.runOnUiThread(new Runnable() {
76             @Override public void run() {
77                 GCMDriver.onMessagesDeleted(getApplicationContext(), UNKNOWN_APP_ID);
78             }
79         });
80     }
81 }