OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/notifications/notifications_api.h" | 5 #include "chrome/browser/extensions/api/notifications/notifications_api.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/guid.h" | 8 #include "base/guid.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 float image_scale = | 250 float image_scale = |
251 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactors().back()); | 251 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactors().back()); |
252 | 252 |
253 // Extract required fields: type, title, message, and icon. | 253 // Extract required fields: type, title, message, and icon. |
254 message_center::NotificationType type = | 254 message_center::NotificationType type = |
255 MapApiTemplateTypeToType(options->type); | 255 MapApiTemplateTypeToType(options->type); |
256 const base::string16 title(base::UTF8ToUTF16(*options->title)); | 256 const base::string16 title(base::UTF8ToUTF16(*options->title)); |
257 const base::string16 message(base::UTF8ToUTF16(*options->message)); | 257 const base::string16 message(base::UTF8ToUTF16(*options->message)); |
258 gfx::Image icon; | 258 gfx::Image icon; |
259 | 259 |
260 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | 260 if (!options->icon_bitmap.get() || |
261 image_scale, | 261 !NotificationConversionHelper::NotificationBitmapToGfxImage( |
262 bitmap_sizes.icon_size, | 262 image_scale, bitmap_sizes.icon_size, *options->icon_bitmap, &icon)) { |
263 options->icon_bitmap.get(), | |
264 &icon)) { | |
265 SetError(kUnableToDecodeIconError); | 263 SetError(kUnableToDecodeIconError); |
266 return false; | 264 return false; |
267 } | 265 } |
268 | 266 |
269 // Then, handle any optional data that's been provided. | 267 // Then, handle any optional data that's been provided. |
270 message_center::RichNotificationData optional_fields; | 268 message_center::RichNotificationData optional_fields; |
271 if (options->app_icon_mask_url.get()) { | 269 if (options->app_icon_mask_url.get()) { |
272 gfx::Image small_icon_mask; | 270 gfx::Image small_icon_mask; |
273 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | 271 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( |
274 image_scale, bitmap_sizes.app_icon_mask_size, | 272 image_scale, bitmap_sizes.app_icon_mask_size, |
275 options->app_icon_mask_bitmap.get(), &small_icon_mask)) { | 273 *options->app_icon_mask_bitmap, &small_icon_mask)) { |
276 SetError(kUnableToDecodeIconError); | 274 SetError(kUnableToDecodeIconError); |
277 return false; | 275 return false; |
278 } | 276 } |
279 optional_fields.small_image = | 277 optional_fields.small_image = |
280 GetMaskedSmallImage(small_icon_mask.AsImageSkia()); | 278 GetMaskedSmallImage(small_icon_mask.AsImageSkia()); |
281 } | 279 } |
282 | 280 |
283 if (options->priority.get()) | 281 if (options->priority.get()) |
284 optional_fields.priority = *options->priority; | 282 optional_fields.priority = *options->priority; |
285 | 283 |
286 if (options->event_time.get()) | 284 if (options->event_time.get()) |
287 optional_fields.timestamp = base::Time::FromJsTime(*options->event_time); | 285 optional_fields.timestamp = base::Time::FromJsTime(*options->event_time); |
288 | 286 |
289 if (options->buttons.get()) { | 287 if (options->buttons.get()) { |
290 // Currently we allow up to 2 buttons. | 288 // Currently we allow up to 2 buttons. |
291 size_t number_of_buttons = options->buttons->size(); | 289 size_t number_of_buttons = options->buttons->size(); |
292 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; | 290 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; |
293 | 291 |
294 for (size_t i = 0; i < number_of_buttons; i++) { | 292 for (size_t i = 0; i < number_of_buttons; i++) { |
295 message_center::ButtonInfo info( | 293 message_center::ButtonInfo info( |
296 base::UTF8ToUTF16((*options->buttons)[i]->title)); | 294 base::UTF8ToUTF16((*options->buttons)[i]->title)); |
297 NotificationConversionHelper::NotificationBitmapToGfxImage( | 295 extensions::api::notifications::NotificationBitmap* icon_bitmap_ptr = |
298 image_scale, | 296 (*options->buttons)[i]->icon_bitmap.get(); |
299 bitmap_sizes.button_icon_size, | 297 if (icon_bitmap_ptr) { |
300 (*options->buttons)[i]->icon_bitmap.get(), | 298 NotificationConversionHelper::NotificationBitmapToGfxImage( |
301 &info.icon); | 299 image_scale, bitmap_sizes.button_icon_size, *icon_bitmap_ptr, |
302 optional_fields.buttons.push_back(info); | 300 &info.icon); |
| 301 optional_fields.buttons.push_back(info); |
| 302 } |
303 } | 303 } |
304 } | 304 } |
305 | 305 |
306 if (options->context_message) { | 306 if (options->context_message) { |
307 optional_fields.context_message = | 307 optional_fields.context_message = |
308 base::UTF8ToUTF16(*options->context_message); | 308 base::UTF8ToUTF16(*options->context_message); |
309 } | 309 } |
310 | 310 |
311 bool has_image = NotificationConversionHelper::NotificationBitmapToGfxImage( | 311 bool has_image = options->image_bitmap.get() && |
312 image_scale, | 312 NotificationConversionHelper::NotificationBitmapToGfxImage( |
313 bitmap_sizes.image_size, | 313 image_scale, bitmap_sizes.image_size, |
314 options->image_bitmap.get(), | 314 *options->image_bitmap, &optional_fields.image); |
315 &optional_fields.image); | 315 |
316 // We should have an image if and only if the type is an image type. | 316 // We should have an image if and only if the type is an image type. |
317 if (has_image != (type == message_center::NOTIFICATION_TYPE_IMAGE)) { | 317 if (has_image != (type == message_center::NOTIFICATION_TYPE_IMAGE)) { |
318 SetError(kExtraImageProvided); | 318 SetError(kExtraImageProvided); |
319 return false; | 319 return false; |
320 } | 320 } |
321 | 321 |
322 // We should have list items if and only if the type is a multiple type. | 322 // We should have list items if and only if the type is a multiple type. |
323 bool has_list_items = options->items.get() && options->items->size() > 0; | 323 bool has_list_items = options->items.get() && options->items->size() > 0; |
324 if (has_list_items != (type == message_center::NOTIFICATION_TYPE_MULTIPLE)) { | 324 if (has_list_items != (type == message_center::NOTIFICATION_TYPE_MULTIPLE)) { |
325 SetError(kExtraListItemsProvided); | 325 SetError(kExtraListItemsProvided); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactors().back()); | 383 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactors().back()); |
384 | 384 |
385 // Update optional fields if provided. | 385 // Update optional fields if provided. |
386 if (options->type != api::notifications::TEMPLATE_TYPE_NONE) | 386 if (options->type != api::notifications::TEMPLATE_TYPE_NONE) |
387 notification->set_type(MapApiTemplateTypeToType(options->type)); | 387 notification->set_type(MapApiTemplateTypeToType(options->type)); |
388 if (options->title) | 388 if (options->title) |
389 notification->set_title(base::UTF8ToUTF16(*options->title)); | 389 notification->set_title(base::UTF8ToUTF16(*options->title)); |
390 if (options->message) | 390 if (options->message) |
391 notification->set_message(base::UTF8ToUTF16(*options->message)); | 391 notification->set_message(base::UTF8ToUTF16(*options->message)); |
392 | 392 |
393 // TODO(dewittj): Return error if this fails. | 393 if (options->icon_bitmap.get()) { |
394 if (options->icon_bitmap) { | |
395 gfx::Image icon; | 394 gfx::Image icon; |
396 NotificationConversionHelper::NotificationBitmapToGfxImage( | 395 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( |
397 image_scale, bitmap_sizes.icon_size, options->icon_bitmap.get(), &icon); | 396 image_scale, bitmap_sizes.icon_size, *options->icon_bitmap, |
| 397 &icon)) { |
| 398 SetError(kUnableToDecodeIconError); |
| 399 return false; |
| 400 } |
398 notification->set_icon(icon); | 401 notification->set_icon(icon); |
399 } | 402 } |
400 | 403 |
401 gfx::Image app_icon_mask; | 404 if (options->app_icon_mask_bitmap.get()) { |
402 if (NotificationConversionHelper::NotificationBitmapToGfxImage( | 405 gfx::Image app_icon_mask; |
403 image_scale, | 406 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( |
404 bitmap_sizes.app_icon_mask_size, | 407 image_scale, bitmap_sizes.app_icon_mask_size, |
405 options->app_icon_mask_bitmap.get(), | 408 *options->app_icon_mask_bitmap, &app_icon_mask)) { |
406 &app_icon_mask)) { | 409 SetError(kUnableToDecodeIconError); |
| 410 return false; |
| 411 } |
407 notification->set_small_image( | 412 notification->set_small_image( |
408 GetMaskedSmallImage(app_icon_mask.AsImageSkia())); | 413 GetMaskedSmallImage(app_icon_mask.AsImageSkia())); |
409 } | 414 } |
410 | 415 |
411 if (options->priority) | 416 if (options->priority) |
412 notification->set_priority(*options->priority); | 417 notification->set_priority(*options->priority); |
413 | 418 |
414 if (options->event_time) | 419 if (options->event_time) |
415 notification->set_timestamp(base::Time::FromJsTime(*options->event_time)); | 420 notification->set_timestamp(base::Time::FromJsTime(*options->event_time)); |
416 | 421 |
417 if (options->buttons) { | 422 if (options->buttons) { |
418 // Currently we allow up to 2 buttons. | 423 // Currently we allow up to 2 buttons. |
419 size_t number_of_buttons = options->buttons->size(); | 424 size_t number_of_buttons = options->buttons->size(); |
420 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; | 425 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; |
421 | 426 |
422 std::vector<message_center::ButtonInfo> buttons; | 427 std::vector<message_center::ButtonInfo> buttons; |
423 for (size_t i = 0; i < number_of_buttons; i++) { | 428 for (size_t i = 0; i < number_of_buttons; i++) { |
424 message_center::ButtonInfo button( | 429 message_center::ButtonInfo button( |
425 base::UTF8ToUTF16((*options->buttons)[i]->title)); | 430 base::UTF8ToUTF16((*options->buttons)[i]->title)); |
426 NotificationConversionHelper::NotificationBitmapToGfxImage( | 431 extensions::api::notifications::NotificationBitmap* icon_bitmap_ptr = |
427 image_scale, | 432 (*options->buttons)[i]->icon_bitmap.get(); |
428 bitmap_sizes.button_icon_size, | 433 if (icon_bitmap_ptr) { |
429 (*options->buttons)[i]->icon_bitmap.get(), | 434 NotificationConversionHelper::NotificationBitmapToGfxImage( |
430 &button.icon); | 435 image_scale, bitmap_sizes.button_icon_size, *icon_bitmap_ptr, |
431 buttons.push_back(button); | 436 &button.icon); |
| 437 buttons.push_back(button); |
| 438 } |
432 } | 439 } |
433 notification->set_buttons(buttons); | 440 notification->set_buttons(buttons); |
434 } | 441 } |
435 | 442 |
436 if (options->context_message) { | 443 if (options->context_message) { |
437 notification->set_context_message( | 444 notification->set_context_message( |
438 base::UTF8ToUTF16(*options->context_message)); | 445 base::UTF8ToUTF16(*options->context_message)); |
439 } | 446 } |
440 | 447 |
441 gfx::Image image; | 448 gfx::Image image; |
442 bool has_image = NotificationConversionHelper::NotificationBitmapToGfxImage( | 449 bool has_image = |
443 image_scale, | 450 options->image_bitmap.get() && |
444 bitmap_sizes.image_size, | 451 NotificationConversionHelper::NotificationBitmapToGfxImage( |
445 options->image_bitmap.get(), | 452 image_scale, bitmap_sizes.image_size, *options->image_bitmap, &image); |
446 &image); | 453 |
447 if (has_image) { | 454 if (has_image) { |
448 // We should have an image if and only if the type is an image type. | 455 // We should have an image if and only if the type is an image type. |
449 if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) { | 456 if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) { |
450 SetError(kExtraImageProvided); | 457 SetError(kExtraImageProvided); |
451 return false; | 458 return false; |
452 } | 459 } |
453 notification->set_image(image); | 460 notification->set_image(image); |
454 } | 461 } |
455 | 462 |
456 if (options->progress) { | 463 if (options->progress) { |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 ? api::notifications::PERMISSION_LEVEL_GRANTED | 683 ? api::notifications::PERMISSION_LEVEL_GRANTED |
677 : api::notifications::PERMISSION_LEVEL_DENIED; | 684 : api::notifications::PERMISSION_LEVEL_DENIED; |
678 | 685 |
679 SetResult(new base::StringValue(api::notifications::ToString(result))); | 686 SetResult(new base::StringValue(api::notifications::ToString(result))); |
680 SendResponse(true); | 687 SendResponse(true); |
681 | 688 |
682 return true; | 689 return true; |
683 } | 690 } |
684 | 691 |
685 } // namespace extensions | 692 } // namespace extensions |
OLD | NEW |