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 CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_SERVICE_IMPL_H_
6 #define CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_SERVICE_IMPL_H_
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/weak_ptr.h"
15 #include "chrome/browser/background/background_trigger.h"
16 #include "components/content_settings/core/browser/content_settings_observer.h"
17 #include "components/content_settings/core/common/content_settings.h"
18 #include "components/gcm_driver/common/gcm_messages.h"
19 #include "components/gcm_driver/gcm_app_handler.h"
20 #include "components/gcm_driver/gcm_client.h"
21 #include "components/keyed_service/core/keyed_service.h"
22 #include "content/public/browser/push_messaging_service.h"
23 #include "content/public/common/permission_status.mojom.h"
24 #include "content/public/common/push_messaging_status.h"
25 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushPermissionStatus.h"
27 #if defined(ENABLE_NOTIFICATIONS)
28 #include "chrome/browser/push_messaging/push_messaging_notification_manager.h"
32 class PushMessagingAppIdentifier
;
36 class GCMProfileService
;
39 namespace user_prefs
{
40 class PrefRegistrySyncable
;
43 class PushMessagingServiceImpl
: public content::PushMessagingService
,
44 public gcm::GCMAppHandler
,
45 public content_settings::Observer
,
47 public BackgroundTrigger
{
49 // Register profile-specific prefs for GCM.
50 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
52 // If any Service Workers are using push, starts GCM and adds an app handler.
53 static void InitializeForProfile(Profile
* profile
);
55 explicit PushMessagingServiceImpl(Profile
* profile
);
56 ~PushMessagingServiceImpl() override
;
58 // gcm::GCMAppHandler implementation.
59 void ShutdownHandler() override
;
60 void OnMessage(const std::string
& app_id
,
61 const gcm::IncomingMessage
& message
) override
;
62 void OnMessagesDeleted(const std::string
& app_id
) override
;
64 const std::string
& app_id
,
65 const gcm::GCMClient::SendErrorDetails
& send_error_details
) override
;
66 void OnSendAcknowledged(const std::string
& app_id
,
67 const std::string
& message_id
) override
;
68 bool CanHandle(const std::string
& app_id
) const override
;
70 // content::PushMessagingService implementation:
71 GURL
GetPushEndpoint() override
;
72 void SubscribeFromDocument(
73 const GURL
& requesting_origin
,
74 int64_t service_worker_registration_id
,
75 const std::string
& sender_id
,
79 const content::PushMessagingService::RegisterCallback
& callback
) override
;
80 void SubscribeFromWorker(
81 const GURL
& requesting_origin
,
82 int64_t service_worker_registration_id
,
83 const std::string
& sender_id
,
85 const content::PushMessagingService::RegisterCallback
& callback
) override
;
86 void GetPublicEncryptionKey(
88 int64_t service_worker_registration_id
,
89 const content::PushMessagingService::PublicKeyCallback
&
92 const GURL
& requesting_origin
,
93 int64_t service_worker_registration_id
,
94 const std::string
& sender_id
,
95 const content::PushMessagingService::UnregisterCallback
&) override
;
96 blink::WebPushPermissionStatus
GetPermissionStatus(
97 const GURL
& requesting_origin
,
98 const GURL
& embedding_origin
,
99 bool user_visible
) override
;
100 bool SupportNonVisibleMessages() override
;
103 // content_settings::Observer implementation.
104 void OnContentSettingChanged(const ContentSettingsPattern
& primary_pattern
,
105 const ContentSettingsPattern
& secondary_pattern
,
106 ContentSettingsType content_type
,
107 std::string resource_identifier
) override
;
109 // KeyedService implementation.
110 void Shutdown() override
;
112 // BackgroundTrigger implementation.
113 base::string16
GetName() override
;
114 gfx::ImageSkia
* GetIcon() override
;
115 void OnMenuClick() override
;
117 void SetMessageCallbackForTesting(const base::Closure
& callback
);
118 void SetContentSettingChangedCallbackForTesting(
119 const base::Closure
& callback
);
122 // A subscription is pending until it has succeeded or failed.
123 void IncreasePushSubscriptionCount(int add
, bool is_pending
);
124 void DecreasePushSubscriptionCount(int subtract
, bool was_pending
);
126 // OnMessage methods ---------------------------------------------------------
128 void DeliverMessageCallback(const std::string
& app_id
,
129 const GURL
& requesting_origin
,
130 int64 service_worker_registration_id
,
131 const gcm::IncomingMessage
& message
,
132 const base::Closure
& message_handled_closure
,
133 content::PushDeliveryStatus status
);
135 // Subscribe methods ---------------------------------------------------------
138 const content::PushMessagingService::RegisterCallback
& callback
,
139 const std::string
& subscription_id
,
140 const std::vector
<uint8_t>& curve25519dh
,
141 content::PushRegistrationStatus status
);
143 void SubscribeEndWithError(
144 const content::PushMessagingService::RegisterCallback
& callback
,
145 content::PushRegistrationStatus status
);
148 const PushMessagingAppIdentifier
& app_identifier
,
149 const content::PushMessagingService::RegisterCallback
& callback
,
150 const std::string
& subscription_id
,
151 gcm::GCMClient::Result result
);
153 void DidSubscribeWithPublicKey(
154 const PushMessagingAppIdentifier
& app_identifier
,
155 const content::PushMessagingService::RegisterCallback
& callback
,
156 const std::string
& subscription_id
,
157 const std::string
& public_key
);
159 void DidRequestPermission(
160 const PushMessagingAppIdentifier
& app_identifier
,
161 const std::string
& sender_id
,
162 const content::PushMessagingService::RegisterCallback
& callback
,
163 content::PermissionStatus permission_status
);
165 // GetPublicEncryptionKey method ---------------------------------------------
167 void DidGetPublicKey(
168 const PushMessagingService::PublicKeyCallback
& callback
,
169 const std::string
& public_key
) const;
172 // Unsubscribe methods -------------------------------------------------------
174 void Unsubscribe(const std::string
& app_id
,
175 const std::string
& sender_id
,
176 const content::PushMessagingService::UnregisterCallback
&);
178 void DidUnsubscribe(bool was_subscribed
,
179 const content::PushMessagingService::UnregisterCallback
&,
180 gcm::GCMClient::Result result
);
182 // OnContentSettingChanged methods -------------------------------------------
184 void UnsubscribeBecausePermissionRevoked(
185 const PushMessagingAppIdentifier
& app_identifier
,
186 const base::Closure
& closure
,
187 const std::string
& sender_id
,
191 // Helper methods ------------------------------------------------------------
193 // Checks if a given origin is allowed to use Push.
194 bool IsPermissionSet(const GURL
& origin
);
196 // Returns whether incoming messages should support payloads.
197 bool AreMessagePayloadsEnabled() const;
199 gcm::GCMDriver
* GetGCMDriver() const;
203 int push_subscription_count_
;
204 int pending_push_subscription_count_
;
206 base::Closure message_callback_for_testing_
;
207 base::Closure content_setting_changed_callback_for_testing_
;
209 #if defined(ENABLE_NOTIFICATIONS)
210 PushMessagingNotificationManager notification_manager_
;
213 // A multiset containing one entry for each in-flight push message delivery,
214 // keyed by the receiver's app id.
215 std::multiset
<std::string
> in_flight_message_deliveries_
;
217 base::WeakPtrFactory
<PushMessagingServiceImpl
> weak_factory_
;
219 DISALLOW_COPY_AND_ASSIGN(PushMessagingServiceImpl
);
222 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_SERVICE_IMPL_H_