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