Popular sites on the NTP: Try to keep the ordering constant
[chromium-blink-merge.git] / content / public / browser / push_messaging_service.h
blob4e533d1d2fcb7602e05300f4b33e933aec0316cd
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_
8 #include <stdint.h>
9 #include <string>
10 #include <vector>
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"
16 #include "url/gurl.h"
18 namespace content {
20 class BrowserContext;
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 {
26 public:
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(
34 bool success,
35 const std::vector<uint8_t>& curve25519dh)>;
37 using StringCallback = base::Callback<void(const std::string& data,
38 bool success,
39 bool not_found)>;
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
52 // to the user.
53 virtual void SubscribeFromDocument(const GURL& requesting_origin,
54 int64_t service_worker_registration_id,
55 const std::string& sender_id,
56 int renderer_id,
57 int render_frame_id,
58 bool user_visible,
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,
67 bool user_visible,
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,
110 const GURL& origin,
111 const std::string& notifications_shown,
112 const ResultCallback& callback);
114 protected:
115 static void GetSenderId(BrowserContext* browser_context,
116 const GURL& origin,
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,
123 const GURL& origin,
124 int64_t service_worker_registration_id,
125 const base::Closure& callback);
128 } // namespace content
130 #endif // CONTENT_PUBLIC_BROWSER_PUSH_MESSAGING_SERVICE_H_