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/push_messaging/push_messaging_service_impl.h" | 5 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 DCHECK(push_registration_count_ >= 0); | 138 DCHECK(push_registration_count_ >= 0); |
139 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, | 139 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, |
140 push_registration_count_); | 140 push_registration_count_); |
141 } | 141 } |
142 if (push_registration_count_ + pending_push_registration_count_ == 0) { | 142 if (push_registration_count_ + pending_push_registration_count_ == 0) { |
143 GetGCMDriver()->RemoveAppHandler(kPushMessagingAppIdentifierPrefix); | 143 GetGCMDriver()->RemoveAppHandler(kPushMessagingAppIdentifierPrefix); |
144 } | 144 } |
145 } | 145 } |
146 | 146 |
147 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const { | 147 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const { |
148 return PushMessagingAppIdentifier::Get(profile_, app_id).IsValid(); | 148 return !PushMessagingAppIdentifier::Get(profile_, app_id).is_null(); |
149 } | 149 } |
150 | 150 |
151 void PushMessagingServiceImpl::ShutdownHandler() { | 151 void PushMessagingServiceImpl::ShutdownHandler() { |
152 // Shutdown() should come before and it removes us from the list of app | 152 // Shutdown() should come before and it removes us from the list of app |
153 // handlers of gcm::GCMDriver so this shouldn't ever been called. | 153 // handlers of gcm::GCMDriver so this shouldn't ever been called. |
154 NOTREACHED(); | 154 NOTREACHED(); |
155 } | 155 } |
156 | 156 |
157 // OnMessage methods ----------------------------------------------------------- | 157 // OnMessage methods ----------------------------------------------------------- |
158 | 158 |
159 void PushMessagingServiceImpl::OnMessage( | 159 void PushMessagingServiceImpl::OnMessage( |
160 const std::string& app_id, | 160 const std::string& app_id, |
161 const gcm::GCMClient::IncomingMessage& message) { | 161 const gcm::GCMClient::IncomingMessage& message) { |
162 base::Closure message_handled_closure = | 162 base::Closure message_handled_closure = |
163 message_callback_for_testing_.is_null() ? base::Bind(&base::DoNothing) | 163 message_callback_for_testing_.is_null() ? base::Bind(&base::DoNothing) |
164 : message_callback_for_testing_; | 164 : message_callback_for_testing_; |
165 PushMessagingAppIdentifier app_identifier = | 165 PushMessagingAppIdentifier app_identifier = |
166 PushMessagingAppIdentifier::Get(profile_, app_id); | 166 PushMessagingAppIdentifier::Get(profile_, app_id); |
167 // Drop message and unregister if app_id was unknown (maybe recently deleted). | 167 // Drop message and unregister if app_id was unknown (maybe recently deleted). |
168 if (!app_identifier.IsValid()) { | 168 if (app_identifier.is_null()) { |
169 DeliverMessageCallback(app_id, GURL::EmptyGURL(), -1, message, | 169 DeliverMessageCallback(app_id, GURL::EmptyGURL(), -1, message, |
170 message_handled_closure, | 170 message_handled_closure, |
171 content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID); | 171 content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID); |
172 return; | 172 return; |
173 } | 173 } |
174 // Drop message and unregister if |origin| has lost push permission. | 174 // Drop message and unregister if |origin| has lost push permission. |
175 if (!HasPermission(app_identifier.origin())) { | 175 if (!HasPermission(app_identifier.origin())) { |
176 DeliverMessageCallback(app_id, app_identifier.origin(), | 176 DeliverMessageCallback(app_id, app_identifier.origin(), |
177 app_identifier.service_worker_registration_id(), | 177 app_identifier.service_worker_registration_id(), |
178 message, message_handled_closure, | 178 message, message_handled_closure, |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 const GURL& requesting_origin, | 297 const GURL& requesting_origin, |
298 int64 service_worker_registration_id, | 298 int64 service_worker_registration_id, |
299 const std::string& sender_id, | 299 const std::string& sender_id, |
300 int renderer_id, | 300 int renderer_id, |
301 int render_frame_id, | 301 int render_frame_id, |
302 bool user_visible, | 302 bool user_visible, |
303 const content::PushMessagingService::RegisterCallback& callback) { | 303 const content::PushMessagingService::RegisterCallback& callback) { |
304 PushMessagingAppIdentifier app_identifier = | 304 PushMessagingAppIdentifier app_identifier = |
305 PushMessagingAppIdentifier::Generate(requesting_origin, | 305 PushMessagingAppIdentifier::Generate(requesting_origin, |
306 service_worker_registration_id); | 306 service_worker_registration_id); |
307 DCHECK(app_identifier.IsValid()); | 307 app_identifier.DCheckValid(); |
Peter Beverloo
2015/05/11 16:54:08
Remove this line, PushMessagingAppIdentifier::Gene
johnme
2015/05/12 13:21:50
Done.
| |
308 | 308 |
309 if (push_registration_count_ + pending_push_registration_count_ | 309 if (push_registration_count_ + pending_push_registration_count_ |
310 >= kMaxRegistrations) { | 310 >= kMaxRegistrations) { |
311 RegisterEnd(callback, | 311 RegisterEnd(callback, |
312 std::string(), | 312 std::string(), |
313 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 313 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
314 return; | 314 return; |
315 } | 315 } |
316 | 316 |
317 content::RenderFrameHost* render_frame_host = | 317 content::RenderFrameHost* render_frame_host = |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 | 355 |
356 void PushMessagingServiceImpl::RegisterFromWorker( | 356 void PushMessagingServiceImpl::RegisterFromWorker( |
357 const GURL& requesting_origin, | 357 const GURL& requesting_origin, |
358 int64 service_worker_registration_id, | 358 int64 service_worker_registration_id, |
359 const std::string& sender_id, | 359 const std::string& sender_id, |
360 bool user_visible, | 360 bool user_visible, |
361 const content::PushMessagingService::RegisterCallback& register_callback) { | 361 const content::PushMessagingService::RegisterCallback& register_callback) { |
362 PushMessagingAppIdentifier app_identifier = | 362 PushMessagingAppIdentifier app_identifier = |
363 PushMessagingAppIdentifier::Generate(requesting_origin, | 363 PushMessagingAppIdentifier::Generate(requesting_origin, |
364 service_worker_registration_id); | 364 service_worker_registration_id); |
365 DCHECK(app_identifier.IsValid()); | 365 app_identifier.DCheckValid(); |
Peter Beverloo
2015/05/11 16:54:08
dito
johnme
2015/05/12 13:21:50
Done.
| |
366 | 366 |
367 if (profile_->GetPrefs()->GetInteger( | 367 if (profile_->GetPrefs()->GetInteger( |
368 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { | 368 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { |
369 RegisterEnd(register_callback, std::string(), | 369 RegisterEnd(register_callback, std::string(), |
370 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 370 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
371 return; | 371 return; |
372 } | 372 } |
373 | 373 |
374 // TODO(peter): Consider |user_visible| when getting the permission status | 374 // TODO(peter): Consider |user_visible| when getting the permission status |
375 // for registering from a worker. | 375 // for registering from a worker. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
416 void PushMessagingServiceImpl::DidRegister( | 416 void PushMessagingServiceImpl::DidRegister( |
417 const PushMessagingAppIdentifier& app_identifier, | 417 const PushMessagingAppIdentifier& app_identifier, |
418 const content::PushMessagingService::RegisterCallback& callback, | 418 const content::PushMessagingService::RegisterCallback& callback, |
419 const std::string& registration_id, | 419 const std::string& registration_id, |
420 gcm::GCMClient::Result result) { | 420 gcm::GCMClient::Result result) { |
421 content::PushRegistrationStatus status = | 421 content::PushRegistrationStatus status = |
422 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 422 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
423 switch (result) { | 423 switch (result) { |
424 case gcm::GCMClient::SUCCESS: | 424 case gcm::GCMClient::SUCCESS: |
425 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; | 425 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; |
426 app_identifier.PersistToDisk(profile_); | 426 app_identifier.PersistToPrefs(profile_); |
427 IncreasePushRegistrationCount(1, false /* is_pending */); | 427 IncreasePushRegistrationCount(1, false /* is_pending */); |
428 break; | 428 break; |
429 case gcm::GCMClient::INVALID_PARAMETER: | 429 case gcm::GCMClient::INVALID_PARAMETER: |
430 case gcm::GCMClient::GCM_DISABLED: | 430 case gcm::GCMClient::GCM_DISABLED: |
431 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 431 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
432 case gcm::GCMClient::SERVER_ERROR: | 432 case gcm::GCMClient::SERVER_ERROR: |
433 case gcm::GCMClient::UNKNOWN_ERROR: | 433 case gcm::GCMClient::UNKNOWN_ERROR: |
434 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 434 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
435 break; | 435 break; |
436 case gcm::GCMClient::NETWORK_ERROR: | 436 case gcm::GCMClient::NETWORK_ERROR: |
(...skipping 29 matching lines...) Expand all Loading... | |
466 | 466 |
467 // Unregister methods ---------------------------------------------------------- | 467 // Unregister methods ---------------------------------------------------------- |
468 | 468 |
469 void PushMessagingServiceImpl::Unregister( | 469 void PushMessagingServiceImpl::Unregister( |
470 const GURL& requesting_origin, | 470 const GURL& requesting_origin, |
471 int64 service_worker_registration_id, | 471 int64 service_worker_registration_id, |
472 const std::string& sender_id, | 472 const std::string& sender_id, |
473 const content::PushMessagingService::UnregisterCallback& callback) { | 473 const content::PushMessagingService::UnregisterCallback& callback) { |
474 PushMessagingAppIdentifier app_identifier = PushMessagingAppIdentifier::Get( | 474 PushMessagingAppIdentifier app_identifier = PushMessagingAppIdentifier::Get( |
475 profile_, requesting_origin, service_worker_registration_id); | 475 profile_, requesting_origin, service_worker_registration_id); |
476 if (!app_identifier.IsValid()) { | 476 if (app_identifier.is_null()) { |
477 if (!callback.is_null()) { | 477 if (!callback.is_null()) { |
478 callback.Run( | 478 callback.Run( |
479 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); | 479 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); |
480 } | 480 } |
481 return; | 481 return; |
482 } | 482 } |
483 | 483 |
484 Unregister(app_identifier.app_id(), sender_id, callback); | 484 Unregister(app_identifier.app_id(), sender_id, callback); |
485 } | 485 } |
486 | 486 |
487 void PushMessagingServiceImpl::Unregister( | 487 void PushMessagingServiceImpl::Unregister( |
488 const std::string& app_id, | 488 const std::string& app_id, |
489 const std::string& sender_id, | 489 const std::string& sender_id, |
490 const content::PushMessagingService::UnregisterCallback& callback) { | 490 const content::PushMessagingService::UnregisterCallback& callback) { |
491 // Delete the mapping for this app_id, to guarantee that no messages get | 491 // Delete the mapping for this app_id, to guarantee that no messages get |
492 // delivered in future (even if unregistration fails). | 492 // delivered in future (even if unregistration fails). |
493 // TODO(johnme): Instead of deleting these app ids, store them elsewhere, and | 493 // TODO(johnme): Instead of deleting these app ids, store them elsewhere, and |
494 // retry unregistration if it fails due to network errors (crbug.com/465399). | 494 // retry unregistration if it fails due to network errors (crbug.com/465399). |
495 PushMessagingAppIdentifier app_identifier = | 495 PushMessagingAppIdentifier app_identifier = |
496 PushMessagingAppIdentifier::Get(profile_, app_id); | 496 PushMessagingAppIdentifier::Get(profile_, app_id); |
497 bool was_registered = app_identifier.IsValid(); | 497 bool was_registered = !app_identifier.is_null(); |
498 if (was_registered) | 498 if (was_registered) |
499 app_identifier.DeleteFromDisk(profile_); | 499 app_identifier.DeleteFromPrefs(profile_); |
500 | 500 |
501 const auto& unregister_callback = | 501 const auto& unregister_callback = |
502 base::Bind(&PushMessagingServiceImpl::DidUnregister, | 502 base::Bind(&PushMessagingServiceImpl::DidUnregister, |
503 weak_factory_.GetWeakPtr(), | 503 weak_factory_.GetWeakPtr(), |
504 was_registered, callback); | 504 was_registered, callback); |
505 #if defined(OS_ANDROID) | 505 #if defined(OS_ANDROID) |
506 // On Android the backend is different, and requires the original sender_id. | 506 // On Android the backend is different, and requires the original sender_id. |
507 // UnregisterBecausePermissionRevoked sometimes calls us with an empty one. | 507 // UnregisterBecausePermissionRevoked sometimes calls us with an empty one. |
508 if (sender_id.empty()) | 508 if (sender_id.empty()) |
509 unregister_callback.Run(gcm::GCMClient::INVALID_PARAMETER); | 509 unregister_callback.Run(gcm::GCMClient::INVALID_PARAMETER); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
640 CONTENT_SETTING_ALLOW; | 640 CONTENT_SETTING_ALLOW; |
641 } | 641 } |
642 | 642 |
643 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 643 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { |
644 gcm::GCMProfileService* gcm_profile_service = | 644 gcm::GCMProfileService* gcm_profile_service = |
645 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 645 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
646 CHECK(gcm_profile_service); | 646 CHECK(gcm_profile_service); |
647 CHECK(gcm_profile_service->driver()); | 647 CHECK(gcm_profile_service->driver()); |
648 return gcm_profile_service->driver(); | 648 return gcm_profile_service->driver(); |
649 } | 649 } |
OLD | NEW |