Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2467)

Unified Diff: chrome/browser/notifications/notification_conversion_helper.cc

Issue 441753002: Route newly created notifications to notification provider API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: bug fix Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..09f01b91160dedc34f5cac6dd100e8f679eeb1d8
--- /dev/null
+++ b/chrome/browser/notifications/notification_conversion_helper.cc
@@ -0,0 +1,155 @@
+// 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"
+#include "chrome/common/extensions/api/notification_provider.h"
+#include "ui/gfx/skia_util.h"
+
+NotificationConversionHelper::NotificationConversionHelper() {
+}
+
+NotificationConversionHelper::~NotificationConversionHelper() {
+}
+
+// This function converts Notification::Notification data to
+// extensions::api::notifications::NotificationOptions
+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(&notification.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 =
+ &notification.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));
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
+
+ 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(&notification.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.";
+ }
+
+ // 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/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
+ const gfx::Image* gfx_image,
+ extensions::api::notifications::NotificationBitmap* notification_bitmap) {
+ SkBitmap sk_bitmap = gfx_image->AsBitmap();
+ 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.
+
+ notification_bitmap->width = sk_bitmap.width();
+ notification_bitmap->height = sk_bitmap.height();
+ 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.
+
+ 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_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.
+
+ gfx::ConvertSkiaToRGBA(bitmap, pixel_width, bitmap_data);
+
+ notification_bitmap->data = scoped_ptr<std::string>(
+ new std::string(bitmap_data, (bitmap_data + pixel_width * 4)));
+ 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:
+ 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.
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698