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_
8 #include "base/callback.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/weak_ptr.h"
11 #include "components/content_settings/core/browser/content_settings_observer.h"
12 #include "components/content_settings/core/common/content_settings.h"
13 #include "components/gcm_driver/gcm_app_handler.h"
14 #include "components/gcm_driver/gcm_client.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 #include "content/public/browser/push_messaging_service.h"
17 #include "content/public/common/push_messaging_status.h"
18 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushPermissionStatus.h"
21 class PushMessagingApplicationId
;
23 namespace user_prefs
{
24 class PrefRegistrySyncable
;
29 class GCMProfileService
;
32 class PushMessagingServiceImpl
: public content::PushMessagingService
,
33 public gcm::GCMAppHandler
,
34 public content_settings::Observer
,
37 // Register profile-specific prefs for GCM.
38 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
40 // If any Service Workers are using push, starts GCM and adds an app handler.
41 static void InitializeForProfile(Profile
* profile
);
43 explicit PushMessagingServiceImpl(Profile
* profile
);
44 ~PushMessagingServiceImpl() override
;
46 // gcm::GCMAppHandler implementation.
47 void ShutdownHandler() override
;
48 void OnMessage(const std::string
& app_id
,
49 const gcm::GCMClient::IncomingMessage
& message
) override
;
50 void OnMessagesDeleted(const std::string
& app_id
) override
;
52 const std::string
& app_id
,
53 const gcm::GCMClient::SendErrorDetails
& send_error_details
) override
;
54 void OnSendAcknowledged(const std::string
& app_id
,
55 const std::string
& message_id
) override
;
56 bool CanHandle(const std::string
& app_id
) const override
;
58 // content::PushMessagingService implementation:
59 GURL
GetPushEndpoint() override
;
60 void RegisterFromDocument(
61 const GURL
& requesting_origin
,
62 int64 service_worker_registration_id
,
63 const std::string
& sender_id
,
67 const content::PushMessagingService::RegisterCallback
& callback
) override
;
68 void RegisterFromWorker(
69 const GURL
& requesting_origin
,
70 int64 service_worker_registration_id
,
71 const std::string
& sender_id
,
73 const content::PushMessagingService::RegisterCallback
& callback
) override
;
75 const GURL
& requesting_origin
,
76 int64 service_worker_registration_id
,
77 const std::string
& sender_id
,
78 const content::PushMessagingService::UnregisterCallback
&) override
;
79 blink::WebPushPermissionStatus
GetPermissionStatus(
80 const GURL
& requesting_origin
,
81 const GURL
& embedding_origin
,
82 bool user_visible
) override
;
84 // content_settings::Observer implementation.
85 void OnContentSettingChanged(const ContentSettingsPattern
& primary_pattern
,
86 const ContentSettingsPattern
& secondary_pattern
,
87 ContentSettingsType content_type
,
88 std::string resource_identifier
) override
;
90 // KeyedService implementation.
91 void Shutdown() override
;
93 void SetContentSettingChangedCallbackForTesting(
94 const base::Closure
& callback
);
97 // A registration is pending until it has succeeded or failed.
98 void IncreasePushRegistrationCount(int add
, bool is_pending
);
99 void DecreasePushRegistrationCount(int subtract
, bool was_pending
);
101 // OnMessage methods ---------------------------------------------------------
103 void DeliverMessageCallback(const std::string
& app_id_guid
,
104 const GURL
& requesting_origin
,
105 int64 service_worker_registration_id
,
106 const gcm::GCMClient::IncomingMessage
& message
,
107 content::PushDeliveryStatus status
);
109 // Developers are required to display a Web Notification in response to an
110 // incoming push message in order to clarify to the user that something has
111 // happened in the background. When they forget to do so, display a default
112 // notification on their behalf.
113 void RequireUserVisibleUX(const GURL
& requesting_origin
,
114 int64 service_worker_registration_id
);
115 void DidGetNotificationsShown(
116 const GURL
& requesting_origin
,
117 int64 service_worker_registration_id
,
118 bool notification_shown
,
119 bool notification_needed
,
120 const std::string
& data
,
124 // Register methods ----------------------------------------------------------
127 const content::PushMessagingService::RegisterCallback
& callback
,
128 const std::string
& registration_id
,
129 content::PushRegistrationStatus status
);
132 const PushMessagingApplicationId
& application_id
,
133 const content::PushMessagingService::RegisterCallback
& callback
,
134 const std::string
& registration_id
,
135 gcm::GCMClient::Result result
);
137 void DidRequestPermission(
138 const PushMessagingApplicationId
& application_id
,
139 const std::string
& sender_id
,
140 const content::PushMessagingService::RegisterCallback
& callback
,
141 ContentSetting content_setting
);
143 // Unregister methods --------------------------------------------------------
145 void Unregister(const std::string
& app_id_guid
,
146 const std::string
& sender_id
,
147 const content::PushMessagingService::UnregisterCallback
&);
149 void DidUnregister(bool was_registered
,
150 const content::PushMessagingService::UnregisterCallback
&,
151 gcm::GCMClient::Result result
);
153 // OnContentSettingChanged methods -------------------------------------------
155 void UnregisterBecausePermissionRevoked(const PushMessagingApplicationId
& id
,
156 const base::Closure
& closure
,
157 const std::string
& sender_id
,
158 bool success
, bool not_found
);
160 // Helper methods ------------------------------------------------------------
162 // Checks if a given origin is allowed to use Push.
163 bool HasPermission(const GURL
& origin
);
165 gcm::GCMDriver
* GetGCMDriver() const;
169 int push_registration_count_
;
170 int pending_push_registration_count_
;
172 base::Closure content_setting_changed_callback_for_testing_
;
174 base::WeakPtrFactory
<PushMessagingServiceImpl
> weak_factory_
;
176 DISALLOW_COPY_AND_ASSIGN(PushMessagingServiceImpl
);
179 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_SERVICE_IMPL_H_