Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/notifications/notifications_api.h" | 5 #include "chrome/browser/extensions/api/notifications/notifications_api.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/guid.h" | 8 #include "base/guid.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 float image_scale = | 176 float image_scale = |
| 177 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactors().back()); | 177 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactors().back()); |
| 178 | 178 |
| 179 // Extract required fields: type, title, message, and icon. | 179 // Extract required fields: type, title, message, and icon. |
| 180 message_center::NotificationType type = | 180 message_center::NotificationType type = |
| 181 MapApiTemplateTypeToType(options->type); | 181 MapApiTemplateTypeToType(options->type); |
| 182 const base::string16 title(base::UTF8ToUTF16(*options->title)); | 182 const base::string16 title(base::UTF8ToUTF16(*options->title)); |
| 183 const base::string16 message(base::UTF8ToUTF16(*options->message)); | 183 const base::string16 message(base::UTF8ToUTF16(*options->message)); |
| 184 gfx::Image icon; | 184 gfx::Image icon; |
| 185 | 185 |
| 186 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | 186 if (options->icon_bitmap.get() && |
|
dewittj
2015/06/01 16:00:57
this line should read
if (!options->icon_bitmap.g
Deepak
2015/06/02 07:36:35
correct, I agree with you.
| |
| 187 !NotificationConversionHelper::NotificationBitmapToGfxImage( | |
| 187 image_scale, | 188 image_scale, |
| 188 bitmap_sizes.icon_size, | 189 bitmap_sizes.icon_size, |
| 189 options->icon_bitmap.get(), | 190 *options->icon_bitmap, |
| 190 &icon)) { | 191 &icon)) { |
| 191 SetError(kUnableToDecodeIconError); | 192 SetError(kUnableToDecodeIconError); |
| 192 return false; | 193 return false; |
| 193 } | 194 } |
| 194 | 195 |
| 195 // Then, handle any optional data that's been provided. | 196 // Then, handle any optional data that's been provided. |
| 196 message_center::RichNotificationData optional_fields; | 197 message_center::RichNotificationData optional_fields; |
| 197 if (options->app_icon_mask_url.get()) { | 198 if (options->app_icon_mask_bitmap.get() && |
| 198 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | 199 !NotificationConversionHelper::NotificationBitmapToGfxImage( |
| 199 image_scale, | 200 image_scale, |
| 200 bitmap_sizes.app_icon_mask_size, | 201 bitmap_sizes.app_icon_mask_size, |
| 201 options->app_icon_mask_bitmap.get(), | 202 *options->app_icon_mask_bitmap, |
| 202 &optional_fields.small_image)) { | 203 &optional_fields.small_image)) { |
| 203 SetError(kUnableToDecodeIconError); | 204 SetError(kUnableToDecodeIconError); |
| 204 return false; | 205 return false; |
| 205 } | |
| 206 } | 206 } |
| 207 | 207 |
| 208 if (options->priority.get()) | 208 if (options->priority.get()) |
| 209 optional_fields.priority = *options->priority; | 209 optional_fields.priority = *options->priority; |
| 210 | 210 |
| 211 if (options->event_time.get()) | 211 if (options->event_time.get()) |
| 212 optional_fields.timestamp = base::Time::FromJsTime(*options->event_time); | 212 optional_fields.timestamp = base::Time::FromJsTime(*options->event_time); |
| 213 | 213 |
| 214 if (options->buttons.get()) { | 214 if (options->buttons.get()) { |
| 215 // Currently we allow up to 2 buttons. | 215 // Currently we allow up to 2 buttons. |
| 216 size_t number_of_buttons = options->buttons->size(); | 216 size_t number_of_buttons = options->buttons->size(); |
| 217 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; | 217 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; |
| 218 | 218 |
| 219 for (size_t i = 0; i < number_of_buttons; i++) { | 219 for (size_t i = 0; i < number_of_buttons; i++) { |
| 220 message_center::ButtonInfo info( | 220 message_center::ButtonInfo info( |
| 221 base::UTF8ToUTF16((*options->buttons)[i]->title)); | 221 base::UTF8ToUTF16((*options->buttons)[i]->title)); |
| 222 NotificationConversionHelper::NotificationBitmapToGfxImage( | 222 if ((*options->buttons)[i]->icon_bitmap.get()) { |
| 223 image_scale, | 223 NotificationConversionHelper::NotificationBitmapToGfxImage( |
| 224 bitmap_sizes.button_icon_size, | 224 image_scale, |
| 225 (*options->buttons)[i]->icon_bitmap.get(), | 225 bitmap_sizes.button_icon_size, |
| 226 &info.icon); | 226 *(*options->buttons)[i]->icon_bitmap, |
| 227 optional_fields.buttons.push_back(info); | 227 &info.icon); |
| 228 optional_fields.buttons.push_back(info); | |
| 229 } | |
| 228 } | 230 } |
| 229 } | 231 } |
| 230 | 232 |
| 231 if (options->context_message) { | 233 if (options->context_message) { |
| 232 optional_fields.context_message = | 234 optional_fields.context_message = |
| 233 base::UTF8ToUTF16(*options->context_message); | 235 base::UTF8ToUTF16(*options->context_message); |
| 234 } | 236 } |
| 235 | 237 |
| 236 bool has_image = NotificationConversionHelper::NotificationBitmapToGfxImage( | 238 bool has_image; |
| 237 image_scale, | 239 if (!options->image_bitmap.get()) { |
| 238 bitmap_sizes.image_size, | 240 has_image = false; |
| 239 options->image_bitmap.get(), | 241 } else { |
| 240 &optional_fields.image); | 242 has_image = NotificationConversionHelper::NotificationBitmapToGfxImage( |
| 243 image_scale, | |
| 244 bitmap_sizes.image_size, | |
| 245 *options->image_bitmap, | |
| 246 &optional_fields.image); | |
| 247 } | |
| 241 // We should have an image if and only if the type is an image type. | 248 // We should have an image if and only if the type is an image type. |
| 242 if (has_image != (type == message_center::NOTIFICATION_TYPE_IMAGE)) { | 249 if (has_image != (type == message_center::NOTIFICATION_TYPE_IMAGE)) { |
| 243 SetError(kExtraImageProvided); | 250 SetError(kExtraImageProvided); |
| 244 return false; | 251 return false; |
| 245 } | 252 } |
| 246 | 253 |
| 247 // We should have list items if and only if the type is a multiple type. | 254 // We should have list items if and only if the type is a multiple type. |
| 248 bool has_list_items = options->items.get() && options->items->size() > 0; | 255 bool has_list_items = options->items.get() && options->items->size() > 0; |
| 249 if (has_list_items != (type == message_center::NOTIFICATION_TYPE_MULTIPLE)) { | 256 if (has_list_items != (type == message_center::NOTIFICATION_TYPE_MULTIPLE)) { |
| 250 SetError(kExtraListItemsProvided); | 257 SetError(kExtraListItemsProvided); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactors().back()); | 315 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactors().back()); |
| 309 | 316 |
| 310 // Update optional fields if provided. | 317 // Update optional fields if provided. |
| 311 if (options->type != api::notifications::TEMPLATE_TYPE_NONE) | 318 if (options->type != api::notifications::TEMPLATE_TYPE_NONE) |
| 312 notification->set_type(MapApiTemplateTypeToType(options->type)); | 319 notification->set_type(MapApiTemplateTypeToType(options->type)); |
| 313 if (options->title) | 320 if (options->title) |
| 314 notification->set_title(base::UTF8ToUTF16(*options->title)); | 321 notification->set_title(base::UTF8ToUTF16(*options->title)); |
| 315 if (options->message) | 322 if (options->message) |
| 316 notification->set_message(base::UTF8ToUTF16(*options->message)); | 323 notification->set_message(base::UTF8ToUTF16(*options->message)); |
| 317 | 324 |
| 318 // TODO(dewittj): Return error if this fails. | 325 if (options->icon_bitmap.get()) { |
| 319 if (options->icon_bitmap) { | |
| 320 gfx::Image icon; | 326 gfx::Image icon; |
| 321 NotificationConversionHelper::NotificationBitmapToGfxImage( | 327 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( |
| 322 image_scale, bitmap_sizes.icon_size, options->icon_bitmap.get(), &icon); | 328 image_scale, |
| 329 bitmap_sizes.icon_size, | |
| 330 *options->icon_bitmap, | |
| 331 &icon)) { | |
| 332 SetError(kUnableToDecodeIconError); | |
| 333 return false; | |
| 334 } | |
| 323 notification->set_icon(icon); | 335 notification->set_icon(icon); |
| 324 } | 336 } |
| 325 | 337 |
| 326 gfx::Image app_icon_mask; | 338 if (options->app_icon_mask_bitmap.get()) { |
| 327 if (NotificationConversionHelper::NotificationBitmapToGfxImage( | 339 gfx::Image app_icon_mask; |
| 340 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | |
| 328 image_scale, | 341 image_scale, |
| 329 bitmap_sizes.app_icon_mask_size, | 342 bitmap_sizes.app_icon_mask_size, |
| 330 options->app_icon_mask_bitmap.get(), | 343 *options->app_icon_mask_bitmap, |
| 331 &app_icon_mask)) { | 344 &app_icon_mask)) { |
| 345 SetError(kUnableToDecodeIconError); | |
| 346 return false; | |
| 347 } | |
| 332 notification->set_small_image(app_icon_mask); | 348 notification->set_small_image(app_icon_mask); |
| 333 } | 349 } |
| 334 | 350 |
| 335 if (options->priority) | 351 if (options->priority) |
| 336 notification->set_priority(*options->priority); | 352 notification->set_priority(*options->priority); |
| 337 | 353 |
| 338 if (options->event_time) | 354 if (options->event_time) |
| 339 notification->set_timestamp(base::Time::FromJsTime(*options->event_time)); | 355 notification->set_timestamp(base::Time::FromJsTime(*options->event_time)); |
| 340 | 356 |
| 341 if (options->buttons) { | 357 if (options->buttons) { |
| 342 // Currently we allow up to 2 buttons. | 358 // Currently we allow up to 2 buttons. |
| 343 size_t number_of_buttons = options->buttons->size(); | 359 size_t number_of_buttons = options->buttons->size(); |
| 344 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; | 360 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; |
| 345 | 361 |
| 346 std::vector<message_center::ButtonInfo> buttons; | 362 std::vector<message_center::ButtonInfo> buttons; |
| 347 for (size_t i = 0; i < number_of_buttons; i++) { | 363 for (size_t i = 0; i < number_of_buttons; i++) { |
| 348 message_center::ButtonInfo button( | 364 message_center::ButtonInfo button( |
| 349 base::UTF8ToUTF16((*options->buttons)[i]->title)); | 365 base::UTF8ToUTF16((*options->buttons)[i]->title)); |
| 350 NotificationConversionHelper::NotificationBitmapToGfxImage( | 366 if ((*options->buttons)[i]->icon_bitmap.get()) { |
| 351 image_scale, | 367 NotificationConversionHelper::NotificationBitmapToGfxImage( |
| 352 bitmap_sizes.button_icon_size, | 368 image_scale, |
| 353 (*options->buttons)[i]->icon_bitmap.get(), | 369 bitmap_sizes.button_icon_size, |
| 354 &button.icon); | 370 *(*options->buttons)[i]->icon_bitmap, |
|
dewittj
2015/06/01 16:00:57
please pull this out into its own variable, all th
| |
| 355 buttons.push_back(button); | 371 &button.icon); |
| 372 buttons.push_back(button); | |
| 373 } | |
| 356 } | 374 } |
| 357 notification->set_buttons(buttons); | 375 notification->set_buttons(buttons); |
| 358 } | 376 } |
| 359 | 377 |
| 360 if (options->context_message) { | 378 if (options->context_message) { |
| 361 notification->set_context_message( | 379 notification->set_context_message( |
| 362 base::UTF8ToUTF16(*options->context_message)); | 380 base::UTF8ToUTF16(*options->context_message)); |
| 363 } | 381 } |
| 364 | 382 |
| 365 gfx::Image image; | 383 gfx::Image image; |
| 366 bool has_image = NotificationConversionHelper::NotificationBitmapToGfxImage( | 384 bool has_image; |
|
dewittj
2015/06/01 16:00:57
This code is a little convoluted. Possibly shorte
Deepak
2015/06/02 07:36:35
Done.
| |
| 367 image_scale, | 385 if (!options->image_bitmap.get()) { |
| 368 bitmap_sizes.image_size, | 386 has_image = false; |
| 369 options->image_bitmap.get(), | 387 } else { |
| 370 &image); | 388 has_image = NotificationConversionHelper::NotificationBitmapToGfxImage( |
| 389 image_scale, | |
| 390 bitmap_sizes.image_size, | |
| 391 *options->image_bitmap, | |
| 392 &image); | |
| 393 } | |
| 371 if (has_image) { | 394 if (has_image) { |
| 372 // We should have an image if and only if the type is an image type. | 395 // We should have an image if and only if the type is an image type. |
| 373 if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) { | 396 if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) { |
| 374 SetError(kExtraImageProvided); | 397 SetError(kExtraImageProvided); |
| 375 return false; | 398 return false; |
| 376 } | 399 } |
| 377 notification->set_image(image); | 400 notification->set_image(image); |
| 378 } | 401 } |
| 379 | 402 |
| 380 if (options->progress) { | 403 if (options->progress) { |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 600 ? api::notifications::PERMISSION_LEVEL_GRANTED | 623 ? api::notifications::PERMISSION_LEVEL_GRANTED |
| 601 : api::notifications::PERMISSION_LEVEL_DENIED; | 624 : api::notifications::PERMISSION_LEVEL_DENIED; |
| 602 | 625 |
| 603 SetResult(new base::StringValue(api::notifications::ToString(result))); | 626 SetResult(new base::StringValue(api::notifications::ToString(result))); |
| 604 SendResponse(true); | 627 SendResponse(true); |
| 605 | 628 |
| 606 return true; | 629 return true; |
| 607 } | 630 } |
| 608 | 631 |
| 609 } // namespace extensions | 632 } // namespace extensions |
| OLD | NEW |