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_SERVICES_GCM_PUSH_MESSAGING_SERVICE_IMPL_H_
6 #define CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_SERVICE_IMPL_H_
8 #include "base/compiler_specific.h"
9 #include "base/memory/weak_ptr.h"
10 #include "components/content_settings/core/browser/content_settings_observer.h"
11 #include "components/content_settings/core/common/content_settings.h"
12 #include "components/gcm_driver/gcm_app_handler.h"
13 #include "components/gcm_driver/gcm_client.h"
14 #include "content/public/browser/push_messaging_service.h"
15 #include "content/public/common/push_messaging_status.h"
16 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushPermissionStatus.h"
20 namespace user_prefs
{
21 class PrefRegistrySyncable
;
26 class GCMProfileService
;
27 class PushMessagingApplicationId
;
29 class PushMessagingServiceImpl
: public content::PushMessagingService
,
31 public content_settings::Observer
{
33 // Register profile-specific prefs for GCM.
34 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
36 // If any Service Workers are using push, starts GCM and adds an app handler.
37 static void InitializeForProfile(Profile
* profile
);
39 PushMessagingServiceImpl(GCMProfileService
* gcm_profile_service
,
41 ~PushMessagingServiceImpl() override
;
43 // GCMAppHandler implementation.
44 void ShutdownHandler() override
;
45 void OnMessage(const std::string
& app_id
,
46 const GCMClient::IncomingMessage
& message
) override
;
47 void OnMessagesDeleted(const std::string
& app_id
) override
;
49 const std::string
& app_id
,
50 const GCMClient::SendErrorDetails
& send_error_details
) override
;
51 void OnSendAcknowledged(const std::string
& app_id
,
52 const std::string
& message_id
) override
;
53 bool CanHandle(const std::string
& app_id
) const override
;
55 // content::PushMessagingService implementation:
56 GURL
GetPushEndpoint() override
;
57 void RegisterFromDocument(
58 const GURL
& requesting_origin
,
59 int64 service_worker_registration_id
,
60 const std::string
& sender_id
,
63 bool user_visible_only
,
64 const content::PushMessagingService::RegisterCallback
& callback
) override
;
65 void RegisterFromWorker(
66 const GURL
& requesting_origin
,
67 int64 service_worker_registration_id
,
68 const std::string
& sender_id
,
69 const content::PushMessagingService::RegisterCallback
& callback
) override
;
71 const GURL
& requesting_origin
,
72 int64 service_worker_registration_id
,
73 const std::string
& sender_id
,
74 bool retry_on_failure
,
75 const content::PushMessagingService::UnregisterCallback
&) override
;
76 blink::WebPushPermissionStatus
GetPermissionStatus(
77 const GURL
& requesting_origin
,
78 const GURL
& embedding_origin
) override
;
80 // content_settings::Observer implementation.
81 void OnContentSettingChanged(const ContentSettingsPattern
& primary_pattern
,
82 const ContentSettingsPattern
& secondary_pattern
,
83 ContentSettingsType content_type
,
84 std::string resource_identifier
) override
;
86 void SetProfileForTesting(Profile
* profile
);
89 // A registration is pending until it has succeeded or failed.
90 void IncreasePushRegistrationCount(int add
, bool is_pending
);
91 void DecreasePushRegistrationCount(int subtract
, bool was_pending
);
93 // OnMessage methods ---------------------------------------------------------
95 void DeliverMessageCallback(const std::string
& app_id_guid
,
96 const GURL
& requesting_origin
,
97 int64 service_worker_registration_id
,
98 const GCMClient::IncomingMessage
& message
,
99 content::PushDeliveryStatus status
);
101 // Developers are required to display a Web Notification in response to an
102 // incoming push message in order to clarify to the user that something has
103 // happened in the background. When they forget to do so, display a default
104 // notification on their behalf.
105 void RequireUserVisibleUX(const GURL
& requesting_origin
,
106 int64 service_worker_registration_id
);
107 void DidGetNotificationsShown(
108 const GURL
& requesting_origin
,
109 int64 service_worker_registration_id
,
110 bool notification_shown
,
111 bool notification_needed
,
112 const std::string
& data
,
116 // Register methods ----------------------------------------------------------
119 const content::PushMessagingService::RegisterCallback
& callback
,
120 const std::string
& registration_id
,
121 content::PushRegistrationStatus status
);
124 const PushMessagingApplicationId
& application_id
,
125 const content::PushMessagingService::RegisterCallback
& callback
,
126 const std::string
& registration_id
,
127 GCMClient::Result result
);
129 void DidRequestPermission(
130 const PushMessagingApplicationId
& application_id
,
131 const std::string
& sender_id
,
132 const content::PushMessagingService::RegisterCallback
& callback
,
133 ContentSetting content_setting
);
135 // Unregister methods --------------------------------------------------------
137 void Unregister(const std::string
& app_id_guid
,
138 const std::string
& sender_id
,
139 bool retry_on_failure
,
140 const content::PushMessagingService::UnregisterCallback
&);
142 void DidUnregister(const std::string
& app_id_guid
,
143 bool retry_on_failure
,
144 const content::PushMessagingService::UnregisterCallback
&,
145 GCMClient::Result result
);
147 // OnContentSettingChanged methods -------------------------------------------
149 void UnregisterBecausePermissionRevoked(const PushMessagingApplicationId
& id
,
150 const std::string
& sender_id
,
151 bool success
, bool not_found
);
153 // Helper methods ------------------------------------------------------------
155 // Checks if a given origin is allowed to use Push.
156 bool HasPermission(const GURL
& origin
);
158 GCMProfileService
* gcm_profile_service_
; // It owns us.
160 Profile
* profile_
; // It owns our owner.
162 int push_registration_count_
;
163 int pending_push_registration_count_
;
165 base::WeakPtrFactory
<PushMessagingServiceImpl
> weak_factory_
;
167 DISALLOW_COPY_AND_ASSIGN(PushMessagingServiceImpl
);
172 #endif // CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_SERVICE_IMPL_H_