Chromium Code Reviews| 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 <bitset> | 7 #include <bitset> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/barrier_closure.h" | 10 #include "base/barrier_closure.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 55 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 56 #else | 56 #else |
| 57 #include "chrome/browser/ui/browser.h" | 57 #include "chrome/browser/ui/browser.h" |
| 58 #include "chrome/browser/ui/browser_iterator.h" | 58 #include "chrome/browser/ui/browser_iterator.h" |
| 59 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 59 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 60 #endif | 60 #endif |
| 61 | 61 |
| 62 using content::BrowserThread; | 62 using content::BrowserThread; |
| 63 | 63 |
| 64 namespace { | 64 namespace { |
| 65 const int kMaxRegistrations = 1000000; | 65 const int kMaxSubscriptions = 1000000; |
| 66 | 66 |
| 67 void RecordDeliveryStatus(content::PushDeliveryStatus status) { | 67 void RecordDeliveryStatus(content::PushDeliveryStatus status) { |
| 68 UMA_HISTOGRAM_ENUMERATION("PushMessaging.DeliveryStatus", | 68 UMA_HISTOGRAM_ENUMERATION("PushMessaging.DeliveryStatus", |
| 69 status, | 69 status, |
| 70 content::PUSH_DELIVERY_STATUS_LAST + 1); | 70 content::PUSH_DELIVERY_STATUS_LAST + 1); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void RecordUserVisibleStatus(content::PushUserVisibleStatus status) { | 73 void RecordUserVisibleStatus(content::PushUserVisibleStatus status) { |
| 74 UMA_HISTOGRAM_ENUMERATION("PushMessaging.UserVisibleStatus", | 74 UMA_HISTOGRAM_ENUMERATION("PushMessaging.UserVisibleStatus", |
| 75 status, | 75 status, |
| 76 content::PUSH_USER_VISIBLE_STATUS_LAST + 1); | 76 content::PUSH_USER_VISIBLE_STATUS_LAST + 1); |
| 77 } | 77 } |
| 78 | 78 |
| 79 blink::WebPushPermissionStatus ToPushPermission(ContentSetting setting) { | 79 blink::WebPushPermissionStatus ToPushPermission(ContentSetting setting) { |
| 80 switch (setting) { | 80 switch (setting) { |
| 81 case CONTENT_SETTING_ALLOW: | 81 case CONTENT_SETTING_ALLOW: |
| 82 return blink::WebPushPermissionStatusGranted; | 82 return blink::WebPushPermissionStatusGranted; |
| 83 case CONTENT_SETTING_BLOCK: | 83 case CONTENT_SETTING_BLOCK: |
| 84 return blink::WebPushPermissionStatusDenied; | 84 return blink::WebPushPermissionStatusDenied; |
| 85 case CONTENT_SETTING_ASK: | 85 case CONTENT_SETTING_ASK: |
| 86 return blink::WebPushPermissionStatusPrompt; | 86 return blink::WebPushPermissionStatusPrompt; |
| 87 default: | 87 default: |
| 88 NOTREACHED(); | 88 NOTREACHED(); |
| 89 return blink::WebPushPermissionStatusDenied; | 89 return blink::WebPushPermissionStatusDenied; |
| 90 } | 90 } |
| 91 } | 91 } |
| 92 | 92 |
| 93 void UnregisterCallbackToClosure( | 93 void UnsubscribeCallbackToClosure( |
| 94 const base::Closure& closure, content::PushUnregistrationStatus status) { | 94 const base::Closure& closure, content::PushUnsubscriptionStatus status) { |
| 95 closure.Run(); | 95 closure.Run(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 } // namespace | 98 } // namespace |
| 99 | 99 |
| 100 // static | 100 // static |
| 101 void PushMessagingServiceImpl::RegisterProfilePrefs( | 101 void PushMessagingServiceImpl::RegisterProfilePrefs( |
| 102 user_prefs::PrefRegistrySyncable* registry) { | 102 user_prefs::PrefRegistrySyncable* registry) { |
| 103 registry->RegisterIntegerPref(prefs::kPushMessagingRegistrationCount, 0); | 103 registry->RegisterIntegerPref(prefs::kPushMessagingRegistrationCount, 0); |
| 104 PushMessagingApplicationId::RegisterProfilePrefs(registry); | 104 PushMessagingApplicationId::RegisterProfilePrefs(registry); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 115 // defined in a regular profile are visible in the corresponding incognito | 115 // defined in a regular profile are visible in the corresponding incognito |
| 116 // profile unless overridden. | 116 // profile unless overridden. |
| 117 // TODO(johnme): Make sure this pref doesn't get out of sync after crashes. | 117 // TODO(johnme): Make sure this pref doesn't get out of sync after crashes. |
| 118 int count = profile->GetPrefs()->GetInteger( | 118 int count = profile->GetPrefs()->GetInteger( |
| 119 prefs::kPushMessagingRegistrationCount); | 119 prefs::kPushMessagingRegistrationCount); |
| 120 if (count <= 0) | 120 if (count <= 0) |
| 121 return; | 121 return; |
| 122 | 122 |
| 123 PushMessagingServiceImpl* push_service = | 123 PushMessagingServiceImpl* push_service = |
| 124 PushMessagingServiceFactory::GetForProfile(profile); | 124 PushMessagingServiceFactory::GetForProfile(profile); |
| 125 push_service->IncreasePushRegistrationCount(count, false /* is_pending */); | 125 push_service->IncreasePushSubscriptionCount(count, false /* is_pending */); |
| 126 } | 126 } |
| 127 | 127 |
| 128 PushMessagingServiceImpl::PushMessagingServiceImpl(Profile* profile) | 128 PushMessagingServiceImpl::PushMessagingServiceImpl(Profile* profile) |
| 129 : profile_(profile), | 129 : profile_(profile), |
| 130 push_registration_count_(0), | 130 push_subscription_count_(0), |
| 131 pending_push_registration_count_(0), | 131 pending_push_subscription_count_(0), |
| 132 weak_factory_(this) { | 132 weak_factory_(this) { |
| 133 DCHECK(profile); | 133 DCHECK(profile); |
| 134 profile_->GetHostContentSettingsMap()->AddObserver(this); | 134 profile_->GetHostContentSettingsMap()->AddObserver(this); |
| 135 } | 135 } |
| 136 | 136 |
| 137 PushMessagingServiceImpl::~PushMessagingServiceImpl() { | 137 PushMessagingServiceImpl::~PushMessagingServiceImpl() { |
| 138 profile_->GetHostContentSettingsMap()->RemoveObserver(this); | 138 profile_->GetHostContentSettingsMap()->RemoveObserver(this); |
| 139 } | 139 } |
| 140 | 140 |
| 141 void PushMessagingServiceImpl::IncreasePushRegistrationCount(int add, | 141 void PushMessagingServiceImpl::IncreasePushSubscriptionCount(int add, |
| 142 bool is_pending) { | 142 bool is_pending) { |
| 143 DCHECK(add > 0); | 143 DCHECK(add > 0); |
| 144 if (push_registration_count_ + pending_push_registration_count_ == 0) { | 144 if (push_subscription_count_ + pending_push_subscription_count_ == 0) { |
| 145 GetGCMDriver()->AddAppHandler(kPushMessagingApplicationIdPrefix, this); | 145 GetGCMDriver()->AddAppHandler(kPushMessagingApplicationIdPrefix, this); |
| 146 } | 146 } |
| 147 if (is_pending) { | 147 if (is_pending) { |
| 148 pending_push_registration_count_ += add; | 148 pending_push_subscription_count_ += add; |
| 149 } else { | 149 } else { |
| 150 push_registration_count_ += add; | 150 push_subscription_count_ += add; |
| 151 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, | 151 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, |
| 152 push_registration_count_); | 152 push_subscription_count_); |
| 153 } | 153 } |
| 154 } | 154 } |
| 155 | 155 |
| 156 void PushMessagingServiceImpl::DecreasePushRegistrationCount(int subtract, | 156 void PushMessagingServiceImpl::DecreasePushSubscriptionCount(int subtract, |
| 157 bool was_pending) { | 157 bool was_pending) { |
| 158 DCHECK(subtract > 0); | 158 DCHECK(subtract > 0); |
| 159 if (was_pending) { | 159 if (was_pending) { |
| 160 pending_push_registration_count_ -= subtract; | 160 pending_push_subscription_count_ -= subtract; |
| 161 DCHECK(pending_push_registration_count_ >= 0); | 161 DCHECK(pending_push_subscription_count_ >= 0); |
| 162 } else { | 162 } else { |
| 163 push_registration_count_ -= subtract; | 163 push_subscription_count_ -= subtract; |
| 164 DCHECK(push_registration_count_ >= 0); | 164 DCHECK(push_subscription_count_ >= 0); |
| 165 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, | 165 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, |
| 166 push_registration_count_); | 166 push_subscription_count_); |
| 167 } | 167 } |
| 168 if (push_registration_count_ + pending_push_registration_count_ == 0) { | 168 if (push_subscription_count_ + pending_push_subscription_count_ == 0) { |
| 169 GetGCMDriver()->RemoveAppHandler(kPushMessagingApplicationIdPrefix); | 169 GetGCMDriver()->RemoveAppHandler(kPushMessagingApplicationIdPrefix); |
| 170 } | 170 } |
| 171 } | 171 } |
| 172 | 172 |
| 173 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const { | 173 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const { |
| 174 return PushMessagingApplicationId::Get(profile_, app_id).IsValid(); | 174 return PushMessagingApplicationId::Get(profile_, app_id).IsValid(); |
| 175 } | 175 } |
| 176 | 176 |
| 177 void PushMessagingServiceImpl::ShutdownHandler() { | 177 void PushMessagingServiceImpl::ShutdownHandler() { |
| 178 // Shutdown() should come before and it removes us from the list of app | 178 // Shutdown() should come before and it removes us from the list of app |
| 179 // handlers of gcm::GCMDriver so this shouldn't ever been called. | 179 // handlers of gcm::GCMDriver so this shouldn't ever been called. |
| 180 NOTREACHED(); | 180 NOTREACHED(); |
| 181 } | 181 } |
| 182 | 182 |
| 183 // OnMessage methods ----------------------------------------------------------- | 183 // OnMessage methods ----------------------------------------------------------- |
| 184 | 184 |
| 185 void PushMessagingServiceImpl::OnMessage( | 185 void PushMessagingServiceImpl::OnMessage( |
| 186 const std::string& app_id, | 186 const std::string& app_id, |
| 187 const gcm::GCMClient::IncomingMessage& message) { | 187 const gcm::GCMClient::IncomingMessage& message) { |
| 188 base::Closure message_handled_closure = | 188 base::Closure message_handled_closure = |
| 189 message_callback_for_testing_.is_null() ? base::Bind(&base::DoNothing) | 189 message_callback_for_testing_.is_null() ? base::Bind(&base::DoNothing) |
| 190 : message_callback_for_testing_; | 190 : message_callback_for_testing_; |
| 191 PushMessagingApplicationId application_id = | 191 PushMessagingApplicationId application_id = |
| 192 PushMessagingApplicationId::Get(profile_, app_id); | 192 PushMessagingApplicationId::Get(profile_, app_id); |
| 193 // Drop message and unregister if app id was unknown (maybe recently deleted). | 193 // Drop message and unsibscribe if app id was unknown (recently deleted?). |
|
johnme
2015/05/07 13:31:25
Typo
| |
| 194 if (!application_id.IsValid()) { | 194 if (!application_id.IsValid()) { |
| 195 DeliverMessageCallback(app_id, GURL::EmptyGURL(), -1, message, | 195 DeliverMessageCallback(app_id, GURL::EmptyGURL(), -1, message, |
| 196 message_handled_closure, | 196 message_handled_closure, |
| 197 content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID); | 197 content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID); |
| 198 return; | 198 return; |
| 199 } | 199 } |
| 200 // Drop message and unregister if |origin| has lost push permission. | 200 // Drop message and unsubscribe if |origin| has lost push permission. |
| 201 if (!HasPermission(application_id.origin())) { | 201 if (!HasPermission(application_id.origin())) { |
| 202 DeliverMessageCallback(app_id, application_id.origin(), | 202 DeliverMessageCallback(app_id, application_id.origin(), |
| 203 application_id.service_worker_registration_id(), | 203 application_id.service_worker_registration_id(), |
| 204 message, message_handled_closure, | 204 message, message_handled_closure, |
| 205 content::PUSH_DELIVERY_STATUS_PERMISSION_DENIED); | 205 content::PUSH_DELIVERY_STATUS_PERMISSION_DENIED); |
| 206 return; | 206 return; |
| 207 } | 207 } |
| 208 | 208 |
| 209 rappor::SampleDomainAndRegistryFromGURL( | 209 rappor::SampleDomainAndRegistryFromGURL( |
| 210 g_browser_process->rappor_service(), | 210 g_browser_process->rappor_service(), |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 RequireUserVisibleUX(requesting_origin, service_worker_registration_id, | 266 RequireUserVisibleUX(requesting_origin, service_worker_registration_id, |
| 267 message_handled_closure); | 267 message_handled_closure); |
| 268 break; | 268 break; |
| 269 case content::PUSH_DELIVERY_STATUS_INVALID_MESSAGE: | 269 case content::PUSH_DELIVERY_STATUS_INVALID_MESSAGE: |
| 270 case content::PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR: | 270 case content::PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR: |
| 271 message_handled_closure.Run(); | 271 message_handled_closure.Run(); |
| 272 break; | 272 break; |
| 273 case content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID: | 273 case content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID: |
| 274 case content::PUSH_DELIVERY_STATUS_PERMISSION_DENIED: | 274 case content::PUSH_DELIVERY_STATUS_PERMISSION_DENIED: |
| 275 case content::PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER: | 275 case content::PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER: |
| 276 Unregister(app_id_guid, message.sender_id, | 276 Unsubscribe(app_id_guid, message.sender_id, |
| 277 base::Bind(&UnregisterCallbackToClosure, | 277 base::Bind(&UnsubscribeCallbackToClosure, |
| 278 message_handled_closure)); | 278 message_handled_closure)); |
| 279 break; | 279 break; |
| 280 } | 280 } |
| 281 RecordDeliveryStatus(status); | 281 RecordDeliveryStatus(status); |
| 282 } | 282 } |
| 283 | 283 |
| 284 void PushMessagingServiceImpl::RequireUserVisibleUX( | 284 void PushMessagingServiceImpl::RequireUserVisibleUX( |
| 285 const GURL& requesting_origin, int64_t service_worker_registration_id, | 285 const GURL& requesting_origin, int64_t service_worker_registration_id, |
| 286 const base::Closure& message_handled_closure) { | 286 const base::Closure& message_handled_closure) { |
| 287 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 287 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 288 #if defined(ENABLE_NOTIFICATIONS) | 288 #if defined(ENABLE_NOTIFICATIONS) |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 553 const std::string& message_id) { | 553 const std::string& message_id) { |
| 554 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; | 554 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; |
| 555 } | 555 } |
| 556 | 556 |
| 557 // GetPushEndpoint method ------------------------------------------------------ | 557 // GetPushEndpoint method ------------------------------------------------------ |
| 558 | 558 |
| 559 GURL PushMessagingServiceImpl::GetPushEndpoint() { | 559 GURL PushMessagingServiceImpl::GetPushEndpoint() { |
| 560 return GURL(std::string(kPushMessagingEndpoint)); | 560 return GURL(std::string(kPushMessagingEndpoint)); |
| 561 } | 561 } |
| 562 | 562 |
| 563 // Register and GetPermissionStatus methods ------------------------------------ | 563 // Subscribe and GetPermissionStatus methods ----------------------------------- |
| 564 | 564 |
| 565 void PushMessagingServiceImpl::RegisterFromDocument( | 565 void PushMessagingServiceImpl::SubscribeFromDocument( |
| 566 const GURL& requesting_origin, | 566 const GURL& requesting_origin, |
| 567 int64 service_worker_registration_id, | 567 int64 service_worker_registration_id, |
| 568 const std::string& sender_id, | 568 const std::string& sender_id, |
| 569 int renderer_id, | 569 int renderer_id, |
| 570 int render_frame_id, | 570 int render_frame_id, |
| 571 bool user_visible, | 571 bool user_visible, |
| 572 const content::PushMessagingService::RegisterCallback& callback) { | 572 const content::PushMessagingService::SubscribeCallback& callback) { |
| 573 PushMessagingApplicationId application_id = | 573 PushMessagingApplicationId application_id = |
| 574 PushMessagingApplicationId::Generate(requesting_origin, | 574 PushMessagingApplicationId::Generate(requesting_origin, |
| 575 service_worker_registration_id); | 575 service_worker_registration_id); |
| 576 DCHECK(application_id.IsValid()); | 576 DCHECK(application_id.IsValid()); |
| 577 | 577 |
| 578 if (push_registration_count_ + pending_push_registration_count_ | 578 if (push_subscription_count_ + pending_push_subscription_count_ |
| 579 >= kMaxRegistrations) { | 579 >= kMaxSubscriptions) { |
| 580 RegisterEnd(callback, | 580 SubscribeEnd(callback, |
| 581 std::string(), | 581 std::string(), |
| 582 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 582 content::PUSH_SUBSCRIPTION_STATUS_LIMIT_REACHED); |
| 583 return; | 583 return; |
| 584 } | 584 } |
| 585 | 585 |
| 586 content::RenderFrameHost* render_frame_host = | 586 content::RenderFrameHost* render_frame_host = |
| 587 content::RenderFrameHost::FromID(renderer_id, render_frame_id); | 587 content::RenderFrameHost::FromID(renderer_id, render_frame_id); |
| 588 if (!render_frame_host) | 588 if (!render_frame_host) |
| 589 return; | 589 return; |
| 590 | 590 |
| 591 content::WebContents* web_contents = | 591 content::WebContents* web_contents = |
| 592 content::WebContents::FromRenderFrameHost(render_frame_host); | 592 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 593 if (!web_contents) | 593 if (!web_contents) |
| 594 return; | 594 return; |
| 595 | 595 |
| 596 // TODO(miguelg) need to send this over IPC when bubble support is | 596 // TODO(miguelg) need to send this over IPC when bubble support is |
| 597 // implemented. | 597 // implemented. |
| 598 int bridge_id = -1; | 598 int bridge_id = -1; |
| 599 | 599 |
| 600 const PermissionRequestID id( | 600 const PermissionRequestID id( |
| 601 renderer_id, web_contents->GetRoutingID(), bridge_id, GURL()); | 601 renderer_id, web_contents->GetRoutingID(), bridge_id, GURL()); |
| 602 | 602 |
| 603 PushMessagingPermissionContext* permission_context = | 603 PushMessagingPermissionContext* permission_context = |
| 604 PushMessagingPermissionContextFactory::GetForProfile(profile_); | 604 PushMessagingPermissionContextFactory::GetForProfile(profile_); |
| 605 | 605 |
| 606 if (permission_context == NULL || !user_visible) { | 606 if (permission_context == NULL || !user_visible) { |
| 607 RegisterEnd(callback, | 607 SubscribeEnd(callback, |
| 608 std::string(), | 608 std::string(), |
| 609 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 609 content::PUSH_SUBSCRIPTION_STATUS_PERMISSION_DENIED); |
| 610 return; | 610 return; |
| 611 } | 611 } |
| 612 | 612 |
| 613 // TODO(miguelg): Consider the value of |user_visible| when making the | 613 // TODO(miguelg): Consider the value of |user_visible| when making the |
| 614 // permission request. | 614 // permission request. |
| 615 // TODO(mlamouri): Move requesting Push permission over to using Mojo, and | 615 // TODO(mlamouri): Move requesting Push permission over to using Mojo, and |
| 616 // re-introduce the ability of |user_gesture| when bubbles require this. | 616 // re-introduce the ability of |user_gesture| when bubbles require this. |
| 617 // https://crbug.com/423770. | 617 // https://crbug.com/423770. |
| 618 permission_context->RequestPermission( | 618 permission_context->RequestPermission( |
| 619 web_contents, id, requesting_origin, true /* user_gesture */, | 619 web_contents, id, requesting_origin, true /* user_gesture */, |
| 620 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, | 620 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, |
| 621 weak_factory_.GetWeakPtr(), application_id, sender_id, | 621 weak_factory_.GetWeakPtr(), application_id, sender_id, |
| 622 callback)); | 622 callback)); |
| 623 } | 623 } |
| 624 | 624 |
| 625 void PushMessagingServiceImpl::RegisterFromWorker( | 625 void PushMessagingServiceImpl::SubscribeFromWorker( |
| 626 const GURL& requesting_origin, | 626 const GURL& requesting_origin, |
| 627 int64 service_worker_registration_id, | 627 int64 service_worker_registration_id, |
| 628 const std::string& sender_id, | 628 const std::string& sender_id, |
| 629 bool user_visible, | 629 bool user_visible, |
| 630 const content::PushMessagingService::RegisterCallback& register_callback) { | 630 const content::PushMessagingService::SubscribeCallback& callback) { |
| 631 PushMessagingApplicationId application_id = | 631 PushMessagingApplicationId application_id = |
| 632 PushMessagingApplicationId::Generate(requesting_origin, | 632 PushMessagingApplicationId::Generate(requesting_origin, |
| 633 service_worker_registration_id); | 633 service_worker_registration_id); |
| 634 DCHECK(application_id.IsValid()); | 634 DCHECK(application_id.IsValid()); |
| 635 | 635 |
| 636 if (profile_->GetPrefs()->GetInteger( | 636 if (profile_->GetPrefs()->GetInteger( |
| 637 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { | 637 prefs::kPushMessagingRegistrationCount) >= kMaxSubscriptions) { |
| 638 RegisterEnd(register_callback, std::string(), | 638 SubscribeEnd(callback, std::string(), |
| 639 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 639 content::PUSH_SUBSCRIPTION_STATUS_LIMIT_REACHED); |
| 640 return; | 640 return; |
| 641 } | 641 } |
| 642 | 642 |
| 643 // TODO(peter): Consider |user_visible| when getting the permission status | 643 // TODO(peter): Consider |user_visible| when getting the permission status |
| 644 // for registering from a worker. | 644 // for subscribing from a worker. |
| 645 | 645 |
| 646 GURL embedding_origin = requesting_origin; | 646 GURL embedding_origin = requesting_origin; |
| 647 blink::WebPushPermissionStatus permission_status = | 647 blink::WebPushPermissionStatus permission_status = |
| 648 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, | 648 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, |
| 649 embedding_origin, | 649 embedding_origin, |
| 650 user_visible); | 650 user_visible); |
| 651 if (permission_status != blink::WebPushPermissionStatusGranted) { | 651 if (permission_status != blink::WebPushPermissionStatusGranted) { |
| 652 RegisterEnd(register_callback, std::string(), | 652 SubscribeEnd(callback, std::string(), |
| 653 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 653 content::PUSH_SUBSCRIPTION_STATUS_PERMISSION_DENIED); |
| 654 return; | 654 return; |
| 655 } | 655 } |
| 656 | 656 |
| 657 IncreasePushRegistrationCount(1, true /* is_pending */); | 657 IncreasePushSubscriptionCount(1, true /* is_pending */); |
| 658 std::vector<std::string> sender_ids(1, sender_id); | 658 std::vector<std::string> sender_ids(1, sender_id); |
| 659 GetGCMDriver()->Register( | 659 GetGCMDriver()->Register( |
| 660 application_id.app_id_guid(), sender_ids, | 660 application_id.app_id_guid(), sender_ids, |
| 661 base::Bind(&PushMessagingServiceImpl::DidRegister, | 661 base::Bind(&PushMessagingServiceImpl::DidSubscribe, |
| 662 weak_factory_.GetWeakPtr(), | 662 weak_factory_.GetWeakPtr(), |
| 663 application_id, register_callback)); | 663 application_id, callback)); |
| 664 } | 664 } |
| 665 | 665 |
| 666 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus( | 666 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus( |
| 667 const GURL& requesting_origin, | 667 const GURL& requesting_origin, |
| 668 const GURL& embedding_origin, | 668 const GURL& embedding_origin, |
| 669 bool user_visible) { | 669 bool user_visible) { |
| 670 // TODO(peter): Consider |user_visible| when checking Push permission. | 670 // TODO(peter): Consider |user_visible| when checking Push permission. |
| 671 | 671 |
| 672 PushMessagingPermissionContext* permission_context = | 672 PushMessagingPermissionContext* permission_context = |
| 673 PushMessagingPermissionContextFactory::GetForProfile(profile_); | 673 PushMessagingPermissionContextFactory::GetForProfile(profile_); |
| 674 return ToPushPermission(permission_context->GetPermissionStatus( | 674 return ToPushPermission(permission_context->GetPermissionStatus( |
| 675 requesting_origin, embedding_origin)); | 675 requesting_origin, embedding_origin)); |
| 676 } | 676 } |
| 677 | 677 |
| 678 void PushMessagingServiceImpl::RegisterEnd( | 678 void PushMessagingServiceImpl::SubscribeEnd( |
| 679 const content::PushMessagingService::RegisterCallback& callback, | 679 const content::PushMessagingService::SubscribeCallback& callback, |
| 680 const std::string& registration_id, | 680 const std::string& subscription_id, |
| 681 content::PushRegistrationStatus status) { | 681 content::PushSubscriptionStatus status) { |
| 682 callback.Run(registration_id, status); | 682 callback.Run(subscription_id, status); |
| 683 } | 683 } |
| 684 | 684 |
| 685 void PushMessagingServiceImpl::DidRegister( | 685 void PushMessagingServiceImpl::DidSubscribe( |
| 686 const PushMessagingApplicationId& application_id, | 686 const PushMessagingApplicationId& application_id, |
| 687 const content::PushMessagingService::RegisterCallback& callback, | 687 const content::PushMessagingService::SubscribeCallback& callback, |
| 688 const std::string& registration_id, | 688 const std::string& subscription_id, |
| 689 gcm::GCMClient::Result result) { | 689 gcm::GCMClient::Result result) { |
| 690 content::PushRegistrationStatus status = | 690 content::PushSubscriptionStatus status = |
| 691 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 691 content::PUSH_SUBSCRIPTION_STATUS_SERVICE_ERROR; |
| 692 switch (result) { | 692 switch (result) { |
| 693 case gcm::GCMClient::SUCCESS: | 693 case gcm::GCMClient::SUCCESS: |
| 694 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; | 694 status = content::PUSH_SUBSCRIPTION_STATUS_SUCCESS_FROM_PUSH_SERVICE; |
| 695 application_id.PersistToDisk(profile_); | 695 application_id.PersistToDisk(profile_); |
| 696 IncreasePushRegistrationCount(1, false /* is_pending */); | 696 IncreasePushSubscriptionCount(1, false /* is_pending */); |
| 697 break; | 697 break; |
| 698 case gcm::GCMClient::INVALID_PARAMETER: | 698 case gcm::GCMClient::INVALID_PARAMETER: |
| 699 case gcm::GCMClient::GCM_DISABLED: | 699 case gcm::GCMClient::GCM_DISABLED: |
| 700 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 700 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
| 701 case gcm::GCMClient::SERVER_ERROR: | 701 case gcm::GCMClient::SERVER_ERROR: |
| 702 case gcm::GCMClient::UNKNOWN_ERROR: | 702 case gcm::GCMClient::UNKNOWN_ERROR: |
| 703 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 703 status = content::PUSH_SUBSCRIPTION_STATUS_SERVICE_ERROR; |
| 704 break; | 704 break; |
| 705 case gcm::GCMClient::NETWORK_ERROR: | 705 case gcm::GCMClient::NETWORK_ERROR: |
| 706 case gcm::GCMClient::TTL_EXCEEDED: | 706 case gcm::GCMClient::TTL_EXCEEDED: |
| 707 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; | 707 status = content::PUSH_SUBSCRIPTION_STATUS_NETWORK_ERROR; |
| 708 break; | 708 break; |
| 709 } | 709 } |
| 710 RegisterEnd(callback, registration_id, status); | 710 SubscribeEnd(callback, subscription_id, status); |
| 711 DecreasePushRegistrationCount(1, true /* was_pending */); | 711 DecreasePushSubscriptionCount(1, true /* was_pending */); |
| 712 } | 712 } |
| 713 | 713 |
| 714 void PushMessagingServiceImpl::DidRequestPermission( | 714 void PushMessagingServiceImpl::DidRequestPermission( |
| 715 const PushMessagingApplicationId& application_id, | 715 const PushMessagingApplicationId& application_id, |
| 716 const std::string& sender_id, | 716 const std::string& sender_id, |
| 717 const content::PushMessagingService::RegisterCallback& register_callback, | 717 const content::PushMessagingService::SubscribeCallback& callback, |
| 718 ContentSetting content_setting) { | 718 ContentSetting content_setting) { |
| 719 if (content_setting != CONTENT_SETTING_ALLOW) { | 719 if (content_setting != CONTENT_SETTING_ALLOW) { |
| 720 RegisterEnd(register_callback, | 720 SubscribeEnd(callback, std::string(), |
| 721 std::string(), | 721 content::PUSH_SUBSCRIPTION_STATUS_PERMISSION_DENIED); |
| 722 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | |
| 723 return; | 722 return; |
| 724 } | 723 } |
| 725 | 724 |
| 726 IncreasePushRegistrationCount(1, true /* is_pending */); | 725 IncreasePushSubscriptionCount(1, true /* is_pending */); |
| 727 std::vector<std::string> sender_ids(1, sender_id); | 726 std::vector<std::string> sender_ids(1, sender_id); |
| 728 GetGCMDriver()->Register( | 727 GetGCMDriver()->Register( |
| 729 application_id.app_id_guid(), | 728 application_id.app_id_guid(), |
| 730 sender_ids, | 729 sender_ids, |
| 731 base::Bind(&PushMessagingServiceImpl::DidRegister, | 730 base::Bind(&PushMessagingServiceImpl::DidSubscribe, |
| 732 weak_factory_.GetWeakPtr(), | 731 weak_factory_.GetWeakPtr(), |
| 733 application_id, register_callback)); | 732 application_id, callback)); |
| 734 } | 733 } |
| 735 | 734 |
| 736 // Unregister methods ---------------------------------------------------------- | 735 // Unsubscribe methods --------------------------------------------------------- |
| 737 | 736 |
| 738 void PushMessagingServiceImpl::Unregister( | 737 void PushMessagingServiceImpl::Unsubscribe( |
| 739 const GURL& requesting_origin, | 738 const GURL& requesting_origin, |
| 740 int64 service_worker_registration_id, | 739 int64 service_worker_registration_id, |
| 741 const std::string& sender_id, | 740 const std::string& sender_id, |
| 742 const content::PushMessagingService::UnregisterCallback& callback) { | 741 const content::PushMessagingService::UnsubscribeCallback& callback) { |
| 743 PushMessagingApplicationId application_id = PushMessagingApplicationId::Get( | 742 PushMessagingApplicationId application_id = PushMessagingApplicationId::Get( |
| 744 profile_, requesting_origin, service_worker_registration_id); | 743 profile_, requesting_origin, service_worker_registration_id); |
| 745 if (!application_id.IsValid()) { | 744 if (!application_id.IsValid()) { |
| 746 if (!callback.is_null()) { | 745 if (!callback.is_null()) { |
| 747 callback.Run( | 746 callback.Run( |
| 748 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); | 747 content::PUSH_UNSUBSCRIPTION_STATUS_SUCCESS_WAS_NOT_SUBSCRIBED); |
| 749 } | 748 } |
| 750 return; | 749 return; |
| 751 } | 750 } |
| 752 | 751 |
| 753 Unregister(application_id.app_id_guid(), sender_id, callback); | 752 Unsubscribe(application_id.app_id_guid(), sender_id, callback); |
| 754 } | 753 } |
| 755 | 754 |
| 756 void PushMessagingServiceImpl::Unregister( | 755 void PushMessagingServiceImpl::Unsubscribe( |
| 757 const std::string& app_id_guid, | 756 const std::string& app_id_guid, |
| 758 const std::string& sender_id, | 757 const std::string& sender_id, |
| 759 const content::PushMessagingService::UnregisterCallback& callback) { | 758 const content::PushMessagingService::UnsubscribeCallback& callback) { |
| 760 // Delete the mapping for this app id, to guarantee that no messages get | 759 // Delete the mapping for this app id, to guarantee that no messages get |
| 761 // delivered in future (even if unregistration fails). | 760 // delivered in future (even if unsubscription fails). |
| 762 // TODO(johnme): Instead of deleting these app ids, store them elsewhere, and | 761 // TODO(johnme): Instead of deleting these app ids, store them elsewhere, and |
| 763 // retry unregistration if it fails due to network errors (crbug.com/465399). | 762 // retry unsubscription if it fails due to network errors (crbug.com/465399). |
| 764 PushMessagingApplicationId application_id = | 763 PushMessagingApplicationId application_id = |
| 765 PushMessagingApplicationId::Get(profile_, app_id_guid); | 764 PushMessagingApplicationId::Get(profile_, app_id_guid); |
| 766 bool was_registered = application_id.IsValid(); | 765 bool was_subscribed = application_id.IsValid(); |
| 767 if (was_registered) | 766 if (was_subscribed) |
| 768 application_id.DeleteFromDisk(profile_); | 767 application_id.DeleteFromDisk(profile_); |
| 769 | 768 |
| 770 const auto& unregister_callback = | 769 const auto& unsubscribe_callback = |
| 771 base::Bind(&PushMessagingServiceImpl::DidUnregister, | 770 base::Bind(&PushMessagingServiceImpl::DidUnsubscribe, |
| 772 weak_factory_.GetWeakPtr(), | 771 weak_factory_.GetWeakPtr(), |
| 773 was_registered, callback); | 772 was_subscribed, callback); |
| 774 #if defined(OS_ANDROID) | 773 #if defined(OS_ANDROID) |
| 775 // On Android the backend is different, and requires the original sender_id. | 774 // On Android the backend is different, and requires the original sender_id. |
| 776 // UnregisterBecausePermissionRevoked sometimes calls us with an empty one. | 775 // UnsubscribeBecausePermissionRevoked sometimes calls us with an empty one. |
| 777 if (sender_id.empty()) | 776 if (sender_id.empty()) |
| 778 unregister_callback.Run(gcm::GCMClient::INVALID_PARAMETER); | 777 unsubscribe_callback.Run(gcm::GCMClient::INVALID_PARAMETER); |
| 779 else | 778 else |
| 780 GetGCMDriver()->UnregisterWithSenderId(app_id_guid, sender_id, | 779 GetGCMDriver()->UnregisterWithSenderId(app_id_guid, sender_id, |
| 781 unregister_callback); | 780 unsubscribe_callback); |
| 782 #else | 781 #else |
| 783 GetGCMDriver()->Unregister(app_id_guid, unregister_callback); | 782 GetGCMDriver()->Unregister(app_id_guid, unsubscribe_callback); |
| 784 #endif | 783 #endif |
| 785 } | 784 } |
| 786 | 785 |
| 787 void PushMessagingServiceImpl::DidUnregister( | 786 void PushMessagingServiceImpl::DidUnsubscribe( |
| 788 bool was_registered, | 787 bool was_subscribed, |
| 789 const content::PushMessagingService::UnregisterCallback& callback, | 788 const content::PushMessagingService::UnsubscribeCallback& callback, |
| 790 gcm::GCMClient::Result result) { | 789 gcm::GCMClient::Result result) { |
| 791 if (was_registered) | 790 if (was_subscribed) |
| 792 DecreasePushRegistrationCount(1, false /* was_pending */); | 791 DecreasePushSubscriptionCount(1, false /* was_pending */); |
| 793 | 792 |
| 794 // Internal calls pass a null callback. | 793 // Internal calls pass a null callback. |
| 795 if (callback.is_null()) | 794 if (callback.is_null()) |
| 796 return; | 795 return; |
| 797 | 796 |
| 798 if (!was_registered) { | 797 if (!was_subscribed) { |
| 799 callback.Run( | 798 callback.Run( |
| 800 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); | 799 content::PUSH_UNSUBSCRIPTION_STATUS_SUCCESS_WAS_NOT_SUBSCRIBED); |
| 801 return; | 800 return; |
| 802 } | 801 } |
| 803 switch (result) { | 802 switch (result) { |
| 804 case gcm::GCMClient::SUCCESS: | 803 case gcm::GCMClient::SUCCESS: |
| 805 callback.Run(content::PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTERED); | 804 callback.Run(content::PUSH_UNSUBSCRIPTION_STATUS_SUCCESS_UNSUBSCRIBED); |
| 806 break; | 805 break; |
| 807 case gcm::GCMClient::INVALID_PARAMETER: | 806 case gcm::GCMClient::INVALID_PARAMETER: |
| 808 case gcm::GCMClient::GCM_DISABLED: | 807 case gcm::GCMClient::GCM_DISABLED: |
| 809 case gcm::GCMClient::SERVER_ERROR: | 808 case gcm::GCMClient::SERVER_ERROR: |
| 810 case gcm::GCMClient::UNKNOWN_ERROR: | 809 case gcm::GCMClient::UNKNOWN_ERROR: |
| 811 callback.Run(content::PUSH_UNREGISTRATION_STATUS_PENDING_SERVICE_ERROR); | 810 callback.Run(content::PUSH_UNSUBSCRIPTION_STATUS_PENDING_SERVICE_ERROR); |
| 812 break; | 811 break; |
| 813 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 812 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
| 814 case gcm::GCMClient::NETWORK_ERROR: | 813 case gcm::GCMClient::NETWORK_ERROR: |
| 815 case gcm::GCMClient::TTL_EXCEEDED: | 814 case gcm::GCMClient::TTL_EXCEEDED: |
| 816 callback.Run(content::PUSH_UNREGISTRATION_STATUS_PENDING_NETWORK_ERROR); | 815 callback.Run(content::PUSH_UNSUBSCRIPTION_STATUS_PENDING_NETWORK_ERROR); |
| 817 break; | 816 break; |
| 818 } | 817 } |
| 819 } | 818 } |
| 820 | 819 |
| 821 // OnContentSettingChanged methods --------------------------------------------- | 820 // OnContentSettingChanged methods --------------------------------------------- |
| 822 | 821 |
| 823 void PushMessagingServiceImpl::OnContentSettingChanged( | 822 void PushMessagingServiceImpl::OnContentSettingChanged( |
| 824 const ContentSettingsPattern& primary_pattern, | 823 const ContentSettingsPattern& primary_pattern, |
| 825 const ContentSettingsPattern& secondary_pattern, | 824 const ContentSettingsPattern& secondary_pattern, |
| 826 ContentSettingsType content_type, | 825 ContentSettingsType content_type, |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 851 } | 850 } |
| 852 | 851 |
| 853 if (HasPermission(id.origin())) { | 852 if (HasPermission(id.origin())) { |
| 854 barrier_closure.Run(); | 853 barrier_closure.Run(); |
| 855 continue; | 854 continue; |
| 856 } | 855 } |
| 857 | 856 |
| 858 GetSenderId( | 857 GetSenderId( |
| 859 profile_, id.origin(), id.service_worker_registration_id(), | 858 profile_, id.origin(), id.service_worker_registration_id(), |
| 860 base::Bind( | 859 base::Bind( |
| 861 &PushMessagingServiceImpl::UnregisterBecausePermissionRevoked, | 860 &PushMessagingServiceImpl::UnsubscribeBecausePermissionRevoked, |
| 862 weak_factory_.GetWeakPtr(), id, barrier_closure)); | 861 weak_factory_.GetWeakPtr(), id, barrier_closure)); |
| 863 } | 862 } |
| 864 } | 863 } |
| 865 | 864 |
| 866 void PushMessagingServiceImpl::UnregisterBecausePermissionRevoked( | 865 void PushMessagingServiceImpl::UnsubscribeBecausePermissionRevoked( |
| 867 const PushMessagingApplicationId& id, const base::Closure& closure, | 866 const PushMessagingApplicationId& id, const base::Closure& closure, |
| 868 const std::string& sender_id, bool success, bool not_found) { | 867 const std::string& sender_id, bool success, bool not_found) { |
| 869 base::Closure barrier_closure = base::BarrierClosure(2, closure); | 868 base::Closure barrier_closure = base::BarrierClosure(2, closure); |
| 870 | 869 |
| 871 // Unregister the PushMessagingApplicationId with the push service. | 870 // Unsubscribe the PushMessagingApplicationId with the push service. |
| 872 // It's possible for GetSenderId to have failed and sender_id to be empty, if | 871 // It's possible for GetSenderId to have failed and sender_id to be empty, if |
| 873 // cookies (and the SW database) for an origin got cleared before permissions | 872 // cookies (and the SW database) for an origin got cleared before permissions |
| 874 // are cleared for the origin. In that case Unregister will just delete the | 873 // are cleared for the origin. In that case Unubscribe will just delete the |
| 875 // application ID to block future messages. | 874 // application ID to block future messages. |
| 876 // TODO(johnme): Auto-unregister before SW DB is cleared | 875 // TODO(johnme): Auto-unsubscribe before SW DB is cleared |
| 877 // (https://crbug.com/402458). | 876 // (https://crbug.com/402458). |
| 878 Unregister(id.app_id_guid(), sender_id, | 877 Unsubscribe(id.app_id_guid(), sender_id, |
| 879 base::Bind(&UnregisterCallbackToClosure, barrier_closure)); | 878 base::Bind(&UnsubscribeCallbackToClosure, barrier_closure)); |
| 880 | 879 |
| 881 // Clear the associated service worker push registration id. | 880 // Clear the associated service worker push subscription id. |
| 882 ClearPushRegistrationID(profile_, id.origin(), | 881 ClearPushSubscriptionID(profile_, id.origin(), |
| 883 id.service_worker_registration_id(), barrier_closure); | 882 id.service_worker_registration_id(), barrier_closure); |
| 884 } | 883 } |
| 885 | 884 |
| 886 void PushMessagingServiceImpl::SetContentSettingChangedCallbackForTesting( | 885 void PushMessagingServiceImpl::SetContentSettingChangedCallbackForTesting( |
| 887 const base::Closure& callback) { | 886 const base::Closure& callback) { |
| 888 content_setting_changed_callback_for_testing_ = callback; | 887 content_setting_changed_callback_for_testing_ = callback; |
| 889 } | 888 } |
| 890 | 889 |
| 891 // KeyedService methods ------------------------------------------------------- | 890 // KeyedService methods ------------------------------------------------------- |
| 892 | 891 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 905 CONTENT_SETTING_ALLOW; | 904 CONTENT_SETTING_ALLOW; |
| 906 } | 905 } |
| 907 | 906 |
| 908 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 907 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { |
| 909 gcm::GCMProfileService* gcm_profile_service = | 908 gcm::GCMProfileService* gcm_profile_service = |
| 910 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 909 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
| 911 CHECK(gcm_profile_service); | 910 CHECK(gcm_profile_service); |
| 912 CHECK(gcm_profile_service->driver()); | 911 CHECK(gcm_profile_service->driver()); |
| 913 return gcm_profile_service->driver(); | 912 return gcm_profile_service->driver(); |
| 914 } | 913 } |
| OLD | NEW |