summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/push_messaging/push_messaging_client.cc
blob: 95f4c71dbab64c9af472ad0a9798a52a3de82a67 (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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/push_messaging/push_messaging_client.h"

#include <memory>
#include <utility>

#include "base/bind_helpers.h"
#include "base/strings/utf_string_conversions.h"
#include "content/child/child_thread_impl.h"
#include "content/common/push_messaging.mojom.h"
#include "content/public/common/push_messaging_status.mojom.h"
#include "content/public/common/service_names.mojom.h"
#include "content/renderer/push_messaging/push_provider.h"
#include "content/renderer/render_frame_impl.h"
#include "content/renderer/service_worker/web_service_worker_registration_impl.h"
#include "services/service_manager/public/cpp/connector.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/modules/manifest/manifest_manager.mojom.h"
#include "third_party/WebKit/public/platform/modules/push_messaging/WebPushError.h"
#include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubscription.h"
#include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubscriptionOptions.h"
#include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerRegistration.h"
#include "third_party/WebKit/public/web/WebConsoleMessage.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "url/gurl.h"

namespace content {

PushMessagingClient::PushMessagingClient(RenderFrame* render_frame)
    : RenderFrameObserver(render_frame) {
  if (ChildThreadImpl::current()) {
    ChildThreadImpl::current()->GetConnector()->BindInterface(
        mojom::kBrowserServiceName,
        mojo::MakeRequest(&push_messaging_manager_));
  }
}

PushMessagingClient::~PushMessagingClient() {}

void PushMessagingClient::OnDestruct() {
  delete this;
}

void PushMessagingClient::Subscribe(
    blink::WebServiceWorkerRegistration* service_worker_registration,
    const blink::WebPushSubscriptionOptions& options,
    bool user_gesture,
    std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks) {
  DCHECK(service_worker_registration);
  DCHECK(callbacks);

  // If a developer provided an application server key in |options|, skip
  // fetching the manifest.
  if (options.application_server_key.IsEmpty()) {
    RenderFrameImpl::FromRoutingID(routing_id())
        ->GetManifestManager()
        .RequestManifest(base::BindOnce(&PushMessagingClient::DidGetManifest,
                                        base::Unretained(this),
                                        service_worker_registration, options,
                                        user_gesture, std::move(callbacks)));
  } else {
    PushSubscriptionOptions content_options;
    content_options.user_visible_only = options.user_visible_only;
    // Just treat the server key as a string of bytes and pass it to the push
    // service.
    content_options.sender_info = options.application_server_key.Latin1();
    DoSubscribe(service_worker_registration, content_options, user_gesture,
                std::move(callbacks));
  }
}

void PushMessagingClient::DidGetManifest(
    blink::WebServiceWorkerRegistration* service_worker_registration,
    const blink::WebPushSubscriptionOptions& options,
    bool user_gesture,
    std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks,
    const GURL& manifest_url,
    const Manifest& manifest) {
  // Get the sender_info from the manifest since it wasn't provided by
  // the caller.
  if (manifest.IsEmpty()) {
    DidSubscribe(std::move(callbacks),
                 mojom::PushRegistrationStatus::MANIFEST_EMPTY_OR_MISSING,
                 base::nullopt, base::nullopt, base::nullopt, base::nullopt);
    return;
  }

  PushSubscriptionOptions content_options;
  content_options.user_visible_only = options.user_visible_only;
  if (!manifest.gcm_sender_id.is_null()) {
    content_options.sender_info =
        base::UTF16ToUTF8(manifest.gcm_sender_id.string());
  }

  DoSubscribe(service_worker_registration, content_options, user_gesture,
              std::move(callbacks));
}

void PushMessagingClient::DoSubscribe(
    blink::WebServiceWorkerRegistration* service_worker_registration,
    const PushSubscriptionOptions& options,
    bool user_gesture,
    std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks) {
  int64_t service_worker_registration_id =
      static_cast<WebServiceWorkerRegistrationImpl*>(
          service_worker_registration)
          ->RegistrationId();

  if (options.sender_info.empty()) {
    DidSubscribe(std::move(callbacks),
                 mojom::PushRegistrationStatus::NO_SENDER_ID, base::nullopt,
                 base::nullopt, base::nullopt, base::nullopt);
    return;
  }

  DCHECK(push_messaging_manager_);
  push_messaging_manager_->Subscribe(
      routing_id(), service_worker_registration_id, options, user_gesture,
      // Safe to use base::Unretained because |push_messaging_manager_ |is
      // owned by |this|.
      base::BindOnce(&PushMessagingClient::DidSubscribe, base::Unretained(this),
                     std::move(callbacks)));
}

void PushMessagingClient::DidSubscribe(
    std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks,
    mojom::PushRegistrationStatus status,
    const base::Optional<GURL>& endpoint,
    const base::Optional<PushSubscriptionOptions>& options,
    const base::Optional<std::vector<uint8_t>>& p256dh,
    const base::Optional<std::vector<uint8_t>>& auth) {
  DCHECK(callbacks);

  if (status == mojom::PushRegistrationStatus::SUCCESS_FROM_PUSH_SERVICE ||
      status == mojom::PushRegistrationStatus::
                    SUCCESS_NEW_SUBSCRIPTION_FROM_PUSH_SERVICE ||
      status == mojom::PushRegistrationStatus::SUCCESS_FROM_CACHE) {
    DCHECK(endpoint);
    DCHECK(options);
    DCHECK(p256dh);
    DCHECK(auth);

    callbacks->OnSuccess(std::make_unique<blink::WebPushSubscription>(
        endpoint.value(), options.value().user_visible_only,
        blink::WebString::FromLatin1(options.value().sender_info),
        p256dh.value(), auth.value()));
  } else {
    callbacks->OnError(PushRegistrationStatusToWebPushError(status));
  }
}

}  // namespace content