| 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 "content/browser/push_messaging/push_messaging_manager.h" | 5 #include "content/browser/push_messaging/push_messaging_manager.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 return sender_info; | 96 return sender_info; |
| 97 if (base::ContainsOnlyChars(stored_sender_id, "0123456789")) | 97 if (base::ContainsOnlyChars(stored_sender_id, "0123456789")) |
| 98 return stored_sender_id; | 98 return stored_sender_id; |
| 99 return std::string(); | 99 return std::string(); |
| 100 } | 100 } |
| 101 | 101 |
| 102 } // namespace | 102 } // namespace |
| 103 | 103 |
| 104 struct PushMessagingManager::RegisterData { | 104 struct PushMessagingManager::RegisterData { |
| 105 RegisterData(); | 105 RegisterData(); |
| 106 RegisterData(const RegisterData& other) = default; | 106 RegisterData(RegisterData&& other) = default; |
| 107 bool FromDocument() const; | 107 bool FromDocument() const; |
| 108 GURL requesting_origin; | 108 GURL requesting_origin; |
| 109 int64_t service_worker_registration_id; | 109 int64_t service_worker_registration_id; |
| 110 PushSubscriptionOptions options; | 110 PushSubscriptionOptions options; |
| 111 SubscribeCallback callback; | 111 SubscribeCallback callback; |
| 112 // The following member should only be read if FromDocument() is true. | 112 // The following member should only be read if FromDocument() is true. |
| 113 int render_frame_id; | 113 int render_frame_id; |
| 114 }; | 114 }; |
| 115 | 115 |
| 116 // Inner core of the PushMessagingManager which lives on the UI thread. | 116 // Inner core of the PushMessagingManager which lives on the UI thread. |
| 117 class PushMessagingManager::Core { | 117 class PushMessagingManager::Core { |
| 118 public: | 118 public: |
| 119 Core(const base::WeakPtr<PushMessagingManager>& io_parent, | 119 Core(const base::WeakPtr<PushMessagingManager>& io_parent, |
| 120 int render_process_id); | 120 int render_process_id); |
| 121 | 121 |
| 122 // Public Register methods on UI thread -------------------------------------- | 122 // Public Register methods on UI thread -------------------------------------- |
| 123 | 123 |
| 124 // Callback called on UI thread. | 124 // Callback called on UI thread. |
| 125 void SubscribeDidGetInfoOnUI(const RegisterData& data, | 125 void SubscribeDidGetInfoOnUI(RegisterData data, |
| 126 const std::string& push_subscription_id, | 126 const std::string& push_subscription_id, |
| 127 const std::string& sender_id, | 127 const std::string& sender_id, |
| 128 bool is_valid, | 128 bool is_valid, |
| 129 const std::vector<uint8_t>& p256dh, | 129 const std::vector<uint8_t>& p256dh, |
| 130 const std::vector<uint8_t>& auth); | 130 const std::vector<uint8_t>& auth); |
| 131 | 131 |
| 132 // Called via PostTask from IO thread. | 132 // Called via PostTask from IO thread. |
| 133 void RegisterOnUI(const RegisterData& data); | 133 void RegisterOnUI(RegisterData data); |
| 134 | 134 |
| 135 // Public Unregister methods on UI thread ------------------------------------ | 135 // Public Unregister methods on UI thread ------------------------------------ |
| 136 | 136 |
| 137 // Called via PostTask from IO thread. | 137 // Called via PostTask from IO thread. |
| 138 void UnregisterFromService(const UnsubscribeCallback& callback, | 138 void UnregisterFromService(UnsubscribeCallback callback, |
| 139 int64_t service_worker_registration_id, | 139 int64_t service_worker_registration_id, |
| 140 const GURL& requesting_origin, | 140 const GURL& requesting_origin, |
| 141 const std::string& sender_id); | 141 const std::string& sender_id); |
| 142 | 142 |
| 143 // Public GetSubscription methods on UI thread ------------------------------- | 143 // Public GetSubscription methods on UI thread ------------------------------- |
| 144 | 144 |
| 145 // Callback called on UI thread. | 145 // Callback called on UI thread. |
| 146 void GetSubscriptionDidGetInfoOnUI(const GetSubscriptionCallback& callback, | 146 void GetSubscriptionDidGetInfoOnUI(GetSubscriptionCallback callback, |
| 147 const GURL& origin, | 147 const GURL& origin, |
| 148 int64_t service_worker_registration_id, | 148 int64_t service_worker_registration_id, |
| 149 const GURL& endpoint, | 149 const GURL& endpoint, |
| 150 const std::string& sender_info, | 150 const std::string& sender_info, |
| 151 bool is_valid, | 151 bool is_valid, |
| 152 const std::vector<uint8_t>& p256dh, | 152 const std::vector<uint8_t>& p256dh, |
| 153 const std::vector<uint8_t>& auth); | 153 const std::vector<uint8_t>& auth); |
| 154 | 154 |
| 155 // Callback called on UI thread. | 155 // Callback called on UI thread. |
| 156 void GetSubscriptionDidUnsubscribe( | 156 void GetSubscriptionDidUnsubscribe( |
| 157 const GetSubscriptionCallback& callback, | 157 GetSubscriptionCallback callback, |
| 158 PushGetRegistrationStatus get_status, | 158 PushGetRegistrationStatus get_status, |
| 159 PushUnregistrationStatus unsubscribe_status); | 159 PushUnregistrationStatus unsubscribe_status); |
| 160 | 160 |
| 161 // Public GetPermission methods on UI thread --------------------------------- | 161 // Public GetPermission methods on UI thread --------------------------------- |
| 162 | 162 |
| 163 // Called via PostTask from IO thread. | 163 // Called via PostTask from IO thread. |
| 164 void GetPermissionStatusOnUI(const GetPermissionStatusCallback& callback, | 164 void GetPermissionStatusOnUI(GetPermissionStatusCallback callback, |
| 165 const GURL& requesting_origin, | 165 const GURL& requesting_origin, |
| 166 bool user_visible); | 166 bool user_visible); |
| 167 | 167 |
| 168 // Public helper methods on UI thread ---------------------------------------- | 168 // Public helper methods on UI thread ---------------------------------------- |
| 169 | 169 |
| 170 // Called via PostTask from IO thread. |callback| will be run on UI thread. | 170 // Called via PostTask from IO thread. |callback| will be run on UI thread. |
| 171 void GetSubscriptionInfoOnUI( | 171 void GetSubscriptionInfoOnUI( |
| 172 const GURL& origin, | 172 const GURL& origin, |
| 173 int64_t service_worker_registration_id, | 173 int64_t service_worker_registration_id, |
| 174 const std::string& sender_id, | 174 const std::string& sender_id, |
| 175 const std::string& push_subscription_id, | 175 const std::string& push_subscription_id, |
| 176 const PushMessagingService::SubscriptionInfoCallback& callback); | 176 PushMessagingService::SubscriptionInfoCallback callback); |
| 177 | 177 |
| 178 // Called (directly) from both the UI and IO threads. | 178 // Called (directly) from both the UI and IO threads. |
| 179 bool is_incognito() const { return is_incognito_; } | 179 bool is_incognito() const { return is_incognito_; } |
| 180 | 180 |
| 181 // Returns a push messaging service. May return null. | 181 // Returns a push messaging service. May return null. |
| 182 PushMessagingService* service(); | 182 PushMessagingService* service(); |
| 183 | 183 |
| 184 // Returns a weak ptr. Must only be called on the UI thread (and hence can | 184 // Returns a weak ptr. Must only be called on the UI thread (and hence can |
| 185 // only be called from the outer class's constructor). | 185 // only be called from the outer class's constructor). |
| 186 base::WeakPtr<Core> GetWeakPtrFromIOParentConstructor(); | 186 base::WeakPtr<Core> GetWeakPtrFromIOParentConstructor(); |
| 187 | 187 |
| 188 private: | 188 private: |
| 189 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 189 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
| 190 friend class base::DeleteHelper<Core>; | 190 friend class base::DeleteHelper<Core>; |
| 191 | 191 |
| 192 ~Core(); | 192 ~Core(); |
| 193 | 193 |
| 194 // Private Register methods on UI thread ------------------------------------- | 194 // Private Register methods on UI thread ------------------------------------- |
| 195 | 195 |
| 196 void DidRequestPermissionInIncognito(const RegisterData& data, | 196 void DidRequestPermissionInIncognito(RegisterData data, |
| 197 blink::mojom::PermissionStatus status); | 197 blink::mojom::PermissionStatus status); |
| 198 | 198 |
| 199 void DidRegister(const RegisterData& data, | 199 void DidRegister(RegisterData data, |
| 200 const std::string& push_registration_id, | 200 const std::string& push_registration_id, |
| 201 const std::vector<uint8_t>& p256dh, | 201 const std::vector<uint8_t>& p256dh, |
| 202 const std::vector<uint8_t>& auth, | 202 const std::vector<uint8_t>& auth, |
| 203 PushRegistrationStatus status); | 203 PushRegistrationStatus status); |
| 204 | 204 |
| 205 // Private Unregister methods on UI thread ----------------------------------- | 205 // Private Unregister methods on UI thread ----------------------------------- |
| 206 | 206 |
| 207 void DidUnregisterFromService(const UnsubscribeCallback& callback, | 207 void DidUnregisterFromService(UnsubscribeCallback callback, |
| 208 int64_t service_worker_registration_id, | 208 int64_t service_worker_registration_id, |
| 209 PushUnregistrationStatus unregistration_status); | 209 PushUnregistrationStatus unregistration_status); |
| 210 | 210 |
| 211 // Outer part of the PushMessagingManager which lives on the IO thread. | 211 // Outer part of the PushMessagingManager which lives on the IO thread. |
| 212 base::WeakPtr<PushMessagingManager> io_parent_; | 212 base::WeakPtr<PushMessagingManager> io_parent_; |
| 213 | 213 |
| 214 int render_process_id_; | 214 int render_process_id_; |
| 215 | 215 |
| 216 bool is_incognito_; | 216 bool is_incognito_; |
| 217 | 217 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 bindings_.AddBinding(this, std::move(request)); | 272 bindings_.AddBinding(this, std::move(request)); |
| 273 } | 273 } |
| 274 | 274 |
| 275 // Subscribe methods on both IO and UI threads, merged in order of use from | 275 // Subscribe methods on both IO and UI threads, merged in order of use from |
| 276 // PushMessagingManager and Core. | 276 // PushMessagingManager and Core. |
| 277 // ----------------------------------------------------------------------------- | 277 // ----------------------------------------------------------------------------- |
| 278 | 278 |
| 279 void PushMessagingManager::Subscribe(int32_t render_frame_id, | 279 void PushMessagingManager::Subscribe(int32_t render_frame_id, |
| 280 int64_t service_worker_registration_id, | 280 int64_t service_worker_registration_id, |
| 281 const PushSubscriptionOptions& options, | 281 const PushSubscriptionOptions& options, |
| 282 const SubscribeCallback& callback) { | 282 SubscribeCallback callback) { |
| 283 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 283 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 284 // TODO(mvanouwerkerk): Validate arguments? | 284 // TODO(mvanouwerkerk): Validate arguments? |
| 285 RegisterData data; | 285 RegisterData data; |
| 286 | 286 |
| 287 // Will be ChildProcessHost::kInvalidUniqueID in requests from Service Worker. | 287 // Will be ChildProcessHost::kInvalidUniqueID in requests from Service Worker. |
| 288 data.render_frame_id = render_frame_id; | 288 data.render_frame_id = render_frame_id; |
| 289 | 289 |
| 290 data.service_worker_registration_id = service_worker_registration_id; | 290 data.service_worker_registration_id = service_worker_registration_id; |
| 291 data.callback = callback; | 291 data.callback = std::move(callback); |
| 292 data.options = options; | 292 data.options = options; |
| 293 | 293 |
| 294 ServiceWorkerRegistration* service_worker_registration = | 294 ServiceWorkerRegistration* service_worker_registration = |
| 295 service_worker_context_->GetLiveRegistration( | 295 service_worker_context_->GetLiveRegistration( |
| 296 data.service_worker_registration_id); | 296 data.service_worker_registration_id); |
| 297 if (!service_worker_registration || | 297 if (!service_worker_registration || |
| 298 !service_worker_registration->active_version()) { | 298 !service_worker_registration->active_version()) { |
| 299 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_NO_SERVICE_WORKER); | 299 SendSubscriptionError(std::move(data), |
| 300 PUSH_REGISTRATION_STATUS_NO_SERVICE_WORKER); |
| 300 return; | 301 return; |
| 301 } | 302 } |
| 302 data.requesting_origin = service_worker_registration->pattern().GetOrigin(); | 303 data.requesting_origin = service_worker_registration->pattern().GetOrigin(); |
| 303 | 304 |
| 304 DCHECK(!(data.options.sender_info.empty() && data.FromDocument())); | 305 DCHECK(!(data.options.sender_info.empty() && data.FromDocument())); |
| 305 | 306 |
| 307 int64_t registration_id = data.service_worker_registration_id; |
| 306 service_worker_context_->GetRegistrationUserData( | 308 service_worker_context_->GetRegistrationUserData( |
| 307 data.service_worker_registration_id, | 309 registration_id, |
| 308 {kPushRegistrationIdServiceWorkerKey, kPushSenderIdServiceWorkerKey}, | 310 {kPushRegistrationIdServiceWorkerKey, kPushSenderIdServiceWorkerKey}, |
| 309 base::Bind(&PushMessagingManager::DidCheckForExistingRegistration, | 311 base::Bind(&PushMessagingManager::DidCheckForExistingRegistration, |
| 310 weak_factory_io_to_io_.GetWeakPtr(), data)); | 312 weak_factory_io_to_io_.GetWeakPtr(), base::Passed(&data))); |
| 311 } | 313 } |
| 312 | 314 |
| 313 void PushMessagingManager::DidCheckForExistingRegistration( | 315 void PushMessagingManager::DidCheckForExistingRegistration( |
| 314 const RegisterData& data, | 316 RegisterData data, |
| 315 const std::vector<std::string>& push_registration_id_and_sender_id, | 317 const std::vector<std::string>& push_registration_id_and_sender_id, |
| 316 ServiceWorkerStatusCode service_worker_status) { | 318 ServiceWorkerStatusCode service_worker_status) { |
| 317 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 319 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 318 if (service_worker_status == SERVICE_WORKER_OK) { | 320 if (service_worker_status == SERVICE_WORKER_OK) { |
| 319 DCHECK_EQ(2u, push_registration_id_and_sender_id.size()); | 321 DCHECK_EQ(2u, push_registration_id_and_sender_id.size()); |
| 320 const auto& push_subscription_id = push_registration_id_and_sender_id[0]; | 322 const auto& push_subscription_id = push_registration_id_and_sender_id[0]; |
| 321 const auto& stored_sender_id = push_registration_id_and_sender_id[1]; | 323 const auto& stored_sender_id = push_registration_id_and_sender_id[1]; |
| 322 std::string fixed_sender_id = | 324 std::string fixed_sender_id = |
| 323 FixSenderInfo(data.options.sender_info, stored_sender_id); | 325 FixSenderInfo(data.options.sender_info, stored_sender_id); |
| 324 if (fixed_sender_id.empty()) { | 326 if (fixed_sender_id.empty()) { |
| 325 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | 327 SendSubscriptionError(std::move(data), |
| 328 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); |
| 326 return; | 329 return; |
| 327 } | 330 } |
| 328 if (fixed_sender_id != stored_sender_id) { | 331 if (fixed_sender_id != stored_sender_id) { |
| 329 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_SENDER_ID_MISMATCH); | 332 SendSubscriptionError(std::move(data), |
| 333 PUSH_REGISTRATION_STATUS_SENDER_ID_MISMATCH); |
| 330 return; | 334 return; |
| 331 } | 335 } |
| 336 |
| 337 GURL requesting_origin = data.requesting_origin; |
| 338 int64_t registration_id = data.service_worker_registration_id; |
| 332 BrowserThread::PostTask( | 339 BrowserThread::PostTask( |
| 333 BrowserThread::UI, FROM_HERE, | 340 BrowserThread::UI, FROM_HERE, |
| 334 base::Bind(&Core::GetSubscriptionInfoOnUI, | 341 base::Bind(&Core::GetSubscriptionInfoOnUI, |
| 335 base::Unretained(ui_core_.get()), data.requesting_origin, | 342 base::Unretained(ui_core_.get()), requesting_origin, |
| 336 data.service_worker_registration_id, fixed_sender_id, | 343 registration_id, fixed_sender_id, push_subscription_id, |
| 337 push_subscription_id, | |
| 338 base::Bind(&Core::SubscribeDidGetInfoOnUI, ui_core_weak_ptr_, | 344 base::Bind(&Core::SubscribeDidGetInfoOnUI, ui_core_weak_ptr_, |
| 339 data, push_subscription_id, fixed_sender_id))); | 345 base::Passed(&data), push_subscription_id, |
| 346 fixed_sender_id))); |
| 340 return; | 347 return; |
| 341 } | 348 } |
| 342 // TODO(johnme): The spec allows the register algorithm to reject with an | 349 // TODO(johnme): The spec allows the register algorithm to reject with an |
| 343 // AbortError when accessing storage fails. Perhaps we should do that if | 350 // AbortError when accessing storage fails. Perhaps we should do that if |
| 344 // service_worker_status != SERVICE_WORKER_ERROR_NOT_FOUND instead of | 351 // service_worker_status != SERVICE_WORKER_ERROR_NOT_FOUND instead of |
| 345 // attempting to do a fresh registration? | 352 // attempting to do a fresh registration? |
| 346 // https://w3c.github.io/push-api/#widl-PushRegistrationManager-register-Promi
se-PushRegistration | 353 // https://w3c.github.io/push-api/#widl-PushRegistrationManager-register-Promi
se-PushRegistration |
| 347 if (!data.options.sender_info.empty()) { | 354 if (!data.options.sender_info.empty()) { |
| 348 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 355 BrowserThread::PostTask( |
| 349 base::Bind(&Core::RegisterOnUI, | 356 BrowserThread::UI, FROM_HERE, |
| 350 base::Unretained(ui_core_.get()), data)); | 357 base::Bind(&Core::RegisterOnUI, base::Unretained(ui_core_.get()), |
| 358 base::Passed(&data))); |
| 351 } else { | 359 } else { |
| 352 // There is no existing registration and the sender_info passed in was | 360 // There is no existing registration and the sender_info passed in was |
| 353 // empty, but perhaps there is a stored sender id we can use. | 361 // empty, but perhaps there is a stored sender id we can use. |
| 362 int64_t registration_id = data.service_worker_registration_id; |
| 354 service_worker_context_->GetRegistrationUserData( | 363 service_worker_context_->GetRegistrationUserData( |
| 355 data.service_worker_registration_id, {kPushSenderIdServiceWorkerKey}, | 364 registration_id, {kPushSenderIdServiceWorkerKey}, |
| 356 base::Bind(&PushMessagingManager::DidGetSenderIdFromStorage, | 365 base::Bind(&PushMessagingManager::DidGetSenderIdFromStorage, |
| 357 weak_factory_io_to_io_.GetWeakPtr(), data)); | 366 weak_factory_io_to_io_.GetWeakPtr(), base::Passed(&data))); |
| 358 } | 367 } |
| 359 } | 368 } |
| 360 | 369 |
| 361 void PushMessagingManager::Core::SubscribeDidGetInfoOnUI( | 370 void PushMessagingManager::Core::SubscribeDidGetInfoOnUI( |
| 362 const RegisterData& data, | 371 RegisterData data, |
| 363 const std::string& push_subscription_id, | 372 const std::string& push_subscription_id, |
| 364 const std::string& sender_id, | 373 const std::string& sender_id, |
| 365 bool is_valid, | 374 bool is_valid, |
| 366 const std::vector<uint8_t>& p256dh, | 375 const std::vector<uint8_t>& p256dh, |
| 367 const std::vector<uint8_t>& auth) { | 376 const std::vector<uint8_t>& auth) { |
| 368 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 377 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 369 if (is_valid) { | 378 if (is_valid) { |
| 370 BrowserThread::PostTask( | 379 BrowserThread::PostTask( |
| 371 BrowserThread::IO, FROM_HERE, | 380 BrowserThread::IO, FROM_HERE, |
| 372 base::Bind(&PushMessagingManager::SendSubscriptionSuccess, io_parent_, | 381 base::Bind(&PushMessagingManager::SendSubscriptionSuccess, io_parent_, |
| 373 data, PUSH_REGISTRATION_STATUS_SUCCESS_FROM_CACHE, | 382 base::Passed(&data), |
| 383 PUSH_REGISTRATION_STATUS_SUCCESS_FROM_CACHE, |
| 374 push_subscription_id, p256dh, auth)); | 384 push_subscription_id, p256dh, auth)); |
| 375 } else { | 385 } else { |
| 376 PushMessagingService* push_service = service(); | 386 PushMessagingService* push_service = service(); |
| 377 if (!push_service) { | 387 if (!push_service) { |
| 378 // Shouldn't be possible to have a stored push subscription in a profile | 388 // Shouldn't be possible to have a stored push subscription in a profile |
| 379 // with no push service, but this case can occur when the renderer is | 389 // with no push service, but this case can occur when the renderer is |
| 380 // shutting down. | 390 // shutting down. |
| 381 BrowserThread::PostTask( | 391 BrowserThread::PostTask( |
| 382 BrowserThread::IO, FROM_HERE, | 392 BrowserThread::IO, FROM_HERE, |
| 383 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, | 393 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 384 data, PUSH_REGISTRATION_STATUS_RENDERER_SHUTDOWN)); | 394 base::Passed(&data), |
| 395 PUSH_REGISTRATION_STATUS_RENDERER_SHUTDOWN)); |
| 385 return; | 396 return; |
| 386 } | 397 } |
| 387 | 398 |
| 388 // Uh-oh! Although there was a cached subscription in the Service Worker | 399 // Uh-oh! Although there was a cached subscription in the Service Worker |
| 389 // database, it did not have matching counterparts in the | 400 // database, it did not have matching counterparts in the |
| 390 // PushMessagingAppIdentifier map and/or GCM Store. Unsubscribe and | 401 // PushMessagingAppIdentifier map and/or GCM Store. Unsubscribe and |
| 391 // re-subscribe to fix this inconsistency. | 402 // re-subscribe to fix this inconsistency. |
| 392 | 403 |
| 393 // Consider this subscription attempt to have failed. The re-subscribe will | 404 // Consider this subscription attempt to have failed. The re-subscribe will |
| 394 // be logged to UMA as a separate subscription attempt. | 405 // be logged to UMA as a separate subscription attempt. |
| 395 RecordRegistrationStatus(PUSH_REGISTRATION_STATUS_STORAGE_CORRUPT); | 406 RecordRegistrationStatus(PUSH_REGISTRATION_STATUS_STORAGE_CORRUPT); |
| 396 | 407 |
| 408 int64_t registration_id = data.service_worker_registration_id; |
| 409 GURL requesting_origin = data.requesting_origin; |
| 397 auto try_again_on_io = base::Bind( | 410 auto try_again_on_io = base::Bind( |
| 398 &PushMessagingManager::DidCheckForExistingRegistration, io_parent_, | 411 &PushMessagingManager::DidCheckForExistingRegistration, io_parent_, |
| 399 data, | 412 base::Passed(&data), |
| 400 std::vector<std::string>() /* push_registration_id_and_sender_id */, | 413 std::vector<std::string>() /* push_registration_id_and_sender_id */, |
| 401 SERVICE_WORKER_ERROR_NOT_FOUND); | 414 SERVICE_WORKER_ERROR_NOT_FOUND); |
| 402 push_service->Unsubscribe( | 415 push_service->Unsubscribe( |
| 403 PUSH_UNREGISTRATION_REASON_SUBSCRIBE_STORAGE_CORRUPT, | 416 PUSH_UNREGISTRATION_REASON_SUBSCRIBE_STORAGE_CORRUPT, requesting_origin, |
| 404 data.requesting_origin, data.service_worker_registration_id, sender_id, | 417 registration_id, sender_id, |
| 405 base::Bind(&UnregisterCallbackToClosure, | 418 base::Bind(&UnregisterCallbackToClosure, |
| 406 base::Bind(IgnoreResult(&BrowserThread::PostTask), | 419 base::Bind(IgnoreResult(&BrowserThread::PostTask), |
| 407 BrowserThread::IO, FROM_HERE, try_again_on_io))); | 420 BrowserThread::IO, FROM_HERE, try_again_on_io))); |
| 408 } | 421 } |
| 409 } | 422 } |
| 410 | 423 |
| 411 void PushMessagingManager::DidGetSenderIdFromStorage( | 424 void PushMessagingManager::DidGetSenderIdFromStorage( |
| 412 const RegisterData& data, | 425 RegisterData data, |
| 413 const std::vector<std::string>& stored_sender_id, | 426 const std::vector<std::string>& stored_sender_id, |
| 414 ServiceWorkerStatusCode service_worker_status) { | 427 ServiceWorkerStatusCode service_worker_status) { |
| 415 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 428 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 416 if (service_worker_status != SERVICE_WORKER_OK) { | 429 if (service_worker_status != SERVICE_WORKER_OK) { |
| 417 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | 430 SendSubscriptionError(std::move(data), |
| 431 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); |
| 418 return; | 432 return; |
| 419 } | 433 } |
| 420 DCHECK_EQ(1u, stored_sender_id.size()); | 434 DCHECK_EQ(1u, stored_sender_id.size()); |
| 421 // We should only be here because no sender info was supplied to subscribe(). | 435 // We should only be here because no sender info was supplied to subscribe(). |
| 422 DCHECK(data.options.sender_info.empty()); | 436 DCHECK(data.options.sender_info.empty()); |
| 423 std::string fixed_sender_id = | 437 std::string fixed_sender_id = |
| 424 FixSenderInfo(data.options.sender_info, stored_sender_id[0]); | 438 FixSenderInfo(data.options.sender_info, stored_sender_id[0]); |
| 425 if (fixed_sender_id.empty()) { | 439 if (fixed_sender_id.empty()) { |
| 426 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | 440 SendSubscriptionError(std::move(data), |
| 441 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); |
| 427 return; | 442 return; |
| 428 } | 443 } |
| 429 RegisterData mutated_data = data; | 444 data.options.sender_info = fixed_sender_id; |
| 430 mutated_data.options.sender_info = fixed_sender_id; | |
| 431 BrowserThread::PostTask( | 445 BrowserThread::PostTask( |
| 432 BrowserThread::UI, FROM_HERE, | 446 BrowserThread::UI, FROM_HERE, |
| 433 base::Bind(&Core::RegisterOnUI, base::Unretained(ui_core_.get()), | 447 base::Bind(&Core::RegisterOnUI, base::Unretained(ui_core_.get()), |
| 434 mutated_data)); | 448 base::Passed(&data))); |
| 435 } | 449 } |
| 436 | 450 |
| 437 void PushMessagingManager::Core::RegisterOnUI( | 451 void PushMessagingManager::Core::RegisterOnUI( |
| 438 const PushMessagingManager::RegisterData& data) { | 452 PushMessagingManager::RegisterData data) { |
| 439 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 453 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 440 PushMessagingService* push_service = service(); | 454 PushMessagingService* push_service = service(); |
| 441 if (!push_service) { | 455 if (!push_service) { |
| 442 if (!is_incognito()) { | 456 if (!is_incognito()) { |
| 443 // This might happen if InstanceIDProfileService::IsInstanceIDEnabled | 457 // This might happen if InstanceIDProfileService::IsInstanceIDEnabled |
| 444 // returns false because the Instance ID kill switch was enabled. | 458 // returns false because the Instance ID kill switch was enabled. |
| 445 // TODO(johnme): Might be better not to expose the API in this case. | 459 // TODO(johnme): Might be better not to expose the API in this case. |
| 446 BrowserThread::PostTask( | 460 BrowserThread::PostTask( |
| 447 BrowserThread::IO, FROM_HERE, | 461 BrowserThread::IO, FROM_HERE, |
| 448 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, | 462 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 449 data, PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE)); | 463 base::Passed(&data), |
| 464 PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE)); |
| 450 } else { | 465 } else { |
| 451 // Prevent websites from detecting incognito mode, by emulating what would | 466 // Prevent websites from detecting incognito mode, by emulating what would |
| 452 // have happened if we had a PushMessagingService available. | 467 // have happened if we had a PushMessagingService available. |
| 453 if (!data.FromDocument() || !data.options.user_visible_only) { | 468 if (!data.FromDocument() || !data.options.user_visible_only) { |
| 454 // Throw a permission denied error under the same circumstances. | 469 // Throw a permission denied error under the same circumstances. |
| 455 BrowserThread::PostTask( | 470 BrowserThread::PostTask( |
| 456 BrowserThread::IO, FROM_HERE, | 471 BrowserThread::IO, FROM_HERE, |
| 457 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, | 472 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 458 data, | 473 base::Passed(&data), |
| 459 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); | 474 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); |
| 460 } else { | 475 } else { |
| 461 RenderFrameHost* render_frame_host = | 476 RenderFrameHost* render_frame_host = |
| 462 RenderFrameHost::FromID(render_process_id_, data.render_frame_id); | 477 RenderFrameHost::FromID(render_process_id_, data.render_frame_id); |
| 463 WebContents* web_contents = | 478 WebContents* web_contents = |
| 464 WebContents::FromRenderFrameHost(render_frame_host); | 479 WebContents::FromRenderFrameHost(render_frame_host); |
| 465 if (web_contents) { | 480 if (web_contents) { |
| 466 web_contents->GetMainFrame()->AddMessageToConsole( | 481 web_contents->GetMainFrame()->AddMessageToConsole( |
| 467 CONSOLE_MESSAGE_LEVEL_ERROR, kIncognitoPushUnsupportedMessage); | 482 CONSOLE_MESSAGE_LEVEL_ERROR, kIncognitoPushUnsupportedMessage); |
| 468 | 483 |
| 469 BrowserContext* browser_context = web_contents->GetBrowserContext(); | 484 BrowserContext* browser_context = web_contents->GetBrowserContext(); |
| 470 | 485 |
| 471 // It's valid for embedders to return a null permission manager. | 486 // It's valid for embedders to return a null permission manager. |
| 472 // Immediately reject the permission request when this happens. | 487 // Immediately reject the permission request when this happens. |
| 473 if (!browser_context->GetPermissionManager()) { | 488 if (!browser_context->GetPermissionManager()) { |
| 474 BrowserThread::PostTask( | 489 BrowserThread::PostTask( |
| 475 BrowserThread::IO, FROM_HERE, | 490 BrowserThread::IO, FROM_HERE, |
| 476 base::Bind( | 491 base::Bind( |
| 477 &PushMessagingManager::SendSubscriptionError, io_parent_, | 492 &PushMessagingManager::SendSubscriptionError, io_parent_, |
| 478 data, | 493 base::Passed(&data), |
| 479 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); | 494 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); |
| 480 | 495 |
| 481 return; | 496 return; |
| 482 } | 497 } |
| 483 | 498 |
| 484 // Request push messaging permission (which will fail, since | 499 // Request push messaging permission (which will fail, since |
| 485 // notifications aren't supported in incognito), so the website can't | 500 // notifications aren't supported in incognito), so the website can't |
| 486 // detect whether incognito is active. | 501 // detect whether incognito is active. |
| 502 GURL requesting_origin = data.requesting_origin; |
| 487 browser_context->GetPermissionManager()->RequestPermission( | 503 browser_context->GetPermissionManager()->RequestPermission( |
| 488 PermissionType::PUSH_MESSAGING, render_frame_host, | 504 PermissionType::PUSH_MESSAGING, render_frame_host, |
| 489 data.requesting_origin, false /* user_gesture */, | 505 requesting_origin, false /* user_gesture */, |
| 490 base::Bind( | 506 base::Bind( |
| 491 &PushMessagingManager::Core::DidRequestPermissionInIncognito, | 507 &PushMessagingManager::Core::DidRequestPermissionInIncognito, |
| 492 weak_factory_ui_to_ui_.GetWeakPtr(), data)); | 508 weak_factory_ui_to_ui_.GetWeakPtr(), base::Passed(&data))); |
| 493 } | 509 } |
| 494 } | 510 } |
| 495 } | 511 } |
| 496 return; | 512 return; |
| 497 } | 513 } |
| 498 | 514 |
| 515 int64_t registration_id = data.service_worker_registration_id; |
| 516 GURL requesting_origin = data.requesting_origin; |
| 517 PushSubscriptionOptions options = data.options; |
| 518 int render_frame_id = data.render_frame_id; |
| 499 if (data.FromDocument()) { | 519 if (data.FromDocument()) { |
| 500 push_service->SubscribeFromDocument( | 520 push_service->SubscribeFromDocument( |
| 501 data.requesting_origin, data.service_worker_registration_id, | 521 requesting_origin, registration_id, render_process_id_, render_frame_id, |
| 502 render_process_id_, data.render_frame_id, data.options, | 522 options, |
| 503 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), | 523 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), |
| 504 data)); | 524 base::Passed(&data))); |
| 505 } else { | 525 } else { |
| 506 push_service->SubscribeFromWorker( | 526 push_service->SubscribeFromWorker( |
| 507 data.requesting_origin, data.service_worker_registration_id, | 527 requesting_origin, registration_id, options, |
| 508 data.options, | |
| 509 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), | 528 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), |
| 510 data)); | 529 base::Passed(&data))); |
| 511 } | 530 } |
| 512 } | 531 } |
| 513 | 532 |
| 514 void PushMessagingManager::Core::DidRequestPermissionInIncognito( | 533 void PushMessagingManager::Core::DidRequestPermissionInIncognito( |
| 515 const RegisterData& data, | 534 RegisterData data, |
| 516 blink::mojom::PermissionStatus status) { | 535 blink::mojom::PermissionStatus status) { |
| 517 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 536 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 518 // Notification permission should always be denied in incognito. | 537 // Notification permission should always be denied in incognito. |
| 519 DCHECK_EQ(blink::mojom::PermissionStatus::DENIED, status); | 538 DCHECK_EQ(blink::mojom::PermissionStatus::DENIED, status); |
| 520 BrowserThread::PostTask( | 539 BrowserThread::PostTask( |
| 521 BrowserThread::IO, FROM_HERE, | 540 BrowserThread::IO, FROM_HERE, |
| 522 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, data, | 541 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 542 base::Passed(&data), |
| 523 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); | 543 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); |
| 524 } | 544 } |
| 525 | 545 |
| 526 void PushMessagingManager::Core::DidRegister( | 546 void PushMessagingManager::Core::DidRegister( |
| 527 const RegisterData& data, | 547 RegisterData data, |
| 528 const std::string& push_registration_id, | 548 const std::string& push_registration_id, |
| 529 const std::vector<uint8_t>& p256dh, | 549 const std::vector<uint8_t>& p256dh, |
| 530 const std::vector<uint8_t>& auth, | 550 const std::vector<uint8_t>& auth, |
| 531 PushRegistrationStatus status) { | 551 PushRegistrationStatus status) { |
| 532 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 552 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 533 if (status == PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE) { | 553 if (status == PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE) { |
| 534 BrowserThread::PostTask( | 554 BrowserThread::PostTask( |
| 535 BrowserThread::IO, FROM_HERE, | 555 BrowserThread::IO, FROM_HERE, |
| 536 base::Bind(&PushMessagingManager::PersistRegistrationOnIO, io_parent_, | 556 base::Bind(&PushMessagingManager::PersistRegistrationOnIO, io_parent_, |
| 537 data, push_registration_id, p256dh, auth)); | 557 base::Passed(&data), push_registration_id, p256dh, auth)); |
| 538 } else { | 558 } else { |
| 539 BrowserThread::PostTask( | 559 BrowserThread::PostTask( |
| 540 BrowserThread::IO, FROM_HERE, | 560 BrowserThread::IO, FROM_HERE, |
| 541 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, | 561 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 542 data, status)); | 562 base::Passed(&data), status)); |
| 543 } | 563 } |
| 544 } | 564 } |
| 545 | 565 |
| 546 void PushMessagingManager::PersistRegistrationOnIO( | 566 void PushMessagingManager::PersistRegistrationOnIO( |
| 547 const RegisterData& data, | 567 RegisterData data, |
| 548 const std::string& push_registration_id, | 568 const std::string& push_registration_id, |
| 549 const std::vector<uint8_t>& p256dh, | 569 const std::vector<uint8_t>& p256dh, |
| 550 const std::vector<uint8_t>& auth) { | 570 const std::vector<uint8_t>& auth) { |
| 551 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 571 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 572 GURL requesting_origin = data.requesting_origin; |
| 573 int64_t registration_id = data.service_worker_registration_id; |
| 574 std::string sender_info = data.options.sender_info; |
| 552 service_worker_context_->StoreRegistrationUserData( | 575 service_worker_context_->StoreRegistrationUserData( |
| 553 data.service_worker_registration_id, data.requesting_origin, | 576 registration_id, requesting_origin, |
| 554 {{kPushRegistrationIdServiceWorkerKey, push_registration_id}, | 577 {{kPushRegistrationIdServiceWorkerKey, push_registration_id}, |
| 555 {kPushSenderIdServiceWorkerKey, data.options.sender_info}}, | 578 {kPushSenderIdServiceWorkerKey, sender_info}}, |
| 556 base::Bind(&PushMessagingManager::DidPersistRegistrationOnIO, | 579 base::Bind(&PushMessagingManager::DidPersistRegistrationOnIO, |
| 557 weak_factory_io_to_io_.GetWeakPtr(), data, | 580 weak_factory_io_to_io_.GetWeakPtr(), base::Passed(&data), |
| 558 push_registration_id, p256dh, auth)); | 581 push_registration_id, p256dh, auth)); |
| 559 } | 582 } |
| 560 | 583 |
| 561 void PushMessagingManager::DidPersistRegistrationOnIO( | 584 void PushMessagingManager::DidPersistRegistrationOnIO( |
| 562 const RegisterData& data, | 585 RegisterData data, |
| 563 const std::string& push_registration_id, | 586 const std::string& push_registration_id, |
| 564 const std::vector<uint8_t>& p256dh, | 587 const std::vector<uint8_t>& p256dh, |
| 565 const std::vector<uint8_t>& auth, | 588 const std::vector<uint8_t>& auth, |
| 566 ServiceWorkerStatusCode service_worker_status) { | 589 ServiceWorkerStatusCode service_worker_status) { |
| 567 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 590 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 568 if (service_worker_status == SERVICE_WORKER_OK) { | 591 if (service_worker_status == SERVICE_WORKER_OK) { |
| 569 SendSubscriptionSuccess(data, | 592 SendSubscriptionSuccess(std::move(data), |
| 570 PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, | 593 PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, |
| 571 push_registration_id, p256dh, auth); | 594 push_registration_id, p256dh, auth); |
| 572 } else { | 595 } else { |
| 573 // TODO(johnme): Unregister, so PushMessagingServiceImpl can decrease count. | 596 // TODO(johnme): Unregister, so PushMessagingServiceImpl can decrease count. |
| 574 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_STORAGE_ERROR); | 597 SendSubscriptionError(std::move(data), |
| 598 PUSH_REGISTRATION_STATUS_STORAGE_ERROR); |
| 575 } | 599 } |
| 576 } | 600 } |
| 577 | 601 |
| 578 void PushMessagingManager::SendSubscriptionError( | 602 void PushMessagingManager::SendSubscriptionError( |
| 579 const RegisterData& data, | 603 RegisterData data, |
| 580 PushRegistrationStatus status) { | 604 PushRegistrationStatus status) { |
| 581 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 605 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 582 data.callback.Run(status, base::nullopt /* endpoint */, | 606 std::move(data.callback) |
| 583 base::nullopt /* options */, base::nullopt /* p256dh */, | 607 .Run(status, base::nullopt /* endpoint */, base::nullopt /* options */, |
| 584 base::nullopt /* auth */); | 608 base::nullopt /* p256dh */, base::nullopt /* auth */); |
| 585 RecordRegistrationStatus(status); | 609 RecordRegistrationStatus(status); |
| 586 } | 610 } |
| 587 | 611 |
| 588 void PushMessagingManager::SendSubscriptionSuccess( | 612 void PushMessagingManager::SendSubscriptionSuccess( |
| 589 const RegisterData& data, | 613 RegisterData data, |
| 590 PushRegistrationStatus status, | 614 PushRegistrationStatus status, |
| 591 const std::string& push_subscription_id, | 615 const std::string& push_subscription_id, |
| 592 const std::vector<uint8_t>& p256dh, | 616 const std::vector<uint8_t>& p256dh, |
| 593 const std::vector<uint8_t>& auth) { | 617 const std::vector<uint8_t>& auth) { |
| 594 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 618 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 595 if (!service_available_) { | 619 if (!service_available_) { |
| 596 // This shouldn't be possible in incognito mode, since we've already checked | 620 // This shouldn't be possible in incognito mode, since we've already checked |
| 597 // that we have an existing registration. Hence it's ok to throw an error. | 621 // that we have an existing registration. Hence it's ok to throw an error. |
| 598 DCHECK(!ui_core_->is_incognito()); | 622 DCHECK(!ui_core_->is_incognito()); |
| 599 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE); | 623 SendSubscriptionError(std::move(data), |
| 624 PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE); |
| 600 return; | 625 return; |
| 601 } | 626 } |
| 602 | 627 |
| 603 const GURL endpoint = CreateEndpoint( | 628 const GURL endpoint = CreateEndpoint( |
| 604 IsApplicationServerKey(data.options.sender_info), push_subscription_id); | 629 IsApplicationServerKey(data.options.sender_info), push_subscription_id); |
| 605 | 630 |
| 606 data.callback.Run(status, endpoint, data.options, p256dh, auth); | 631 std::move(data.callback).Run(status, endpoint, data.options, p256dh, auth); |
| 607 | 632 |
| 608 RecordRegistrationStatus(status); | 633 RecordRegistrationStatus(status); |
| 609 } | 634 } |
| 610 | 635 |
| 611 // Unsubscribe methods on both IO and UI threads, merged in order of use from | 636 // Unsubscribe methods on both IO and UI threads, merged in order of use from |
| 612 // PushMessagingManager and Core. | 637 // PushMessagingManager and Core. |
| 613 // ----------------------------------------------------------------------------- | 638 // ----------------------------------------------------------------------------- |
| 614 | 639 |
| 615 void PushMessagingManager::Unsubscribe(int64_t service_worker_registration_id, | 640 void PushMessagingManager::Unsubscribe(int64_t service_worker_registration_id, |
| 616 const UnsubscribeCallback& callback) { | 641 UnsubscribeCallback callback) { |
| 617 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 642 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 618 ServiceWorkerRegistration* service_worker_registration = | 643 ServiceWorkerRegistration* service_worker_registration = |
| 619 service_worker_context_->GetLiveRegistration( | 644 service_worker_context_->GetLiveRegistration( |
| 620 service_worker_registration_id); | 645 service_worker_registration_id); |
| 621 if (!service_worker_registration) { | 646 if (!service_worker_registration) { |
| 622 DidUnregister(callback, PUSH_UNREGISTRATION_STATUS_NO_SERVICE_WORKER); | 647 DidUnregister(std::move(callback), |
| 648 PUSH_UNREGISTRATION_STATUS_NO_SERVICE_WORKER); |
| 623 return; | 649 return; |
| 624 } | 650 } |
| 625 | 651 |
| 626 service_worker_context_->GetRegistrationUserData( | 652 service_worker_context_->GetRegistrationUserData( |
| 627 service_worker_registration_id, {kPushSenderIdServiceWorkerKey}, | 653 service_worker_registration_id, {kPushSenderIdServiceWorkerKey}, |
| 628 base::Bind(&PushMessagingManager::UnsubscribeHavingGottenSenderId, | 654 base::Bind(&PushMessagingManager::UnsubscribeHavingGottenSenderId, |
| 629 weak_factory_io_to_io_.GetWeakPtr(), callback, | 655 weak_factory_io_to_io_.GetWeakPtr(), base::Passed(&callback), |
| 630 service_worker_registration_id, | 656 service_worker_registration_id, |
| 631 service_worker_registration->pattern().GetOrigin())); | 657 service_worker_registration->pattern().GetOrigin())); |
| 632 } | 658 } |
| 633 | 659 |
| 634 void PushMessagingManager::UnsubscribeHavingGottenSenderId( | 660 void PushMessagingManager::UnsubscribeHavingGottenSenderId( |
| 635 const UnsubscribeCallback& callback, | 661 UnsubscribeCallback callback, |
| 636 int64_t service_worker_registration_id, | 662 int64_t service_worker_registration_id, |
| 637 const GURL& requesting_origin, | 663 const GURL& requesting_origin, |
| 638 const std::vector<std::string>& sender_ids, | 664 const std::vector<std::string>& sender_ids, |
| 639 ServiceWorkerStatusCode service_worker_status) { | 665 ServiceWorkerStatusCode service_worker_status) { |
| 640 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 666 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 641 | 667 |
| 642 std::string sender_id; | 668 std::string sender_id; |
| 643 if (service_worker_status == SERVICE_WORKER_OK) { | 669 if (service_worker_status == SERVICE_WORKER_OK) { |
| 644 DCHECK_EQ(1u, sender_ids.size()); | 670 DCHECK_EQ(1u, sender_ids.size()); |
| 645 sender_id = sender_ids[0]; | 671 sender_id = sender_ids[0]; |
| 646 } | 672 } |
| 647 BrowserThread::PostTask( | 673 BrowserThread::PostTask( |
| 648 BrowserThread::UI, FROM_HERE, | 674 BrowserThread::UI, FROM_HERE, |
| 649 base::Bind(&Core::UnregisterFromService, base::Unretained(ui_core_.get()), | 675 base::Bind(&Core::UnregisterFromService, base::Unretained(ui_core_.get()), |
| 650 callback, service_worker_registration_id, requesting_origin, | 676 base::Passed(&callback), service_worker_registration_id, |
| 651 sender_id)); | 677 requesting_origin, sender_id)); |
| 652 } | 678 } |
| 653 | 679 |
| 654 void PushMessagingManager::Core::UnregisterFromService( | 680 void PushMessagingManager::Core::UnregisterFromService( |
| 655 const UnsubscribeCallback& callback, | 681 UnsubscribeCallback callback, |
| 656 int64_t service_worker_registration_id, | 682 int64_t service_worker_registration_id, |
| 657 const GURL& requesting_origin, | 683 const GURL& requesting_origin, |
| 658 const std::string& sender_id) { | 684 const std::string& sender_id) { |
| 659 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 685 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 660 PushMessagingService* push_service = service(); | 686 PushMessagingService* push_service = service(); |
| 661 if (!push_service) { | 687 if (!push_service) { |
| 662 // This shouldn't be possible in incognito mode, since we've already checked | 688 // This shouldn't be possible in incognito mode, since we've already checked |
| 663 // that we have an existing registration. Hence it's ok to throw an error. | 689 // that we have an existing registration. Hence it's ok to throw an error. |
| 664 DCHECK(!is_incognito()); | 690 DCHECK(!is_incognito()); |
| 665 BrowserThread::PostTask( | 691 BrowserThread::PostTask( |
| 666 BrowserThread::IO, FROM_HERE, | 692 BrowserThread::IO, FROM_HERE, |
| 667 base::Bind(&PushMessagingManager::DidUnregister, io_parent_, callback, | 693 base::Bind(&PushMessagingManager::DidUnregister, io_parent_, |
| 694 base::Passed(&callback), |
| 668 PUSH_UNREGISTRATION_STATUS_SERVICE_NOT_AVAILABLE)); | 695 PUSH_UNREGISTRATION_STATUS_SERVICE_NOT_AVAILABLE)); |
| 669 return; | 696 return; |
| 670 } | 697 } |
| 671 | 698 |
| 672 push_service->Unsubscribe( | 699 push_service->Unsubscribe( |
| 673 PUSH_UNREGISTRATION_REASON_JAVASCRIPT_API, requesting_origin, | 700 PUSH_UNREGISTRATION_REASON_JAVASCRIPT_API, requesting_origin, |
| 674 service_worker_registration_id, sender_id, | 701 service_worker_registration_id, sender_id, |
| 675 base::Bind(&Core::DidUnregisterFromService, | 702 base::Bind(&Core::DidUnregisterFromService, |
| 676 weak_factory_ui_to_ui_.GetWeakPtr(), callback, | 703 weak_factory_ui_to_ui_.GetWeakPtr(), base::Passed(&callback), |
| 677 service_worker_registration_id)); | 704 service_worker_registration_id)); |
| 678 } | 705 } |
| 679 | 706 |
| 680 void PushMessagingManager::Core::DidUnregisterFromService( | 707 void PushMessagingManager::Core::DidUnregisterFromService( |
| 681 const UnsubscribeCallback& callback, | 708 UnsubscribeCallback callback, |
| 682 int64_t service_worker_registration_id, | 709 int64_t service_worker_registration_id, |
| 683 PushUnregistrationStatus unregistration_status) { | 710 PushUnregistrationStatus unregistration_status) { |
| 684 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 711 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 685 | 712 |
| 686 BrowserThread::PostTask( | 713 BrowserThread::PostTask( |
| 687 BrowserThread::IO, FROM_HERE, | 714 BrowserThread::IO, FROM_HERE, |
| 688 base::Bind(&PushMessagingManager::DidUnregister, io_parent_, callback, | 715 base::Bind(&PushMessagingManager::DidUnregister, io_parent_, |
| 689 unregistration_status)); | 716 base::Passed(&callback), unregistration_status)); |
| 690 } | 717 } |
| 691 | 718 |
| 692 void PushMessagingManager::DidUnregister( | 719 void PushMessagingManager::DidUnregister( |
| 693 const UnsubscribeCallback& callback, | 720 UnsubscribeCallback callback, |
| 694 PushUnregistrationStatus unregistration_status) { | 721 PushUnregistrationStatus unregistration_status) { |
| 695 // Only called from IO thread, but would be safe to call from UI thread. | 722 // Only called from IO thread, but would be safe to call from UI thread. |
| 696 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 723 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 697 switch (unregistration_status) { | 724 switch (unregistration_status) { |
| 698 case PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTERED: | 725 case PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTERED: |
| 699 case PUSH_UNREGISTRATION_STATUS_PENDING_NETWORK_ERROR: | 726 case PUSH_UNREGISTRATION_STATUS_PENDING_NETWORK_ERROR: |
| 700 case PUSH_UNREGISTRATION_STATUS_PENDING_SERVICE_ERROR: | 727 case PUSH_UNREGISTRATION_STATUS_PENDING_SERVICE_ERROR: |
| 701 callback.Run(blink::WebPushError::kErrorTypeNone, | 728 std::move(callback).Run(blink::WebPushError::kErrorTypeNone, |
| 702 true /* did_unsubscribe */, | 729 true /* did_unsubscribe */, |
| 703 base::nullopt /* error_message */); | 730 base::nullopt /* error_message */); |
| 704 break; | 731 break; |
| 705 case PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED: | 732 case PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED: |
| 706 callback.Run(blink::WebPushError::kErrorTypeNone, | 733 std::move(callback).Run(blink::WebPushError::kErrorTypeNone, |
| 707 false /* did_unsubscribe */, | 734 false /* did_unsubscribe */, |
| 708 base::nullopt /* error_message */); | 735 base::nullopt /* error_message */); |
| 709 break; | 736 break; |
| 710 case PUSH_UNREGISTRATION_STATUS_NO_SERVICE_WORKER: | 737 case PUSH_UNREGISTRATION_STATUS_NO_SERVICE_WORKER: |
| 711 case PUSH_UNREGISTRATION_STATUS_SERVICE_NOT_AVAILABLE: | 738 case PUSH_UNREGISTRATION_STATUS_SERVICE_NOT_AVAILABLE: |
| 712 case PUSH_UNREGISTRATION_STATUS_STORAGE_ERROR: | 739 case PUSH_UNREGISTRATION_STATUS_STORAGE_ERROR: |
| 713 callback.Run(blink::WebPushError::kErrorTypeAbort, false, | 740 std::move(callback).Run(blink::WebPushError::kErrorTypeAbort, false, |
| 714 std::string(PushUnregistrationStatusToString( | 741 std::string(PushUnregistrationStatusToString( |
| 715 unregistration_status)) /* error_message */); | 742 unregistration_status)) /* error_message */); |
| 716 break; | 743 break; |
| 717 case PUSH_UNREGISTRATION_STATUS_NETWORK_ERROR: | 744 case PUSH_UNREGISTRATION_STATUS_NETWORK_ERROR: |
| 718 NOTREACHED(); | 745 NOTREACHED(); |
| 719 break; | 746 break; |
| 720 } | 747 } |
| 721 RecordUnregistrationStatus(unregistration_status); | 748 RecordUnregistrationStatus(unregistration_status); |
| 722 } | 749 } |
| 723 | 750 |
| 724 // GetSubscription methods on both IO and UI threads, merged in order of use | 751 // GetSubscription methods on both IO and UI threads, merged in order of use |
| 725 // from PushMessagingManager and Core. | 752 // from PushMessagingManager and Core. |
| 726 // ----------------------------------------------------------------------------- | 753 // ----------------------------------------------------------------------------- |
| 727 | 754 |
| 728 void PushMessagingManager::GetSubscription( | 755 void PushMessagingManager::GetSubscription( |
| 729 int64_t service_worker_registration_id, | 756 int64_t service_worker_registration_id, |
| 730 const GetSubscriptionCallback& callback) { | 757 GetSubscriptionCallback callback) { |
| 731 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 758 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 732 // TODO(johnme): Validate arguments? | 759 // TODO(johnme): Validate arguments? |
| 733 service_worker_context_->GetRegistrationUserData( | 760 service_worker_context_->GetRegistrationUserData( |
| 734 service_worker_registration_id, | 761 service_worker_registration_id, |
| 735 {kPushRegistrationIdServiceWorkerKey, kPushSenderIdServiceWorkerKey}, | 762 {kPushRegistrationIdServiceWorkerKey, kPushSenderIdServiceWorkerKey}, |
| 736 base::Bind(&PushMessagingManager::DidGetSubscription, | 763 base::Bind(&PushMessagingManager::DidGetSubscription, |
| 737 weak_factory_io_to_io_.GetWeakPtr(), callback, | 764 weak_factory_io_to_io_.GetWeakPtr(), base::Passed(&callback), |
| 738 service_worker_registration_id)); | 765 service_worker_registration_id)); |
| 739 } | 766 } |
| 740 | 767 |
| 741 void PushMessagingManager::DidGetSubscription( | 768 void PushMessagingManager::DidGetSubscription( |
| 742 const GetSubscriptionCallback& callback, | 769 GetSubscriptionCallback callback, |
| 743 int64_t service_worker_registration_id, | 770 int64_t service_worker_registration_id, |
| 744 const std::vector<std::string>& push_subscription_id_and_sender_info, | 771 const std::vector<std::string>& push_subscription_id_and_sender_info, |
| 745 ServiceWorkerStatusCode service_worker_status) { | 772 ServiceWorkerStatusCode service_worker_status) { |
| 746 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 773 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 747 PushGetRegistrationStatus get_status = | 774 PushGetRegistrationStatus get_status = |
| 748 PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; | 775 PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; |
| 749 switch (service_worker_status) { | 776 switch (service_worker_status) { |
| 750 case SERVICE_WORKER_OK: { | 777 case SERVICE_WORKER_OK: { |
| 751 DCHECK_EQ(2u, push_subscription_id_and_sender_info.size()); | 778 DCHECK_EQ(2u, push_subscription_id_and_sender_info.size()); |
| 752 const std::string& push_subscription_id = | 779 const std::string& push_subscription_id = |
| (...skipping 23 matching lines...) Expand all Loading... |
| 776 const GURL endpoint = | 803 const GURL endpoint = |
| 777 CreateEndpoint(uses_standard_protocol, push_subscription_id); | 804 CreateEndpoint(uses_standard_protocol, push_subscription_id); |
| 778 | 805 |
| 779 BrowserThread::PostTask( | 806 BrowserThread::PostTask( |
| 780 BrowserThread::UI, FROM_HERE, | 807 BrowserThread::UI, FROM_HERE, |
| 781 base::Bind(&Core::GetSubscriptionInfoOnUI, | 808 base::Bind(&Core::GetSubscriptionInfoOnUI, |
| 782 base::Unretained(ui_core_.get()), origin, | 809 base::Unretained(ui_core_.get()), origin, |
| 783 service_worker_registration_id, sender_info, | 810 service_worker_registration_id, sender_info, |
| 784 push_subscription_id, | 811 push_subscription_id, |
| 785 base::Bind(&Core::GetSubscriptionDidGetInfoOnUI, | 812 base::Bind(&Core::GetSubscriptionDidGetInfoOnUI, |
| 786 ui_core_weak_ptr_, callback, origin, | 813 ui_core_weak_ptr_, base::Passed(&callback), |
| 787 service_worker_registration_id, endpoint, | 814 origin, service_worker_registration_id, |
| 788 sender_info))); | 815 endpoint, sender_info))); |
| 789 | 816 |
| 790 return; | 817 return; |
| 791 } | 818 } |
| 792 case SERVICE_WORKER_ERROR_NOT_FOUND: { | 819 case SERVICE_WORKER_ERROR_NOT_FOUND: { |
| 793 get_status = PUSH_GETREGISTRATION_STATUS_REGISTRATION_NOT_FOUND; | 820 get_status = PUSH_GETREGISTRATION_STATUS_REGISTRATION_NOT_FOUND; |
| 794 break; | 821 break; |
| 795 } | 822 } |
| 796 case SERVICE_WORKER_ERROR_FAILED: { | 823 case SERVICE_WORKER_ERROR_FAILED: { |
| 797 get_status = PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; | 824 get_status = PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; |
| 798 break; | 825 break; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 813 case SERVICE_WORKER_ERROR_DISK_CACHE: | 840 case SERVICE_WORKER_ERROR_DISK_CACHE: |
| 814 case SERVICE_WORKER_ERROR_REDUNDANT: | 841 case SERVICE_WORKER_ERROR_REDUNDANT: |
| 815 case SERVICE_WORKER_ERROR_DISALLOWED: | 842 case SERVICE_WORKER_ERROR_DISALLOWED: |
| 816 case SERVICE_WORKER_ERROR_MAX_VALUE: { | 843 case SERVICE_WORKER_ERROR_MAX_VALUE: { |
| 817 NOTREACHED() << "Got unexpected error code: " << service_worker_status | 844 NOTREACHED() << "Got unexpected error code: " << service_worker_status |
| 818 << " " << ServiceWorkerStatusToString(service_worker_status); | 845 << " " << ServiceWorkerStatusToString(service_worker_status); |
| 819 get_status = PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; | 846 get_status = PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; |
| 820 break; | 847 break; |
| 821 } | 848 } |
| 822 } | 849 } |
| 823 callback.Run(get_status, base::nullopt /* endpoint */, | 850 std::move(callback).Run(get_status, base::nullopt /* endpoint */, |
| 824 base::nullopt /* options */, base::nullopt /* p256dh */, | 851 base::nullopt /* options */, |
| 825 base::nullopt /* auth */); | 852 base::nullopt /* p256dh */, base::nullopt /* auth */); |
| 826 RecordGetRegistrationStatus(get_status); | 853 RecordGetRegistrationStatus(get_status); |
| 827 } | 854 } |
| 828 | 855 |
| 829 void PushMessagingManager::Core::GetSubscriptionDidGetInfoOnUI( | 856 void PushMessagingManager::Core::GetSubscriptionDidGetInfoOnUI( |
| 830 const GetSubscriptionCallback& callback, | 857 GetSubscriptionCallback callback, |
| 831 const GURL& origin, | 858 const GURL& origin, |
| 832 int64_t service_worker_registration_id, | 859 int64_t service_worker_registration_id, |
| 833 const GURL& endpoint, | 860 const GURL& endpoint, |
| 834 const std::string& sender_info, | 861 const std::string& sender_info, |
| 835 bool is_valid, | 862 bool is_valid, |
| 836 const std::vector<uint8_t>& p256dh, | 863 const std::vector<uint8_t>& p256dh, |
| 837 const std::vector<uint8_t>& auth) { | 864 const std::vector<uint8_t>& auth) { |
| 838 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 865 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 839 if (is_valid) { | 866 if (is_valid) { |
| 840 PushSubscriptionOptions options; | 867 PushSubscriptionOptions options; |
| 841 // Chrome rejects subscription requests with userVisibleOnly false, so it | 868 // Chrome rejects subscription requests with userVisibleOnly false, so it |
| 842 // must have been true. TODO(harkness): If Chrome starts accepting silent | 869 // must have been true. TODO(harkness): If Chrome starts accepting silent |
| 843 // push subscriptions with userVisibleOnly false, the bool will need to be | 870 // push subscriptions with userVisibleOnly false, the bool will need to be |
| 844 // stored. | 871 // stored. |
| 845 options.user_visible_only = true; | 872 options.user_visible_only = true; |
| 846 options.sender_info = sender_info; | 873 options.sender_info = sender_info; |
| 847 | 874 |
| 848 PushGetRegistrationStatus status = PUSH_GETREGISTRATION_STATUS_SUCCESS; | 875 PushGetRegistrationStatus status = PUSH_GETREGISTRATION_STATUS_SUCCESS; |
| 849 | 876 |
| 850 BrowserThread::PostTask( | 877 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 851 BrowserThread::IO, FROM_HERE, | 878 base::BindOnce(std::move(callback), status, |
| 852 base::Bind(callback, status, endpoint, options, p256dh, auth)); | 879 endpoint, options, p256dh, auth)); |
| 853 | 880 |
| 854 RecordGetRegistrationStatus(status); | 881 RecordGetRegistrationStatus(status); |
| 855 } else { | 882 } else { |
| 856 PushMessagingService* push_service = service(); | 883 PushMessagingService* push_service = service(); |
| 857 if (!push_service) { | 884 if (!push_service) { |
| 858 // Shouldn't be possible to have a stored push subscription in a profile | 885 // Shouldn't be possible to have a stored push subscription in a profile |
| 859 // with no push service, but this case can occur when the renderer is | 886 // with no push service, but this case can occur when the renderer is |
| 860 // shutting down. | 887 // shutting down. |
| 861 BrowserThread::PostTask( | 888 BrowserThread::PostTask( |
| 862 BrowserThread::IO, FROM_HERE, | 889 BrowserThread::IO, FROM_HERE, |
| 863 base::Bind(callback, PUSH_GETREGISTRATION_STATUS_RENDERER_SHUTDOWN, | 890 base::BindOnce(std::move(callback), |
| 864 base::nullopt /* endpoint */, base::nullopt /* options */, | 891 PUSH_GETREGISTRATION_STATUS_RENDERER_SHUTDOWN, |
| 865 base::nullopt /* p256dh */, base::nullopt /* auth */)); | 892 base::nullopt /* endpoint */, |
| 893 base::nullopt /* options */, |
| 894 base::nullopt /* p256dh */, base::nullopt /* auth */)); |
| 866 return; | 895 return; |
| 867 } | 896 } |
| 868 | 897 |
| 869 // Uh-oh! Although there was a cached subscription in the Service Worker | 898 // Uh-oh! Although there was a cached subscription in the Service Worker |
| 870 // database, it did not have matching counterparts in the | 899 // database, it did not have matching counterparts in the |
| 871 // PushMessagingAppIdentifier map and/or GCM Store. Unsubscribe to fix this | 900 // PushMessagingAppIdentifier map and/or GCM Store. Unsubscribe to fix this |
| 872 // inconsistency. | 901 // inconsistency. |
| 873 PushGetRegistrationStatus status = | 902 PushGetRegistrationStatus status = |
| 874 PUSH_GETREGISTRATION_STATUS_STORAGE_CORRUPT; | 903 PUSH_GETREGISTRATION_STATUS_STORAGE_CORRUPT; |
| 875 | 904 |
| 876 push_service->Unsubscribe( | 905 push_service->Unsubscribe( |
| 877 PUSH_UNREGISTRATION_REASON_GET_SUBSCRIPTION_STORAGE_CORRUPT, origin, | 906 PUSH_UNREGISTRATION_REASON_GET_SUBSCRIPTION_STORAGE_CORRUPT, origin, |
| 878 service_worker_registration_id, sender_info, | 907 service_worker_registration_id, sender_info, |
| 879 base::Bind(&Core::GetSubscriptionDidUnsubscribe, | 908 base::Bind(&Core::GetSubscriptionDidUnsubscribe, |
| 880 weak_factory_ui_to_ui_.GetWeakPtr(), callback, status)); | 909 weak_factory_ui_to_ui_.GetWeakPtr(), base::Passed(&callback), |
| 910 status)); |
| 881 | 911 |
| 882 RecordGetRegistrationStatus(status); | 912 RecordGetRegistrationStatus(status); |
| 883 } | 913 } |
| 884 } | 914 } |
| 885 | 915 |
| 886 void PushMessagingManager::Core::GetSubscriptionDidUnsubscribe( | 916 void PushMessagingManager::Core::GetSubscriptionDidUnsubscribe( |
| 887 const GetSubscriptionCallback& callback, | 917 GetSubscriptionCallback callback, |
| 888 PushGetRegistrationStatus get_status, | 918 PushGetRegistrationStatus get_status, |
| 889 PushUnregistrationStatus unsubscribe_status) { | 919 PushUnregistrationStatus unsubscribe_status) { |
| 890 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 920 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 891 BrowserThread::PostTask( | 921 BrowserThread::PostTask( |
| 892 BrowserThread::IO, FROM_HERE, | 922 BrowserThread::IO, FROM_HERE, |
| 893 base::Bind(callback, get_status, base::nullopt /* endpoint */, | 923 base::BindOnce(std::move(callback), get_status, |
| 894 base::nullopt /* options */, base::nullopt /* p256dh */, | 924 base::nullopt /* endpoint */, base::nullopt /* options */, |
| 895 base::nullopt /* auth */)); | 925 base::nullopt /* p256dh */, base::nullopt /* auth */)); |
| 896 } | 926 } |
| 897 | 927 |
| 898 // GetPermission methods on both IO and UI threads, merged in order of use from | 928 // GetPermission methods on both IO and UI threads, merged in order of use from |
| 899 // PushMessagingManager and Core. | 929 // PushMessagingManager and Core. |
| 900 // ----------------------------------------------------------------------------- | 930 // ----------------------------------------------------------------------------- |
| 901 | 931 |
| 902 void PushMessagingManager::GetPermissionStatus( | 932 void PushMessagingManager::GetPermissionStatus( |
| 903 int64_t service_worker_registration_id, | 933 int64_t service_worker_registration_id, |
| 904 bool user_visible, | 934 bool user_visible, |
| 905 const GetPermissionStatusCallback& callback) { | 935 GetPermissionStatusCallback callback) { |
| 906 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 936 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 907 ServiceWorkerRegistration* service_worker_registration = | 937 ServiceWorkerRegistration* service_worker_registration = |
| 908 service_worker_context_->GetLiveRegistration( | 938 service_worker_context_->GetLiveRegistration( |
| 909 service_worker_registration_id); | 939 service_worker_registration_id); |
| 910 if (!service_worker_registration) { | 940 if (!service_worker_registration) { |
| 911 // Return error: ErrorTypeAbort. | 941 // Return error: ErrorTypeAbort. |
| 912 callback.Run(blink::WebPushError::kErrorTypeAbort, | 942 std::move(callback).Run(blink::WebPushError::kErrorTypeAbort, |
| 913 blink::kWebPushPermissionStatusDenied); | 943 blink::kWebPushPermissionStatusDenied); |
| 914 return; | 944 return; |
| 915 } | 945 } |
| 916 | 946 |
| 917 BrowserThread::PostTask( | 947 BrowserThread::PostTask( |
| 918 BrowserThread::UI, FROM_HERE, | 948 BrowserThread::UI, FROM_HERE, |
| 919 base::Bind(&Core::GetPermissionStatusOnUI, | 949 base::Bind(&Core::GetPermissionStatusOnUI, |
| 920 base::Unretained(ui_core_.get()), callback, | 950 base::Unretained(ui_core_.get()), base::Passed(&callback), |
| 921 service_worker_registration->pattern().GetOrigin(), | 951 service_worker_registration->pattern().GetOrigin(), |
| 922 user_visible)); | 952 user_visible)); |
| 923 } | 953 } |
| 924 | 954 |
| 925 void PushMessagingManager::Core::GetPermissionStatusOnUI( | 955 void PushMessagingManager::Core::GetPermissionStatusOnUI( |
| 926 const GetPermissionStatusCallback& callback, | 956 GetPermissionStatusCallback callback, |
| 927 const GURL& requesting_origin, | 957 const GURL& requesting_origin, |
| 928 bool user_visible) { | 958 bool user_visible) { |
| 929 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 959 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 930 blink::WebPushPermissionStatus permission_status; | 960 blink::WebPushPermissionStatus permission_status; |
| 931 PushMessagingService* push_service = service(); | 961 PushMessagingService* push_service = service(); |
| 932 if (push_service) { | 962 if (push_service) { |
| 933 if (!user_visible && !push_service->SupportNonVisibleMessages()) { | 963 if (!user_visible && !push_service->SupportNonVisibleMessages()) { |
| 934 BrowserThread::PostTask( | 964 BrowserThread::PostTask( |
| 935 BrowserThread::IO, FROM_HERE, | 965 BrowserThread::IO, FROM_HERE, |
| 936 // Return error: ErrorTypeNotSupported. | 966 // Return error: ErrorTypeNotSupported. |
| 937 base::Bind(callback, blink::WebPushError::kErrorTypeNotSupported, | 967 base::BindOnce(std::move(callback), |
| 938 blink::kWebPushPermissionStatusDenied)); | 968 blink::WebPushError::kErrorTypeNotSupported, |
| 969 blink::kWebPushPermissionStatusDenied)); |
| 939 return; | 970 return; |
| 940 } | 971 } |
| 941 permission_status = | 972 permission_status = |
| 942 push_service->GetPermissionStatus(requesting_origin, user_visible); | 973 push_service->GetPermissionStatus(requesting_origin, user_visible); |
| 943 } else if (is_incognito()) { | 974 } else if (is_incognito()) { |
| 944 // Return prompt, so the website can't detect incognito mode. | 975 // Return prompt, so the website can't detect incognito mode. |
| 945 permission_status = blink::kWebPushPermissionStatusPrompt; | 976 permission_status = blink::kWebPushPermissionStatusPrompt; |
| 946 } else { | 977 } else { |
| 947 BrowserThread::PostTask( | 978 BrowserThread::PostTask( |
| 948 BrowserThread::IO, FROM_HERE, | 979 BrowserThread::IO, FROM_HERE, |
| 949 // Return error: ErrorTypeAbort. | 980 // Return error: ErrorTypeAbort. |
| 950 base::Bind(callback, blink::WebPushError::kErrorTypeAbort, | 981 base::BindOnce(std::move(callback), |
| 951 blink::kWebPushPermissionStatusDenied)); | 982 blink::WebPushError::kErrorTypeAbort, |
| 983 blink::kWebPushPermissionStatusDenied)); |
| 952 return; | 984 return; |
| 953 } | 985 } |
| 954 BrowserThread::PostTask( | 986 BrowserThread::PostTask( |
| 955 BrowserThread::IO, FROM_HERE, | 987 BrowserThread::IO, FROM_HERE, |
| 956 base::Bind(callback, blink::WebPushError::kErrorTypeNone, | 988 base::BindOnce(std::move(callback), blink::WebPushError::kErrorTypeNone, |
| 957 permission_status)); | 989 permission_status)); |
| 958 } | 990 } |
| 959 | 991 |
| 960 // Helper methods on both IO and UI threads, merged from | 992 // Helper methods on both IO and UI threads, merged from |
| 961 // PushMessagingManager and Core. | 993 // PushMessagingManager and Core. |
| 962 // ----------------------------------------------------------------------------- | 994 // ----------------------------------------------------------------------------- |
| 963 | 995 |
| 964 void PushMessagingManager::Core::GetSubscriptionInfoOnUI( | 996 void PushMessagingManager::Core::GetSubscriptionInfoOnUI( |
| 965 const GURL& origin, | 997 const GURL& origin, |
| 966 int64_t service_worker_registration_id, | 998 int64_t service_worker_registration_id, |
| 967 const std::string& sender_id, | 999 const std::string& sender_id, |
| 968 const std::string& push_subscription_id, | 1000 const std::string& push_subscription_id, |
| 969 const PushMessagingService::SubscriptionInfoCallback& callback) { | 1001 PushMessagingService::SubscriptionInfoCallback callback) { |
| 970 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1002 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 971 PushMessagingService* push_service = service(); | 1003 PushMessagingService* push_service = service(); |
| 972 if (!push_service) { | 1004 if (!push_service) { |
| 973 callback.Run(false /* is_valid */, std::vector<uint8_t>() /* p256dh */, | 1005 std::move(callback).Run(false /* is_valid */, |
| 974 std::vector<uint8_t>() /* auth */); | 1006 std::vector<uint8_t>() /* p256dh */, |
| 1007 std::vector<uint8_t>() /* auth */); |
| 975 return; | 1008 return; |
| 976 } | 1009 } |
| 977 | 1010 |
| 978 push_service->GetSubscriptionInfo(origin, service_worker_registration_id, | 1011 push_service->GetSubscriptionInfo(origin, service_worker_registration_id, |
| 979 sender_id, push_subscription_id, callback); | 1012 sender_id, push_subscription_id, |
| 1013 std::move(callback)); |
| 980 } | 1014 } |
| 981 | 1015 |
| 982 GURL PushMessagingManager::CreateEndpoint( | 1016 GURL PushMessagingManager::CreateEndpoint( |
| 983 bool standard_protocol, | 1017 bool standard_protocol, |
| 984 const std::string& subscription_id) const { | 1018 const std::string& subscription_id) const { |
| 985 const GURL& base = | 1019 const GURL& base = |
| 986 standard_protocol ? web_push_protocol_endpoint_ : default_endpoint_; | 1020 standard_protocol ? web_push_protocol_endpoint_ : default_endpoint_; |
| 987 | 1021 |
| 988 return GURL(base.spec() + subscription_id); | 1022 return GURL(base.spec() + subscription_id); |
| 989 } | 1023 } |
| 990 | 1024 |
| 991 PushMessagingService* PushMessagingManager::Core::service() { | 1025 PushMessagingService* PushMessagingManager::Core::service() { |
| 992 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1026 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 993 RenderProcessHost* process_host = | 1027 RenderProcessHost* process_host = |
| 994 RenderProcessHost::FromID(render_process_id_); | 1028 RenderProcessHost::FromID(render_process_id_); |
| 995 return process_host | 1029 return process_host |
| 996 ? process_host->GetBrowserContext()->GetPushMessagingService() | 1030 ? process_host->GetBrowserContext()->GetPushMessagingService() |
| 997 : nullptr; | 1031 : nullptr; |
| 998 } | 1032 } |
| 999 | 1033 |
| 1000 base::WeakPtr<PushMessagingManager::Core> | 1034 base::WeakPtr<PushMessagingManager::Core> |
| 1001 PushMessagingManager::Core::GetWeakPtrFromIOParentConstructor() { | 1035 PushMessagingManager::Core::GetWeakPtrFromIOParentConstructor() { |
| 1002 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1036 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1003 return weak_factory_ui_to_ui_.GetWeakPtr(); | 1037 return weak_factory_ui_to_ui_.GetWeakPtr(); |
| 1004 } | 1038 } |
| 1005 | 1039 |
| 1006 } // namespace content | 1040 } // namespace content |
| OLD | NEW |