summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebPushController.java
blob: f5ea153bfef7e1ec0b3266ca7fb22bcd7ea733ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * vim: ts=4 sw=4 expandtab:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.geckoview;

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.GeckoThread;
import org.mozilla.gecko.util.BundleEventListener;
import org.mozilla.gecko.util.EventCallback;
import org.mozilla.gecko.util.GeckoBundle;
import org.mozilla.gecko.util.ThreadUtils;

public class WebPushController {
  private static final String LOGTAG = "WebPushController";

  private WebPushDelegate mDelegate;
  private BundleEventListener mEventListener;

  /* package */ WebPushController() {
    mEventListener = new EventListener();
    EventDispatcher.getInstance()
        .registerUiThreadListener(
            mEventListener,
            "GeckoView:PushSubscribe",
            "GeckoView:PushUnsubscribe",
            "GeckoView:PushGetSubscription");
  }

  /**
   * Sets the {@link WebPushDelegate} for this instance.
   *
   * @param delegate The {@link WebPushDelegate} instance.
   */
  @UiThread
  public void setDelegate(final @Nullable WebPushDelegate delegate) {
    ThreadUtils.assertOnUiThread();
    mDelegate = delegate;
  }

  /**
   * Gets the {@link WebPushDelegate} for this instance.
   *
   * @return delegate The {@link WebPushDelegate} instance.
   */
  @UiThread
  @Nullable
  public WebPushDelegate getDelegate() {
    ThreadUtils.assertOnUiThread();
    return mDelegate;
  }

  /**
   * Send a push event for a given subscription.
   *
   * @param scope The Service Worker scope associated with this subscription.
   */
  @UiThread
  public void onPushEvent(final @NonNull String scope) {
    ThreadUtils.assertOnUiThread();
    onPushEvent(scope, null);
  }

  /**
   * Send a push event with a payload for a given subscription.
   *
   * @param scope The Service Worker scope associated with this subscription.
   * @param data The unencrypted payload.
   */
  @UiThread
  public void onPushEvent(final @NonNull String scope, final @Nullable byte[] data) {
    ThreadUtils.assertOnUiThread();

    GeckoThread.waitForState(GeckoThread.State.JNI_READY)
        .accept(
            val -> {
              final GeckoBundle msg = new GeckoBundle(2);
              msg.putString("scope", scope);
              msg.putString("data", Base64Utils.encode(data));
              EventDispatcher.getInstance().dispatch("GeckoView:PushEvent", msg);
            },
            e -> Log.e(LOGTAG, "Unable to deliver Web Push message", e));
  }

  /**
   * Notify that a given subscription has changed. This is normally a signal to the content that it
   * needs to re-subscribe.
   *
   * @param scope The Service Worker scope associated with this subscription.
   */
  @UiThread
  public void onSubscriptionChanged(final @NonNull String scope) {
    ThreadUtils.assertOnUiThread();

    final GeckoBundle msg = new GeckoBundle(1);
    msg.putString("scope", scope);
    EventDispatcher.getInstance().dispatch("GeckoView:PushSubscriptionChanged", msg);
  }

  private class EventListener implements BundleEventListener {

    @Override
    public void handleMessage(
        final String event, final GeckoBundle message, final EventCallback callback) {
      if (mDelegate == null) {
        callback.sendError("Not allowed");
        return;
      }

      switch (event) {
        case "GeckoView:PushSubscribe":
          {
            byte[] appServerKey = null;
            if (message.containsKey("appServerKey")) {
              appServerKey = Base64Utils.decode(message.getString("appServerKey"));
            }

            final GeckoResult<WebPushSubscription> result =
                mDelegate.onSubscribe(message.getString("scope"), appServerKey);

            if (result == null) {
              callback.sendSuccess(null);
              return;
            }

            result.accept(
                subscription ->
                    callback.sendSuccess(subscription != null ? subscription.toBundle() : null),
                error -> callback.sendSuccess(null));
            break;
          }
        case "GeckoView:PushUnsubscribe":
          {
            final GeckoResult<Void> result = mDelegate.onUnsubscribe(message.getString("scope"));
            if (result == null) {
              callback.sendSuccess(null);
              return;
            }

            callback.resolveTo(result.map(val -> null));
            break;
          }
        case "GeckoView:PushGetSubscription":
          {
            final GeckoResult<WebPushSubscription> result =
                mDelegate.onGetSubscription(message.getString("scope"));
            if (result == null) {
              callback.sendSuccess(null);
              return;
            }

            callback.resolveTo(
                result.map(subscription -> subscription != null ? subscription.toBundle() : null));
            break;
          }
      }
    }
  }
}