| 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/renderer/push_messaging/push_messaging_client.h" | 5 #include "content/renderer/push_messaging/push_messaging_client.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 PushMessagingClient::~PushMessagingClient() {} | 40 PushMessagingClient::~PushMessagingClient() {} |
| 41 | 41 |
| 42 void PushMessagingClient::OnDestruct() { | 42 void PushMessagingClient::OnDestruct() { |
| 43 delete this; | 43 delete this; |
| 44 } | 44 } |
| 45 | 45 |
| 46 void PushMessagingClient::Subscribe( | 46 void PushMessagingClient::Subscribe( |
| 47 blink::WebServiceWorkerRegistration* service_worker_registration, | 47 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 48 const blink::WebPushSubscriptionOptions& options, | 48 const blink::WebPushSubscriptionOptions& options, |
| 49 bool user_gesture, |
| 49 std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks) { | 50 std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks) { |
| 50 DCHECK(service_worker_registration); | 51 DCHECK(service_worker_registration); |
| 51 DCHECK(callbacks); | 52 DCHECK(callbacks); |
| 52 | 53 |
| 53 // If a developer provided an application server key in |options|, skip | 54 // If a developer provided an application server key in |options|, skip |
| 54 // fetching the manifest. | 55 // fetching the manifest. |
| 55 if (options.application_server_key.IsEmpty()) { | 56 if (options.application_server_key.IsEmpty()) { |
| 56 RenderFrameImpl::FromRoutingID(routing_id()) | 57 RenderFrameImpl::FromRoutingID(routing_id()) |
| 57 ->manifest_manager() | 58 ->manifest_manager() |
| 58 ->GetManifest(base::Bind( | 59 ->GetManifest(base::Bind(&PushMessagingClient::DidGetManifest, |
| 59 &PushMessagingClient::DidGetManifest, base::Unretained(this), | 60 base::Unretained(this), |
| 60 service_worker_registration, options, base::Passed(&callbacks))); | 61 service_worker_registration, options, |
| 62 user_gesture, base::Passed(&callbacks))); |
| 61 } else { | 63 } else { |
| 62 PushSubscriptionOptions content_options; | 64 PushSubscriptionOptions content_options; |
| 63 content_options.user_visible_only = options.user_visible_only; | 65 content_options.user_visible_only = options.user_visible_only; |
| 64 // Just treat the server key as a string of bytes and pass it to the push | 66 // Just treat the server key as a string of bytes and pass it to the push |
| 65 // service. | 67 // service. |
| 66 content_options.sender_info = options.application_server_key.Latin1(); | 68 content_options.sender_info = options.application_server_key.Latin1(); |
| 67 DoSubscribe(service_worker_registration, content_options, | 69 DoSubscribe(service_worker_registration, content_options, user_gesture, |
| 68 std::move(callbacks)); | 70 std::move(callbacks)); |
| 69 } | 71 } |
| 70 } | 72 } |
| 71 | 73 |
| 72 void PushMessagingClient::DidGetManifest( | 74 void PushMessagingClient::DidGetManifest( |
| 73 blink::WebServiceWorkerRegistration* service_worker_registration, | 75 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 74 const blink::WebPushSubscriptionOptions& options, | 76 const blink::WebPushSubscriptionOptions& options, |
| 77 bool user_gesture, |
| 75 std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks, | 78 std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks, |
| 76 const GURL& manifest_url, | 79 const GURL& manifest_url, |
| 77 const Manifest& manifest, | 80 const Manifest& manifest, |
| 78 const ManifestDebugInfo&) { | 81 const ManifestDebugInfo&) { |
| 79 // Get the sender_info from the manifest since it wasn't provided by | 82 // Get the sender_info from the manifest since it wasn't provided by |
| 80 // the caller. | 83 // the caller. |
| 81 if (manifest.IsEmpty()) { | 84 if (manifest.IsEmpty()) { |
| 82 DidSubscribe(std::move(callbacks), | 85 DidSubscribe(std::move(callbacks), |
| 83 PUSH_REGISTRATION_STATUS_MANIFEST_EMPTY_OR_MISSING, | 86 PUSH_REGISTRATION_STATUS_MANIFEST_EMPTY_OR_MISSING, |
| 84 base::nullopt, base::nullopt, base::nullopt, base::nullopt); | 87 base::nullopt, base::nullopt, base::nullopt, base::nullopt); |
| 85 return; | 88 return; |
| 86 } | 89 } |
| 87 | 90 |
| 88 PushSubscriptionOptions content_options; | 91 PushSubscriptionOptions content_options; |
| 89 content_options.user_visible_only = options.user_visible_only; | 92 content_options.user_visible_only = options.user_visible_only; |
| 90 if (!manifest.gcm_sender_id.is_null()) { | 93 if (!manifest.gcm_sender_id.is_null()) { |
| 91 content_options.sender_info = | 94 content_options.sender_info = |
| 92 base::UTF16ToUTF8(manifest.gcm_sender_id.string()); | 95 base::UTF16ToUTF8(manifest.gcm_sender_id.string()); |
| 93 } | 96 } |
| 94 | 97 |
| 95 DoSubscribe(service_worker_registration, content_options, | 98 DoSubscribe(service_worker_registration, content_options, user_gesture, |
| 96 std::move(callbacks)); | 99 std::move(callbacks)); |
| 97 } | 100 } |
| 98 | 101 |
| 99 void PushMessagingClient::DoSubscribe( | 102 void PushMessagingClient::DoSubscribe( |
| 100 blink::WebServiceWorkerRegistration* service_worker_registration, | 103 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 101 const PushSubscriptionOptions& options, | 104 const PushSubscriptionOptions& options, |
| 105 bool user_gesture, |
| 102 std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks) { | 106 std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks) { |
| 103 int64_t service_worker_registration_id = | 107 int64_t service_worker_registration_id = |
| 104 static_cast<WebServiceWorkerRegistrationImpl*>( | 108 static_cast<WebServiceWorkerRegistrationImpl*>( |
| 105 service_worker_registration) | 109 service_worker_registration) |
| 106 ->RegistrationId(); | 110 ->RegistrationId(); |
| 107 | 111 |
| 108 if (options.sender_info.empty()) { | 112 if (options.sender_info.empty()) { |
| 109 DidSubscribe(std::move(callbacks), PUSH_REGISTRATION_STATUS_NO_SENDER_ID, | 113 DidSubscribe(std::move(callbacks), PUSH_REGISTRATION_STATUS_NO_SENDER_ID, |
| 110 base::nullopt, base::nullopt, base::nullopt, base::nullopt); | 114 base::nullopt, base::nullopt, base::nullopt, base::nullopt); |
| 111 return; | 115 return; |
| 112 } | 116 } |
| 113 | 117 |
| 114 DCHECK(push_messaging_manager_); | 118 DCHECK(push_messaging_manager_); |
| 115 push_messaging_manager_->Subscribe( | 119 push_messaging_manager_->Subscribe( |
| 116 routing_id(), service_worker_registration_id, options, | 120 routing_id(), service_worker_registration_id, options, user_gesture, |
| 117 // Safe to use base::Unretained because |push_messaging_manager_ |is | 121 // Safe to use base::Unretained because |push_messaging_manager_ |is |
| 118 // owned by |this|. | 122 // owned by |this|. |
| 119 base::Bind(&PushMessagingClient::DidSubscribe, base::Unretained(this), | 123 base::Bind(&PushMessagingClient::DidSubscribe, base::Unretained(this), |
| 120 base::Passed(&callbacks))); | 124 base::Passed(&callbacks))); |
| 121 } | 125 } |
| 122 | 126 |
| 123 void PushMessagingClient::DidSubscribe( | 127 void PushMessagingClient::DidSubscribe( |
| 124 std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks, | 128 std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks, |
| 125 content::PushRegistrationStatus status, | 129 content::PushRegistrationStatus status, |
| 126 const base::Optional<GURL>& endpoint, | 130 const base::Optional<GURL>& endpoint, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 139 callbacks->OnSuccess(base::MakeUnique<blink::WebPushSubscription>( | 143 callbacks->OnSuccess(base::MakeUnique<blink::WebPushSubscription>( |
| 140 endpoint.value(), options.value().user_visible_only, | 144 endpoint.value(), options.value().user_visible_only, |
| 141 blink::WebString::FromLatin1(options.value().sender_info), | 145 blink::WebString::FromLatin1(options.value().sender_info), |
| 142 p256dh.value(), auth.value())); | 146 p256dh.value(), auth.value())); |
| 143 } else { | 147 } else { |
| 144 callbacks->OnError(PushRegistrationStatusToWebPushError(status)); | 148 callbacks->OnError(PushRegistrationStatusToWebPushError(status)); |
| 145 } | 149 } |
| 146 } | 150 } |
| 147 | 151 |
| 148 } // namespace content | 152 } // namespace content |
| OLD | NEW |