| 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_message_filter.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" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "content/browser/renderer_host/render_process_host_impl.h" | 18 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 19 #include "content/browser/service_worker/service_worker_context_core.h" | 19 #include "content/browser/service_worker/service_worker_context_core.h" |
| 20 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 20 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 21 #include "content/browser/service_worker/service_worker_storage.h" | 21 #include "content/browser/service_worker/service_worker_storage.h" |
| 22 #include "content/common/push_messaging_messages.h" | |
| 23 #include "content/public/browser/browser_context.h" | 22 #include "content/public/browser/browser_context.h" |
| 24 #include "content/public/browser/browser_thread.h" | |
| 25 #include "content/public/browser/permission_manager.h" | 23 #include "content/public/browser/permission_manager.h" |
| 26 #include "content/public/browser/permission_type.h" | 24 #include "content/public/browser/permission_type.h" |
| 27 #include "content/public/browser/push_messaging_service.h" | 25 #include "content/public/browser/push_messaging_service.h" |
| 28 #include "content/public/browser/render_frame_host.h" | 26 #include "content/public/browser/render_frame_host.h" |
| 29 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 30 #include "content/public/common/child_process_host.h" | 28 #include "content/public/common/child_process_host.h" |
| 31 #include "content/public/common/console_message_level.h" | 29 #include "content/public/common/console_message_level.h" |
| 32 #include "content/public/common/content_switches.h" | 30 #include "content/public/common/content_switches.h" |
| 33 #include "content/public/common/push_messaging_status.h" | 31 #include "content/public/common/push_messaging_status.h" |
| 34 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushPermi
ssionStatus.h" | 32 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushPermi
ssionStatus.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 const std::string& stored_sender_id) { | 101 const std::string& stored_sender_id) { |
| 104 if (!sender_info.empty()) | 102 if (!sender_info.empty()) |
| 105 return sender_info; | 103 return sender_info; |
| 106 if (base::ContainsOnlyChars(stored_sender_id, "0123456789")) | 104 if (base::ContainsOnlyChars(stored_sender_id, "0123456789")) |
| 107 return stored_sender_id; | 105 return stored_sender_id; |
| 108 return std::string(); | 106 return std::string(); |
| 109 } | 107 } |
| 110 | 108 |
| 111 } // namespace | 109 } // namespace |
| 112 | 110 |
| 113 struct PushMessagingMessageFilter::RegisterData { | 111 struct PushMessagingManager::RegisterData { |
| 114 RegisterData(); | 112 RegisterData(); |
| 115 RegisterData(const RegisterData& other) = default; | 113 RegisterData(const RegisterData& other) = default; |
| 116 bool FromDocument() const; | 114 bool FromDocument() const; |
| 117 int request_id; | |
| 118 GURL requesting_origin; | 115 GURL requesting_origin; |
| 119 int64_t service_worker_registration_id; | 116 int64_t service_worker_registration_id; |
| 120 PushSubscriptionOptions options; | 117 PushSubscriptionOptions options; |
| 121 // The following member should only be read if FromDocument() is true. | 118 // The following member should only be read if FromDocument() is true. |
| 122 int render_frame_id; | 119 int render_frame_id; |
| 123 }; | 120 }; |
| 124 | 121 |
| 125 // Inner core of this message filter which lives on the UI thread. | 122 // Inner core of the PushMessagingManager which lives on the UI thread. |
| 126 class PushMessagingMessageFilter::Core { | 123 class PushMessagingManager::Core { |
| 127 public: | 124 public: |
| 128 Core(const base::WeakPtr<PushMessagingMessageFilter>& io_parent, | 125 Core(const base::WeakPtr<PushMessagingManager>& io_parent, |
| 129 int render_process_id); | 126 int render_process_id); |
| 130 | 127 |
| 131 // Public Register methods on UI thread -------------------------------------- | 128 // Public Register methods on UI thread -------------------------------------- |
| 132 | 129 |
| 133 // Called via PostTask from IO thread. | 130 // Called via PostTask from IO thread. |
| 134 void RegisterOnUI(const RegisterData& data); | 131 void RegisterOnUI(const SubscribeCallback& callback, |
| 132 const RegisterData& data); |
| 135 | 133 |
| 136 // Public Unregister methods on UI thread ------------------------------------ | 134 // Public Unregister methods on UI thread ------------------------------------ |
| 137 | 135 |
| 138 // Called via PostTask from IO thread. | 136 // Called via PostTask from IO thread. |
| 139 void UnregisterFromService(int request_id, | 137 void UnregisterFromService(const UnsubscribeCallback& callback, |
| 140 int64_t service_worker_registration_id, | 138 int64_t service_worker_registration_id, |
| 141 const GURL& requesting_origin, | 139 const GURL& requesting_origin, |
| 142 const std::string& sender_id); | 140 const std::string& sender_id); |
| 143 | 141 |
| 144 // Public GetPermission methods on UI thread --------------------------------- | 142 // Public GetPermission methods on UI thread --------------------------------- |
| 145 | 143 |
| 146 // Called via PostTask from IO thread. | 144 // Called via PostTask from IO thread. |
| 147 void GetPermissionStatusOnUI(const GURL& requesting_origin, | 145 void GetPermissionStatusOnUI(const GetPermissionStatusCallback& callback, |
| 148 bool user_visible, | 146 const GURL& requesting_origin, |
| 149 int request_id); | 147 bool user_visible); |
| 150 | 148 |
| 151 // Public helper methods on UI thread ---------------------------------------- | 149 // Public helper methods on UI thread ---------------------------------------- |
| 152 | 150 |
| 153 // Called via PostTask from IO thread. The |io_thread_callback| callback | 151 // Called via PostTask from IO thread. The |io_thread_callback| callback |
| 154 // will be invoked on the IO thread. | 152 // will be invoked on the IO thread. |
| 155 void GetEncryptionInfoOnUI( | 153 void GetEncryptionInfoOnUI( |
| 156 const GURL& origin, | 154 const GURL& origin, |
| 157 int64_t service_worker_registration_id, | 155 int64_t service_worker_registration_id, |
| 158 const std::string& sender_id, | 156 const std::string& sender_id, |
| 159 const PushMessagingService::EncryptionInfoCallback& io_thread_callback); | 157 const PushMessagingService::EncryptionInfoCallback& io_thread_callback); |
| 160 | 158 |
| 161 // Called (directly) from both the UI and IO threads. | 159 // Called (directly) from both the UI and IO threads. |
| 162 bool is_incognito() const { return is_incognito_; } | 160 bool is_incognito() const { return is_incognito_; } |
| 163 | 161 |
| 164 // Returns a push messaging service. May return null. | 162 // Returns a push messaging service. May return null. |
| 165 PushMessagingService* service(); | 163 PushMessagingService* service(); |
| 166 | 164 |
| 167 private: | 165 private: |
| 168 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 166 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
| 169 friend class base::DeleteHelper<Core>; | 167 friend class base::DeleteHelper<Core>; |
| 170 | 168 |
| 171 ~Core(); | 169 ~Core(); |
| 172 | 170 |
| 173 // Private Register methods on UI thread ------------------------------------- | 171 // Private Register methods on UI thread ------------------------------------- |
| 174 | 172 |
| 175 void DidRequestPermissionInIncognito(const RegisterData& data, | 173 void DidRequestPermissionInIncognito(const SubscribeCallback& callback, |
| 174 const RegisterData& data, |
| 176 blink::mojom::PermissionStatus status); | 175 blink::mojom::PermissionStatus status); |
| 177 | 176 |
| 178 void DidRegister(const RegisterData& data, | 177 void DidRegister(const SubscribeCallback& callback, |
| 178 const RegisterData& data, |
| 179 const std::string& push_registration_id, | 179 const std::string& push_registration_id, |
| 180 const std::vector<uint8_t>& p256dh, | 180 const std::vector<uint8_t>& p256dh, |
| 181 const std::vector<uint8_t>& auth, | 181 const std::vector<uint8_t>& auth, |
| 182 PushRegistrationStatus status); | 182 PushRegistrationStatus status); |
| 183 | 183 |
| 184 // Private Unregister methods on UI thread ----------------------------------- | 184 // Private Unregister methods on UI thread ----------------------------------- |
| 185 | 185 |
| 186 void DidUnregisterFromService(int request_id, | 186 void DidUnregisterFromService(const UnsubscribeCallback& callback, |
| 187 int64_t service_worker_registration_id, | 187 int64_t service_worker_registration_id, |
| 188 PushUnregistrationStatus unregistration_status); | 188 PushUnregistrationStatus unregistration_status); |
| 189 | 189 |
| 190 // Private helper methods on UI thread --------------------------------------- | 190 // Outer part of the PushMessagingManager which lives on the IO thread. |
| 191 | 191 base::WeakPtr<PushMessagingManager> io_parent_; |
| 192 void Send(IPC::Message* message); | |
| 193 | |
| 194 // Outer part of this message filter which lives on the IO thread. | |
| 195 base::WeakPtr<PushMessagingMessageFilter> io_parent_; | |
| 196 | 192 |
| 197 int render_process_id_; | 193 int render_process_id_; |
| 198 | 194 |
| 199 bool is_incognito_; | 195 bool is_incognito_; |
| 200 | 196 |
| 201 base::WeakPtrFactory<Core> weak_factory_ui_to_ui_; | 197 base::WeakPtrFactory<Core> weak_factory_ui_to_ui_; |
| 202 | 198 |
| 203 DISALLOW_COPY_AND_ASSIGN(Core); | 199 DISALLOW_COPY_AND_ASSIGN(Core); |
| 204 }; | 200 }; |
| 205 | 201 |
| 206 PushMessagingMessageFilter::RegisterData::RegisterData() | 202 PushMessagingManager::RegisterData::RegisterData() |
| 207 : request_id(0), | 203 : service_worker_registration_id(0), |
| 208 service_worker_registration_id(0), | |
| 209 render_frame_id(ChildProcessHost::kInvalidUniqueID) {} | 204 render_frame_id(ChildProcessHost::kInvalidUniqueID) {} |
| 210 | 205 |
| 211 bool PushMessagingMessageFilter::RegisterData::FromDocument() const { | 206 bool PushMessagingManager::RegisterData::FromDocument() const { |
| 212 return render_frame_id != ChildProcessHost::kInvalidUniqueID; | 207 return render_frame_id != ChildProcessHost::kInvalidUniqueID; |
| 213 } | 208 } |
| 214 | 209 |
| 215 PushMessagingMessageFilter::Core::Core( | 210 PushMessagingManager::Core::Core( |
| 216 const base::WeakPtr<PushMessagingMessageFilter>& io_parent, | 211 const base::WeakPtr<PushMessagingManager>& io_parent, |
| 217 int render_process_id) | 212 int render_process_id) |
| 218 : io_parent_(io_parent), | 213 : io_parent_(io_parent), |
| 219 render_process_id_(render_process_id), | 214 render_process_id_(render_process_id), |
| 220 weak_factory_ui_to_ui_(this) { | 215 weak_factory_ui_to_ui_(this) { |
| 221 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 216 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 222 RenderProcessHost* process_host = | 217 RenderProcessHost* process_host = |
| 223 RenderProcessHost::FromID(render_process_id_); // Can't be null yet. | 218 RenderProcessHost::FromID(render_process_id_); // Can't be null yet. |
| 224 is_incognito_ = process_host->GetBrowserContext()->IsOffTheRecord(); | 219 is_incognito_ = process_host->GetBrowserContext()->IsOffTheRecord(); |
| 225 } | 220 } |
| 226 | 221 |
| 227 PushMessagingMessageFilter::Core::~Core() {} | 222 PushMessagingManager::Core::~Core() {} |
| 228 | 223 |
| 229 PushMessagingMessageFilter::PushMessagingMessageFilter( | 224 PushMessagingManager::PushMessagingManager( |
| 230 int render_process_id, | 225 int render_process_id, |
| 231 ServiceWorkerContextWrapper* service_worker_context) | 226 ServiceWorkerContextWrapper* service_worker_context) |
| 232 : BrowserMessageFilter(PushMessagingMsgStart), | 227 : service_worker_context_(service_worker_context), |
| 233 service_worker_context_(service_worker_context), | |
| 234 weak_factory_io_to_io_(this) { | 228 weak_factory_io_to_io_(this) { |
| 235 // Although this class is used only on the IO thread, it is constructed on UI. | 229 // Although this class is used only on the IO thread, it is constructed on UI. |
| 236 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 230 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 237 // Normally, it would be unsafe to obtain a weak pointer from the UI thread, | 231 // Normally, it would be unsafe to obtain a weak pointer from the UI thread, |
| 238 // but it's ok in the constructor since we can't be destroyed before our | 232 // but it's ok in the constructor since we can't be destroyed before our |
| 239 // constructor finishes. | 233 // constructor finishes. |
| 240 ui_core_.reset( | 234 ui_core_.reset( |
| 241 new Core(weak_factory_io_to_io_.GetWeakPtr(), render_process_id)); | 235 new Core(weak_factory_io_to_io_.GetWeakPtr(), render_process_id)); |
| 242 | 236 |
| 243 PushMessagingService* service = ui_core_->service(); | 237 PushMessagingService* service = ui_core_->service(); |
| 244 service_available_ = !!service; | 238 service_available_ = !!service; |
| 245 | 239 |
| 246 if (service_available_) { | 240 if (service_available_) { |
| 247 default_endpoint_ = service->GetEndpoint(false /* standard_protocol */); | 241 default_endpoint_ = service->GetEndpoint(false /* standard_protocol */); |
| 248 web_push_protocol_endpoint_ = | 242 web_push_protocol_endpoint_ = |
| 249 service->GetEndpoint(true /* standard_protocol */); | 243 service->GetEndpoint(true /* standard_protocol */); |
| 250 } | 244 } |
| 251 } | 245 } |
| 252 | 246 |
| 253 PushMessagingMessageFilter::~PushMessagingMessageFilter() {} | 247 PushMessagingManager::~PushMessagingManager() {} |
| 254 | 248 |
| 255 void PushMessagingMessageFilter::OnDestruct() const { | 249 void PushMessagingManager::BindRequest(mojom::PushMessagingRequest request) { |
| 256 BrowserThread::DeleteOnIOThread::Destruct(this); | 250 bindings_.AddBinding(this, std::move(request)); |
| 257 } | |
| 258 | |
| 259 bool PushMessagingMessageFilter::OnMessageReceived( | |
| 260 const IPC::Message& message) { | |
| 261 bool handled = true; | |
| 262 IPC_BEGIN_MESSAGE_MAP(PushMessagingMessageFilter, message) | |
| 263 IPC_MESSAGE_HANDLER(PushMessagingHostMsg_Subscribe, OnSubscribe) | |
| 264 IPC_MESSAGE_HANDLER(PushMessagingHostMsg_Unsubscribe, OnUnsubscribe) | |
| 265 IPC_MESSAGE_HANDLER(PushMessagingHostMsg_GetSubscription, OnGetSubscription) | |
| 266 IPC_MESSAGE_HANDLER(PushMessagingHostMsg_GetPermissionStatus, | |
| 267 OnGetPermissionStatus) | |
| 268 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 269 IPC_END_MESSAGE_MAP() | |
| 270 return handled; | |
| 271 } | 251 } |
| 272 | 252 |
| 273 // Subscribe methods on both IO and UI threads, merged in order of use from | 253 // Subscribe methods on both IO and UI threads, merged in order of use from |
| 274 // PushMessagingMessageFilter and Core. | 254 // PushMessagingManager and Core. |
| 275 // ----------------------------------------------------------------------------- | 255 // ----------------------------------------------------------------------------- |
| 276 | 256 |
| 277 void PushMessagingMessageFilter::OnSubscribe( | 257 void PushMessagingManager::Subscribe(int32_t render_frame_id, |
| 278 int render_frame_id, | 258 int64_t service_worker_registration_id, |
| 279 int request_id, | 259 const PushSubscriptionOptions& options, |
| 280 int64_t service_worker_registration_id, | 260 const SubscribeCallback& callback) { |
| 281 const PushSubscriptionOptions& options) { | |
| 282 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 261 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 283 // TODO(mvanouwerkerk): Validate arguments? | 262 // TODO(mvanouwerkerk): Validate arguments? |
| 284 RegisterData data; | 263 RegisterData data; |
| 285 | 264 |
| 286 // Will be ChildProcessHost::kInvalidUniqueID in requests from Service Worker. | 265 // Will be ChildProcessHost::kInvalidUniqueID in requests from Service Worker. |
| 287 data.render_frame_id = render_frame_id; | 266 data.render_frame_id = render_frame_id; |
| 288 | 267 |
| 289 data.request_id = request_id; | |
| 290 data.service_worker_registration_id = service_worker_registration_id; | 268 data.service_worker_registration_id = service_worker_registration_id; |
| 291 data.options = options; | 269 data.options = options; |
| 292 | 270 |
| 293 ServiceWorkerRegistration* service_worker_registration = | 271 ServiceWorkerRegistration* service_worker_registration = |
| 294 service_worker_context_->GetLiveRegistration( | 272 service_worker_context_->GetLiveRegistration( |
| 295 data.service_worker_registration_id); | 273 data.service_worker_registration_id); |
| 296 if (!service_worker_registration || | 274 if (!service_worker_registration || |
| 297 !service_worker_registration->active_version()) { | 275 !service_worker_registration->active_version()) { |
| 298 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_NO_SERVICE_WORKER); | 276 SendSubscriptionError(callback, data, |
| 277 PUSH_REGISTRATION_STATUS_NO_SERVICE_WORKER); |
| 299 return; | 278 return; |
| 300 } | 279 } |
| 301 data.requesting_origin = service_worker_registration->pattern().GetOrigin(); | 280 data.requesting_origin = service_worker_registration->pattern().GetOrigin(); |
| 302 | 281 |
| 303 DCHECK(!(data.options.sender_info.empty() && data.FromDocument())); | 282 DCHECK(!(data.options.sender_info.empty() && data.FromDocument())); |
| 304 | 283 |
| 305 service_worker_context_->GetRegistrationUserData( | 284 service_worker_context_->GetRegistrationUserData( |
| 306 data.service_worker_registration_id, | 285 data.service_worker_registration_id, |
| 307 {kPushRegistrationIdServiceWorkerKey, kPushSenderIdServiceWorkerKey}, | 286 {kPushRegistrationIdServiceWorkerKey, kPushSenderIdServiceWorkerKey}, |
| 308 base::Bind(&PushMessagingMessageFilter::DidCheckForExistingRegistration, | 287 base::Bind(&PushMessagingManager::DidCheckForExistingRegistration, |
| 309 weak_factory_io_to_io_.GetWeakPtr(), data)); | 288 weak_factory_io_to_io_.GetWeakPtr(), callback, data)); |
| 310 } | 289 } |
| 311 | 290 |
| 312 void PushMessagingMessageFilter::DidCheckForExistingRegistration( | 291 void PushMessagingManager::DidCheckForExistingRegistration( |
| 292 const SubscribeCallback& callback, |
| 313 const RegisterData& data, | 293 const RegisterData& data, |
| 314 const std::vector<std::string>& push_registration_id_and_sender_id, | 294 const std::vector<std::string>& push_registration_id_and_sender_id, |
| 315 ServiceWorkerStatusCode service_worker_status) { | 295 ServiceWorkerStatusCode service_worker_status) { |
| 316 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 296 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 317 if (service_worker_status == SERVICE_WORKER_OK) { | 297 if (service_worker_status == SERVICE_WORKER_OK) { |
| 318 DCHECK_EQ(2u, push_registration_id_and_sender_id.size()); | 298 DCHECK_EQ(2u, push_registration_id_and_sender_id.size()); |
| 319 const auto& push_registration_id = push_registration_id_and_sender_id[0]; | 299 const auto& push_registration_id = push_registration_id_and_sender_id[0]; |
| 320 const auto& stored_sender_id = push_registration_id_and_sender_id[1]; | 300 const auto& stored_sender_id = push_registration_id_and_sender_id[1]; |
| 321 std::string fixed_sender_id = | 301 std::string fixed_sender_id = |
| 322 FixSenderInfo(data.options.sender_info, stored_sender_id); | 302 FixSenderInfo(data.options.sender_info, stored_sender_id); |
| 323 if (fixed_sender_id.empty()) { | 303 if (fixed_sender_id.empty()) { |
| 324 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | 304 SendSubscriptionError(callback, data, |
| 305 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); |
| 325 return; | 306 return; |
| 326 } | 307 } |
| 327 if (fixed_sender_id != stored_sender_id) { | 308 if (fixed_sender_id != stored_sender_id) { |
| 328 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_SENDER_ID_MISMATCH); | 309 SendSubscriptionError(callback, data, |
| 310 PUSH_REGISTRATION_STATUS_SENDER_ID_MISMATCH); |
| 329 return; | 311 return; |
| 330 } | 312 } |
| 331 auto callback = base::Bind( | 313 auto callback_ui = base::Bind(&PushMessagingManager::DidGetEncryptionKeys, |
| 332 &PushMessagingMessageFilter::DidGetEncryptionKeys, | 314 weak_factory_io_to_io_.GetWeakPtr(), callback, |
| 333 weak_factory_io_to_io_.GetWeakPtr(), data, push_registration_id); | 315 data, push_registration_id); |
| 334 BrowserThread::PostTask( | 316 BrowserThread::PostTask( |
| 335 BrowserThread::UI, FROM_HERE, | 317 BrowserThread::UI, FROM_HERE, |
| 336 base::Bind(&Core::GetEncryptionInfoOnUI, | 318 base::Bind(&Core::GetEncryptionInfoOnUI, |
| 337 base::Unretained(ui_core_.get()), data.requesting_origin, | 319 base::Unretained(ui_core_.get()), data.requesting_origin, |
| 338 data.service_worker_registration_id, fixed_sender_id, | 320 data.service_worker_registration_id, fixed_sender_id, |
| 339 callback)); | 321 callback_ui)); |
| 340 return; | 322 return; |
| 341 } | 323 } |
| 342 // TODO(johnme): The spec allows the register algorithm to reject with an | 324 // TODO(johnme): The spec allows the register algorithm to reject with an |
| 343 // AbortError when accessing storage fails. Perhaps we should do that if | 325 // AbortError when accessing storage fails. Perhaps we should do that if |
| 344 // service_worker_status != SERVICE_WORKER_ERROR_NOT_FOUND instead of | 326 // service_worker_status != SERVICE_WORKER_ERROR_NOT_FOUND instead of |
| 345 // attempting to do a fresh registration? | 327 // attempting to do a fresh registration? |
| 346 // https://w3c.github.io/push-api/#widl-PushRegistrationManager-register-Promi
se-PushRegistration | 328 // https://w3c.github.io/push-api/#widl-PushRegistrationManager-register-Promi
se-PushRegistration |
| 347 if (!data.options.sender_info.empty()) { | 329 if (!data.options.sender_info.empty()) { |
| 348 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 330 BrowserThread::PostTask( |
| 349 base::Bind(&Core::RegisterOnUI, | 331 BrowserThread::UI, FROM_HERE, |
| 350 base::Unretained(ui_core_.get()), data)); | 332 base::Bind(&Core::RegisterOnUI, base::Unretained(ui_core_.get()), |
| 333 callback, data)); |
| 351 } else { | 334 } else { |
| 352 // There is no existing registration and the sender_info passed in was | 335 // 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. | 336 // empty, but perhaps there is a stored sender id we can use. |
| 354 service_worker_context_->GetRegistrationUserData( | 337 service_worker_context_->GetRegistrationUserData( |
| 355 data.service_worker_registration_id, {kPushSenderIdServiceWorkerKey}, | 338 data.service_worker_registration_id, {kPushSenderIdServiceWorkerKey}, |
| 356 base::Bind(&PushMessagingMessageFilter::DidGetSenderIdFromStorage, | 339 base::Bind(&PushMessagingManager::DidGetSenderIdFromStorage, |
| 357 weak_factory_io_to_io_.GetWeakPtr(), data)); | 340 weak_factory_io_to_io_.GetWeakPtr(), callback, data)); |
| 358 } | 341 } |
| 359 } | 342 } |
| 360 | 343 |
| 361 void PushMessagingMessageFilter::DidGetEncryptionKeys( | 344 void PushMessagingManager::DidGetEncryptionKeys( |
| 345 const SubscribeCallback& callback, |
| 362 const RegisterData& data, | 346 const RegisterData& data, |
| 363 const std::string& push_registration_id, | 347 const std::string& push_registration_id, |
| 364 bool success, | 348 bool success, |
| 365 const std::vector<uint8_t>& p256dh, | 349 const std::vector<uint8_t>& p256dh, |
| 366 const std::vector<uint8_t>& auth) { | 350 const std::vector<uint8_t>& auth) { |
| 367 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 351 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 368 if (!success) { | 352 if (!success) { |
| 369 SendSubscriptionError( | 353 SendSubscriptionError(callback, data, |
| 370 data, PUSH_REGISTRATION_STATUS_PUBLIC_KEY_UNAVAILABLE); | 354 PUSH_REGISTRATION_STATUS_PUBLIC_KEY_UNAVAILABLE); |
| 371 return; | 355 return; |
| 372 } | 356 } |
| 373 | 357 |
| 374 SendSubscriptionSuccess(data, PUSH_REGISTRATION_STATUS_SUCCESS_FROM_CACHE, | 358 SendSubscriptionSuccess(callback, data, |
| 359 PUSH_REGISTRATION_STATUS_SUCCESS_FROM_CACHE, |
| 375 push_registration_id, p256dh, auth); | 360 push_registration_id, p256dh, auth); |
| 376 } | 361 } |
| 377 | 362 |
| 378 void PushMessagingMessageFilter::DidGetSenderIdFromStorage( | 363 void PushMessagingManager::DidGetSenderIdFromStorage( |
| 364 const SubscribeCallback& callback, |
| 379 const RegisterData& data, | 365 const RegisterData& data, |
| 380 const std::vector<std::string>& stored_sender_id, | 366 const std::vector<std::string>& stored_sender_id, |
| 381 ServiceWorkerStatusCode service_worker_status) { | 367 ServiceWorkerStatusCode service_worker_status) { |
| 382 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 368 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 383 if (service_worker_status != SERVICE_WORKER_OK) { | 369 if (service_worker_status != SERVICE_WORKER_OK) { |
| 384 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | 370 SendSubscriptionError(callback, data, |
| 371 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); |
| 385 return; | 372 return; |
| 386 } | 373 } |
| 387 DCHECK_EQ(1u, stored_sender_id.size()); | 374 DCHECK_EQ(1u, stored_sender_id.size()); |
| 388 // We should only be here because no sender info was supplied to subscribe(). | 375 // We should only be here because no sender info was supplied to subscribe(). |
| 389 DCHECK(data.options.sender_info.empty()); | 376 DCHECK(data.options.sender_info.empty()); |
| 390 std::string fixed_sender_id = | 377 std::string fixed_sender_id = |
| 391 FixSenderInfo(data.options.sender_info, stored_sender_id[0]); | 378 FixSenderInfo(data.options.sender_info, stored_sender_id[0]); |
| 392 if (fixed_sender_id.empty()) { | 379 if (fixed_sender_id.empty()) { |
| 393 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | 380 SendSubscriptionError(callback, data, |
| 381 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); |
| 394 return; | 382 return; |
| 395 } | 383 } |
| 396 RegisterData mutated_data = data; | 384 RegisterData mutated_data = data; |
| 397 mutated_data.options.sender_info = fixed_sender_id; | 385 mutated_data.options.sender_info = fixed_sender_id; |
| 398 BrowserThread::PostTask( | 386 BrowserThread::PostTask( |
| 399 BrowserThread::UI, FROM_HERE, | 387 BrowserThread::UI, FROM_HERE, |
| 400 base::Bind(&Core::RegisterOnUI, base::Unretained(ui_core_.get()), | 388 base::Bind(&Core::RegisterOnUI, base::Unretained(ui_core_.get()), |
| 401 mutated_data)); | 389 callback, mutated_data)); |
| 402 } | 390 } |
| 403 | 391 |
| 404 void PushMessagingMessageFilter::Core::RegisterOnUI( | 392 void PushMessagingManager::Core::RegisterOnUI( |
| 405 const PushMessagingMessageFilter::RegisterData& data) { | 393 const SubscribeCallback& callback, |
| 394 const PushMessagingManager::RegisterData& data) { |
| 406 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 395 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 407 PushMessagingService* push_service = service(); | 396 PushMessagingService* push_service = service(); |
| 408 if (!push_service) { | 397 if (!push_service) { |
| 409 if (!is_incognito()) { | 398 if (!is_incognito()) { |
| 410 // This might happen if InstanceIDProfileService::IsInstanceIDEnabled | 399 // This might happen if InstanceIDProfileService::IsInstanceIDEnabled |
| 411 // returns false because the Instance ID kill switch was enabled. | 400 // returns false because the Instance ID kill switch was enabled. |
| 412 // TODO(johnme): Might be better not to expose the API in this case. | 401 // TODO(johnme): Might be better not to expose the API in this case. |
| 413 BrowserThread::PostTask( | 402 BrowserThread::PostTask( |
| 414 BrowserThread::IO, FROM_HERE, | 403 BrowserThread::IO, FROM_HERE, |
| 415 base::Bind(&PushMessagingMessageFilter::SendSubscriptionError, | 404 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 416 io_parent_, | 405 callback, data, |
| 417 data, PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE)); | 406 PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE)); |
| 418 } else { | 407 } else { |
| 419 // Prevent websites from detecting incognito mode, by emulating what would | 408 // Prevent websites from detecting incognito mode, by emulating what would |
| 420 // have happened if we had a PushMessagingService available. | 409 // have happened if we had a PushMessagingService available. |
| 421 if (!data.FromDocument() || !data.options.user_visible_only) { | 410 if (!data.FromDocument() || !data.options.user_visible_only) { |
| 422 // Throw a permission denied error under the same circumstances. | 411 // Throw a permission denied error under the same circumstances. |
| 423 BrowserThread::PostTask( | 412 BrowserThread::PostTask( |
| 424 BrowserThread::IO, FROM_HERE, | 413 BrowserThread::IO, FROM_HERE, |
| 425 base::Bind(&PushMessagingMessageFilter::SendSubscriptionError, | 414 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 426 io_parent_, data, | 415 callback, data, |
| 427 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); | 416 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); |
| 428 } else { | 417 } else { |
| 429 RenderFrameHost* render_frame_host = | 418 RenderFrameHost* render_frame_host = |
| 430 RenderFrameHost::FromID(render_process_id_, data.render_frame_id); | 419 RenderFrameHost::FromID(render_process_id_, data.render_frame_id); |
| 431 WebContents* web_contents = | 420 WebContents* web_contents = |
| 432 WebContents::FromRenderFrameHost(render_frame_host); | 421 WebContents::FromRenderFrameHost(render_frame_host); |
| 433 if (web_contents) { | 422 if (web_contents) { |
| 434 web_contents->GetMainFrame()->AddMessageToConsole( | 423 web_contents->GetMainFrame()->AddMessageToConsole( |
| 435 CONSOLE_MESSAGE_LEVEL_ERROR, kIncognitoPushUnsupportedMessage); | 424 CONSOLE_MESSAGE_LEVEL_ERROR, kIncognitoPushUnsupportedMessage); |
| 436 // Request push messaging permission (which will fail, since | 425 // Request push messaging permission (which will fail, since |
| 437 // notifications aren't supported in incognito), so the website can't | 426 // notifications aren't supported in incognito), so the website can't |
| 438 // detect whether incognito is active. | 427 // detect whether incognito is active. |
| 439 web_contents->GetBrowserContext() | 428 web_contents->GetBrowserContext() |
| 440 ->GetPermissionManager() | 429 ->GetPermissionManager() |
| 441 ->RequestPermission( | 430 ->RequestPermission( |
| 442 PermissionType::PUSH_MESSAGING, render_frame_host, | 431 PermissionType::PUSH_MESSAGING, render_frame_host, |
| 443 data.requesting_origin, false /* user_gesture */, | 432 data.requesting_origin, false /* user_gesture */, |
| 444 base::Bind(&PushMessagingMessageFilter::Core:: | 433 base::Bind(&PushMessagingManager::Core:: |
| 445 DidRequestPermissionInIncognito, | 434 DidRequestPermissionInIncognito, |
| 446 weak_factory_ui_to_ui_.GetWeakPtr(), data)); | 435 weak_factory_ui_to_ui_.GetWeakPtr(), callback, |
| 436 data)); |
| 447 } | 437 } |
| 448 } | 438 } |
| 449 } | 439 } |
| 450 return; | 440 return; |
| 451 } | 441 } |
| 452 | 442 |
| 453 if (data.FromDocument()) { | 443 if (data.FromDocument()) { |
| 454 push_service->SubscribeFromDocument( | 444 push_service->SubscribeFromDocument( |
| 455 data.requesting_origin, data.service_worker_registration_id, | 445 data.requesting_origin, data.service_worker_registration_id, |
| 456 render_process_id_, data.render_frame_id, data.options, | 446 render_process_id_, data.render_frame_id, data.options, |
| 457 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), | 447 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), |
| 458 data)); | 448 callback, data)); |
| 459 } else { | 449 } else { |
| 460 push_service->SubscribeFromWorker( | 450 push_service->SubscribeFromWorker( |
| 461 data.requesting_origin, data.service_worker_registration_id, | 451 data.requesting_origin, data.service_worker_registration_id, |
| 462 data.options, base::Bind(&Core::DidRegister, | 452 data.options, |
| 463 weak_factory_ui_to_ui_.GetWeakPtr(), data)); | 453 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), |
| 454 callback, data)); |
| 464 } | 455 } |
| 465 } | 456 } |
| 466 | 457 |
| 467 void PushMessagingMessageFilter::Core::DidRequestPermissionInIncognito( | 458 void PushMessagingManager::Core::DidRequestPermissionInIncognito( |
| 459 const SubscribeCallback& callback, |
| 468 const RegisterData& data, | 460 const RegisterData& data, |
| 469 blink::mojom::PermissionStatus status) { | 461 blink::mojom::PermissionStatus status) { |
| 470 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 462 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 471 // Notification permission should always be denied in incognito. | 463 // Notification permission should always be denied in incognito. |
| 472 DCHECK_EQ(blink::mojom::PermissionStatus::DENIED, status); | 464 DCHECK_EQ(blink::mojom::PermissionStatus::DENIED, status); |
| 473 BrowserThread::PostTask( | 465 BrowserThread::PostTask( |
| 474 BrowserThread::IO, FROM_HERE, | 466 BrowserThread::IO, FROM_HERE, |
| 475 base::Bind(&PushMessagingMessageFilter::SendSubscriptionError, io_parent_, | 467 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 476 data, PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); | 468 callback, data, |
| 469 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); |
| 477 } | 470 } |
| 478 | 471 |
| 479 void PushMessagingMessageFilter::Core::DidRegister( | 472 void PushMessagingManager::Core::DidRegister( |
| 473 const SubscribeCallback& callback, |
| 480 const RegisterData& data, | 474 const RegisterData& data, |
| 481 const std::string& push_registration_id, | 475 const std::string& push_registration_id, |
| 482 const std::vector<uint8_t>& p256dh, | 476 const std::vector<uint8_t>& p256dh, |
| 483 const std::vector<uint8_t>& auth, | 477 const std::vector<uint8_t>& auth, |
| 484 PushRegistrationStatus status) { | 478 PushRegistrationStatus status) { |
| 485 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 479 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 486 if (status == PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE) { | 480 if (status == PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE) { |
| 487 BrowserThread::PostTask( | 481 BrowserThread::PostTask( |
| 488 BrowserThread::IO, FROM_HERE, | 482 BrowserThread::IO, FROM_HERE, |
| 489 base::Bind(&PushMessagingMessageFilter::PersistRegistrationOnIO, | 483 base::Bind(&PushMessagingManager::PersistRegistrationOnIO, io_parent_, |
| 490 io_parent_, data, push_registration_id, p256dh, auth)); | 484 callback, data, push_registration_id, p256dh, auth)); |
| 491 } else { | 485 } else { |
| 492 BrowserThread::PostTask( | 486 BrowserThread::PostTask( |
| 493 BrowserThread::IO, FROM_HERE, | 487 BrowserThread::IO, FROM_HERE, |
| 494 base::Bind(&PushMessagingMessageFilter::SendSubscriptionError, | 488 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 495 io_parent_, data, status)); | 489 callback, data, status)); |
| 496 } | 490 } |
| 497 } | 491 } |
| 498 | 492 |
| 499 void PushMessagingMessageFilter::PersistRegistrationOnIO( | 493 void PushMessagingManager::PersistRegistrationOnIO( |
| 494 const SubscribeCallback& callback, |
| 500 const RegisterData& data, | 495 const RegisterData& data, |
| 501 const std::string& push_registration_id, | 496 const std::string& push_registration_id, |
| 502 const std::vector<uint8_t>& p256dh, | 497 const std::vector<uint8_t>& p256dh, |
| 503 const std::vector<uint8_t>& auth) { | 498 const std::vector<uint8_t>& auth) { |
| 504 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 499 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 505 service_worker_context_->StoreRegistrationUserData( | 500 service_worker_context_->StoreRegistrationUserData( |
| 506 data.service_worker_registration_id, data.requesting_origin, | 501 data.service_worker_registration_id, data.requesting_origin, |
| 507 {{kPushRegistrationIdServiceWorkerKey, push_registration_id}, | 502 {{kPushRegistrationIdServiceWorkerKey, push_registration_id}, |
| 508 {kPushSenderIdServiceWorkerKey, data.options.sender_info}}, | 503 {kPushSenderIdServiceWorkerKey, data.options.sender_info}}, |
| 509 base::Bind(&PushMessagingMessageFilter::DidPersistRegistrationOnIO, | 504 base::Bind(&PushMessagingManager::DidPersistRegistrationOnIO, |
| 510 weak_factory_io_to_io_.GetWeakPtr(), data, | 505 weak_factory_io_to_io_.GetWeakPtr(), callback, data, |
| 511 push_registration_id, p256dh, auth)); | 506 push_registration_id, p256dh, auth)); |
| 512 } | 507 } |
| 513 | 508 |
| 514 void PushMessagingMessageFilter::DidPersistRegistrationOnIO( | 509 void PushMessagingManager::DidPersistRegistrationOnIO( |
| 510 const SubscribeCallback& callback, |
| 515 const RegisterData& data, | 511 const RegisterData& data, |
| 516 const std::string& push_registration_id, | 512 const std::string& push_registration_id, |
| 517 const std::vector<uint8_t>& p256dh, | 513 const std::vector<uint8_t>& p256dh, |
| 518 const std::vector<uint8_t>& auth, | 514 const std::vector<uint8_t>& auth, |
| 519 ServiceWorkerStatusCode service_worker_status) { | 515 ServiceWorkerStatusCode service_worker_status) { |
| 520 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 516 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 521 if (service_worker_status == SERVICE_WORKER_OK) { | 517 if (service_worker_status == SERVICE_WORKER_OK) { |
| 522 SendSubscriptionSuccess(data, | 518 SendSubscriptionSuccess(callback, data, |
| 523 PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, | 519 PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, |
| 524 push_registration_id, p256dh, auth); | 520 push_registration_id, p256dh, auth); |
| 525 } else { | 521 } else { |
| 526 // TODO(johnme): Unregister, so PushMessagingServiceImpl can decrease count. | 522 // TODO(johnme): Unregister, so PushMessagingServiceImpl can decrease count. |
| 527 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_STORAGE_ERROR); | 523 SendSubscriptionError(callback, data, |
| 524 PUSH_REGISTRATION_STATUS_STORAGE_ERROR); |
| 528 } | 525 } |
| 529 } | 526 } |
| 530 | 527 |
| 531 void PushMessagingMessageFilter::SendSubscriptionError( | 528 void PushMessagingManager::SendSubscriptionError( |
| 532 const RegisterData& data, PushRegistrationStatus status) { | 529 const SubscribeCallback& callback, |
| 530 const RegisterData& data, |
| 531 PushRegistrationStatus status) { |
| 533 // Only called from IO thread, but would be safe to call from UI thread. | 532 // Only called from IO thread, but would be safe to call from UI thread. |
| 534 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 533 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 535 if (data.FromDocument()) { | 534 callback.Run(status, base::nullopt /* endpoint */, |
| 536 Send(new PushMessagingMsg_SubscribeFromDocumentError( | 535 base::nullopt /* options */, base::nullopt /* p256dh */, |
| 537 data.render_frame_id, data.request_id, status)); | 536 base::nullopt /* auth */); |
| 538 } else { | |
| 539 Send( | |
| 540 new PushMessagingMsg_SubscribeFromWorkerError(data.request_id, status)); | |
| 541 } | |
| 542 RecordRegistrationStatus(status); | 537 RecordRegistrationStatus(status); |
| 543 } | 538 } |
| 544 | 539 |
| 545 void PushMessagingMessageFilter::SendSubscriptionSuccess( | 540 void PushMessagingManager::SendSubscriptionSuccess( |
| 541 const SubscribeCallback& callback, |
| 546 const RegisterData& data, | 542 const RegisterData& data, |
| 547 PushRegistrationStatus status, | 543 PushRegistrationStatus status, |
| 548 const std::string& push_subscription_id, | 544 const std::string& push_subscription_id, |
| 549 const std::vector<uint8_t>& p256dh, | 545 const std::vector<uint8_t>& p256dh, |
| 550 const std::vector<uint8_t>& auth) { | 546 const std::vector<uint8_t>& auth) { |
| 551 // Only called from IO thread, but would be safe to call from UI thread. | 547 // Only called from IO thread, but would be safe to call from UI thread. |
| 552 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 548 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 553 if (!service_available_) { | 549 if (!service_available_) { |
| 554 // This shouldn't be possible in incognito mode, since we've already checked | 550 // This shouldn't be possible in incognito mode, since we've already checked |
| 555 // that we have an existing registration. Hence it's ok to throw an error. | 551 // that we have an existing registration. Hence it's ok to throw an error. |
| 556 DCHECK(!ui_core_->is_incognito()); | 552 DCHECK(!ui_core_->is_incognito()); |
| 557 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE); | 553 SendSubscriptionError(callback, data, |
| 554 PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE); |
| 558 return; | 555 return; |
| 559 } | 556 } |
| 560 | 557 |
| 561 const GURL endpoint = CreateEndpoint( | 558 const GURL endpoint = CreateEndpoint( |
| 562 IsApplicationServerKey(data.options.sender_info), push_subscription_id); | 559 IsApplicationServerKey(data.options.sender_info), push_subscription_id); |
| 563 | 560 |
| 564 if (data.FromDocument()) { | 561 callback.Run(status, endpoint, data.options, p256dh, auth); |
| 565 Send(new PushMessagingMsg_SubscribeFromDocumentSuccess( | 562 |
| 566 data.render_frame_id, data.request_id, endpoint, data.options, p256dh, | |
| 567 auth)); | |
| 568 } else { | |
| 569 Send(new PushMessagingMsg_SubscribeFromWorkerSuccess( | |
| 570 data.request_id, endpoint, data.options, p256dh, auth)); | |
| 571 } | |
| 572 RecordRegistrationStatus(status); | 563 RecordRegistrationStatus(status); |
| 573 } | 564 } |
| 574 | 565 |
| 575 // Unsubscribe methods on both IO and UI threads, merged in order of use from | 566 // Unsubscribe methods on both IO and UI threads, merged in order of use from |
| 576 // PushMessagingMessageFilter and Core. | 567 // PushMessagingManager and Core. |
| 577 // ----------------------------------------------------------------------------- | 568 // ----------------------------------------------------------------------------- |
| 578 | 569 |
| 579 void PushMessagingMessageFilter::OnUnsubscribe( | 570 void PushMessagingManager::Unsubscribe(int64_t service_worker_registration_id, |
| 580 int request_id, int64_t service_worker_registration_id) { | 571 const UnsubscribeCallback& callback) { |
| 581 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 572 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 582 ServiceWorkerRegistration* service_worker_registration = | 573 ServiceWorkerRegistration* service_worker_registration = |
| 583 service_worker_context_->GetLiveRegistration( | 574 service_worker_context_->GetLiveRegistration( |
| 584 service_worker_registration_id); | 575 service_worker_registration_id); |
| 585 if (!service_worker_registration) { | 576 if (!service_worker_registration) { |
| 586 DidUnregister(request_id, PUSH_UNREGISTRATION_STATUS_NO_SERVICE_WORKER); | 577 DidUnregister(callback, PUSH_UNREGISTRATION_STATUS_NO_SERVICE_WORKER); |
| 587 return; | 578 return; |
| 588 } | 579 } |
| 589 | 580 |
| 590 service_worker_context_->GetRegistrationUserData( | 581 service_worker_context_->GetRegistrationUserData( |
| 591 service_worker_registration_id, {kPushSenderIdServiceWorkerKey}, | 582 service_worker_registration_id, {kPushSenderIdServiceWorkerKey}, |
| 592 base::Bind(&PushMessagingMessageFilter::UnsubscribeHavingGottenSenderId, | 583 base::Bind(&PushMessagingManager::UnsubscribeHavingGottenSenderId, |
| 593 weak_factory_io_to_io_.GetWeakPtr(), request_id, | 584 weak_factory_io_to_io_.GetWeakPtr(), callback, |
| 594 service_worker_registration_id, | 585 service_worker_registration_id, |
| 595 service_worker_registration->pattern().GetOrigin())); | 586 service_worker_registration->pattern().GetOrigin())); |
| 596 } | 587 } |
| 597 | 588 |
| 598 void PushMessagingMessageFilter::UnsubscribeHavingGottenSenderId( | 589 void PushMessagingManager::UnsubscribeHavingGottenSenderId( |
| 599 int request_id, | 590 const UnsubscribeCallback& callback, |
| 600 int64_t service_worker_registration_id, | 591 int64_t service_worker_registration_id, |
| 601 const GURL& requesting_origin, | 592 const GURL& requesting_origin, |
| 602 const std::vector<std::string>& sender_ids, | 593 const std::vector<std::string>& sender_ids, |
| 603 ServiceWorkerStatusCode service_worker_status) { | 594 ServiceWorkerStatusCode service_worker_status) { |
| 604 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 595 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 605 | 596 |
| 606 std::string sender_id; | 597 std::string sender_id; |
| 607 if (service_worker_status == SERVICE_WORKER_OK) { | 598 if (service_worker_status == SERVICE_WORKER_OK) { |
| 608 DCHECK_EQ(1u, sender_ids.size()); | 599 DCHECK_EQ(1u, sender_ids.size()); |
| 609 sender_id = sender_ids[0]; | 600 sender_id = sender_ids[0]; |
| 610 } | 601 } |
| 611 BrowserThread::PostTask( | 602 BrowserThread::PostTask( |
| 612 BrowserThread::UI, FROM_HERE, | 603 BrowserThread::UI, FROM_HERE, |
| 613 base::Bind(&Core::UnregisterFromService, base::Unretained(ui_core_.get()), | 604 base::Bind(&Core::UnregisterFromService, base::Unretained(ui_core_.get()), |
| 614 request_id, service_worker_registration_id, requesting_origin, | 605 callback, service_worker_registration_id, requesting_origin, |
| 615 sender_id)); | 606 sender_id)); |
| 616 } | 607 } |
| 617 | 608 |
| 618 void PushMessagingMessageFilter::Core::UnregisterFromService( | 609 void PushMessagingManager::Core::UnregisterFromService( |
| 619 int request_id, | 610 const UnsubscribeCallback& callback, |
| 620 int64_t service_worker_registration_id, | 611 int64_t service_worker_registration_id, |
| 621 const GURL& requesting_origin, | 612 const GURL& requesting_origin, |
| 622 const std::string& sender_id) { | 613 const std::string& sender_id) { |
| 623 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 614 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 624 PushMessagingService* push_service = service(); | 615 PushMessagingService* push_service = service(); |
| 625 if (!push_service) { | 616 if (!push_service) { |
| 626 // This shouldn't be possible in incognito mode, since we've already checked | 617 // This shouldn't be possible in incognito mode, since we've already checked |
| 627 // that we have an existing registration. Hence it's ok to throw an error. | 618 // that we have an existing registration. Hence it's ok to throw an error. |
| 628 DCHECK(!is_incognito()); | 619 DCHECK(!is_incognito()); |
| 629 BrowserThread::PostTask( | 620 BrowserThread::PostTask( |
| 630 BrowserThread::IO, FROM_HERE, | 621 BrowserThread::IO, FROM_HERE, |
| 631 base::Bind(&PushMessagingMessageFilter::DidUnregister, io_parent_, | 622 base::Bind(&PushMessagingManager::DidUnregister, io_parent_, callback, |
| 632 request_id, | |
| 633 PUSH_UNREGISTRATION_STATUS_SERVICE_NOT_AVAILABLE)); | 623 PUSH_UNREGISTRATION_STATUS_SERVICE_NOT_AVAILABLE)); |
| 634 return; | 624 return; |
| 635 } | 625 } |
| 636 | 626 |
| 637 push_service->Unsubscribe( | 627 push_service->Unsubscribe( |
| 638 requesting_origin, service_worker_registration_id, sender_id, | 628 requesting_origin, service_worker_registration_id, sender_id, |
| 639 base::Bind(&Core::DidUnregisterFromService, | 629 base::Bind(&Core::DidUnregisterFromService, |
| 640 weak_factory_ui_to_ui_.GetWeakPtr(), request_id, | 630 weak_factory_ui_to_ui_.GetWeakPtr(), callback, |
| 641 service_worker_registration_id)); | 631 service_worker_registration_id)); |
| 642 } | 632 } |
| 643 | 633 |
| 644 void PushMessagingMessageFilter::Core::DidUnregisterFromService( | 634 void PushMessagingManager::Core::DidUnregisterFromService( |
| 645 int request_id, | 635 const UnsubscribeCallback& callback, |
| 646 int64_t service_worker_registration_id, | 636 int64_t service_worker_registration_id, |
| 647 PushUnregistrationStatus unregistration_status) { | 637 PushUnregistrationStatus unregistration_status) { |
| 648 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 638 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 649 | 639 |
| 650 BrowserThread::PostTask( | 640 BrowserThread::PostTask( |
| 651 BrowserThread::IO, FROM_HERE, | 641 BrowserThread::IO, FROM_HERE, |
| 652 base::Bind(&PushMessagingMessageFilter::DidUnregister, io_parent_, | 642 base::Bind(&PushMessagingManager::DidUnregister, io_parent_, callback, |
| 653 request_id, unregistration_status)); | 643 unregistration_status)); |
| 654 } | 644 } |
| 655 | 645 |
| 656 void PushMessagingMessageFilter::DidUnregister( | 646 void PushMessagingManager::DidUnregister( |
| 657 int request_id, | 647 const UnsubscribeCallback& callback, |
| 658 PushUnregistrationStatus unregistration_status) { | 648 PushUnregistrationStatus unregistration_status) { |
| 659 // Only called from IO thread, but would be safe to call from UI thread. | 649 // Only called from IO thread, but would be safe to call from UI thread. |
| 660 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 650 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 661 switch (unregistration_status) { | 651 switch (unregistration_status) { |
| 662 case PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTERED: | 652 case PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTERED: |
| 663 case PUSH_UNREGISTRATION_STATUS_PENDING_NETWORK_ERROR: | 653 case PUSH_UNREGISTRATION_STATUS_PENDING_NETWORK_ERROR: |
| 664 case PUSH_UNREGISTRATION_STATUS_PENDING_SERVICE_ERROR: | 654 case PUSH_UNREGISTRATION_STATUS_PENDING_SERVICE_ERROR: |
| 665 Send(new PushMessagingMsg_UnsubscribeSuccess(request_id, true)); | 655 callback.Run(true /* success */, true /* did_unsubscribe */, |
| 656 blink::WebPushError::ErrorTypeUnknown, |
| 657 base::nullopt /* error_message */); |
| 666 break; | 658 break; |
| 667 case PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED: | 659 case PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED: |
| 668 Send(new PushMessagingMsg_UnsubscribeSuccess(request_id, false)); | 660 callback.Run(true /* success */, false /* did_unsubscribe */, |
| 661 blink::WebPushError::ErrorTypeUnknown, |
| 662 base::nullopt /* error_message */); |
| 669 break; | 663 break; |
| 670 case PUSH_UNREGISTRATION_STATUS_NO_SERVICE_WORKER: | 664 case PUSH_UNREGISTRATION_STATUS_NO_SERVICE_WORKER: |
| 671 case PUSH_UNREGISTRATION_STATUS_SERVICE_NOT_AVAILABLE: | 665 case PUSH_UNREGISTRATION_STATUS_SERVICE_NOT_AVAILABLE: |
| 672 case PUSH_UNREGISTRATION_STATUS_STORAGE_ERROR: | 666 case PUSH_UNREGISTRATION_STATUS_STORAGE_ERROR: |
| 673 Send(new PushMessagingMsg_UnsubscribeError( | 667 callback.Run( |
| 674 request_id, blink::WebPushError::ErrorTypeAbort, | 668 false /* error */, false, blink::WebPushError::ErrorTypeAbort, |
| 675 PushUnregistrationStatusToString(unregistration_status))); | 669 std::string(PushUnregistrationStatusToString(unregistration_status))); |
| 676 break; | 670 break; |
| 677 case PUSH_UNREGISTRATION_STATUS_NETWORK_ERROR: | 671 case PUSH_UNREGISTRATION_STATUS_NETWORK_ERROR: |
| 678 NOTREACHED(); | 672 NOTREACHED(); |
| 679 break; | 673 break; |
| 680 } | 674 } |
| 681 RecordUnregistrationStatus(unregistration_status); | 675 RecordUnregistrationStatus(unregistration_status); |
| 682 } | 676 } |
| 683 | 677 |
| 684 // GetSubscription methods on both IO and UI threads, merged in order of use | 678 // GetSubscription methods on both IO and UI threads, merged in order of use |
| 685 // from PushMessagingMessageFilter and Core. | 679 // from PushMessagingManager and Core. |
| 686 // ----------------------------------------------------------------------------- | 680 // ----------------------------------------------------------------------------- |
| 687 | 681 |
| 688 void PushMessagingMessageFilter::OnGetSubscription( | 682 void PushMessagingManager::GetSubscription( |
| 689 int request_id, | 683 int64_t service_worker_registration_id, |
| 690 int64_t service_worker_registration_id) { | 684 const GetSubscriptionCallback& callback) { |
| 691 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 685 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 692 // TODO(johnme): Validate arguments? | 686 // TODO(johnme): Validate arguments? |
| 693 service_worker_context_->GetRegistrationUserData( | 687 service_worker_context_->GetRegistrationUserData( |
| 694 service_worker_registration_id, | 688 service_worker_registration_id, |
| 695 {kPushRegistrationIdServiceWorkerKey, kPushSenderIdServiceWorkerKey}, | 689 {kPushRegistrationIdServiceWorkerKey, kPushSenderIdServiceWorkerKey}, |
| 696 base::Bind(&PushMessagingMessageFilter::DidGetSubscription, | 690 base::Bind(&PushMessagingManager::DidGetSubscription, |
| 697 weak_factory_io_to_io_.GetWeakPtr(), request_id, | 691 weak_factory_io_to_io_.GetWeakPtr(), callback, |
| 698 service_worker_registration_id)); | 692 service_worker_registration_id)); |
| 699 } | 693 } |
| 700 | 694 |
| 701 void PushMessagingMessageFilter::DidGetSubscription( | 695 void PushMessagingManager::DidGetSubscription( |
| 702 int request_id, | 696 const GetSubscriptionCallback& callback, |
| 703 int64_t service_worker_registration_id, | 697 int64_t service_worker_registration_id, |
| 704 const std::vector<std::string>& push_subscription_id_and_sender_info, | 698 const std::vector<std::string>& push_subscription_id_and_sender_info, |
| 705 ServiceWorkerStatusCode service_worker_status) { | 699 ServiceWorkerStatusCode service_worker_status) { |
| 706 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 700 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 707 PushGetRegistrationStatus get_status = | 701 PushGetRegistrationStatus get_status = |
| 708 PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; | 702 PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; |
| 709 switch (service_worker_status) { | 703 switch (service_worker_status) { |
| 710 case SERVICE_WORKER_OK: { | 704 case SERVICE_WORKER_OK: { |
| 711 DCHECK_EQ(2u, push_subscription_id_and_sender_info.size()); | 705 DCHECK_EQ(2u, push_subscription_id_and_sender_info.size()); |
| 712 | 706 |
| 713 if (!service_available_) { | 707 if (!service_available_) { |
| 714 // Return not found in incognito mode, so websites can't detect it. | 708 // Return not found in incognito mode, so websites can't detect it. |
| 715 get_status = | 709 get_status = |
| 716 ui_core_->is_incognito() | 710 ui_core_->is_incognito() |
| 717 ? PUSH_GETREGISTRATION_STATUS_INCOGNITO_REGISTRATION_NOT_FOUND | 711 ? PUSH_GETREGISTRATION_STATUS_INCOGNITO_REGISTRATION_NOT_FOUND |
| 718 : PUSH_GETREGISTRATION_STATUS_SERVICE_NOT_AVAILABLE; | 712 : PUSH_GETREGISTRATION_STATUS_SERVICE_NOT_AVAILABLE; |
| 719 break; | 713 break; |
| 720 } | 714 } |
| 721 | 715 |
| 722 ServiceWorkerRegistration* registration = | 716 ServiceWorkerRegistration* registration = |
| 723 service_worker_context_->GetLiveRegistration( | 717 service_worker_context_->GetLiveRegistration( |
| 724 service_worker_registration_id); | 718 service_worker_registration_id); |
| 725 const GURL origin = registration->pattern().GetOrigin(); | 719 const GURL origin = registration->pattern().GetOrigin(); |
| 726 | 720 |
| 727 const bool uses_standard_protocol = | 721 const bool uses_standard_protocol = |
| 728 IsApplicationServerKey(push_subscription_id_and_sender_info[1]); | 722 IsApplicationServerKey(push_subscription_id_and_sender_info[1]); |
| 729 const GURL endpoint = CreateEndpoint( | 723 const GURL endpoint = CreateEndpoint( |
| 730 uses_standard_protocol, push_subscription_id_and_sender_info[0]); | 724 uses_standard_protocol, push_subscription_id_and_sender_info[0]); |
| 731 | 725 |
| 732 auto callback = | 726 auto callback_ui = |
| 733 base::Bind(&PushMessagingMessageFilter::DidGetSubscriptionKeys, | 727 base::Bind(&PushMessagingManager::DidGetSubscriptionKeys, |
| 734 weak_factory_io_to_io_.GetWeakPtr(), request_id, endpoint, | 728 weak_factory_io_to_io_.GetWeakPtr(), callback, endpoint, |
| 735 push_subscription_id_and_sender_info[1]); | 729 push_subscription_id_and_sender_info[1]); |
| 736 | 730 |
| 737 BrowserThread::PostTask( | 731 BrowserThread::PostTask( |
| 738 BrowserThread::UI, FROM_HERE, | 732 BrowserThread::UI, FROM_HERE, |
| 739 base::Bind(&Core::GetEncryptionInfoOnUI, | 733 base::Bind(&Core::GetEncryptionInfoOnUI, |
| 740 base::Unretained(ui_core_.get()), origin, | 734 base::Unretained(ui_core_.get()), origin, |
| 741 service_worker_registration_id, | 735 service_worker_registration_id, |
| 742 push_subscription_id_and_sender_info[1], callback)); | 736 push_subscription_id_and_sender_info[1], callback_ui)); |
| 743 | 737 |
| 744 return; | 738 return; |
| 745 } | 739 } |
| 746 case SERVICE_WORKER_ERROR_NOT_FOUND: { | 740 case SERVICE_WORKER_ERROR_NOT_FOUND: { |
| 747 get_status = PUSH_GETREGISTRATION_STATUS_REGISTRATION_NOT_FOUND; | 741 get_status = PUSH_GETREGISTRATION_STATUS_REGISTRATION_NOT_FOUND; |
| 748 break; | 742 break; |
| 749 } | 743 } |
| 750 case SERVICE_WORKER_ERROR_FAILED: { | 744 case SERVICE_WORKER_ERROR_FAILED: { |
| 751 get_status = PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; | 745 get_status = PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; |
| 752 break; | 746 break; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 767 case SERVICE_WORKER_ERROR_DISK_CACHE: | 761 case SERVICE_WORKER_ERROR_DISK_CACHE: |
| 768 case SERVICE_WORKER_ERROR_REDUNDANT: | 762 case SERVICE_WORKER_ERROR_REDUNDANT: |
| 769 case SERVICE_WORKER_ERROR_DISALLOWED: | 763 case SERVICE_WORKER_ERROR_DISALLOWED: |
| 770 case SERVICE_WORKER_ERROR_MAX_VALUE: { | 764 case SERVICE_WORKER_ERROR_MAX_VALUE: { |
| 771 NOTREACHED() << "Got unexpected error code: " << service_worker_status | 765 NOTREACHED() << "Got unexpected error code: " << service_worker_status |
| 772 << " " << ServiceWorkerStatusToString(service_worker_status); | 766 << " " << ServiceWorkerStatusToString(service_worker_status); |
| 773 get_status = PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; | 767 get_status = PUSH_GETREGISTRATION_STATUS_STORAGE_ERROR; |
| 774 break; | 768 break; |
| 775 } | 769 } |
| 776 } | 770 } |
| 777 Send(new PushMessagingMsg_GetSubscriptionError(request_id, get_status)); | 771 callback.Run(get_status, base::nullopt /* endpoint */, |
| 772 base::nullopt /* options */, base::nullopt /* p256dh */, |
| 773 base::nullopt /* auth */); |
| 778 RecordGetRegistrationStatus(get_status); | 774 RecordGetRegistrationStatus(get_status); |
| 779 } | 775 } |
| 780 | 776 |
| 781 void PushMessagingMessageFilter::DidGetSubscriptionKeys( | 777 void PushMessagingManager::DidGetSubscriptionKeys( |
| 782 int request_id, | 778 const GetSubscriptionCallback& callback, |
| 783 const GURL& endpoint, | 779 const GURL& endpoint, |
| 784 const std::string& sender_info, | 780 const std::string& sender_info, |
| 785 bool success, | 781 bool success, |
| 786 const std::vector<uint8_t>& p256dh, | 782 const std::vector<uint8_t>& p256dh, |
| 787 const std::vector<uint8_t>& auth) { | 783 const std::vector<uint8_t>& auth) { |
| 788 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 784 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 789 if (!success) { | 785 if (!success) { |
| 790 PushGetRegistrationStatus status = | 786 PushGetRegistrationStatus status = |
| 791 PUSH_GETREGISTRATION_STATUS_PUBLIC_KEY_UNAVAILABLE; | 787 PUSH_GETREGISTRATION_STATUS_PUBLIC_KEY_UNAVAILABLE; |
| 792 | 788 |
| 793 Send(new PushMessagingMsg_GetSubscriptionError(request_id, status)); | 789 callback.Run(status, base::nullopt /* endpoint */, |
| 790 base::nullopt /* options */, base::nullopt /* p256dh */, |
| 791 base::nullopt /* auth */); |
| 794 | 792 |
| 795 RecordGetRegistrationStatus(status); | 793 RecordGetRegistrationStatus(status); |
| 796 return; | 794 return; |
| 797 } | 795 } |
| 798 | 796 |
| 799 PushSubscriptionOptions options; | 797 PushSubscriptionOptions options; |
| 800 // Chrome rejects subscription requests with userVisibleOnly false, so it must | 798 // Chrome rejects subscription requests with userVisibleOnly false, so it must |
| 801 // have been true. TODO(harkness): If Chrome starts accepting silent push | 799 // have been true. TODO(harkness): If Chrome starts accepting silent push |
| 802 // subscriptions with userVisibleOnly false, the bool will need to be stored. | 800 // subscriptions with userVisibleOnly false, the bool will need to be stored. |
| 803 options.user_visible_only = true; | 801 options.user_visible_only = true; |
| 804 options.sender_info = sender_info; | 802 options.sender_info = sender_info; |
| 805 | 803 |
| 806 Send(new PushMessagingMsg_GetSubscriptionSuccess(request_id, endpoint, | 804 callback.Run(PUSH_GETREGISTRATION_STATUS_SUCCESS, endpoint, options, p256dh, |
| 807 options, p256dh, auth)); | 805 auth); |
| 808 | 806 |
| 809 RecordGetRegistrationStatus(PUSH_GETREGISTRATION_STATUS_SUCCESS); | 807 RecordGetRegistrationStatus(PUSH_GETREGISTRATION_STATUS_SUCCESS); |
| 810 } | 808 } |
| 811 | 809 |
| 812 // GetPermission methods on both IO and UI threads, merged in order of use from | 810 // GetPermission methods on both IO and UI threads, merged in order of use from |
| 813 // PushMessagingMessageFilter and Core. | 811 // PushMessagingManager and Core. |
| 814 // ----------------------------------------------------------------------------- | 812 // ----------------------------------------------------------------------------- |
| 815 | 813 |
| 816 void PushMessagingMessageFilter::OnGetPermissionStatus( | 814 void PushMessagingManager::GetPermissionStatus( |
| 817 int request_id, | |
| 818 int64_t service_worker_registration_id, | 815 int64_t service_worker_registration_id, |
| 819 bool user_visible) { | 816 bool user_visible, |
| 817 const GetPermissionStatusCallback& callback) { |
| 820 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 818 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 821 ServiceWorkerRegistration* service_worker_registration = | 819 ServiceWorkerRegistration* service_worker_registration = |
| 822 service_worker_context_->GetLiveRegistration( | 820 service_worker_context_->GetLiveRegistration( |
| 823 service_worker_registration_id); | 821 service_worker_registration_id); |
| 824 if (!service_worker_registration) { | 822 if (!service_worker_registration) { |
| 825 Send(new PushMessagingMsg_GetPermissionStatusError( | 823 callback.Run(false /* error */, blink::WebPushPermissionStatusDenied, |
| 826 request_id, blink::WebPushError::ErrorTypeAbort)); | 824 blink::WebPushError::ErrorTypeAbort); |
| 827 return; | 825 return; |
| 828 } | 826 } |
| 829 | 827 |
| 830 BrowserThread::PostTask( | 828 BrowserThread::PostTask( |
| 831 BrowserThread::UI, FROM_HERE, | 829 BrowserThread::UI, FROM_HERE, |
| 832 base::Bind(&Core::GetPermissionStatusOnUI, | 830 base::Bind(&Core::GetPermissionStatusOnUI, |
| 833 base::Unretained(ui_core_.get()), | 831 base::Unretained(ui_core_.get()), callback, |
| 834 service_worker_registration->pattern().GetOrigin(), | 832 service_worker_registration->pattern().GetOrigin(), |
| 835 user_visible, request_id)); | 833 user_visible)); |
| 836 } | 834 } |
| 837 | 835 |
| 838 void PushMessagingMessageFilter::Core::GetPermissionStatusOnUI( | 836 void PushMessagingManager::Core::GetPermissionStatusOnUI( |
| 839 const GURL& requesting_origin, bool user_visible, int request_id) { | 837 const GetPermissionStatusCallback& callback, |
| 838 const GURL& requesting_origin, |
| 839 bool user_visible) { |
| 840 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 840 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 841 blink::WebPushPermissionStatus permission_status; | 841 blink::WebPushPermissionStatus permission_status; |
| 842 PushMessagingService* push_service = service(); | 842 PushMessagingService* push_service = service(); |
| 843 if (push_service) { | 843 if (push_service) { |
| 844 if (!user_visible && !push_service->SupportNonVisibleMessages()) { | 844 if (!user_visible && !push_service->SupportNonVisibleMessages()) { |
| 845 Send(new PushMessagingMsg_GetPermissionStatusError( | 845 BrowserThread::PostTask( |
| 846 request_id, blink::WebPushError::ErrorTypeNotSupported)); | 846 BrowserThread::IO, FROM_HERE, |
| 847 base::Bind(callback, false /* error */, |
| 848 blink::WebPushPermissionStatusDenied, |
| 849 blink::WebPushError::ErrorTypeNotSupported)); |
| 847 return; | 850 return; |
| 848 } | 851 } |
| 849 permission_status = | 852 permission_status = |
| 850 push_service->GetPermissionStatus(requesting_origin, user_visible); | 853 push_service->GetPermissionStatus(requesting_origin, user_visible); |
| 851 } else if (is_incognito()) { | 854 } else if (is_incognito()) { |
| 852 // Return prompt, so the website can't detect incognito mode. | 855 // Return prompt, so the website can't detect incognito mode. |
| 853 permission_status = blink::WebPushPermissionStatusPrompt; | 856 permission_status = blink::WebPushPermissionStatusPrompt; |
| 854 } else { | 857 } else { |
| 855 Send(new PushMessagingMsg_GetPermissionStatusError( | 858 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 856 request_id, blink::WebPushError::ErrorTypeAbort)); | 859 base::Bind(callback, false /* error */, |
| 860 blink::WebPushPermissionStatusDenied, |
| 861 blink::WebPushError::ErrorTypeAbort)); |
| 857 return; | 862 return; |
| 858 } | 863 } |
| 859 Send(new PushMessagingMsg_GetPermissionStatusSuccess(request_id, | 864 BrowserThread::PostTask( |
| 860 permission_status)); | 865 BrowserThread::IO, FROM_HERE, |
| 866 base::Bind(callback, true /* success */, permission_status, |
| 867 blink::WebPushError::ErrorTypeUnknown)); |
| 861 } | 868 } |
| 862 | 869 |
| 863 // Helper methods on both IO and UI threads, merged from | 870 // Helper methods on both IO and UI threads, merged from |
| 864 // PushMessagingMessageFilter and Core. | 871 // PushMessagingManager and Core. |
| 865 // ----------------------------------------------------------------------------- | 872 // ----------------------------------------------------------------------------- |
| 866 | 873 |
| 867 void PushMessagingMessageFilter::Core::GetEncryptionInfoOnUI( | 874 void PushMessagingManager::Core::GetEncryptionInfoOnUI( |
| 868 const GURL& origin, | 875 const GURL& origin, |
| 869 int64_t service_worker_registration_id, | 876 int64_t service_worker_registration_id, |
| 870 const std::string& sender_id, | 877 const std::string& sender_id, |
| 871 const PushMessagingService::EncryptionInfoCallback& io_thread_callback) { | 878 const PushMessagingService::EncryptionInfoCallback& io_thread_callback) { |
| 872 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 879 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 873 PushMessagingService* push_service = service(); | 880 PushMessagingService* push_service = service(); |
| 874 if (push_service) { | 881 if (push_service) { |
| 875 push_service->GetEncryptionInfo( | 882 push_service->GetEncryptionInfo( |
| 876 origin, service_worker_registration_id, sender_id, | 883 origin, service_worker_registration_id, sender_id, |
| 877 base::Bind(&ForwardEncryptionInfoToIOThreadProxy, io_thread_callback)); | 884 base::Bind(&ForwardEncryptionInfoToIOThreadProxy, io_thread_callback)); |
| 878 return; | 885 return; |
| 879 } | 886 } |
| 880 | 887 |
| 881 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 888 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 882 base::Bind(io_thread_callback, false /* success */, | 889 base::Bind(io_thread_callback, false /* success */, |
| 883 std::vector<uint8_t>() /* p256dh */, | 890 std::vector<uint8_t>() /* p256dh */, |
| 884 std::vector<uint8_t>() /* auth */)); | 891 std::vector<uint8_t>() /* auth */)); |
| 885 } | 892 } |
| 886 | 893 |
| 887 void PushMessagingMessageFilter::Core::Send(IPC::Message* message) { | 894 GURL PushMessagingManager::CreateEndpoint( |
| 888 BrowserThread::PostTask( | |
| 889 BrowserThread::IO, FROM_HERE, | |
| 890 base::Bind(&PushMessagingMessageFilter::SendIPC, io_parent_, | |
| 891 base::Passed(base::WrapUnique(message)))); | |
| 892 } | |
| 893 | |
| 894 void PushMessagingMessageFilter::SendIPC( | |
| 895 std::unique_ptr<IPC::Message> message) { | |
| 896 Send(message.release()); | |
| 897 } | |
| 898 | |
| 899 GURL PushMessagingMessageFilter::CreateEndpoint( | |
| 900 bool standard_protocol, | 895 bool standard_protocol, |
| 901 const std::string& subscription_id) const { | 896 const std::string& subscription_id) const { |
| 902 const GURL& base = | 897 const GURL& base = |
| 903 standard_protocol ? web_push_protocol_endpoint_ : default_endpoint_; | 898 standard_protocol ? web_push_protocol_endpoint_ : default_endpoint_; |
| 904 | 899 |
| 905 return GURL(base.spec() + subscription_id); | 900 return GURL(base.spec() + subscription_id); |
| 906 } | 901 } |
| 907 | 902 |
| 908 PushMessagingService* PushMessagingMessageFilter::Core::service() { | 903 PushMessagingService* PushMessagingManager::Core::service() { |
| 909 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 904 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 910 RenderProcessHost* process_host = | 905 RenderProcessHost* process_host = |
| 911 RenderProcessHost::FromID(render_process_id_); | 906 RenderProcessHost::FromID(render_process_id_); |
| 912 return process_host | 907 return process_host |
| 913 ? process_host->GetBrowserContext()->GetPushMessagingService() | 908 ? process_host->GetBrowserContext()->GetPushMessagingService() |
| 914 : nullptr; | 909 : nullptr; |
| 915 } | 910 } |
| 916 | 911 |
| 917 } // namespace content | 912 } // namespace content |
| OLD | NEW |