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.
5 #ifndef CONTENT_PUBLIC_BROWSER_PUSH_MESSAGING_SERVICE_H_
6 #define CONTENT_PUBLIC_BROWSER_PUSH_MESSAGING_SERVICE_H_
12 #include "base/callback_forward.h"
13 #include "content/common/content_export.h"
14 #include "content/public/common/push_messaging_status.h"
15 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushPermissionStatus.h"
21 class ServiceWorkerContext
;
23 // A push service-agnostic interface that the Push API uses for talking to
24 // push messaging services like GCM. Must only be used on the UI thread.
25 class CONTENT_EXPORT PushMessagingService
{
27 using RegisterCallback
=
28 base::Callback
<void(const std::string
& registration_id
,
29 const std::vector
<uint8_t>& curve25519dh
,
30 PushRegistrationStatus status
)>;
31 using UnregisterCallback
= base::Callback
<void(PushUnregistrationStatus
)>;
33 using PublicKeyCallback
= base::Callback
<void(
35 const std::vector
<uint8_t>& curve25519dh
)>;
37 using StringCallback
= base::Callback
<void(const std::string
& data
,
41 using ResultCallback
= base::Callback
<void(bool success
)>;
43 virtual ~PushMessagingService() {}
45 // Returns the absolute URL exposed by the push server where the webapp server
46 // can send push messages. This is currently assumed to be the same for all
47 // origins and push registrations.
48 virtual GURL
GetPushEndpoint() = 0;
50 // Subscribe the given |sender_id| with the push messaging service in a
51 // document context. The frame is known and a permission UI may be displayed
53 virtual void SubscribeFromDocument(const GURL
& requesting_origin
,
54 int64_t service_worker_registration_id
,
55 const std::string
& sender_id
,
59 const RegisterCallback
& callback
) = 0;
61 // Subscribe the given |sender_id| with the push messaging service. The frame
62 // is not known so if permission was not previously granted by the user this
63 // request should fail.
64 virtual void SubscribeFromWorker(const GURL
& requesting_origin
,
65 int64_t service_worker_registration_id
,
66 const std::string
& sender_id
,
68 const RegisterCallback
& callback
) = 0;
70 // Retrieves the public encryption key associated with |origin| and
71 // |service_worker_registration_id|, and invokes |callback| with the result
72 // when it is available.
73 virtual void GetPublicEncryptionKey(const GURL
& origin
,
74 int64_t service_worker_registration_id
,
75 const PublicKeyCallback
& callback
) = 0;
77 // Unsubscribe the given |sender_id| from the push messaging service. The
78 // subscription will be synchronously deactivated locally, and asynchronously
79 // sent to the push service, with automatic retry.
80 virtual void Unsubscribe(const GURL
& requesting_origin
,
81 int64_t service_worker_registration_id
,
82 const std::string
& sender_id
,
83 const UnregisterCallback
& callback
) = 0;
85 // Checks the permission status for the requesting origin. Permission is only
86 // ever granted when the requesting origin matches the top level embedding
87 // origin. The |user_visible| boolean indicates whether the permission status
88 // only has to cover push messages resulting in visible effects to the user.
89 virtual blink::WebPushPermissionStatus
GetPermissionStatus(
90 const GURL
& requesting_origin
,
91 const GURL
& embedding_origin
,
92 bool user_visible
) = 0;
94 // Returns whether subscriptions that do not mandate user visible UI upon
95 // receiving a push message are supported. Influences permission request and
96 // permission check behaviour.
97 virtual bool SupportNonVisibleMessages() = 0;
99 // Provide a storage mechanism to read/write an opaque
100 // "notifications_shown_by_last_few_pushes" string associated with a Service
101 // Worker registration. Stored data is deleted when the associated
102 // registration is deleted.
103 static void GetNotificationsShownByLastFewPushes(
104 ServiceWorkerContext
* service_worker_context
,
105 int64_t service_worker_registration_id
,
106 const StringCallback
& callback
);
107 static void SetNotificationsShownByLastFewPushes(
108 ServiceWorkerContext
* service_worker_context
,
109 int64_t service_worker_registration_id
,
111 const std::string
& notifications_shown
,
112 const ResultCallback
& callback
);
115 static void GetSenderId(BrowserContext
* browser_context
,
117 int64_t service_worker_registration_id
,
118 const StringCallback
& callback
);
120 // Clear the push subscription id stored in the service worker with the given
121 // |service_worker_registration_id| for the given |origin|.
122 static void ClearPushSubscriptionID(BrowserContext
* browser_context
,
124 int64_t service_worker_registration_id
,
125 const base::Closure
& callback
);
128 } // namespace content
130 #endif // CONTENT_PUBLIC_BROWSER_PUSH_MESSAGING_SERVICE_H_