| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/services/gcm/push_messaging_service_impl.h" | 5 #include "chrome/browser/services/gcm/push_messaging_service_impl.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 16 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 18 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 15 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 19 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 16 #include "chrome/browser/services/gcm/push_messaging_application_id.h" | 20 #include "chrome/browser/services/gcm/push_messaging_application_id.h" |
| 17 #include "chrome/browser/services/gcm/push_messaging_constants.h" | 21 #include "chrome/browser/services/gcm/push_messaging_constants.h" |
| 18 #include "chrome/browser/services/gcm/push_messaging_permission_context.h" | 22 #include "chrome/browser/services/gcm/push_messaging_permission_context.h" |
| 19 #include "chrome/browser/services/gcm/push_messaging_permission_context_factory.
h" | 23 #include "chrome/browser/services/gcm/push_messaging_permission_context_factory.
h" |
| 20 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
| 21 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 26 #include "chrome/grit/generated_resources.h" |
| 22 #include "components/content_settings/core/common/permission_request_id.h" | 27 #include "components/content_settings/core/common/permission_request_id.h" |
| 23 #include "components/gcm_driver/gcm_driver.h" | 28 #include "components/gcm_driver/gcm_driver.h" |
| 24 #include "components/pref_registry/pref_registry_syncable.h" | 29 #include "components/pref_registry/pref_registry_syncable.h" |
| 25 #include "content/public/browser/browser_context.h" | 30 #include "content/public/browser/browser_context.h" |
| 26 #include "content/public/browser/render_frame_host.h" | 31 #include "content/public/browser/render_frame_host.h" |
| 27 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 33 #include "content/public/common/child_process_host.h" |
| 34 #include "content/public/common/platform_notification_data.h" |
| 35 #include "third_party/skia/include/core/SkBitmap.h" |
| 36 #include "ui/base/l10n/l10n_util.h" |
| 28 | 37 |
| 29 namespace gcm { | 38 namespace gcm { |
| 30 | 39 |
| 31 namespace { | 40 namespace { |
| 32 const int kMaxRegistrations = 1000000; | 41 const int kMaxRegistrations = 1000000; |
| 33 | 42 |
| 34 blink::WebPushPermissionStatus ToPushPermission(ContentSetting setting) { | 43 blink::WebPushPermissionStatus ToPushPermission(ContentSetting setting) { |
| 35 switch (setting) { | 44 switch (setting) { |
| 36 case CONTENT_SETTING_ALLOW: | 45 case CONTENT_SETTING_ALLOW: |
| 37 return blink::WebPushPermissionStatusGranted; | 46 return blink::WebPushPermissionStatusGranted; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // { | 154 // { |
| 146 // "registration_ids": ["FOO", "BAR"], | 155 // "registration_ids": ["FOO", "BAR"], |
| 147 // "data": { | 156 // "data": { |
| 148 // "data": "BAZ", | 157 // "data": "BAZ", |
| 149 // }, | 158 // }, |
| 150 // "delay_while_idle": true, | 159 // "delay_while_idle": true, |
| 151 // } | 160 // } |
| 152 // TODO(johnme): Make sure this is clearly documented for developers. | 161 // TODO(johnme): Make sure this is clearly documented for developers. |
| 153 PushMessagingApplicationId application_id = | 162 PushMessagingApplicationId application_id = |
| 154 PushMessagingApplicationId::Parse(app_id); | 163 PushMessagingApplicationId::Parse(app_id); |
| 155 DCHECK(application_id.IsValid()); | |
| 156 GCMClient::MessageData::const_iterator it = message.data.find("data"); | 164 GCMClient::MessageData::const_iterator it = message.data.find("data"); |
| 157 if (application_id.IsValid() && it != message.data.end()) { | 165 if (application_id.IsValid() && it != message.data.end()) { |
| 158 if (!HasPermission(application_id.origin)) { | 166 if (!HasPermission(application_id.origin)) { |
| 159 // The |origin| lost push permission. We need to unregister and drop this | 167 // The |origin| lost push permission. We need to unregister and drop this |
| 160 // message. | 168 // message. |
| 161 Unregister(application_id, UnregisterCallback()); | 169 Unregister(application_id, UnregisterCallback()); |
| 162 return; | 170 return; |
| 163 } | 171 } |
| 164 | 172 |
| 165 const std::string& data = it->second; | 173 const std::string& data = it->second; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 185 | 193 |
| 186 void PushMessagingServiceImpl::DeliverMessageCallback( | 194 void PushMessagingServiceImpl::DeliverMessageCallback( |
| 187 const PushMessagingApplicationId& application_id, | 195 const PushMessagingApplicationId& application_id, |
| 188 const GCMClient::IncomingMessage& message, | 196 const GCMClient::IncomingMessage& message, |
| 189 content::PushDeliveryStatus status) { | 197 content::PushDeliveryStatus status) { |
| 190 // TODO(mvanouwerkerk): UMA logging. | 198 // TODO(mvanouwerkerk): UMA logging. |
| 191 // TODO(mvanouwerkerk): Show a warning in the developer console of the | 199 // TODO(mvanouwerkerk): Show a warning in the developer console of the |
| 192 // Service Worker corresponding to app_id (and/or on an internals page). | 200 // Service Worker corresponding to app_id (and/or on an internals page). |
| 193 // TODO(mvanouwerkerk): Is there a way to recover from failure? | 201 // TODO(mvanouwerkerk): Is there a way to recover from failure? |
| 194 switch (status) { | 202 switch (status) { |
| 203 // Call RequireUserVisibleUX if the message was delivered to the Service |
| 204 // Worker JS, even if the website's event handler failed (to prevent sites |
| 205 // deliberately failing in order to avoid having to show notifications). |
| 195 case content::PUSH_DELIVERY_STATUS_SUCCESS: | 206 case content::PUSH_DELIVERY_STATUS_SUCCESS: |
| 207 case content::PUSH_DELIVERY_STATUS_EVENT_WAITUNTIL_REJECTED: |
| 208 RequireUserVisibleUX(application_id); |
| 209 break; |
| 196 case content::PUSH_DELIVERY_STATUS_INVALID_MESSAGE: | 210 case content::PUSH_DELIVERY_STATUS_INVALID_MESSAGE: |
| 197 case content::PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR: | 211 case content::PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR: |
| 198 case content::PUSH_DELIVERY_STATUS_EVENT_WAITUNTIL_REJECTED: | |
| 199 break; | 212 break; |
| 200 case content::PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER: | 213 case content::PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER: |
| 201 Unregister(application_id, UnregisterCallback()); | 214 Unregister(application_id, UnregisterCallback()); |
| 202 break; | 215 break; |
| 203 } | 216 } |
| 204 } | 217 } |
| 205 | 218 |
| 219 void PushMessagingServiceImpl::RequireUserVisibleUX( |
| 220 const PushMessagingApplicationId& application_id) { |
| 221 #if defined(ENABLE_NOTIFICATIONS) |
| 222 // TODO(johnme): Add test. |
| 223 // TODO(johnme): Relax this heuristic slightly. |
| 224 int notification_count = g_browser_process->notification_ui_manager()-> |
| 225 GetAllIdsByProfileAndSourceOrigin(profile_, application_id.origin).size(); |
| 226 if (notification_count > 0) |
| 227 return; |
| 228 |
| 229 // If we haven't returned yet, the site failed to show a notification, so we |
| 230 // will show a generic notification. See https://crbug.com/437277 |
| 231 // TODO(johnme): The generic notification should probably automatically close |
| 232 // itself when the next push message arrives? |
| 233 content::PlatformNotificationData notification_data; |
| 234 // TODO(johnme): Switch to FormatOriginForDisplay from crbug.com/402698 |
| 235 notification_data.title = l10n_util::GetStringFUTF16( |
| 236 IDS_PUSH_MESSAGING_GENERIC_NOTIFICATION_TITLE, |
| 237 base::UTF8ToUTF16(application_id.origin.host())); |
| 238 notification_data.direction = |
| 239 content::PlatformNotificationData::NotificationDirectionLeftToRight; |
| 240 notification_data.body = |
| 241 l10n_util::GetStringUTF16(IDS_PUSH_MESSAGING_GENERIC_NOTIFICATION_BODY); |
| 242 notification_data.tag = |
| 243 base::ASCIIToUTF16("user_visible_auto_notification"); |
| 244 notification_data.icon = GURL(); // TODO(johnme): Better icon? |
| 245 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( |
| 246 profile_, |
| 247 application_id.service_worker_registration_id, |
| 248 application_id.origin, |
| 249 SkBitmap() /* icon */, |
| 250 notification_data, |
| 251 content::ChildProcessHost::kInvalidUniqueID /* render_process_id */); |
| 252 #endif |
| 253 } |
| 254 |
| 206 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { | 255 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { |
| 207 // TODO(mvanouwerkerk): Fire push error event on the Service Worker | 256 // TODO(mvanouwerkerk): Fire push error event on the Service Worker |
| 208 // corresponding to app_id. | 257 // corresponding to app_id. |
| 209 } | 258 } |
| 210 | 259 |
| 211 void PushMessagingServiceImpl::OnSendError( | 260 void PushMessagingServiceImpl::OnSendError( |
| 212 const std::string& app_id, | 261 const std::string& app_id, |
| 213 const GCMClient::SendErrorDetails& send_error_details) { | 262 const GCMClient::SendErrorDetails& send_error_details) { |
| 214 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; | 263 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; |
| 215 } | 264 } |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 } | 507 } |
| 459 | 508 |
| 460 void PushMessagingServiceImpl::AddAppHandlerIfNecessary() { | 509 void PushMessagingServiceImpl::AddAppHandlerIfNecessary() { |
| 461 if (gcm_profile_service_->driver()->GetAppHandler( | 510 if (gcm_profile_service_->driver()->GetAppHandler( |
| 462 kPushMessagingApplicationIdPrefix) != this) | 511 kPushMessagingApplicationIdPrefix) != this) |
| 463 gcm_profile_service_->driver()->AddAppHandler( | 512 gcm_profile_service_->driver()->AddAppHandler( |
| 464 kPushMessagingApplicationIdPrefix, this); | 513 kPushMessagingApplicationIdPrefix, this); |
| 465 } | 514 } |
| 466 | 515 |
| 467 } // namespace gcm | 516 } // namespace gcm |
| OLD | NEW |