OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/notifications/notification_platform_bridge_mac.h" | 5 #include "chrome/browser/notifications/notification_platform_bridge_mac.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
10 #include "base/mac/mac_util.h" | 10 #include "base/mac/mac_util.h" |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 objectForKey:notification_constants::kNotificationId])); | 177 objectForKey:notification_constants::kNotificationId])); |
178 } | 178 } |
179 } | 179 } |
180 return true; | 180 return true; |
181 } | 181 } |
182 | 182 |
183 bool NotificationPlatformBridgeMac::SupportsNotificationCenter() const { | 183 bool NotificationPlatformBridgeMac::SupportsNotificationCenter() const { |
184 return true; | 184 return true; |
185 } | 185 } |
186 | 186 |
| 187 // static |
| 188 bool NotificationPlatformBridgeMac::VerifyNotificationData( |
| 189 NSDictionary* response) { |
| 190 if (![response |
| 191 objectForKey:notification_constants::kNotificationButtonIndex] || |
| 192 ![response objectForKey:notification_constants::kNotificationOperation] || |
| 193 ![response objectForKey:notification_constants::kNotificationId] || |
| 194 ![response objectForKey:notification_constants::kNotificationProfileId] || |
| 195 ![response objectForKey:notification_constants::kNotificationIncognito]) { |
| 196 LOG(ERROR) << "Missing required key"; |
| 197 return false; |
| 198 } |
| 199 |
| 200 NSNumber* button_index = |
| 201 [response objectForKey:notification_constants::kNotificationButtonIndex]; |
| 202 NSNumber* operation = |
| 203 [response objectForKey:notification_constants::kNotificationOperation]; |
| 204 NSString* notification_id = |
| 205 [response objectForKey:notification_constants::kNotificationId]; |
| 206 NSString* profile_id = |
| 207 [response objectForKey:notification_constants::kNotificationProfileId]; |
| 208 |
| 209 if (button_index.intValue < -1 || |
| 210 button_index.intValue >= |
| 211 static_cast<int>(blink::kWebNotificationMaxActions)) { |
| 212 LOG(ERROR) << "Invalid number of buttons supplied " |
| 213 << button_index.intValue; |
| 214 return false; |
| 215 } |
| 216 |
| 217 if (operation.unsignedIntValue > NotificationCommon::OPERATION_MAX) { |
| 218 LOG(ERROR) << operation.unsignedIntValue |
| 219 << " does not correspond to a valid operation."; |
| 220 return false; |
| 221 } |
| 222 |
| 223 if (notification_id.length <= 0) { |
| 224 LOG(ERROR) << "Notification Id is empty"; |
| 225 return false; |
| 226 } |
| 227 |
| 228 if (profile_id.length <= 0) { |
| 229 LOG(ERROR) << "Profile Id is empty"; |
| 230 return false; |
| 231 } |
| 232 |
| 233 // Origin is not actually required but if it's there it should be a valid one. |
| 234 NSString* origin = |
| 235 [response objectForKey:notification_constants::kNotificationOrigin]; |
| 236 if (origin) { |
| 237 std::string notificationOrigin = base::SysNSStringToUTF8(origin); |
| 238 GURL url(notificationOrigin); |
| 239 if (!url.is_valid()) |
| 240 return false; |
| 241 } |
| 242 |
| 243 return true; |
| 244 } |
| 245 |
187 // ///////////////////////////////////////////////////////////////////////////// | 246 // ///////////////////////////////////////////////////////////////////////////// |
188 | 247 |
189 @implementation NotificationCenterDelegate | 248 @implementation NotificationCenterDelegate |
190 - (void)userNotificationCenter:(NSUserNotificationCenter*)center | 249 - (void)userNotificationCenter:(NSUserNotificationCenter*)center |
191 didActivateNotification:(NSUserNotification*)notification { | 250 didActivateNotification:(NSUserNotification*)notification { |
192 NSDictionary* response = | 251 NSDictionary* response = |
193 [NotificationResponseBuilder buildDictionary:notification]; | 252 [NotificationResponseBuilder buildDictionary:notification]; |
| 253 if (!NotificationPlatformBridgeMac::VerifyNotificationData(response)) |
| 254 return; |
194 | 255 |
195 NSNumber* buttonIndex = | 256 NSNumber* buttonIndex = |
196 [response objectForKey:notification_constants::kNotificationButtonIndex]; | 257 [response objectForKey:notification_constants::kNotificationButtonIndex]; |
197 NSNumber* operation = | 258 NSNumber* operation = |
198 [response objectForKey:notification_constants::kNotificationOperation]; | 259 [response objectForKey:notification_constants::kNotificationOperation]; |
199 | 260 |
200 std::string notificationOrigin = base::SysNSStringToUTF8( | 261 std::string notificationOrigin = base::SysNSStringToUTF8( |
201 [response objectForKey:notification_constants::kNotificationOrigin]); | 262 [response objectForKey:notification_constants::kNotificationOrigin]); |
202 NSString* notificationId = [notification.userInfo | 263 NSString* notificationId = |
203 objectForKey:notification_constants::kNotificationId]; | 264 [response objectForKey:notification_constants::kNotificationId]; |
204 std::string persistentNotificationId = | 265 std::string persistentNotificationId = |
205 base::SysNSStringToUTF8(notificationId); | 266 base::SysNSStringToUTF8(notificationId); |
206 int64_t persistentId; | 267 int64_t persistentId; |
207 if (!base::StringToInt64(persistentNotificationId, &persistentId)) { | 268 if (!base::StringToInt64(persistentNotificationId, &persistentId)) { |
208 LOG(ERROR) << "Unable to convert notification ID: " | 269 LOG(ERROR) << "Unable to convert notification ID: " |
209 << persistentNotificationId << " to integer."; | 270 << persistentNotificationId << " to integer."; |
210 return; | 271 return; |
211 } | 272 } |
212 std::string profileId = base::SysNSStringToUTF8( | 273 std::string profileId = base::SysNSStringToUTF8( |
213 [response objectForKey:notification_constants::kNotificationProfileId]); | 274 [response objectForKey:notification_constants::kNotificationProfileId]); |
214 NSNumber* isIncognito = | 275 NSNumber* isIncognito = |
215 [response objectForKey:notification_constants::kNotificationIncognito]; | 276 [response objectForKey:notification_constants::kNotificationIncognito]; |
216 | 277 |
217 GURL origin(notificationOrigin); | 278 GURL origin(notificationOrigin); |
218 | 279 |
219 PlatformNotificationServiceImpl::GetInstance() | 280 PlatformNotificationServiceImpl::GetInstance() |
220 ->ProcessPersistentNotificationOperation( | 281 ->ProcessPersistentNotificationOperation( |
221 static_cast<NotificationCommon::Operation>(operation.intValue), | 282 static_cast<NotificationCommon::Operation>(operation.intValue), |
222 profileId, [isIncognito boolValue], origin, persistentId, | 283 profileId, [isIncognito boolValue], origin, persistentId, |
223 buttonIndex.intValue); | 284 buttonIndex.intValue); |
224 } | 285 } |
225 | 286 |
226 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center | 287 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center |
227 shouldPresentNotification:(NSUserNotification*)nsNotification { | 288 shouldPresentNotification:(NSUserNotification*)nsNotification { |
228 // Always display notifications, regardless of whether the app is foreground. | 289 // Always display notifications, regardless of whether the app is foreground. |
229 return YES; | 290 return YES; |
230 } | 291 } |
231 | 292 |
232 @end | 293 @end |
OLD | NEW |