Chromium Code Reviews| Index: chrome/browser/notifications/notification_conversion_helper.cc |
| diff --git a/chrome/browser/notifications/notification_conversion_helper.cc b/chrome/browser/notifications/notification_conversion_helper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a1fe8bb78c13861da888d7b1a0367fa9896f385c |
| --- /dev/null |
| +++ b/chrome/browser/notifications/notification_conversion_helper.cc |
| @@ -0,0 +1,150 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/notifications/notification_conversion_helper.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/extensions/api/notification_provider/notification_provider_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.
|
| +#include "chrome/common/extensions/api/notification_provider.h" |
| +#include "ui/gfx/skia_util.h" |
| + |
| +void NotificationConversionHelper::NotificationToNotificationOptions( |
| + const Notification& notification, |
| + extensions::api::notifications::NotificationOptions* options) { |
| + // Extract required fields: type, title, message, and icon. |
| + std::string type = MapTypeToString(notification.type()); |
| + options->type = extensions::api::notifications::ParseTemplateType(type); |
| + |
| + if (!notification.icon().IsEmpty()) { |
| + scoped_ptr<extensions::api::notifications::NotificationBitmap> icon( |
| + new extensions::api::notifications::NotificationBitmap()); |
| + GfxImageToNotificationBitmap(¬ification.icon(), icon.get()); |
| + options->icon_bitmap = icon.Pass(); |
| + } |
| + |
| + options->title = scoped_ptr<std::string>( |
| + new std::string(base::UTF16ToUTF8(notification.title()))); |
| + options->message = scoped_ptr<std::string>( |
| + new std::string(base::UTF16ToUTF8(notification.message()))); |
| + |
| + // Handle optional data provided. |
| + const message_center::RichNotificationData* rich_data = |
| + ¬ification.rich_notification_data(); |
| + |
| + if (!rich_data->small_image.IsEmpty()) { |
| + scoped_ptr<extensions::api::notifications::NotificationBitmap> icon_mask( |
| + new extensions::api::notifications::NotificationBitmap()); |
| + GfxImageToNotificationBitmap(&rich_data->small_image, icon_mask.get()); |
| + options->app_icon_mask_bitmap = icon_mask.Pass(); |
| + } |
| + |
| + options->priority = scoped_ptr<int>(new int(rich_data->priority)); |
| + |
| + options->is_clickable = scoped_ptr<bool>(new bool(rich_data->clickable)); |
| + |
| + options->event_time = |
| + scoped_ptr<double>(new double(rich_data->timestamp.ToDoubleT())); |
| + |
| + if (!rich_data->context_message.empty()) |
| + options->context_message = scoped_ptr<std::string>( |
| + new std::string(base::UTF16ToUTF8(rich_data->context_message))); |
| + |
| + if (!rich_data->buttons.empty()) { |
| + scoped_ptr<std::vector< |
| + linked_ptr<extensions::api::notifications::NotificationButton> > > |
| + button_list(new std::vector< |
| + linked_ptr<extensions::api::notifications::NotificationButton> >); |
| + for (size_t i = 0; i < rich_data->buttons.size(); i++) { |
| + linked_ptr<extensions::api::notifications::NotificationButton> button( |
| + new extensions::api::notifications::NotificationButton); |
| + button->title = base::UTF16ToUTF8(rich_data->buttons[i].title); |
| + |
| + if (!rich_data->buttons[i].icon.IsEmpty()) { |
| + scoped_ptr<extensions::api::notifications::NotificationBitmap> icon( |
| + new extensions::api::notifications::NotificationBitmap()); |
| + GfxImageToNotificationBitmap(&rich_data->buttons[i].icon, icon.get()); |
| + button->icon_bitmap = icon.Pass(); |
| + } |
| + button_list->push_back(button); |
| + } |
| + options->buttons = button_list.Pass(); |
| + } |
| + |
| + // Only image type notifications should have images. |
| + if (type == "image" && !rich_data->image.IsEmpty()) { |
| + scoped_ptr<extensions::api::notifications::NotificationBitmap> image( |
| + new extensions::api::notifications::NotificationBitmap()); |
| + GfxImageToNotificationBitmap(¬ification.image(), image.get()); |
| + options->image_bitmap = image.Pass(); |
| + } else if (type != "image" && !rich_data->image.IsEmpty()) { |
| + 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.
|
| + } |
| + |
| + // Only progress type notifications should have progress bars. |
| + if (type == "progress") |
| + options->progress = scoped_ptr<int>(new int(rich_data->progress)); |
| + else if (rich_data->progress != 0) |
| + LOG(WARNING) << "Only progress type notifications should have progress."; |
| + |
| + // Only list type notifications should have lists. |
| + if (type == "list" && !rich_data->items.empty()) { |
| + scoped_ptr<std::vector< |
| + linked_ptr<extensions::api::notifications::NotificationItem> > > |
| + list(new std::vector< |
| + linked_ptr<extensions::api::notifications::NotificationItem> >); |
| + for (size_t j = 0; j < rich_data->items.size(); j++) { |
| + linked_ptr<extensions::api::notifications::NotificationItem> item( |
| + new extensions::api::notifications::NotificationItem); |
| + item->title = base::UTF16ToUTF8(rich_data->items[j].title); |
| + item->message = base::UTF16ToUTF8(rich_data->items[j].message); |
| + list->push_back(item); |
| + } |
| + options->items = list.Pass(); |
| + } else if (type != "list" && !rich_data->items.empty()) { |
| + LOG(WARNING) << "Only list type notifications should have lists."; |
| + } |
| +} |
| + |
| +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.
|
| + const gfx::Image* gfx_image, |
| + extensions::api::notifications::NotificationBitmap* notification_bitmap) { |
| + SkBitmap sk_bitmap = gfx_image->AsBitmap(); |
| + sk_bitmap.lockPixels(); |
| + |
| + notification_bitmap->width = sk_bitmap.width(); |
| + notification_bitmap->height = sk_bitmap.height(); |
| + int pixel_count = sk_bitmap.width() * sk_bitmap.height(); |
| + const int BYTES_PER_PIXEL = 4; |
| + |
| + uint32_t* bitmap_pixels = sk_bitmap.getAddr32(0, 0); |
| + const unsigned char* bitmap = |
| + reinterpret_cast<const unsigned char*>(bitmap_pixels); |
| + unsigned char* bitmap_data(new unsigned char[pixel_count * BYTES_PER_PIXEL]); |
| + |
| + gfx::ConvertSkiaToRGBA(bitmap, pixel_count, bitmap_data); |
| + sk_bitmap.unlockPixels(); |
| + |
| + 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
|
| + bitmap_data, (bitmap_data + pixel_count * BYTES_PER_PIXEL))); |
| + return; |
| +} |
| + |
| +std::string NotificationConversionHelper::MapTypeToString( |
| + message_center::NotificationType type) { |
| + switch (type) { |
| + case message_center::NOTIFICATION_TYPE_BASE_FORMAT: |
| + return "basic"; |
| + case message_center::NOTIFICATION_TYPE_IMAGE: |
| + return "image"; |
| + case message_center::NOTIFICATION_TYPE_MULTIPLE: |
| + return "list"; |
| + case message_center::NOTIFICATION_TYPE_PROGRESS: |
| + return "progress"; |
| + default: |
| + NOTREACHED(); |
| + return ""; |
| + } |
| +} |