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 bool NotificationBitmapToGfxImage( | |
Devlin
2017/01/28 02:45:03
nit: add function level comment
Peter Beverloo
2017/02/09 16:03:10
Done.
| |
129 float max_scale, | |
130 const gfx::Size& target_size_dips, | |
131 const extensions::api::notifications::NotificationBitmap& | |
Devlin
2017/01/28 02:45:03
s/extensions::api::notifications/notifications::
Peter Beverloo
2017/02/09 16:03:10
Done.
| |
132 notification_bitmap, | |
133 gfx::Image* return_image) { | |
134 const int max_device_pixel_width = target_size_dips.width() * max_scale; | |
135 const int max_device_pixel_height = target_size_dips.height() * max_scale; | |
136 | |
137 const int BYTES_PER_PIXEL = 4; | |
Devlin
2017/01/28 02:45:03
I think this was just copy-pasted, but since we're
Peter Beverloo
2017/02/09 16:03:10
Done.
| |
138 | |
139 const int width = notification_bitmap.width; | |
140 const int height = notification_bitmap.height; | |
141 | |
142 if (width < 0 || height < 0 || width > max_device_pixel_width || | |
143 height > max_device_pixel_height) | |
144 return false; | |
145 | |
146 // Ensure we have rgba data. | |
147 std::vector<char>* rgba_data = notification_bitmap.data.get(); | |
148 if (!rgba_data) | |
149 return false; | |
150 | |
151 const size_t rgba_data_length = rgba_data->size(); | |
152 const size_t rgba_area = width * height; | |
153 | |
154 if (rgba_data_length != rgba_area * BYTES_PER_PIXEL) | |
155 return false; | |
156 | |
157 SkBitmap bitmap; | |
158 // Allocate the actual backing store with the sanitized dimensions. | |
159 if (!bitmap.tryAllocN32Pixels(width, height)) | |
160 return false; | |
161 | |
162 // Ensure that our bitmap and our data now refer to the same number of pixels. | |
163 if (rgba_data_length != bitmap.getSafeSize()) | |
164 return false; | |
165 | |
166 uint32_t* pixels = bitmap.getAddr32(0, 0); | |
167 const char* c_rgba_data = rgba_data->data(); | |
168 | |
169 for (size_t t = 0; t < rgba_area; ++t) { | |
170 // |c_rgba_data| is RGBA, pixels is ARGB. | |
171 size_t rgba_index = t * BYTES_PER_PIXEL; | |
172 pixels[t] = | |
173 SkPreMultiplyColor(((c_rgba_data[rgba_index + 3] & 0xFF) << 24) | | |
174 ((c_rgba_data[rgba_index + 0] & 0xFF) << 16) | | |
175 ((c_rgba_data[rgba_index + 1] & 0xFF) << 8) | | |
176 ((c_rgba_data[rgba_index + 2] & 0xFF) << 0)); | |
177 } | |
178 | |
179 // TODO(dewittj): Handle HiDPI images with more than one scale factor | |
180 // representation. | |
181 gfx::ImageSkia skia(gfx::ImageSkiaRep(bitmap, 1.0f)); | |
182 *return_image = gfx::Image(skia); | |
183 return true; | |
184 } | |
185 | |
128 class ShutdownNotifierFactory | 186 class ShutdownNotifierFactory |
129 : public BrowserContextKeyedServiceShutdownNotifierFactory { | 187 : public BrowserContextKeyedServiceShutdownNotifierFactory { |
130 public: | 188 public: |
131 static ShutdownNotifierFactory* GetInstance() { | 189 static ShutdownNotifierFactory* GetInstance() { |
132 return base::Singleton<ShutdownNotifierFactory>::get(); | 190 return base::Singleton<ShutdownNotifierFactory>::get(); |
133 } | 191 } |
134 | 192 |
135 private: | 193 private: |
136 friend struct base::DefaultSingletonTraits<ShutdownNotifierFactory>; | 194 friend struct base::DefaultSingletonTraits<ShutdownNotifierFactory>; |
137 | 195 |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 message_center::NotificationType type = | 373 message_center::NotificationType type = |
316 MapApiTemplateTypeToType(options->type); | 374 MapApiTemplateTypeToType(options->type); |
317 UMA_HISTOGRAM_ENUMERATION("Notifications.ExtensionNotificationType", type, | 375 UMA_HISTOGRAM_ENUMERATION("Notifications.ExtensionNotificationType", type, |
318 message_center::NOTIFICATION_TYPE_LAST); | 376 message_center::NOTIFICATION_TYPE_LAST); |
319 | 377 |
320 const base::string16 title(base::UTF8ToUTF16(*options->title)); | 378 const base::string16 title(base::UTF8ToUTF16(*options->title)); |
321 const base::string16 message(base::UTF8ToUTF16(*options->message)); | 379 const base::string16 message(base::UTF8ToUTF16(*options->message)); |
322 gfx::Image icon; | 380 gfx::Image icon; |
323 | 381 |
324 if (!options->icon_bitmap.get() || | 382 if (!options->icon_bitmap.get() || |
325 !NotificationConversionHelper::NotificationBitmapToGfxImage( | 383 !NotificationBitmapToGfxImage( |
326 image_scale, bitmap_sizes.icon_size, *options->icon_bitmap, &icon)) { | 384 image_scale, bitmap_sizes.icon_size, *options->icon_bitmap, &icon)) { |
327 SetError(kUnableToDecodeIconError); | 385 SetError(kUnableToDecodeIconError); |
328 return false; | 386 return false; |
329 } | 387 } |
330 | 388 |
331 // Then, handle any optional data that's been provided. | 389 // Then, handle any optional data that's been provided. |
332 message_center::RichNotificationData optional_fields; | 390 message_center::RichNotificationData optional_fields; |
333 if (options->app_icon_mask_url.get()) { | 391 if (options->app_icon_mask_url.get()) { |
334 gfx::Image small_icon_mask; | 392 gfx::Image small_icon_mask; |
335 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | 393 if (!NotificationBitmapToGfxImage( |
336 image_scale, bitmap_sizes.app_icon_mask_size, | 394 image_scale, bitmap_sizes.app_icon_mask_size, |
337 *options->app_icon_mask_bitmap, &small_icon_mask)) { | 395 *options->app_icon_mask_bitmap, &small_icon_mask)) { |
338 SetError(kUnableToDecodeIconError); | 396 SetError(kUnableToDecodeIconError); |
339 return false; | 397 return false; |
340 } | 398 } |
341 optional_fields.small_image = | 399 optional_fields.small_image = |
342 GetMaskedSmallImage(small_icon_mask.AsImageSkia()); | 400 GetMaskedSmallImage(small_icon_mask.AsImageSkia()); |
343 } | 401 } |
344 | 402 |
345 if (options->priority.get()) | 403 if (options->priority.get()) |
(...skipping 13 matching lines...) Expand all Loading... | |
359 number_of_buttons, 17); | 417 number_of_buttons, 17); |
360 | 418 |
361 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; | 419 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; |
362 | 420 |
363 for (size_t i = 0; i < number_of_buttons; i++) { | 421 for (size_t i = 0; i < number_of_buttons; i++) { |
364 message_center::ButtonInfo info( | 422 message_center::ButtonInfo info( |
365 base::UTF8ToUTF16((*options->buttons)[i].title)); | 423 base::UTF8ToUTF16((*options->buttons)[i].title)); |
366 extensions::api::notifications::NotificationBitmap* icon_bitmap_ptr = | 424 extensions::api::notifications::NotificationBitmap* icon_bitmap_ptr = |
367 (*options->buttons)[i].icon_bitmap.get(); | 425 (*options->buttons)[i].icon_bitmap.get(); |
368 if (icon_bitmap_ptr) { | 426 if (icon_bitmap_ptr) { |
369 NotificationConversionHelper::NotificationBitmapToGfxImage( | 427 NotificationBitmapToGfxImage( |
370 image_scale, bitmap_sizes.button_icon_size, *icon_bitmap_ptr, | 428 image_scale, bitmap_sizes.button_icon_size, *icon_bitmap_ptr, |
371 &info.icon); | 429 &info.icon); |
372 } | 430 } |
373 optional_fields.buttons.push_back(info); | 431 optional_fields.buttons.push_back(info); |
374 } | 432 } |
375 } | 433 } |
376 | 434 |
377 if (options->context_message) { | 435 if (options->context_message) { |
378 optional_fields.context_message = | 436 optional_fields.context_message = |
379 base::UTF8ToUTF16(*options->context_message); | 437 base::UTF8ToUTF16(*options->context_message); |
380 } | 438 } |
381 | 439 |
382 bool has_image = options->image_bitmap.get() && | 440 bool has_image = options->image_bitmap.get() && |
383 NotificationConversionHelper::NotificationBitmapToGfxImage( | 441 NotificationBitmapToGfxImage( |
384 image_scale, bitmap_sizes.image_size, | 442 image_scale, bitmap_sizes.image_size, |
385 *options->image_bitmap, &optional_fields.image); | 443 *options->image_bitmap, &optional_fields.image); |
386 | 444 |
387 // We should have an image if and only if the type is an image type. | 445 // 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)) { | 446 if (has_image != (type == message_center::NOTIFICATION_TYPE_IMAGE)) { |
389 SetError(kExtraImageProvided); | 447 SetError(kExtraImageProvided); |
390 return false; | 448 return false; |
391 } | 449 } |
392 | 450 |
393 // We should have list items if and only if the type is a multiple type. | 451 // 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. | 518 // Update optional fields if provided. |
461 if (options->type != api::notifications::TEMPLATE_TYPE_NONE) | 519 if (options->type != api::notifications::TEMPLATE_TYPE_NONE) |
462 notification->set_type(MapApiTemplateTypeToType(options->type)); | 520 notification->set_type(MapApiTemplateTypeToType(options->type)); |
463 if (options->title) | 521 if (options->title) |
464 notification->set_title(base::UTF8ToUTF16(*options->title)); | 522 notification->set_title(base::UTF8ToUTF16(*options->title)); |
465 if (options->message) | 523 if (options->message) |
466 notification->set_message(base::UTF8ToUTF16(*options->message)); | 524 notification->set_message(base::UTF8ToUTF16(*options->message)); |
467 | 525 |
468 if (options->icon_bitmap.get()) { | 526 if (options->icon_bitmap.get()) { |
469 gfx::Image icon; | 527 gfx::Image icon; |
470 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | 528 if (!NotificationBitmapToGfxImage( |
471 image_scale, bitmap_sizes.icon_size, *options->icon_bitmap, | 529 image_scale, bitmap_sizes.icon_size, *options->icon_bitmap, |
472 &icon)) { | 530 &icon)) { |
473 SetError(kUnableToDecodeIconError); | 531 SetError(kUnableToDecodeIconError); |
474 return false; | 532 return false; |
475 } | 533 } |
476 notification->set_icon(icon); | 534 notification->set_icon(icon); |
477 } | 535 } |
478 | 536 |
479 if (options->app_icon_mask_bitmap.get()) { | 537 if (options->app_icon_mask_bitmap.get()) { |
480 gfx::Image app_icon_mask; | 538 gfx::Image app_icon_mask; |
481 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | 539 if (!NotificationBitmapToGfxImage( |
482 image_scale, bitmap_sizes.app_icon_mask_size, | 540 image_scale, bitmap_sizes.app_icon_mask_size, |
483 *options->app_icon_mask_bitmap, &app_icon_mask)) { | 541 *options->app_icon_mask_bitmap, &app_icon_mask)) { |
484 SetError(kUnableToDecodeIconError); | 542 SetError(kUnableToDecodeIconError); |
485 return false; | 543 return false; |
486 } | 544 } |
487 notification->set_small_image( | 545 notification->set_small_image( |
488 GetMaskedSmallImage(app_icon_mask.AsImageSkia())); | 546 GetMaskedSmallImage(app_icon_mask.AsImageSkia())); |
489 } | 547 } |
490 | 548 |
491 if (options->priority) | 549 if (options->priority) |
492 notification->set_priority(*options->priority); | 550 notification->set_priority(*options->priority); |
493 | 551 |
494 if (options->event_time) | 552 if (options->event_time) |
495 notification->set_timestamp(base::Time::FromJsTime(*options->event_time)); | 553 notification->set_timestamp(base::Time::FromJsTime(*options->event_time)); |
496 | 554 |
497 if (options->buttons) { | 555 if (options->buttons) { |
498 // Currently we allow up to 2 buttons. | 556 // Currently we allow up to 2 buttons. |
499 size_t number_of_buttons = options->buttons->size(); | 557 size_t number_of_buttons = options->buttons->size(); |
500 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; | 558 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; |
501 | 559 |
502 std::vector<message_center::ButtonInfo> buttons; | 560 std::vector<message_center::ButtonInfo> buttons; |
503 for (size_t i = 0; i < number_of_buttons; i++) { | 561 for (size_t i = 0; i < number_of_buttons; i++) { |
504 message_center::ButtonInfo button( | 562 message_center::ButtonInfo button( |
505 base::UTF8ToUTF16((*options->buttons)[i].title)); | 563 base::UTF8ToUTF16((*options->buttons)[i].title)); |
506 extensions::api::notifications::NotificationBitmap* icon_bitmap_ptr = | 564 extensions::api::notifications::NotificationBitmap* icon_bitmap_ptr = |
507 (*options->buttons)[i].icon_bitmap.get(); | 565 (*options->buttons)[i].icon_bitmap.get(); |
508 if (icon_bitmap_ptr) { | 566 if (icon_bitmap_ptr) { |
509 NotificationConversionHelper::NotificationBitmapToGfxImage( | 567 NotificationBitmapToGfxImage( |
510 image_scale, bitmap_sizes.button_icon_size, *icon_bitmap_ptr, | 568 image_scale, bitmap_sizes.button_icon_size, *icon_bitmap_ptr, |
511 &button.icon); | 569 &button.icon); |
512 } | 570 } |
513 buttons.push_back(button); | 571 buttons.push_back(button); |
514 } | 572 } |
515 notification->set_buttons(buttons); | 573 notification->set_buttons(buttons); |
516 } | 574 } |
517 | 575 |
518 if (options->context_message) { | 576 if (options->context_message) { |
519 notification->set_context_message( | 577 notification->set_context_message( |
520 base::UTF8ToUTF16(*options->context_message)); | 578 base::UTF8ToUTF16(*options->context_message)); |
521 } | 579 } |
522 | 580 |
523 gfx::Image image; | 581 gfx::Image image; |
524 bool has_image = | 582 bool has_image = |
525 options->image_bitmap.get() && | 583 options->image_bitmap.get() && |
526 NotificationConversionHelper::NotificationBitmapToGfxImage( | 584 NotificationBitmapToGfxImage( |
527 image_scale, bitmap_sizes.image_size, *options->image_bitmap, &image); | 585 image_scale, bitmap_sizes.image_size, *options->image_bitmap, &image); |
528 | 586 |
529 if (has_image) { | 587 if (has_image) { |
530 // We should have an image if and only if the type is an image type. | 588 // We should have an image if and only if the type is an image type. |
531 if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) { | 589 if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) { |
532 SetError(kExtraImageProvided); | 590 SetError(kExtraImageProvided); |
533 return false; | 591 return false; |
534 } | 592 } |
535 notification->set_image(image); | 593 notification->set_image(image); |
536 } | 594 } |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
760 : api::notifications::PERMISSION_LEVEL_DENIED; | 818 : api::notifications::PERMISSION_LEVEL_DENIED; |
761 | 819 |
762 SetResult(base::MakeUnique<base::StringValue>( | 820 SetResult(base::MakeUnique<base::StringValue>( |
763 api::notifications::ToString(result))); | 821 api::notifications::ToString(result))); |
764 SendResponse(true); | 822 SendResponse(true); |
765 | 823 |
766 return true; | 824 return true; |
767 } | 825 } |
768 | 826 |
769 } // namespace extensions | 827 } // namespace extensions |
OLD | NEW |