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" | |
|
dewittj
2014/08/12 18:47:39
I don't think this include is used.
liyanhou
2014/08/13 17:30:37
Done.
| |
| 11 #include "chrome/common/extensions/api/notification_provider.h" | |
| 12 #include "ui/gfx/skia_util.h" | |
| 13 | |
| 14 void NotificationConversionHelper::NotificationToNotificationOptions( | |
| 15 const Notification& notification, | |
| 16 extensions::api::notifications::NotificationOptions* options) { | |
| 17 // Extract required fields: type, title, message, and icon. | |
| 18 std::string type = MapTypeToString(notification.type()); | |
| 19 options->type = extensions::api::notifications::ParseTemplateType(type); | |
| 20 | |
| 21 if (!notification.icon().IsEmpty()) { | |
| 22 scoped_ptr<extensions::api::notifications::NotificationBitmap> icon( | |
| 23 new extensions::api::notifications::NotificationBitmap()); | |
| 24 GfxImageToNotificationBitmap(¬ification.icon(), icon.get()); | |
| 25 options->icon_bitmap = icon.Pass(); | |
| 26 } | |
| 27 | |
| 28 options->title = scoped_ptr<std::string>( | |
| 29 new std::string(base::UTF16ToUTF8(notification.title()))); | |
| 30 options->message = scoped_ptr<std::string>( | |
| 31 new std::string(base::UTF16ToUTF8(notification.message()))); | |
| 32 | |
| 33 // Handle optional data provided. | |
| 34 const message_center::RichNotificationData* rich_data = | |
| 35 ¬ification.rich_notification_data(); | |
| 36 | |
| 37 if (!rich_data->small_image.IsEmpty()) { | |
| 38 scoped_ptr<extensions::api::notifications::NotificationBitmap> icon_mask( | |
| 39 new extensions::api::notifications::NotificationBitmap()); | |
| 40 GfxImageToNotificationBitmap(&rich_data->small_image, icon_mask.get()); | |
| 41 options->app_icon_mask_bitmap = icon_mask.Pass(); | |
| 42 } | |
| 43 | |
| 44 options->priority = scoped_ptr<int>(new int(rich_data->priority)); | |
| 45 | |
| 46 options->is_clickable = scoped_ptr<bool>(new bool(rich_data->clickable)); | |
| 47 | |
| 48 options->event_time = | |
| 49 scoped_ptr<double>(new double(rich_data->timestamp.ToDoubleT())); | |
| 50 | |
| 51 if (!rich_data->context_message.empty()) | |
| 52 options->context_message = scoped_ptr<std::string>( | |
| 53 new std::string(base::UTF16ToUTF8(rich_data->context_message))); | |
| 54 | |
| 55 if (!rich_data->buttons.empty()) { | |
| 56 scoped_ptr<std::vector< | |
| 57 linked_ptr<extensions::api::notifications::NotificationButton> > > | |
| 58 button_list(new std::vector< | |
| 59 linked_ptr<extensions::api::notifications::NotificationButton> >); | |
| 60 for (size_t i = 0; i < rich_data->buttons.size(); i++) { | |
| 61 linked_ptr<extensions::api::notifications::NotificationButton> button( | |
| 62 new extensions::api::notifications::NotificationButton); | |
| 63 button->title = base::UTF16ToUTF8(rich_data->buttons[i].title); | |
| 64 | |
| 65 if (!rich_data->buttons[i].icon.IsEmpty()) { | |
| 66 scoped_ptr<extensions::api::notifications::NotificationBitmap> icon( | |
| 67 new extensions::api::notifications::NotificationBitmap()); | |
| 68 GfxImageToNotificationBitmap(&rich_data->buttons[i].icon, icon.get()); | |
| 69 button->icon_bitmap = icon.Pass(); | |
| 70 } | |
| 71 button_list->push_back(button); | |
| 72 } | |
| 73 options->buttons = button_list.Pass(); | |
| 74 } | |
| 75 | |
| 76 // Only image type notifications should have images. | |
| 77 if (type == "image" && !rich_data->image.IsEmpty()) { | |
| 78 scoped_ptr<extensions::api::notifications::NotificationBitmap> image( | |
| 79 new extensions::api::notifications::NotificationBitmap()); | |
| 80 GfxImageToNotificationBitmap(¬ification.image(), image.get()); | |
| 81 options->image_bitmap = image.Pass(); | |
| 82 } else if (type != "image" && !rich_data->image.IsEmpty()) { | |
| 83 LOG(WARNING) << "Only image type notifications should have images."; | |
|
dewittj
2014/08/12 18:47:39
I am not sure that this deserves a LOG, possibly m
liyanhou
2014/08/13 17:30:37
Done.
| |
| 84 } | |
| 85 | |
| 86 // Only progress type notifications should have progress bars. | |
| 87 if (type == "progress") | |
| 88 options->progress = scoped_ptr<int>(new int(rich_data->progress)); | |
| 89 else if (rich_data->progress != 0) | |
| 90 LOG(WARNING) << "Only progress type notifications should have progress."; | |
| 91 | |
| 92 // Only list type notifications should have lists. | |
| 93 if (type == "list" && !rich_data->items.empty()) { | |
| 94 scoped_ptr<std::vector< | |
| 95 linked_ptr<extensions::api::notifications::NotificationItem> > > | |
| 96 list(new std::vector< | |
| 97 linked_ptr<extensions::api::notifications::NotificationItem> >); | |
| 98 for (size_t j = 0; j < rich_data->items.size(); j++) { | |
| 99 linked_ptr<extensions::api::notifications::NotificationItem> item( | |
| 100 new extensions::api::notifications::NotificationItem); | |
| 101 item->title = base::UTF16ToUTF8(rich_data->items[j].title); | |
| 102 item->message = base::UTF16ToUTF8(rich_data->items[j].message); | |
| 103 list->push_back(item); | |
| 104 } | |
| 105 options->items = list.Pass(); | |
| 106 } else if (type != "list" && !rich_data->items.empty()) { | |
| 107 LOG(WARNING) << "Only list type notifications should have lists."; | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 void NotificationConversionHelper::GfxImageToNotificationBitmap( | |
|
dewittj
2014/08/12 18:47:39
Please bring the reverse function from notificatio
liyanhou
2014/08/13 17:30:37
Done.
| |
| 112 const gfx::Image* gfx_image, | |
| 113 extensions::api::notifications::NotificationBitmap* notification_bitmap) { | |
| 114 SkBitmap sk_bitmap = gfx_image->AsBitmap(); | |
| 115 sk_bitmap.lockPixels(); | |
| 116 | |
| 117 notification_bitmap->width = sk_bitmap.width(); | |
| 118 notification_bitmap->height = sk_bitmap.height(); | |
| 119 int pixel_count = sk_bitmap.width() * sk_bitmap.height(); | |
| 120 const int BYTES_PER_PIXEL = 4; | |
| 121 | |
| 122 uint32_t* bitmap_pixels = sk_bitmap.getAddr32(0, 0); | |
| 123 const unsigned char* bitmap = | |
| 124 reinterpret_cast<const unsigned char*>(bitmap_pixels); | |
| 125 unsigned char* bitmap_data(new unsigned char[pixel_count * BYTES_PER_PIXEL]); | |
| 126 | |
| 127 gfx::ConvertSkiaToRGBA(bitmap, pixel_count, bitmap_data); | |
| 128 sk_bitmap.unlockPixels(); | |
| 129 | |
| 130 notification_bitmap->data = scoped_ptr<std::string>(new std::string( | |
|
dewittj
2014/08/12 18:47:39
nit: notification_bitmap->data.reset(new std::stri
liyanhou
2014/08/13 17:30:37
Done. Also changed all other occurrences in this f
| |
| 131 bitmap_data, (bitmap_data + pixel_count * BYTES_PER_PIXEL))); | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 std::string NotificationConversionHelper::MapTypeToString( | |
| 136 message_center::NotificationType type) { | |
| 137 switch (type) { | |
| 138 case message_center::NOTIFICATION_TYPE_BASE_FORMAT: | |
| 139 return "basic"; | |
| 140 case message_center::NOTIFICATION_TYPE_IMAGE: | |
| 141 return "image"; | |
| 142 case message_center::NOTIFICATION_TYPE_MULTIPLE: | |
| 143 return "list"; | |
| 144 case message_center::NOTIFICATION_TYPE_PROGRESS: | |
| 145 return "progress"; | |
| 146 default: | |
| 147 NOTREACHED(); | |
| 148 return ""; | |
| 149 } | |
| 150 } | |
| OLD | NEW |