Cleanup PushMessagingAppIdentifier
[chromium-blink-merge.git] / chrome / browser / push_messaging / push_messaging_service_impl.h
blob4919c0fd4e79f9ca6dc6f6810bf6d22af6cae873
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 <stdint.h>
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/weak_ptr.h"
13 #include "components/content_settings/core/browser/content_settings_observer.h"
14 #include "components/content_settings/core/common/content_settings.h"
15 #include "components/gcm_driver/gcm_app_handler.h"
16 #include "components/gcm_driver/gcm_client.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "content/public/browser/push_messaging_service.h"
19 #include "content/public/common/push_messaging_status.h"
20 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushPermissionStatus.h"
22 #if defined(ENABLE_NOTIFICATIONS)
23 #include "chrome/browser/push_messaging/push_messaging_notification_manager.h"
24 #endif
26 class Profile;
27 class PushMessagingAppIdentifier;
29 namespace gcm {
30 class GCMDriver;
31 class GCMProfileService;
34 namespace user_prefs {
35 class PrefRegistrySyncable;
38 class PushMessagingServiceImpl : public content::PushMessagingService,
39 public gcm::GCMAppHandler,
40 public content_settings::Observer,
41 public KeyedService {
42 public:
43 // Register profile-specific prefs for GCM.
44 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
46 // If any Service Workers are using push, starts GCM and adds an app handler.
47 static void InitializeForProfile(Profile* profile);
49 explicit PushMessagingServiceImpl(Profile* profile);
50 ~PushMessagingServiceImpl() override;
52 // gcm::GCMAppHandler implementation.
53 void ShutdownHandler() override;
54 void OnMessage(const std::string& app_id,
55 const gcm::GCMClient::IncomingMessage& message) override;
56 void OnMessagesDeleted(const std::string& app_id) override;
57 void OnSendError(
58 const std::string& app_id,
59 const gcm::GCMClient::SendErrorDetails& send_error_details) override;
60 void OnSendAcknowledged(const std::string& app_id,
61 const std::string& message_id) override;
62 bool CanHandle(const std::string& app_id) const override;
64 // content::PushMessagingService implementation:
65 GURL GetPushEndpoint() override;
66 void RegisterFromDocument(
67 const GURL& requesting_origin,
68 int64 service_worker_registration_id,
69 const std::string& sender_id,
70 int renderer_id,
71 int render_frame_id,
72 bool user_visible,
73 const content::PushMessagingService::RegisterCallback& callback) override;
74 void RegisterFromWorker(
75 const GURL& requesting_origin,
76 int64 service_worker_registration_id,
77 const std::string& sender_id,
78 bool user_visible,
79 const content::PushMessagingService::RegisterCallback& callback) override;
80 void Unregister(
81 const GURL& requesting_origin,
82 int64 service_worker_registration_id,
83 const std::string& sender_id,
84 const content::PushMessagingService::UnregisterCallback&) override;
85 blink::WebPushPermissionStatus GetPermissionStatus(
86 const GURL& requesting_origin,
87 const GURL& embedding_origin,
88 bool user_visible) override;
90 // content_settings::Observer implementation.
91 void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern,
92 const ContentSettingsPattern& secondary_pattern,
93 ContentSettingsType content_type,
94 std::string resource_identifier) override;
96 // KeyedService implementation.
97 void Shutdown() override;
99 void SetMessageCallbackForTesting(const base::Closure& callback);
100 void SetContentSettingChangedCallbackForTesting(
101 const base::Closure& callback);
103 private:
104 // A registration is pending until it has succeeded or failed.
105 void IncreasePushRegistrationCount(int add, bool is_pending);
106 void DecreasePushRegistrationCount(int subtract, bool was_pending);
108 // OnMessage methods ---------------------------------------------------------
110 void DeliverMessageCallback(const std::string& app_id,
111 const GURL& requesting_origin,
112 int64 service_worker_registration_id,
113 const gcm::GCMClient::IncomingMessage& message,
114 const base::Closure& message_handled_closure,
115 content::PushDeliveryStatus status);
117 // Register methods ----------------------------------------------------------
119 void RegisterEnd(
120 const content::PushMessagingService::RegisterCallback& callback,
121 const std::string& registration_id,
122 content::PushRegistrationStatus status);
124 void DidRegister(
125 const PushMessagingAppIdentifier& app_identifier,
126 const content::PushMessagingService::RegisterCallback& callback,
127 const std::string& registration_id,
128 gcm::GCMClient::Result result);
130 void DidRequestPermission(
131 const PushMessagingAppIdentifier& app_identifier,
132 const std::string& sender_id,
133 const content::PushMessagingService::RegisterCallback& callback,
134 ContentSetting content_setting);
136 // Unregister methods --------------------------------------------------------
138 void Unregister(const std::string& app_id,
139 const std::string& sender_id,
140 const content::PushMessagingService::UnregisterCallback&);
142 void DidUnregister(bool was_registered,
143 const content::PushMessagingService::UnregisterCallback&,
144 gcm::GCMClient::Result result);
146 // OnContentSettingChanged methods -------------------------------------------
148 void UnregisterBecausePermissionRevoked(
149 const PushMessagingAppIdentifier& app_identifier,
150 const base::Closure& closure,
151 const std::string& sender_id,
152 bool success,
153 bool not_found);
155 // Helper methods ------------------------------------------------------------
157 // Checks if a given origin is allowed to use Push.
158 bool HasPermission(const GURL& origin);
160 gcm::GCMDriver* GetGCMDriver() const;
162 Profile* profile_;
164 int push_registration_count_;
165 int pending_push_registration_count_;
167 base::Closure message_callback_for_testing_;
168 base::Closure content_setting_changed_callback_for_testing_;
170 #if defined(ENABLE_NOTIFICATIONS)
171 PushMessagingNotificationManager notification_manager_;
172 #endif
174 base::WeakPtrFactory<PushMessagingServiceImpl> weak_factory_;
176 DISALLOW_COPY_AND_ASSIGN(PushMessagingServiceImpl);
179 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_SERVICE_IMPL_H_