Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/notifications/notification_conversion_helper.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "chrome/browser/extensions/api/notification_provider/notification_provi der_api.h" | |
| 11 #include "chrome/common/extensions/api/notification_provider.h" | |
| 12 #include "ui/gfx/skia_util.h" | |
| 13 | |
| 14 NotificationConversionHelper::NotificationConversionHelper() { | |
| 15 } | |
| 16 | |
| 17 NotificationConversionHelper::~NotificationConversionHelper() { | |
| 18 } | |
| 19 | |
| 20 // This function converts Notification::Notification data to | |
| 21 // extensions::api::notifications::NotificationOptions | |
| 22 void NotificationConversionHelper::NotificationToNotificationOptions( | |
| 23 const Notification& notification, | |
| 24 extensions::api::notifications::NotificationOptions* options) { | |
| 25 // Extract required fields: type, title, message, and icon. | |
| 26 std::string type = MapTypeToString(notification.type()); | |
| 27 options->type = extensions::api::notifications::ParseTemplateType(type); | |
| 28 | |
| 29 if (!notification.icon().IsEmpty()) { | |
| 30 scoped_ptr<extensions::api::notifications::NotificationBitmap> icon( | |
| 31 new extensions::api::notifications::NotificationBitmap()); | |
| 32 GfxImageToNotificationBitmap(¬ification.icon(), icon.get()); | |
| 33 options->icon_bitmap = icon.Pass(); | |
| 34 } | |
| 35 | |
| 36 options->title = scoped_ptr<std::string>( | |
| 37 new std::string(base::UTF16ToUTF8(notification.title()))); | |
| 38 options->message = scoped_ptr<std::string>( | |
| 39 new std::string(base::UTF16ToUTF8(notification.message()))); | |
| 40 | |
| 41 // Handle optional data provided. | |
| 42 const message_center::RichNotificationData* rich_data = | |
| 43 ¬ification.rich_notification_data(); | |
| 44 | |
| 45 if (!rich_data->small_image.IsEmpty()) { | |
| 46 scoped_ptr<extensions::api::notifications::NotificationBitmap> icon_mask( | |
| 47 new extensions::api::notifications::NotificationBitmap()); | |
| 48 GfxImageToNotificationBitmap(&rich_data->small_image, icon_mask.get()); | |
| 49 options->app_icon_mask_bitmap = icon_mask.Pass(); | |
| 50 } | |
| 51 | |
| 52 options->priority = scoped_ptr<int>(new int(rich_data->priority)); | |
|
Pete Williamson
2014/08/09 00:38:19
Is options->priority really a scoped_ptr<int>? It
liyanhou
2014/08/11 17:21:10
Yes, it's a scoped_ptr<int> in the generated notif
| |
| 53 | |
| 54 options->is_clickable = scoped_ptr<bool>(new bool(rich_data->clickable)); | |
| 55 | |
| 56 options->event_time = | |
| 57 scoped_ptr<double>(new double(rich_data->timestamp.ToDoubleT())); | |
| 58 | |
| 59 if (!rich_data->context_message.empty()) | |
| 60 options->context_message = scoped_ptr<std::string>( | |
| 61 new std::string(base::UTF16ToUTF8(rich_data->context_message))); | |
| 62 | |
| 63 if (!rich_data->buttons.empty()) { | |
| 64 scoped_ptr<std::vector< | |
| 65 linked_ptr<extensions::api::notifications::NotificationButton> > > | |
| 66 button_list(new std::vector< | |
| 67 linked_ptr<extensions::api::notifications::NotificationButton> >); | |
| 68 for (size_t i = 0; i < rich_data->buttons.size(); i++) { | |
| 69 linked_ptr<extensions::api::notifications::NotificationButton> button( | |
| 70 new extensions::api::notifications::NotificationButton); | |
| 71 button->title = base::UTF16ToUTF8(rich_data->buttons[i].title); | |
| 72 | |
| 73 if (!rich_data->buttons[i].icon.IsEmpty()) { | |
| 74 scoped_ptr<extensions::api::notifications::NotificationBitmap> icon( | |
| 75 new extensions::api::notifications::NotificationBitmap()); | |
| 76 GfxImageToNotificationBitmap(&rich_data->buttons[i].icon, icon.get()); | |
| 77 button->icon_bitmap = icon.Pass(); | |
| 78 } | |
| 79 button_list->push_back(button); | |
| 80 } | |
| 81 options->buttons = button_list.Pass(); | |
| 82 } | |
| 83 | |
| 84 // Only image type notifications should have images. | |
| 85 if (type == "image" && !rich_data->image.IsEmpty()) { | |
| 86 scoped_ptr<extensions::api::notifications::NotificationBitmap> image( | |
| 87 new extensions::api::notifications::NotificationBitmap()); | |
| 88 GfxImageToNotificationBitmap(¬ification.image(), image.get()); | |
| 89 options->image_bitmap = image.Pass(); | |
| 90 } else if (type != "image" && !rich_data->image.IsEmpty()) { | |
| 91 LOG(WARNING) << "Only image type notifications should have images."; | |
| 92 } | |
| 93 | |
| 94 // Only progress type notifications should have progress bars. | |
| 95 if (type == "progress") | |
| 96 options->progress = scoped_ptr<int>(new int(rich_data->progress)); | |
| 97 else if (rich_data->progress != 0) | |
| 98 LOG(WARNING) << "Only progress type notifications should have progress."; | |
| 99 | |
| 100 // Only list type notifications should have lists. | |
| 101 if (type == "list" && !rich_data->items.empty()) { | |
| 102 scoped_ptr<std::vector< | |
| 103 linked_ptr<extensions::api::notifications::NotificationItem> > > | |
| 104 list(new std::vector< | |
| 105 linked_ptr<extensions::api::notifications::NotificationItem> >); | |
| 106 for (size_t j = 0; j < rich_data->items.size(); j++) { | |
| 107 linked_ptr<extensions::api::notifications::NotificationItem> item( | |
| 108 new extensions::api::notifications::NotificationItem); | |
| 109 item->title = base::UTF16ToUTF8(rich_data->items[j].title); | |
| 110 item->message = base::UTF16ToUTF8(rich_data->items[j].message); | |
| 111 list->push_back(item); | |
| 112 } | |
| 113 options->items = list.Pass(); | |
| 114 } else if (type != "list" && !rich_data->items.empty()) { | |
| 115 LOG(WARNING) << "Only list type notifications should have lists."; | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 void NotificationConversionHelper::GfxImageToNotificationBitmap( | |
|
dewittj
2014/08/09 00:29:52
can we also move the conversion from NotificationB
liyanhou
2014/08/11 17:21:10
Yes. Can that be in a separate CL, since this CL i
| |
| 120 const gfx::Image* gfx_image, | |
| 121 extensions::api::notifications::NotificationBitmap* notification_bitmap) { | |
| 122 SkBitmap sk_bitmap = gfx_image->AsBitmap(); | |
| 123 sk_bitmap.lockPixels(); | |
|
dewittj
2014/08/09 00:29:52
hmm, do you know why this line is here? Why isn't
liyanhou
2014/08/11 17:21:10
Done.
| |
| 124 | |
| 125 notification_bitmap->width = sk_bitmap.width(); | |
| 126 notification_bitmap->height = sk_bitmap.height(); | |
| 127 int pixel_width = sk_bitmap.width() * sk_bitmap.height(); | |
|
Pete Williamson
2014/08/09 00:38:19
This isn't really a with per say, more of a size.
liyanhou
2014/08/11 17:21:10
Done.
| |
| 128 | |
| 129 uint32_t* bitmap_pixels = sk_bitmap.getAddr32(0, 0); | |
| 130 const unsigned char* bitmap = | |
| 131 reinterpret_cast<const unsigned char*>(bitmap_pixels); | |
| 132 unsigned char* bitmap_data(new unsigned char[pixel_width * 4]); | |
|
Pete Williamson
2014/08/09 00:38:19
Let's put the 4 back to BYTES_PER_PIXEL, both here
liyanhou
2014/08/11 17:21:10
Done.
| |
| 133 | |
| 134 gfx::ConvertSkiaToRGBA(bitmap, pixel_width, bitmap_data); | |
| 135 | |
| 136 notification_bitmap->data = scoped_ptr<std::string>( | |
| 137 new std::string(bitmap_data, (bitmap_data + pixel_width * 4))); | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 std::string NotificationConversionHelper::MapTypeToString( | |
| 142 message_center::NotificationType type) { | |
| 143 switch (type) { | |
| 144 case message_center::NOTIFICATION_TYPE_BASE_FORMAT: | |
| 145 return "basic"; | |
| 146 case message_center::NOTIFICATION_TYPE_IMAGE: | |
| 147 return "image"; | |
| 148 case message_center::NOTIFICATION_TYPE_MULTIPLE: | |
| 149 return "list"; | |
| 150 case message_center::NOTIFICATION_TYPE_PROGRESS: | |
| 151 return "progress"; | |
| 152 default: | |
| 153 return ""; | |
|
dewittj
2014/08/09 00:29:52
NOTREACHED() here before the return since it's an
liyanhou
2014/08/11 17:21:10
Done.
| |
| 154 } | |
| 155 } | |
| OLD | NEW |