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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
13 #include "base/guid.h" | 13 #include "base/guid.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/rand_util.h" | 17 #include "base/rand_util.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
22 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
23 #include "chrome/browser/notifications/notification.h" | 23 #include "chrome/browser/notifications/notification.h" |
24 #include "chrome/browser/notifications/notification_conversion_helper.h" | |
25 #include "chrome/browser/notifications/notification_ui_manager.h" | 24 #include "chrome/browser/notifications/notification_ui_manager.h" |
26 #include "chrome/browser/notifications/notifier_state_tracker.h" | 25 #include "chrome/browser/notifications/notifier_state_tracker.h" |
27 #include "chrome/browser/notifications/notifier_state_tracker_factory.h" | 26 #include "chrome/browser/notifications/notifier_state_tracker_factory.h" |
28 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
29 #include "chrome/common/extensions/api/notifications/notification_style.h" | 28 #include "chrome/common/extensions/api/notifications/notification_style.h" |
30 #include "components/keyed_service/content/browser_context_keyed_service_shutdow
n_notifier_factory.h" | 29 #include "components/keyed_service/content/browser_context_keyed_service_shutdow
n_notifier_factory.h" |
31 #include "components/keyed_service/core/keyed_service_shutdown_notifier.h" | 30 #include "components/keyed_service/core/keyed_service_shutdown_notifier.h" |
32 #include "content/public/browser/render_process_host.h" | 31 #include "content/public/browser/render_process_host.h" |
33 #include "content/public/browser/render_view_host.h" | 32 #include "content/public/browser/render_view_host.h" |
34 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
35 #include "extensions/browser/app_window/app_window.h" | 34 #include "extensions/browser/app_window/app_window.h" |
36 #include "extensions/browser/app_window/app_window_registry.h" | 35 #include "extensions/browser/app_window/app_window_registry.h" |
37 #include "extensions/browser/app_window/native_app_window.h" | 36 #include "extensions/browser/app_window/native_app_window.h" |
38 #include "extensions/browser/event_router.h" | 37 #include "extensions/browser/event_router.h" |
39 #include "extensions/browser/extension_system_provider.h" | 38 #include "extensions/browser/extension_system_provider.h" |
40 #include "extensions/browser/extensions_browser_client.h" | 39 #include "extensions/browser/extensions_browser_client.h" |
41 #include "extensions/common/extension.h" | 40 #include "extensions/common/extension.h" |
42 #include "extensions/common/features/feature.h" | 41 #include "extensions/common/features/feature.h" |
43 #include "third_party/skia/include/core/SkBitmap.h" | 42 #include "third_party/skia/include/core/SkBitmap.h" |
44 #include "ui/base/layout.h" | 43 #include "ui/base/layout.h" |
45 #include "ui/gfx/image/image.h" | 44 #include "ui/gfx/image/image.h" |
46 #include "ui/gfx/image/image_skia.h" | 45 #include "ui/gfx/image/image_skia.h" |
47 #include "ui/gfx/image/image_skia_operations.h" | 46 #include "ui/gfx/image/image_skia_operations.h" |
48 #include "ui/gfx/image/image_skia_rep.h" | 47 #include "ui/gfx/image/image_skia_rep.h" |
| 48 #include "ui/gfx/skia_util.h" |
49 #include "ui/message_center/message_center_style.h" | 49 #include "ui/message_center/message_center_style.h" |
50 #include "ui/message_center/notifier_settings.h" | 50 #include "ui/message_center/notifier_settings.h" |
51 #include "url/gurl.h" | 51 #include "url/gurl.h" |
52 | 52 |
53 using message_center::NotifierId; | 53 using message_center::NotifierId; |
54 | 54 |
55 namespace extensions { | 55 namespace extensions { |
56 | 56 |
57 namespace notifications = api::notifications; | 57 namespace notifications = api::notifications; |
58 | 58 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 width, height, message_center::kSmallImageMaskBackgroundColor); | 118 width, height, message_center::kSmallImageMaskBackgroundColor); |
119 // Foreground color white | 119 // Foreground color white |
120 const gfx::ImageSkia foreground = CreateSolidColorImage( | 120 const gfx::ImageSkia foreground = CreateSolidColorImage( |
121 width, height, message_center::kSmallImageMaskForegroundColor); | 121 width, height, message_center::kSmallImageMaskForegroundColor); |
122 const gfx::ImageSkia masked_small_image = | 122 const gfx::ImageSkia masked_small_image = |
123 gfx::ImageSkiaOperations::CreateMaskedImage(foreground, small_image); | 123 gfx::ImageSkiaOperations::CreateMaskedImage(foreground, small_image); |
124 return gfx::Image(gfx::ImageSkiaOperations::CreateSuperimposedImage( | 124 return gfx::Image(gfx::ImageSkiaOperations::CreateSuperimposedImage( |
125 background, masked_small_image)); | 125 background, masked_small_image)); |
126 } | 126 } |
127 | 127 |
| 128 // Converts the |notification_bitmap| (in RGBA format) to the |*return_image| |
| 129 // (which is in ARGB format). |
| 130 bool NotificationBitmapToGfxImage( |
| 131 float max_scale, |
| 132 const gfx::Size& target_size_dips, |
| 133 const notifications::NotificationBitmap& notification_bitmap, |
| 134 gfx::Image* return_image) { |
| 135 const int max_device_pixel_width = target_size_dips.width() * max_scale; |
| 136 const int max_device_pixel_height = target_size_dips.height() * max_scale; |
| 137 |
| 138 const int kBytesPerPixel = 4; |
| 139 |
| 140 const int width = notification_bitmap.width; |
| 141 const int height = notification_bitmap.height; |
| 142 |
| 143 if (width < 0 || height < 0 || width > max_device_pixel_width || |
| 144 height > max_device_pixel_height) |
| 145 return false; |
| 146 |
| 147 // Ensure we have rgba data. |
| 148 std::vector<char>* rgba_data = notification_bitmap.data.get(); |
| 149 if (!rgba_data) |
| 150 return false; |
| 151 |
| 152 const size_t rgba_data_length = rgba_data->size(); |
| 153 const size_t rgba_area = width * height; |
| 154 |
| 155 if (rgba_data_length != rgba_area * kBytesPerPixel) |
| 156 return false; |
| 157 |
| 158 SkBitmap bitmap; |
| 159 // Allocate the actual backing store with the sanitized dimensions. |
| 160 if (!bitmap.tryAllocN32Pixels(width, height)) |
| 161 return false; |
| 162 |
| 163 // Ensure that our bitmap and our data now refer to the same number of pixels. |
| 164 if (rgba_data_length != bitmap.getSafeSize()) |
| 165 return false; |
| 166 |
| 167 uint32_t* pixels = bitmap.getAddr32(0, 0); |
| 168 const char* c_rgba_data = rgba_data->data(); |
| 169 |
| 170 for (size_t t = 0; t < rgba_area; ++t) { |
| 171 // |c_rgba_data| is RGBA, pixels is ARGB. |
| 172 size_t rgba_index = t * kBytesPerPixel; |
| 173 pixels[t] = |
| 174 SkPreMultiplyColor(((c_rgba_data[rgba_index + 3] & 0xFF) << 24) | |
| 175 ((c_rgba_data[rgba_index + 0] & 0xFF) << 16) | |
| 176 ((c_rgba_data[rgba_index + 1] & 0xFF) << 8) | |
| 177 ((c_rgba_data[rgba_index + 2] & 0xFF) << 0)); |
| 178 } |
| 179 |
| 180 // TODO(dewittj): Handle HiDPI images with more than one scale factor |
| 181 // representation. |
| 182 gfx::ImageSkia skia(gfx::ImageSkiaRep(bitmap, 1.0f)); |
| 183 *return_image = gfx::Image(skia); |
| 184 return true; |
| 185 } |
| 186 |
128 class ShutdownNotifierFactory | 187 class ShutdownNotifierFactory |
129 : public BrowserContextKeyedServiceShutdownNotifierFactory { | 188 : public BrowserContextKeyedServiceShutdownNotifierFactory { |
130 public: | 189 public: |
131 static ShutdownNotifierFactory* GetInstance() { | 190 static ShutdownNotifierFactory* GetInstance() { |
132 return base::Singleton<ShutdownNotifierFactory>::get(); | 191 return base::Singleton<ShutdownNotifierFactory>::get(); |
133 } | 192 } |
134 | 193 |
135 private: | 194 private: |
136 friend struct base::DefaultSingletonTraits<ShutdownNotifierFactory>; | 195 friend struct base::DefaultSingletonTraits<ShutdownNotifierFactory>; |
137 | 196 |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 message_center::NotificationType type = | 374 message_center::NotificationType type = |
316 MapApiTemplateTypeToType(options->type); | 375 MapApiTemplateTypeToType(options->type); |
317 UMA_HISTOGRAM_ENUMERATION("Notifications.ExtensionNotificationType", type, | 376 UMA_HISTOGRAM_ENUMERATION("Notifications.ExtensionNotificationType", type, |
318 message_center::NOTIFICATION_TYPE_LAST); | 377 message_center::NOTIFICATION_TYPE_LAST); |
319 | 378 |
320 const base::string16 title(base::UTF8ToUTF16(*options->title)); | 379 const base::string16 title(base::UTF8ToUTF16(*options->title)); |
321 const base::string16 message(base::UTF8ToUTF16(*options->message)); | 380 const base::string16 message(base::UTF8ToUTF16(*options->message)); |
322 gfx::Image icon; | 381 gfx::Image icon; |
323 | 382 |
324 if (!options->icon_bitmap.get() || | 383 if (!options->icon_bitmap.get() || |
325 !NotificationConversionHelper::NotificationBitmapToGfxImage( | 384 !NotificationBitmapToGfxImage( |
326 image_scale, bitmap_sizes.icon_size, *options->icon_bitmap, &icon)) { | 385 image_scale, bitmap_sizes.icon_size, *options->icon_bitmap, &icon)) { |
327 SetError(kUnableToDecodeIconError); | 386 SetError(kUnableToDecodeIconError); |
328 return false; | 387 return false; |
329 } | 388 } |
330 | 389 |
331 // Then, handle any optional data that's been provided. | 390 // Then, handle any optional data that's been provided. |
332 message_center::RichNotificationData optional_fields; | 391 message_center::RichNotificationData optional_fields; |
333 if (options->app_icon_mask_url.get()) { | 392 if (options->app_icon_mask_url.get()) { |
334 gfx::Image small_icon_mask; | 393 gfx::Image small_icon_mask; |
335 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | 394 if (!NotificationBitmapToGfxImage( |
336 image_scale, bitmap_sizes.app_icon_mask_size, | 395 image_scale, bitmap_sizes.app_icon_mask_size, |
337 *options->app_icon_mask_bitmap, &small_icon_mask)) { | 396 *options->app_icon_mask_bitmap, &small_icon_mask)) { |
338 SetError(kUnableToDecodeIconError); | 397 SetError(kUnableToDecodeIconError); |
339 return false; | 398 return false; |
340 } | 399 } |
341 optional_fields.small_image = | 400 optional_fields.small_image = |
342 GetMaskedSmallImage(small_icon_mask.AsImageSkia()); | 401 GetMaskedSmallImage(small_icon_mask.AsImageSkia()); |
343 } | 402 } |
344 | 403 |
345 if (options->priority.get()) | 404 if (options->priority.get()) |
(...skipping 13 matching lines...) Expand all Loading... |
359 number_of_buttons, 17); | 418 number_of_buttons, 17); |
360 | 419 |
361 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; | 420 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; |
362 | 421 |
363 for (size_t i = 0; i < number_of_buttons; i++) { | 422 for (size_t i = 0; i < number_of_buttons; i++) { |
364 message_center::ButtonInfo info( | 423 message_center::ButtonInfo info( |
365 base::UTF8ToUTF16((*options->buttons)[i].title)); | 424 base::UTF8ToUTF16((*options->buttons)[i].title)); |
366 extensions::api::notifications::NotificationBitmap* icon_bitmap_ptr = | 425 extensions::api::notifications::NotificationBitmap* icon_bitmap_ptr = |
367 (*options->buttons)[i].icon_bitmap.get(); | 426 (*options->buttons)[i].icon_bitmap.get(); |
368 if (icon_bitmap_ptr) { | 427 if (icon_bitmap_ptr) { |
369 NotificationConversionHelper::NotificationBitmapToGfxImage( | 428 NotificationBitmapToGfxImage( |
370 image_scale, bitmap_sizes.button_icon_size, *icon_bitmap_ptr, | 429 image_scale, bitmap_sizes.button_icon_size, *icon_bitmap_ptr, |
371 &info.icon); | 430 &info.icon); |
372 } | 431 } |
373 optional_fields.buttons.push_back(info); | 432 optional_fields.buttons.push_back(info); |
374 } | 433 } |
375 } | 434 } |
376 | 435 |
377 if (options->context_message) { | 436 if (options->context_message) { |
378 optional_fields.context_message = | 437 optional_fields.context_message = |
379 base::UTF8ToUTF16(*options->context_message); | 438 base::UTF8ToUTF16(*options->context_message); |
380 } | 439 } |
381 | 440 |
382 bool has_image = options->image_bitmap.get() && | 441 bool has_image = options->image_bitmap.get() && |
383 NotificationConversionHelper::NotificationBitmapToGfxImage( | 442 NotificationBitmapToGfxImage( |
384 image_scale, bitmap_sizes.image_size, | 443 image_scale, bitmap_sizes.image_size, |
385 *options->image_bitmap, &optional_fields.image); | 444 *options->image_bitmap, &optional_fields.image); |
386 | 445 |
387 // We should have an image if and only if the type is an image type. | 446 // We should have an image if and only if the type is an image type. |
388 if (has_image != (type == message_center::NOTIFICATION_TYPE_IMAGE)) { | 447 if (has_image != (type == message_center::NOTIFICATION_TYPE_IMAGE)) { |
389 SetError(kExtraImageProvided); | 448 SetError(kExtraImageProvided); |
390 return false; | 449 return false; |
391 } | 450 } |
392 | 451 |
393 // We should have list items if and only if the type is a multiple type. | 452 // We should have list items if and only if the type is a multiple type. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 // Update optional fields if provided. | 519 // Update optional fields if provided. |
461 if (options->type != api::notifications::TEMPLATE_TYPE_NONE) | 520 if (options->type != api::notifications::TEMPLATE_TYPE_NONE) |
462 notification->set_type(MapApiTemplateTypeToType(options->type)); | 521 notification->set_type(MapApiTemplateTypeToType(options->type)); |
463 if (options->title) | 522 if (options->title) |
464 notification->set_title(base::UTF8ToUTF16(*options->title)); | 523 notification->set_title(base::UTF8ToUTF16(*options->title)); |
465 if (options->message) | 524 if (options->message) |
466 notification->set_message(base::UTF8ToUTF16(*options->message)); | 525 notification->set_message(base::UTF8ToUTF16(*options->message)); |
467 | 526 |
468 if (options->icon_bitmap.get()) { | 527 if (options->icon_bitmap.get()) { |
469 gfx::Image icon; | 528 gfx::Image icon; |
470 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | 529 if (!NotificationBitmapToGfxImage( |
471 image_scale, bitmap_sizes.icon_size, *options->icon_bitmap, | 530 image_scale, bitmap_sizes.icon_size, *options->icon_bitmap, |
472 &icon)) { | 531 &icon)) { |
473 SetError(kUnableToDecodeIconError); | 532 SetError(kUnableToDecodeIconError); |
474 return false; | 533 return false; |
475 } | 534 } |
476 notification->set_icon(icon); | 535 notification->set_icon(icon); |
477 } | 536 } |
478 | 537 |
479 if (options->app_icon_mask_bitmap.get()) { | 538 if (options->app_icon_mask_bitmap.get()) { |
480 gfx::Image app_icon_mask; | 539 gfx::Image app_icon_mask; |
481 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | 540 if (!NotificationBitmapToGfxImage( |
482 image_scale, bitmap_sizes.app_icon_mask_size, | 541 image_scale, bitmap_sizes.app_icon_mask_size, |
483 *options->app_icon_mask_bitmap, &app_icon_mask)) { | 542 *options->app_icon_mask_bitmap, &app_icon_mask)) { |
484 SetError(kUnableToDecodeIconError); | 543 SetError(kUnableToDecodeIconError); |
485 return false; | 544 return false; |
486 } | 545 } |
487 notification->set_small_image( | 546 notification->set_small_image( |
488 GetMaskedSmallImage(app_icon_mask.AsImageSkia())); | 547 GetMaskedSmallImage(app_icon_mask.AsImageSkia())); |
489 } | 548 } |
490 | 549 |
491 if (options->priority) | 550 if (options->priority) |
492 notification->set_priority(*options->priority); | 551 notification->set_priority(*options->priority); |
493 | 552 |
494 if (options->event_time) | 553 if (options->event_time) |
495 notification->set_timestamp(base::Time::FromJsTime(*options->event_time)); | 554 notification->set_timestamp(base::Time::FromJsTime(*options->event_time)); |
496 | 555 |
497 if (options->buttons) { | 556 if (options->buttons) { |
498 // Currently we allow up to 2 buttons. | 557 // Currently we allow up to 2 buttons. |
499 size_t number_of_buttons = options->buttons->size(); | 558 size_t number_of_buttons = options->buttons->size(); |
500 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; | 559 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; |
501 | 560 |
502 std::vector<message_center::ButtonInfo> buttons; | 561 std::vector<message_center::ButtonInfo> buttons; |
503 for (size_t i = 0; i < number_of_buttons; i++) { | 562 for (size_t i = 0; i < number_of_buttons; i++) { |
504 message_center::ButtonInfo button( | 563 message_center::ButtonInfo button( |
505 base::UTF8ToUTF16((*options->buttons)[i].title)); | 564 base::UTF8ToUTF16((*options->buttons)[i].title)); |
506 extensions::api::notifications::NotificationBitmap* icon_bitmap_ptr = | 565 extensions::api::notifications::NotificationBitmap* icon_bitmap_ptr = |
507 (*options->buttons)[i].icon_bitmap.get(); | 566 (*options->buttons)[i].icon_bitmap.get(); |
508 if (icon_bitmap_ptr) { | 567 if (icon_bitmap_ptr) { |
509 NotificationConversionHelper::NotificationBitmapToGfxImage( | 568 NotificationBitmapToGfxImage( |
510 image_scale, bitmap_sizes.button_icon_size, *icon_bitmap_ptr, | 569 image_scale, bitmap_sizes.button_icon_size, *icon_bitmap_ptr, |
511 &button.icon); | 570 &button.icon); |
512 } | 571 } |
513 buttons.push_back(button); | 572 buttons.push_back(button); |
514 } | 573 } |
515 notification->set_buttons(buttons); | 574 notification->set_buttons(buttons); |
516 } | 575 } |
517 | 576 |
518 if (options->context_message) { | 577 if (options->context_message) { |
519 notification->set_context_message( | 578 notification->set_context_message( |
520 base::UTF8ToUTF16(*options->context_message)); | 579 base::UTF8ToUTF16(*options->context_message)); |
521 } | 580 } |
522 | 581 |
523 gfx::Image image; | 582 gfx::Image image; |
524 bool has_image = | 583 bool has_image = |
525 options->image_bitmap.get() && | 584 options->image_bitmap.get() && |
526 NotificationConversionHelper::NotificationBitmapToGfxImage( | 585 NotificationBitmapToGfxImage( |
527 image_scale, bitmap_sizes.image_size, *options->image_bitmap, &image); | 586 image_scale, bitmap_sizes.image_size, *options->image_bitmap, &image); |
528 | 587 |
529 if (has_image) { | 588 if (has_image) { |
530 // We should have an image if and only if the type is an image type. | 589 // We should have an image if and only if the type is an image type. |
531 if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) { | 590 if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) { |
532 SetError(kExtraImageProvided); | 591 SetError(kExtraImageProvided); |
533 return false; | 592 return false; |
534 } | 593 } |
535 notification->set_image(image); | 594 notification->set_image(image); |
536 } | 595 } |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
760 : api::notifications::PERMISSION_LEVEL_DENIED; | 819 : api::notifications::PERMISSION_LEVEL_DENIED; |
761 | 820 |
762 SetResult(base::MakeUnique<base::StringValue>( | 821 SetResult(base::MakeUnique<base::StringValue>( |
763 api::notifications::ToString(result))); | 822 api::notifications::ToString(result))); |
764 SendResponse(true); | 823 SendResponse(true); |
765 | 824 |
766 return true; | 825 return true; |
767 } | 826 } |
768 | 827 |
769 } // namespace extensions | 828 } // namespace extensions |
OLD | NEW |