OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/services/gcm/push_messaging_service_impl.h" | 5 #include "chrome/browser/services/gcm/push_messaging_service_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
26 #include "chrome/grit/generated_resources.h" | 26 #include "chrome/grit/generated_resources.h" |
27 #include "components/content_settings/core/common/permission_request_id.h" | 27 #include "components/content_settings/core/common/permission_request_id.h" |
28 #include "components/gcm_driver/gcm_driver.h" | 28 #include "components/gcm_driver/gcm_driver.h" |
29 #include "components/pref_registry/pref_registry_syncable.h" | 29 #include "components/pref_registry/pref_registry_syncable.h" |
30 #include "content/public/browser/browser_context.h" | 30 #include "content/public/browser/browser_context.h" |
31 #include "content/public/browser/render_frame_host.h" | 31 #include "content/public/browser/render_frame_host.h" |
32 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
33 #include "content/public/common/child_process_host.h" | 33 #include "content/public/common/child_process_host.h" |
| 34 #include "content/public/common/content_switches.h" |
34 #include "content/public/common/platform_notification_data.h" | 35 #include "content/public/common/platform_notification_data.h" |
35 #include "third_party/skia/include/core/SkBitmap.h" | 36 #include "third_party/skia/include/core/SkBitmap.h" |
36 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
37 | 38 |
38 namespace gcm { | 39 namespace gcm { |
39 | 40 |
40 namespace { | 41 namespace { |
41 const int kMaxRegistrations = 1000000; | 42 const int kMaxRegistrations = 1000000; |
42 | 43 |
43 blink::WebPushPermissionStatus ToPushPermission(ContentSetting setting) { | 44 blink::WebPushPermissionStatus ToPushPermission(ContentSetting setting) { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 | 142 |
142 void PushMessagingServiceImpl::ShutdownHandler() { | 143 void PushMessagingServiceImpl::ShutdownHandler() { |
143 // TODO(johnme): Do any necessary cleanup. | 144 // TODO(johnme): Do any necessary cleanup. |
144 } | 145 } |
145 | 146 |
146 void PushMessagingServiceImpl::OnMessage( | 147 void PushMessagingServiceImpl::OnMessage( |
147 const std::string& app_id, | 148 const std::string& app_id, |
148 const GCMClient::IncomingMessage& message) { | 149 const GCMClient::IncomingMessage& message) { |
149 // The Push API only exposes a single string of data in the push event fired | 150 // The Push API only exposes a single string of data in the push event fired |
150 // on the Service Worker. When developers send messages using GCM to the Push | 151 // on the Service Worker. When developers send messages using GCM to the Push |
151 // API, they must pass a single key-value pair, where the key is "data" and | 152 // API and want to include a message payload, they must pass a single key- |
152 // the value is the string they want to be passed to their Service Worker. | 153 // value pair, where the key is "data" and the value is the string they want |
153 // For example, they could send the following JSON using the HTTPS GCM API: | 154 // to be passed to their Service Worker. For example, they could send the |
| 155 // following JSON using the HTTPS GCM API: |
154 // { | 156 // { |
155 // "registration_ids": ["FOO", "BAR"], | 157 // "registration_ids": ["FOO", "BAR"], |
156 // "data": { | 158 // "data": { |
157 // "data": "BAZ", | 159 // "data": "BAZ", |
158 // }, | 160 // }, |
159 // "delay_while_idle": true, | 161 // "delay_while_idle": true, |
160 // } | 162 // } |
161 // TODO(johnme): Make sure this is clearly documented for developers. | 163 // TODO(johnme): Make sure this is clearly documented for developers. |
162 PushMessagingApplicationId application_id = | 164 PushMessagingApplicationId application_id = |
163 PushMessagingApplicationId::Parse(app_id); | 165 PushMessagingApplicationId::Parse(app_id); |
164 GCMClient::MessageData::const_iterator it = message.data.find("data"); | |
165 if (application_id.IsValid() && it != message.data.end()) { | |
166 if (!HasPermission(application_id.origin)) { | |
167 // The |origin| lost push permission. We need to unregister and drop this | |
168 // message. | |
169 Unregister(application_id, UnregisterCallback()); | |
170 return; | |
171 } | |
172 | 166 |
173 const std::string& data = it->second; | 167 // Drop messages whose application is is invalid. |
174 content::BrowserContext::DeliverPushMessage( | 168 if (!application_id.IsValid()) { |
175 profile_, | |
176 application_id.origin, | |
177 application_id.service_worker_registration_id, | |
178 data, | |
179 base::Bind(&PushMessagingServiceImpl::DeliverMessageCallback, | |
180 weak_factory_.GetWeakPtr(), | |
181 application_id, | |
182 message)); | |
183 } else { | |
184 // Drop the message, as it is invalid. | |
185 DeliverMessageCallback(application_id, message, | 169 DeliverMessageCallback(application_id, message, |
186 content::PUSH_DELIVERY_STATUS_INVALID_MESSAGE); | 170 content::PUSH_DELIVERY_STATUS_INVALID_MESSAGE); |
| 171 return; |
187 } | 172 } |
| 173 |
| 174 // |origin| may have lost push permission. Unregister and drop this message. |
| 175 if (!HasPermission(application_id.origin)) { |
| 176 Unregister(application_id, UnregisterCallback()); |
| 177 return; |
| 178 } |
| 179 |
| 180 std::string data; |
| 181 |
| 182 // TODO(peter): Message payloads are disabled pending mandatory encryption. |
| 183 // https://crbug.com/449184 |
| 184 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 185 switches::kEnablePushMessagePayload)) { |
| 186 GCMClient::MessageData::const_iterator it = message.data.find("data"); |
| 187 if (it != message.data.end()) |
| 188 data = it->second; |
| 189 } |
| 190 |
| 191 content::BrowserContext::DeliverPushMessage( |
| 192 profile_, |
| 193 application_id.origin, |
| 194 application_id.service_worker_registration_id, |
| 195 data, |
| 196 base::Bind(&PushMessagingServiceImpl::DeliverMessageCallback, |
| 197 weak_factory_.GetWeakPtr(), |
| 198 application_id, |
| 199 message)); |
188 } | 200 } |
189 | 201 |
190 void PushMessagingServiceImpl::SetProfileForTesting(Profile* profile) { | 202 void PushMessagingServiceImpl::SetProfileForTesting(Profile* profile) { |
191 profile_ = profile; | 203 profile_ = profile; |
192 } | 204 } |
193 | 205 |
194 void PushMessagingServiceImpl::DeliverMessageCallback( | 206 void PushMessagingServiceImpl::DeliverMessageCallback( |
195 const PushMessagingApplicationId& application_id, | 207 const PushMessagingApplicationId& application_id, |
196 const GCMClient::IncomingMessage& message, | 208 const GCMClient::IncomingMessage& message, |
197 content::PushDeliveryStatus status) { | 209 content::PushDeliveryStatus status) { |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { | 504 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { |
493 gcm::PushMessagingPermissionContext* permission_context = | 505 gcm::PushMessagingPermissionContext* permission_context = |
494 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); | 506 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); |
495 DCHECK(permission_context); | 507 DCHECK(permission_context); |
496 | 508 |
497 return permission_context->GetPermissionStatus(origin, origin) == | 509 return permission_context->GetPermissionStatus(origin, origin) == |
498 CONTENT_SETTING_ALLOW; | 510 CONTENT_SETTING_ALLOW; |
499 } | 511 } |
500 | 512 |
501 } // namespace gcm | 513 } // namespace gcm |
OLD | NEW |