| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/notifications/notification_conversion_helper.h" | 5 #include "chrome/browser/notifications/notification_conversion_helper.h" |
| 6 | 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 7 #include "base/logging.h" | 10 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/stl_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/common/extensions/api/notification_provider.h" | 14 #include "chrome/common/extensions/api/notification_provider.h" |
| 11 #include "chrome/common/extensions/api/notifications/notification_style.h" | 15 #include "chrome/common/extensions/api/notifications/notification_style.h" |
| 12 #include "ui/gfx/image/image_skia.h" | 16 #include "ui/gfx/image/image_skia.h" |
| 13 #include "ui/gfx/image/image_skia_rep.h" | 17 #include "ui/gfx/image/image_skia_rep.h" |
| 14 #include "ui/gfx/skia_util.h" | 18 #include "ui/gfx/skia_util.h" |
| 15 | 19 |
| 16 void NotificationConversionHelper::NotificationToNotificationOptions( | 20 void NotificationConversionHelper::NotificationToNotificationOptions( |
| 17 const Notification& notification, | 21 const Notification& notification, |
| 18 extensions::api::notifications::NotificationOptions* options) { | 22 extensions::api::notifications::NotificationOptions* options) { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 sk_bitmap.lockPixels(); | 120 sk_bitmap.lockPixels(); |
| 117 | 121 |
| 118 notification_bitmap->width = sk_bitmap.width(); | 122 notification_bitmap->width = sk_bitmap.width(); |
| 119 notification_bitmap->height = sk_bitmap.height(); | 123 notification_bitmap->height = sk_bitmap.height(); |
| 120 int pixel_count = sk_bitmap.width() * sk_bitmap.height(); | 124 int pixel_count = sk_bitmap.width() * sk_bitmap.height(); |
| 121 const int BYTES_PER_PIXEL = 4; | 125 const int BYTES_PER_PIXEL = 4; |
| 122 | 126 |
| 123 uint32_t* bitmap_pixels = sk_bitmap.getAddr32(0, 0); | 127 uint32_t* bitmap_pixels = sk_bitmap.getAddr32(0, 0); |
| 124 const unsigned char* bitmap = | 128 const unsigned char* bitmap = |
| 125 reinterpret_cast<const unsigned char*>(bitmap_pixels); | 129 reinterpret_cast<const unsigned char*>(bitmap_pixels); |
| 126 scoped_ptr<unsigned char[]> rgba_bitmap_data( | 130 scoped_ptr<std::vector<char>> rgba_bitmap_data( |
| 127 new unsigned char[pixel_count * BYTES_PER_PIXEL]); | 131 new std::vector<char>(pixel_count * BYTES_PER_PIXEL)); |
| 128 | 132 |
| 129 gfx::ConvertSkiaToRGBA(bitmap, pixel_count, rgba_bitmap_data.get()); | 133 gfx::ConvertSkiaToRGBA(bitmap, pixel_count, |
| 134 reinterpret_cast<unsigned char*>( |
| 135 vector_as_array(rgba_bitmap_data.get()))); |
| 130 sk_bitmap.unlockPixels(); | 136 sk_bitmap.unlockPixels(); |
| 131 | 137 |
| 132 notification_bitmap->data.reset(new std::string( | 138 notification_bitmap->data = rgba_bitmap_data.Pass(); |
| 133 rgba_bitmap_data.get(), | |
| 134 (rgba_bitmap_data.get() + pixel_count * BYTES_PER_PIXEL))); | |
| 135 return; | 139 return; |
| 136 } | 140 } |
| 137 | 141 |
| 138 bool NotificationConversionHelper::NotificationBitmapToGfxImage( | 142 bool NotificationConversionHelper::NotificationBitmapToGfxImage( |
| 139 float max_scale, | 143 float max_scale, |
| 140 const gfx::Size& target_size_dips, | 144 const gfx::Size& target_size_dips, |
| 141 extensions::api::notifications::NotificationBitmap* notification_bitmap, | 145 extensions::api::notifications::NotificationBitmap* notification_bitmap, |
| 142 gfx::Image* return_image) { | 146 gfx::Image* return_image) { |
| 143 if (!notification_bitmap) | 147 if (!notification_bitmap) |
| 144 return false; | 148 return false; |
| 145 | 149 |
| 146 const int max_device_pixel_width = target_size_dips.width() * max_scale; | 150 const int max_device_pixel_width = target_size_dips.width() * max_scale; |
| 147 const int max_device_pixel_height = target_size_dips.height() * max_scale; | 151 const int max_device_pixel_height = target_size_dips.height() * max_scale; |
| 148 | 152 |
| 149 const int BYTES_PER_PIXEL = 4; | 153 const int BYTES_PER_PIXEL = 4; |
| 150 | 154 |
| 151 const int width = notification_bitmap->width; | 155 const int width = notification_bitmap->width; |
| 152 const int height = notification_bitmap->height; | 156 const int height = notification_bitmap->height; |
| 153 | 157 |
| 154 if (width < 0 || height < 0 || width > max_device_pixel_width || | 158 if (width < 0 || height < 0 || width > max_device_pixel_width || |
| 155 height > max_device_pixel_height) | 159 height > max_device_pixel_height) |
| 156 return false; | 160 return false; |
| 157 | 161 |
| 158 // Ensure we have rgba data. | 162 // Ensure we have rgba data. |
| 159 std::string* rgba_data = notification_bitmap->data.get(); | 163 std::vector<char>* rgba_data = notification_bitmap->data.get(); |
| 160 if (!rgba_data) | 164 if (!rgba_data) |
| 161 return false; | 165 return false; |
| 162 | 166 |
| 163 const size_t rgba_data_length = rgba_data->length(); | 167 const size_t rgba_data_length = rgba_data->size(); |
| 164 const size_t rgba_area = width * height; | 168 const size_t rgba_area = width * height; |
| 165 | 169 |
| 166 if (rgba_data_length != rgba_area * BYTES_PER_PIXEL) | 170 if (rgba_data_length != rgba_area * BYTES_PER_PIXEL) |
| 167 return false; | 171 return false; |
| 168 | 172 |
| 169 SkBitmap bitmap; | 173 SkBitmap bitmap; |
| 170 // Allocate the actual backing store with the sanitized dimensions. | 174 // Allocate the actual backing store with the sanitized dimensions. |
| 171 if (!bitmap.tryAllocN32Pixels(width, height)) | 175 if (!bitmap.tryAllocN32Pixels(width, height)) |
| 172 return false; | 176 return false; |
| 173 | 177 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 204 return "image"; | 208 return "image"; |
| 205 case message_center::NOTIFICATION_TYPE_MULTIPLE: | 209 case message_center::NOTIFICATION_TYPE_MULTIPLE: |
| 206 return "list"; | 210 return "list"; |
| 207 case message_center::NOTIFICATION_TYPE_PROGRESS: | 211 case message_center::NOTIFICATION_TYPE_PROGRESS: |
| 208 return "progress"; | 212 return "progress"; |
| 209 default: | 213 default: |
| 210 NOTREACHED(); | 214 NOTREACHED(); |
| 211 return ""; | 215 return ""; |
| 212 } | 216 } |
| 213 } | 217 } |
| OLD | NEW |