summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/push_messaging/push_provider.h
blob: 223d4cbc8b1dc752cac605b5c3aa3c5bcf1642ab (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
// 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.

#ifndef CONTENT_RENDERER_PUSH_MESSAGING_PUSH_PROVIDER_H_
#define CONTENT_RENDERER_PUSH_MESSAGING_PUSH_PROVIDER_H_

#include <stdint.h>

#include <memory>
#include <string>
#include <vector>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "content/common/push_messaging.mojom.h"
#include "content/public/renderer/worker_thread.h"
#include "third_party/blink/public/platform/modules/push_messaging/web_push_error.h"
#include "third_party/blink/public/platform/modules/push_messaging/web_push_provider.h"

class GURL;

namespace blink {
struct WebPushSubscriptionOptions;
}

namespace content {

namespace mojom {
enum class PushGetRegistrationStatus;
enum class PushRegistrationStatus;
}  // namespace mojom

struct PushSubscriptionOptions;

blink::WebPushError PushRegistrationStatusToWebPushError(
    mojom::PushRegistrationStatus status);

class PushProvider : public blink::WebPushProvider,
                     public WorkerThread::Observer {
 public:
  ~PushProvider() override;

  static PushProvider* ThreadSpecificInstance(
      const scoped_refptr<base::SingleThreadTaskRunner>&
          main_thread_task_runner);

  // WorkerThread::Observer implementation.
  void WillStopCurrentWorkerThread() override;

  // blink::WebPushProvider implementation.
  void Subscribe(
      blink::WebServiceWorkerRegistration* service_worker_registration,
      const blink::WebPushSubscriptionOptions& options,
      bool user_gesture,
      std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks) override;
  void Unsubscribe(
      blink::WebServiceWorkerRegistration* service_worker_registration,
      std::unique_ptr<blink::WebPushUnsubscribeCallbacks> callbacks) override;
  void GetSubscription(
      blink::WebServiceWorkerRegistration* service_worker_registration,
      std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks) override;

 private:
  explicit PushProvider(const scoped_refptr<base::SingleThreadTaskRunner>&
                            main_thread_task_runner);

  static void GetInterface(mojom::PushMessagingRequest request);

  void 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);

  void DidUnsubscribe(
      std::unique_ptr<blink::WebPushUnsubscribeCallbacks> callbacks,
      blink::WebPushError::ErrorType error_type,
      bool did_unsubscribe,
      const base::Optional<std::string>& error_message);

  void DidGetSubscription(
      std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks,
      mojom::PushGetRegistrationStatus 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);

  mojom::PushMessagingPtr push_messaging_manager_;

  DISALLOW_COPY_AND_ASSIGN(PushProvider);
};

}  // namespace content

#endif  // CONTENT_RENDERER_PUSH_MESSAGING_PUSH_PROVIDER_H_