| 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 18 matching lines...) Expand all Loading... |
| 771 const GURL endpoint = | 798 const GURL endpoint = |
| 772 CreateEndpoint(uses_standard_protocol, push_subscription_id); | 799 CreateEndpoint(uses_standard_protocol, push_subscription_id); |
| 773 | 800 |
| 774 BrowserThread::PostTask( | 801 BrowserThread::PostTask( |
| 775 BrowserThread::UI, FROM_HERE, | 802 BrowserThread::UI, FROM_HERE, |
| 776 base::Bind(&Core::GetSubscriptionInfoOnUI, | 803 base::Bind(&Core::GetSubscriptionInfoOnUI, |
| 777 base::Unretained(ui_core_.get()), origin, | 804 base::Unretained(ui_core_.get()), origin, |
| 778 service_worker_registration_id, sender_info, | 805 service_worker_registration_id, sender_info, |
| 779 push_subscription_id, | 806 push_subscription_id, |
| 780 base::Bind(&Core::GetSubscriptionDidGetInfoOnUI, | 807 base::Bind(&Core::GetSubscriptionDidGetInfoOnUI, |
| 781 ui_core_weak_ptr_, callback, origin, | 808 ui_core_weak_ptr_, base::Passed(&callback), |
| 782 service_worker_registration_id, endpoint, | 809 origin, service_worker_registration_id, |
| 783 sender_info))); | 810 endpoint, sender_info))); |
| 784 | 811 |
| 785 return; | 812 return; |
| 786 } | 813 } |
| 787 case SERVICE_WORKER_ERROR_NOT_FOUND: { | 814 case SERVICE_WORKER_ERROR_NOT_FOUND: { |
| 788 get_status = PUSH_GETREGISTRATION_STATUS_REGISTRATION_NOT_FOUND; | 815 get_status = PUSH_GETREGISTRATION_STATUS_REGISTRATION_NOT_FOUND; |
| 789 break; | 816 break; |
| 790 } | 817 } |
| 791 case SERVICE_WORKER_ERROR_FAILED: { | 818 case SERVICE_WORKER_ERROR_FAILED: { |
| 792 get_status = PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; | 819 get_status = PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; |
| 793 break; | 820 break; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 808 case SERVICE_WORKER_ERROR_DISK_CACHE: | 835 case SERVICE_WORKER_ERROR_DISK_CACHE: |
| 809 case SERVICE_WORKER_ERROR_REDUNDANT: | 836 case SERVICE_WORKER_ERROR_REDUNDANT: |
| 810 case SERVICE_WORKER_ERROR_DISALLOWED: | 837 case SERVICE_WORKER_ERROR_DISALLOWED: |
| 811 case SERVICE_WORKER_ERROR_MAX_VALUE: { | 838 case SERVICE_WORKER_ERROR_MAX_VALUE: { |
| 812 NOTREACHED() << "Got unexpected error code: " << service_worker_status | 839 NOTREACHED() << "Got unexpected error code: " << service_worker_status |
| 813 << " " << ServiceWorkerStatusToString(service_worker_status); | 840 << " " << ServiceWorkerStatusToString(service_worker_status); |
| 814 get_status = PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; | 841 get_status = PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; |
| 815 break; | 842 break; |
| 816 } | 843 } |
| 817 } | 844 } |
| 818 callback.Run(get_status, base::nullopt /* endpoint */, | 845 std::move(callback).Run(get_status, base::nullopt /* endpoint */, |
| 819 base::nullopt /* options */, base::nullopt /* p256dh */, | 846 base::nullopt /* options */, |
| 820 base::nullopt /* auth */); | 847 base::nullopt /* p256dh */, base::nullopt /* auth */); |
| 821 RecordGetRegistrationStatus(get_status); | 848 RecordGetRegistrationStatus(get_status); |
| 822 } | 849 } |
| 823 | 850 |
| 824 void PushMessagingManager::Core::GetSubscriptionDidGetInfoOnUI( | 851 void PushMessagingManager::Core::GetSubscriptionDidGetInfoOnUI( |
| 825 const GetSubscriptionCallback& callback, | 852 GetSubscriptionCallback callback, |
| 826 const GURL& origin, | 853 const GURL& origin, |
| 827 int64_t service_worker_registration_id, | 854 int64_t service_worker_registration_id, |
| 828 const GURL& endpoint, | 855 const GURL& endpoint, |
| 829 const std::string& sender_info, | 856 const std::string& sender_info, |
| 830 bool is_valid, | 857 bool is_valid, |
| 831 const std::vector<uint8_t>& p256dh, | 858 const std::vector<uint8_t>& p256dh, |
| 832 const std::vector<uint8_t>& auth) { | 859 const std::vector<uint8_t>& auth) { |
| 833 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 860 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 834 if (is_valid) { | 861 if (is_valid) { |
| 835 PushSubscriptionOptions options; | 862 PushSubscriptionOptions options; |
| 836 // Chrome rejects subscription requests with userVisibleOnly false, so it | 863 // Chrome rejects subscription requests with userVisibleOnly false, so it |
| 837 // must have been true. TODO(harkness): If Chrome starts accepting silent | 864 // must have been true. TODO(harkness): If Chrome starts accepting silent |
| 838 // push subscriptions with userVisibleOnly false, the bool will need to be | 865 // push subscriptions with userVisibleOnly false, the bool will need to be |
| 839 // stored. | 866 // stored. |
| 840 options.user_visible_only = true; | 867 options.user_visible_only = true; |
| 841 options.sender_info = sender_info; | 868 options.sender_info = sender_info; |
| 842 | 869 |
| 843 PushGetRegistrationStatus status = PUSH_GETREGISTRATION_STATUS_SUCCESS; | 870 PushGetRegistrationStatus status = PUSH_GETREGISTRATION_STATUS_SUCCESS; |
| 844 | 871 |
| 845 BrowserThread::PostTask( | 872 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 846 BrowserThread::IO, FROM_HERE, | 873 base::BindOnce(std::move(callback), status, |
| 847 base::Bind(callback, status, endpoint, options, p256dh, auth)); | 874 endpoint, options, p256dh, auth)); |
| 848 | 875 |
| 849 RecordGetRegistrationStatus(status); | 876 RecordGetRegistrationStatus(status); |
| 850 } else { | 877 } else { |
| 851 PushMessagingService* push_service = service(); | 878 PushMessagingService* push_service = service(); |
| 852 if (!push_service) { | 879 if (!push_service) { |
| 853 // Shouldn't be possible to have a stored push subscription in a profile | 880 // Shouldn't be possible to have a stored push subscription in a profile |
| 854 // with no push service, but this case can occur when the renderer is | 881 // with no push service, but this case can occur when the renderer is |
| 855 // shutting down. | 882 // shutting down. |
| 856 BrowserThread::PostTask( | 883 BrowserThread::PostTask( |
| 857 BrowserThread::IO, FROM_HERE, | 884 BrowserThread::IO, FROM_HERE, |
| 858 base::Bind(callback, PUSH_GETREGISTRATION_STATUS_RENDERER_SHUTDOWN, | 885 base::BindOnce(std::move(callback), |
| 859 base::nullopt /* endpoint */, base::nullopt /* options */, | 886 PUSH_GETREGISTRATION_STATUS_RENDERER_SHUTDOWN, |
| 860 base::nullopt /* p256dh */, base::nullopt /* auth */)); | 887 base::nullopt /* endpoint */, |
| 888 base::nullopt /* options */, |
| 889 base::nullopt /* p256dh */, base::nullopt /* auth */)); |
| 861 return; | 890 return; |
| 862 } | 891 } |
| 863 | 892 |
| 864 // Uh-oh! Although there was a cached subscription in the Service Worker | 893 // Uh-oh! Although there was a cached subscription in the Service Worker |
| 865 // database, it did not have matching counterparts in the | 894 // database, it did not have matching counterparts in the |
| 866 // PushMessagingAppIdentifier map and/or GCM Store. Unsubscribe to fix this | 895 // PushMessagingAppIdentifier map and/or GCM Store. Unsubscribe to fix this |
| 867 // inconsistency. | 896 // inconsistency. |
| 868 PushGetRegistrationStatus status = | 897 PushGetRegistrationStatus status = |
| 869 PUSH_GETREGISTRATION_STATUS_STORAGE_CORRUPT; | 898 PUSH_GETREGISTRATION_STATUS_STORAGE_CORRUPT; |
| 870 | 899 |
| 871 push_service->Unsubscribe( | 900 push_service->Unsubscribe( |
| 872 PUSH_UNREGISTRATION_REASON_GET_SUBSCRIPTION_STORAGE_CORRUPT, origin, | 901 PUSH_UNREGISTRATION_REASON_GET_SUBSCRIPTION_STORAGE_CORRUPT, origin, |
| 873 service_worker_registration_id, sender_info, | 902 service_worker_registration_id, sender_info, |
| 874 base::Bind(&Core::GetSubscriptionDidUnsubscribe, | 903 base::Bind(&Core::GetSubscriptionDidUnsubscribe, |
| 875 weak_factory_ui_to_ui_.GetWeakPtr(), callback, status)); | 904 weak_factory_ui_to_ui_.GetWeakPtr(), base::Passed(&callback), |
| 905 status)); |
| 876 | 906 |
| 877 RecordGetRegistrationStatus(status); | 907 RecordGetRegistrationStatus(status); |
| 878 } | 908 } |
| 879 } | 909 } |
| 880 | 910 |
| 881 void PushMessagingManager::Core::GetSubscriptionDidUnsubscribe( | 911 void PushMessagingManager::Core::GetSubscriptionDidUnsubscribe( |
| 882 const GetSubscriptionCallback& callback, | 912 GetSubscriptionCallback callback, |
| 883 PushGetRegistrationStatus get_status, | 913 PushGetRegistrationStatus get_status, |
| 884 PushUnregistrationStatus unsubscribe_status) { | 914 PushUnregistrationStatus unsubscribe_status) { |
| 885 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 915 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 886 BrowserThread::PostTask( | 916 BrowserThread::PostTask( |
| 887 BrowserThread::IO, FROM_HERE, | 917 BrowserThread::IO, FROM_HERE, |
| 888 base::Bind(callback, get_status, base::nullopt /* endpoint */, | 918 base::BindOnce(std::move(callback), get_status, |
| 889 base::nullopt /* options */, base::nullopt /* p256dh */, | 919 base::nullopt /* endpoint */, base::nullopt /* options */, |
| 890 base::nullopt /* auth */)); | 920 base::nullopt /* p256dh */, base::nullopt /* auth */)); |
| 891 } | 921 } |
| 892 | 922 |
| 893 // GetPermission methods on both IO and UI threads, merged in order of use from | 923 // GetPermission methods on both IO and UI threads, merged in order of use from |
| 894 // PushMessagingManager and Core. | 924 // PushMessagingManager and Core. |
| 895 // ----------------------------------------------------------------------------- | 925 // ----------------------------------------------------------------------------- |
| 896 | 926 |
| 897 void PushMessagingManager::GetPermissionStatus( | 927 void PushMessagingManager::GetPermissionStatus( |
| 898 int64_t service_worker_registration_id, | 928 int64_t service_worker_registration_id, |
| 899 bool user_visible, | 929 bool user_visible, |
| 900 const GetPermissionStatusCallback& callback) { | 930 GetPermissionStatusCallback callback) { |
| 901 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 931 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 902 ServiceWorkerRegistration* service_worker_registration = | 932 ServiceWorkerRegistration* service_worker_registration = |
| 903 service_worker_context_->GetLiveRegistration( | 933 service_worker_context_->GetLiveRegistration( |
| 904 service_worker_registration_id); | 934 service_worker_registration_id); |
| 905 if (!service_worker_registration) { | 935 if (!service_worker_registration) { |
| 906 // Return error: ErrorTypeAbort. | 936 // Return error: ErrorTypeAbort. |
| 907 callback.Run(blink::WebPushError::kErrorTypeAbort, | 937 std::move(callback).Run(blink::WebPushError::kErrorTypeAbort, |
| 908 blink::kWebPushPermissionStatusDenied); | 938 blink::kWebPushPermissionStatusDenied); |
| 909 return; | 939 return; |
| 910 } | 940 } |
| 911 | 941 |
| 912 BrowserThread::PostTask( | 942 BrowserThread::PostTask( |
| 913 BrowserThread::UI, FROM_HERE, | 943 BrowserThread::UI, FROM_HERE, |
| 914 base::Bind(&Core::GetPermissionStatusOnUI, | 944 base::Bind(&Core::GetPermissionStatusOnUI, |
| 915 base::Unretained(ui_core_.get()), callback, | 945 base::Unretained(ui_core_.get()), base::Passed(&callback), |
| 916 service_worker_registration->pattern().GetOrigin(), | 946 service_worker_registration->pattern().GetOrigin(), |
| 917 user_visible)); | 947 user_visible)); |
| 918 } | 948 } |
| 919 | 949 |
| 920 void PushMessagingManager::Core::GetPermissionStatusOnUI( | 950 void PushMessagingManager::Core::GetPermissionStatusOnUI( |
| 921 const GetPermissionStatusCallback& callback, | 951 GetPermissionStatusCallback callback, |
| 922 const GURL& requesting_origin, | 952 const GURL& requesting_origin, |
| 923 bool user_visible) { | 953 bool user_visible) { |
| 924 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 954 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 925 blink::WebPushPermissionStatus permission_status; | 955 blink::WebPushPermissionStatus permission_status; |
| 926 PushMessagingService* push_service = service(); | 956 PushMessagingService* push_service = service(); |
| 927 if (push_service) { | 957 if (push_service) { |
| 928 if (!user_visible && !push_service->SupportNonVisibleMessages()) { | 958 if (!user_visible && !push_service->SupportNonVisibleMessages()) { |
| 929 BrowserThread::PostTask( | 959 BrowserThread::PostTask( |
| 930 BrowserThread::IO, FROM_HERE, | 960 BrowserThread::IO, FROM_HERE, |
| 931 // Return error: ErrorTypeNotSupported. | 961 // Return error: ErrorTypeNotSupported. |
| 932 base::Bind(callback, blink::WebPushError::kErrorTypeNotSupported, | 962 base::BindOnce(std::move(callback), |
| 933 blink::kWebPushPermissionStatusDenied)); | 963 blink::WebPushError::kErrorTypeNotSupported, |
| 964 blink::kWebPushPermissionStatusDenied)); |
| 934 return; | 965 return; |
| 935 } | 966 } |
| 936 permission_status = | 967 permission_status = |
| 937 push_service->GetPermissionStatus(requesting_origin, user_visible); | 968 push_service->GetPermissionStatus(requesting_origin, user_visible); |
| 938 } else if (is_incognito()) { | 969 } else if (is_incognito()) { |
| 939 // Return prompt, so the website can't detect incognito mode. | 970 // Return prompt, so the website can't detect incognito mode. |
| 940 permission_status = blink::kWebPushPermissionStatusPrompt; | 971 permission_status = blink::kWebPushPermissionStatusPrompt; |
| 941 } else { | 972 } else { |
| 942 BrowserThread::PostTask( | 973 BrowserThread::PostTask( |
| 943 BrowserThread::IO, FROM_HERE, | 974 BrowserThread::IO, FROM_HERE, |
| 944 // Return error: ErrorTypeAbort. | 975 // Return error: ErrorTypeAbort. |
| 945 base::Bind(callback, blink::WebPushError::kErrorTypeAbort, | 976 base::BindOnce(std::move(callback), |
| 946 blink::kWebPushPermissionStatusDenied)); | 977 blink::WebPushError::kErrorTypeAbort, |
| 978 blink::kWebPushPermissionStatusDenied)); |
| 947 return; | 979 return; |
| 948 } | 980 } |
| 949 BrowserThread::PostTask( | 981 BrowserThread::PostTask( |
| 950 BrowserThread::IO, FROM_HERE, | 982 BrowserThread::IO, FROM_HERE, |
| 951 base::Bind(callback, blink::WebPushError::kErrorTypeNone, | 983 base::BindOnce(std::move(callback), blink::WebPushError::kErrorTypeNone, |
| 952 permission_status)); | 984 permission_status)); |
| 953 } | 985 } |
| 954 | 986 |
| 955 // Helper methods on both IO and UI threads, merged from | 987 // Helper methods on both IO and UI threads, merged from |
| 956 // PushMessagingManager and Core. | 988 // PushMessagingManager and Core. |
| 957 // ----------------------------------------------------------------------------- | 989 // ----------------------------------------------------------------------------- |
| 958 | 990 |
| 959 void PushMessagingManager::Core::GetSubscriptionInfoOnUI( | 991 void PushMessagingManager::Core::GetSubscriptionInfoOnUI( |
| 960 const GURL& origin, | 992 const GURL& origin, |
| 961 int64_t service_worker_registration_id, | 993 int64_t service_worker_registration_id, |
| 962 const std::string& sender_id, | 994 const std::string& sender_id, |
| 963 const std::string& push_subscription_id, | 995 const std::string& push_subscription_id, |
| 964 const PushMessagingService::SubscriptionInfoCallback& callback) { | 996 PushMessagingService::SubscriptionInfoCallback callback) { |
| 965 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 997 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 966 PushMessagingService* push_service = service(); | 998 PushMessagingService* push_service = service(); |
| 967 if (!push_service) { | 999 if (!push_service) { |
| 968 callback.Run(false /* is_valid */, std::vector<uint8_t>() /* p256dh */, | 1000 std::move(callback).Run(false /* is_valid */, |
| 969 std::vector<uint8_t>() /* auth */); | 1001 std::vector<uint8_t>() /* p256dh */, |
| 1002 std::vector<uint8_t>() /* auth */); |
| 970 return; | 1003 return; |
| 971 } | 1004 } |
| 972 | 1005 |
| 973 push_service->GetSubscriptionInfo(origin, service_worker_registration_id, | 1006 push_service->GetSubscriptionInfo(origin, service_worker_registration_id, |
| 974 sender_id, push_subscription_id, callback); | 1007 sender_id, push_subscription_id, |
| 1008 std::move(callback)); |
| 975 } | 1009 } |
| 976 | 1010 |
| 977 GURL PushMessagingManager::CreateEndpoint( | 1011 GURL PushMessagingManager::CreateEndpoint( |
| 978 bool standard_protocol, | 1012 bool standard_protocol, |
| 979 const std::string& subscription_id) const { | 1013 const std::string& subscription_id) const { |
| 980 const GURL& base = | 1014 const GURL& base = |
| 981 standard_protocol ? web_push_protocol_endpoint_ : default_endpoint_; | 1015 standard_protocol ? web_push_protocol_endpoint_ : default_endpoint_; |
| 982 | 1016 |
| 983 return GURL(base.spec() + subscription_id); | 1017 return GURL(base.spec() + subscription_id); |
| 984 } | 1018 } |
| 985 | 1019 |
| 986 PushMessagingService* PushMessagingManager::Core::service() { | 1020 PushMessagingService* PushMessagingManager::Core::service() { |
| 987 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1021 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 988 RenderProcessHost* process_host = | 1022 RenderProcessHost* process_host = |
| 989 RenderProcessHost::FromID(render_process_id_); | 1023 RenderProcessHost::FromID(render_process_id_); |
| 990 return process_host | 1024 return process_host |
| 991 ? process_host->GetBrowserContext()->GetPushMessagingService() | 1025 ? process_host->GetBrowserContext()->GetPushMessagingService() |
| 992 : nullptr; | 1026 : nullptr; |
| 993 } | 1027 } |
| 994 | 1028 |
| 995 base::WeakPtr<PushMessagingManager::Core> | 1029 base::WeakPtr<PushMessagingManager::Core> |
| 996 PushMessagingManager::Core::GetWeakPtrFromIOParentConstructor() { | 1030 PushMessagingManager::Core::GetWeakPtrFromIOParentConstructor() { |
| 997 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1031 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 998 return weak_factory_ui_to_ui_.GetWeakPtr(); | 1032 return weak_factory_ui_to_ui_.GetWeakPtr(); |
| 999 } | 1033 } |
| 1000 | 1034 |
| 1001 } // namespace content | 1035 } // namespace content |
| OLD | NEW |