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 "components/content_settings/core/browser/content_settings_observer.h"
16 #include "components/content_settings/core/common/content_settings.h"
17 #include "components/gcm_driver/common/gcm_messages.h"
18 #include "components/gcm_driver/gcm_app_handler.h"
19 #include "components/gcm_driver/gcm_client.h"
20 #include "components/keyed_service/core/keyed_service.h"
21 #include "content/public/browser/push_messaging_service.h"
22 #include "content/public/common/permission_status.mojom.h"
23 #include "content/public/common/push_messaging_status.h"
24 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushPermissionStatus.h"
26 #if defined(ENABLE_NOTIFICATIONS)
27 #include "chrome/browser/push_messaging/push_messaging_notification_manager.h"
31 class PushMessagingAppIdentifier
;
35 class GCMProfileService
;
38 namespace user_prefs
{
39 class PrefRegistrySyncable
;
42 class PushMessagingServiceImpl
: public content::PushMessagingService
,
43 public gcm::GCMAppHandler
,
44 public content_settings::Observer
,
47 // Register profile-specific prefs for GCM.
48 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
50 // If any Service Workers are using push, starts GCM and adds an app handler.
51 static void InitializeForProfile(Profile
* profile
);
53 explicit PushMessagingServiceImpl(Profile
* profile
);
54 ~PushMessagingServiceImpl() override
;
56 // gcm::GCMAppHandler implementation.
57 void ShutdownHandler() override
;
58 void OnMessage(const std::string
& app_id
,
59 const gcm::IncomingMessage
& message
) override
;
60 void OnMessagesDeleted(const std::string
& app_id
) override
;
62 const std::string
& app_id
,
63 const gcm::GCMClient::SendErrorDetails
& send_error_details
) override
;
64 void OnSendAcknowledged(const std::string
& app_id
,
65 const std::string
& message_id
) override
;
66 bool CanHandle(const std::string
& app_id
) const override
;
68 // content::PushMessagingService implementation:
69 GURL
GetPushEndpoint() override
;
70 void SubscribeFromDocument(
71 const GURL
& requesting_origin
,
72 int64_t service_worker_registration_id
,
73 const std::string
& sender_id
,
77 const content::PushMessagingService::RegisterCallback
& callback
) override
;
78 void SubscribeFromWorker(
79 const GURL
& requesting_origin
,
80 int64_t service_worker_registration_id
,
81 const std::string
& sender_id
,
83 const content::PushMessagingService::RegisterCallback
& callback
) override
;
84 void GetPublicEncryptionKey(
86 int64_t service_worker_registration_id
,
87 const content::PushMessagingService::PublicKeyCallback
&
90 const GURL
& requesting_origin
,
91 int64_t service_worker_registration_id
,
92 const std::string
& sender_id
,
93 const content::PushMessagingService::UnregisterCallback
&) override
;
94 blink::WebPushPermissionStatus
GetPermissionStatus(
95 const GURL
& requesting_origin
,
96 const GURL
& embedding_origin
,
97 bool user_visible
) override
;
98 bool SupportNonVisibleMessages() override
;
101 // content_settings::Observer implementation.
102 void OnContentSettingChanged(const ContentSettingsPattern
& primary_pattern
,
103 const ContentSettingsPattern
& secondary_pattern
,
104 ContentSettingsType content_type
,
105 std::string resource_identifier
) override
;
107 // KeyedService implementation.
108 void Shutdown() override
;
110 void SetMessageCallbackForTesting(const base::Closure
& callback
);
111 void SetContentSettingChangedCallbackForTesting(
112 const base::Closure
& callback
);
115 // A subscription is pending until it has succeeded or failed.
116 void IncreasePushSubscriptionCount(int add
, bool is_pending
);
117 void DecreasePushSubscriptionCount(int subtract
, bool was_pending
);
119 // OnMessage methods ---------------------------------------------------------
121 void DeliverMessageCallback(const std::string
& app_id
,
122 const GURL
& requesting_origin
,
123 int64 service_worker_registration_id
,
124 const gcm::IncomingMessage
& message
,
125 const base::Closure
& message_handled_closure
,
126 content::PushDeliveryStatus status
);
128 // Subscribe methods ---------------------------------------------------------
131 const content::PushMessagingService::RegisterCallback
& callback
,
132 const std::string
& subscription_id
,
133 const std::vector
<uint8_t>& curve25519dh
,
134 content::PushRegistrationStatus status
);
136 void SubscribeEndWithError(
137 const content::PushMessagingService::RegisterCallback
& callback
,
138 content::PushRegistrationStatus status
);
141 const PushMessagingAppIdentifier
& app_identifier
,
142 const content::PushMessagingService::RegisterCallback
& callback
,
143 const std::string
& subscription_id
,
144 gcm::GCMClient::Result result
);
146 void DidRequestPermission(
147 const PushMessagingAppIdentifier
& app_identifier
,
148 const std::string
& sender_id
,
149 const content::PushMessagingService::RegisterCallback
& callback
,
150 content::PermissionStatus permission_status
);
152 // Unsubscribe methods -------------------------------------------------------
154 void Unsubscribe(const std::string
& app_id
,
155 const std::string
& sender_id
,
156 const content::PushMessagingService::UnregisterCallback
&);
158 void DidUnsubscribe(bool was_subscribed
,
159 const content::PushMessagingService::UnregisterCallback
&,
160 gcm::GCMClient::Result result
);
162 // OnContentSettingChanged methods -------------------------------------------
164 void UnsubscribeBecausePermissionRevoked(
165 const PushMessagingAppIdentifier
& app_identifier
,
166 const base::Closure
& closure
,
167 const std::string
& sender_id
,
171 // Helper methods ------------------------------------------------------------
173 // Checks if a given origin is allowed to use Push.
174 bool IsPermissionSet(const GURL
& origin
);
176 gcm::GCMDriver
* GetGCMDriver() const;
180 int push_subscription_count_
;
181 int pending_push_subscription_count_
;
183 base::Closure message_callback_for_testing_
;
184 base::Closure content_setting_changed_callback_for_testing_
;
186 #if defined(ENABLE_NOTIFICATIONS)
187 PushMessagingNotificationManager notification_manager_
;
190 // A multiset containing one entry for each in-flight push message delivery,
191 // keyed by the receiver's app id.
192 std::multiset
<std::string
> in_flight_message_deliveries_
;
194 base::WeakPtrFactory
<PushMessagingServiceImpl
> weak_factory_
;
196 DISALLOW_COPY_AND_ASSIGN(PushMessagingServiceImpl
);
199 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_SERVICE_IMPL_H_