Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / chrome / browser / push_messaging / push_messaging_service_impl.h
blobb27745b9692b24e93b1e9f5d79bbfc472fe244e8
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>
9 #include <set>
10 #include <vector>
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"
28 #endif
30 class Profile;
31 class PushMessagingAppIdentifier;
33 namespace gcm {
34 class GCMDriver;
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,
45 public KeyedService {
46 public:
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;
61 void OnSendError(
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,
74 int renderer_id,
75 int render_frame_id,
76 bool user_visible,
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,
82 bool user_visible,
83 const content::PushMessagingService::RegisterCallback& callback) override;
84 void GetPublicEncryptionKey(
85 const GURL& origin,
86 int64_t service_worker_registration_id,
87 const content::PushMessagingService::PublicKeyCallback&
88 callback) override;
89 void Unsubscribe(
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);
114 private:
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 ---------------------------------------------------------
130 void SubscribeEnd(
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);
140 void DidSubscribe(
141 const PushMessagingAppIdentifier& app_identifier,
142 const content::PushMessagingService::RegisterCallback& callback,
143 const std::string& subscription_id,
144 gcm::GCMClient::Result result);
146 void DidSubscribeWithPublicKey(
147 const PushMessagingAppIdentifier& app_identifier,
148 const content::PushMessagingService::RegisterCallback& callback,
149 const std::string& subscription_id,
150 const std::string& public_key);
152 void DidRequestPermission(
153 const PushMessagingAppIdentifier& app_identifier,
154 const std::string& sender_id,
155 const content::PushMessagingService::RegisterCallback& callback,
156 content::PermissionStatus permission_status);
158 // GetPublicEncryptionKey method ---------------------------------------------
160 void DidGetPublicKey(
161 const PushMessagingService::PublicKeyCallback& callback,
162 const std::string& public_key) const;
165 // Unsubscribe methods -------------------------------------------------------
167 void Unsubscribe(const std::string& app_id,
168 const std::string& sender_id,
169 const content::PushMessagingService::UnregisterCallback&);
171 void DidUnsubscribe(bool was_subscribed,
172 const content::PushMessagingService::UnregisterCallback&,
173 gcm::GCMClient::Result result);
175 // OnContentSettingChanged methods -------------------------------------------
177 void UnsubscribeBecausePermissionRevoked(
178 const PushMessagingAppIdentifier& app_identifier,
179 const base::Closure& closure,
180 const std::string& sender_id,
181 bool success,
182 bool not_found);
184 // Helper methods ------------------------------------------------------------
186 // Checks if a given origin is allowed to use Push.
187 bool IsPermissionSet(const GURL& origin);
189 // Returns whether incoming messages should support payloads.
190 bool AreMessagePayloadsEnabled() const;
192 gcm::GCMDriver* GetGCMDriver() const;
194 Profile* profile_;
196 int push_subscription_count_;
197 int pending_push_subscription_count_;
199 base::Closure message_callback_for_testing_;
200 base::Closure content_setting_changed_callback_for_testing_;
202 #if defined(ENABLE_NOTIFICATIONS)
203 PushMessagingNotificationManager notification_manager_;
204 #endif
206 // A multiset containing one entry for each in-flight push message delivery,
207 // keyed by the receiver's app id.
208 std::multiset<std::string> in_flight_message_deliveries_;
210 base::WeakPtrFactory<PushMessagingServiceImpl> weak_factory_;
212 DISALLOW_COPY_AND_ASSIGN(PushMessagingServiceImpl);
215 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_SERVICE_IMPL_H_