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 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 MapApiTemplateTypeToType(options->type); | 247 MapApiTemplateTypeToType(options->type); |
248 const base::string16 title(base::UTF8ToUTF16(*options->title)); | 248 const base::string16 title(base::UTF8ToUTF16(*options->title)); |
249 const base::string16 message(base::UTF8ToUTF16(*options->message)); | 249 const base::string16 message(base::UTF8ToUTF16(*options->message)); |
250 gfx::Image icon; | 250 gfx::Image icon; |
251 | 251 |
252 // TODO(dewittj): Return error if this fails. | 252 // TODO(dewittj): Return error if this fails. |
253 NotificationBitmapToGfxImage(options->icon_bitmap.get(), &icon); | 253 NotificationBitmapToGfxImage(options->icon_bitmap.get(), &icon); |
254 | 254 |
255 // Then, handle any optional data that's been provided. | 255 // Then, handle any optional data that's been provided. |
256 message_center::RichNotificationData optional_fields; | 256 message_center::RichNotificationData optional_fields; |
257 if (message_center::IsRichNotificationEnabled()) { | 257 if (options->priority.get()) |
258 if (options->priority.get()) | 258 optional_fields.priority = *options->priority; |
259 optional_fields.priority = *options->priority; | |
260 | 259 |
261 if (options->event_time.get()) | 260 if (options->event_time.get()) |
262 optional_fields.timestamp = base::Time::FromJsTime(*options->event_time); | 261 optional_fields.timestamp = base::Time::FromJsTime(*options->event_time); |
263 | 262 |
264 if (options->buttons.get()) { | 263 if (options->buttons.get()) { |
265 // Currently we allow up to 2 buttons. | 264 // Currently we allow up to 2 buttons. |
266 size_t number_of_buttons = options->buttons->size(); | 265 size_t number_of_buttons = options->buttons->size(); |
267 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; | 266 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; |
268 | 267 |
269 for (size_t i = 0; i < number_of_buttons; i++) { | 268 for (size_t i = 0; i < number_of_buttons; i++) { |
270 message_center::ButtonInfo info( | 269 message_center::ButtonInfo info( |
271 base::UTF8ToUTF16((*options->buttons)[i]->title)); | 270 base::UTF8ToUTF16((*options->buttons)[i]->title)); |
272 NotificationBitmapToGfxImage((*options->buttons)[i]->icon_bitmap.get(), | 271 NotificationBitmapToGfxImage((*options->buttons)[i]->icon_bitmap.get(), |
273 &info.icon); | 272 &info.icon); |
274 optional_fields.buttons.push_back(info); | 273 optional_fields.buttons.push_back(info); |
275 } | |
276 } | |
277 | |
278 if (options->context_message) { | |
279 optional_fields.context_message = | |
280 base::UTF8ToUTF16(*options->context_message); | |
281 } | |
282 | |
283 bool has_image = NotificationBitmapToGfxImage(options->image_bitmap.get(), | |
284 &optional_fields.image); | |
285 // We should have an image if and only if the type is an image type. | |
286 if (has_image != (type == message_center::NOTIFICATION_TYPE_IMAGE)) | |
287 return false; | |
288 | |
289 // We should have list items if and only if the type is a multiple type. | |
290 bool has_list_items = options->items.get() && options->items->size() > 0; | |
291 if (has_list_items != (type == message_center::NOTIFICATION_TYPE_MULTIPLE)) | |
292 return false; | |
293 | |
294 if (options->progress.get() != NULL) { | |
295 // We should have progress if and only if the type is a progress type. | |
296 if (type != message_center::NOTIFICATION_TYPE_PROGRESS) { | |
297 SetError(kUnexpectedProgressValueForNonProgressType); | |
298 return false; | |
299 } | |
300 optional_fields.progress = *options->progress; | |
301 // Progress value should range from 0 to 100. | |
302 if (optional_fields.progress < 0 || optional_fields.progress > 100) { | |
303 SetError(kInvalidProgressValue); | |
304 return false; | |
305 } | |
306 } | |
307 | |
308 if (has_list_items) { | |
309 using api::notifications::NotificationItem; | |
310 std::vector<linked_ptr<NotificationItem> >::iterator i; | |
311 for (i = options->items->begin(); i != options->items->end(); ++i) { | |
312 message_center::NotificationItem item( | |
313 base::UTF8ToUTF16(i->get()->title), | |
314 base::UTF8ToUTF16(i->get()->message)); | |
315 optional_fields.items.push_back(item); | |
316 } | |
317 } | 274 } |
318 } | 275 } |
319 | 276 |
| 277 if (options->context_message) { |
| 278 optional_fields.context_message = |
| 279 base::UTF8ToUTF16(*options->context_message); |
| 280 } |
| 281 |
| 282 bool has_image = NotificationBitmapToGfxImage(options->image_bitmap.get(), |
| 283 &optional_fields.image); |
| 284 // We should have an image if and only if the type is an image type. |
| 285 if (has_image != (type == message_center::NOTIFICATION_TYPE_IMAGE)) |
| 286 return false; |
| 287 |
| 288 // We should have list items if and only if the type is a multiple type. |
| 289 bool has_list_items = options->items.get() && options->items->size() > 0; |
| 290 if (has_list_items != (type == message_center::NOTIFICATION_TYPE_MULTIPLE)) |
| 291 return false; |
| 292 |
| 293 if (options->progress.get() != NULL) { |
| 294 // We should have progress if and only if the type is a progress type. |
| 295 if (type != message_center::NOTIFICATION_TYPE_PROGRESS) { |
| 296 SetError(kUnexpectedProgressValueForNonProgressType); |
| 297 return false; |
| 298 } |
| 299 optional_fields.progress = *options->progress; |
| 300 // Progress value should range from 0 to 100. |
| 301 if (optional_fields.progress < 0 || optional_fields.progress > 100) { |
| 302 SetError(kInvalidProgressValue); |
| 303 return false; |
| 304 } |
| 305 } |
| 306 |
| 307 if (has_list_items) { |
| 308 using api::notifications::NotificationItem; |
| 309 std::vector<linked_ptr<NotificationItem> >::iterator i; |
| 310 for (i = options->items->begin(); i != options->items->end(); ++i) { |
| 311 message_center::NotificationItem item( |
| 312 base::UTF8ToUTF16(i->get()->title), |
| 313 base::UTF8ToUTF16(i->get()->message)); |
| 314 optional_fields.items.push_back(item); |
| 315 } |
| 316 } |
| 317 |
320 if (options->is_clickable.get()) | 318 if (options->is_clickable.get()) |
321 optional_fields.clickable = *options->is_clickable; | 319 optional_fields.clickable = *options->is_clickable; |
322 | 320 |
323 NotificationsApiDelegate* api_delegate(new NotificationsApiDelegate( | 321 NotificationsApiDelegate* api_delegate(new NotificationsApiDelegate( |
324 this, GetProfile(), extension_->id(), id)); // ownership is passed to | 322 this, GetProfile(), extension_->id(), id)); // ownership is passed to |
325 // Notification | 323 // Notification |
326 Notification notification(type, | 324 Notification notification(type, |
327 extension_->url(), | 325 extension_->url(), |
328 title, | 326 title, |
329 message, | 327 message, |
(...skipping 23 matching lines...) Expand all Loading... |
353 if (options->message) | 351 if (options->message) |
354 notification->set_message(base::UTF8ToUTF16(*options->message)); | 352 notification->set_message(base::UTF8ToUTF16(*options->message)); |
355 | 353 |
356 // TODO(dewittj): Return error if this fails. | 354 // TODO(dewittj): Return error if this fails. |
357 if (options->icon_bitmap) { | 355 if (options->icon_bitmap) { |
358 gfx::Image icon; | 356 gfx::Image icon; |
359 NotificationBitmapToGfxImage(options->icon_bitmap.get(), &icon); | 357 NotificationBitmapToGfxImage(options->icon_bitmap.get(), &icon); |
360 notification->set_icon(icon); | 358 notification->set_icon(icon); |
361 } | 359 } |
362 | 360 |
363 if (message_center::IsRichNotificationEnabled()) { | 361 if (options->priority) |
364 if (options->priority) | 362 notification->set_priority(*options->priority); |
365 notification->set_priority(*options->priority); | |
366 | 363 |
367 if (options->event_time) | 364 if (options->event_time) |
368 notification->set_timestamp(base::Time::FromJsTime(*options->event_time)); | 365 notification->set_timestamp(base::Time::FromJsTime(*options->event_time)); |
369 | 366 |
370 if (options->buttons) { | 367 if (options->buttons) { |
371 // Currently we allow up to 2 buttons. | 368 // Currently we allow up to 2 buttons. |
372 size_t number_of_buttons = options->buttons->size(); | 369 size_t number_of_buttons = options->buttons->size(); |
373 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; | 370 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; |
374 | 371 |
375 std::vector<message_center::ButtonInfo> buttons; | 372 std::vector<message_center::ButtonInfo> buttons; |
376 for (size_t i = 0; i < number_of_buttons; i++) { | 373 for (size_t i = 0; i < number_of_buttons; i++) { |
377 message_center::ButtonInfo button( | 374 message_center::ButtonInfo button( |
378 base::UTF8ToUTF16((*options->buttons)[i]->title)); | 375 base::UTF8ToUTF16((*options->buttons)[i]->title)); |
379 NotificationBitmapToGfxImage((*options->buttons)[i]->icon_bitmap.get(), | 376 NotificationBitmapToGfxImage((*options->buttons)[i]->icon_bitmap.get(), |
380 &button.icon); | 377 &button.icon); |
381 buttons.push_back(button); | 378 buttons.push_back(button); |
382 } | |
383 notification->set_buttons(buttons); | |
384 } | 379 } |
| 380 notification->set_buttons(buttons); |
| 381 } |
385 | 382 |
386 if (options->context_message) { | 383 if (options->context_message) { |
387 notification->set_context_message( | 384 notification->set_context_message( |
388 base::UTF8ToUTF16(*options->context_message)); | 385 base::UTF8ToUTF16(*options->context_message)); |
| 386 } |
| 387 |
| 388 gfx::Image image; |
| 389 if (NotificationBitmapToGfxImage(options->image_bitmap.get(), &image)) { |
| 390 // We should have an image if and only if the type is an image type. |
| 391 if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) |
| 392 return false; |
| 393 notification->set_image(image); |
| 394 } |
| 395 |
| 396 if (options->progress) { |
| 397 // We should have progress if and only if the type is a progress type. |
| 398 if (notification->type() != message_center::NOTIFICATION_TYPE_PROGRESS) { |
| 399 SetError(kUnexpectedProgressValueForNonProgressType); |
| 400 return false; |
389 } | 401 } |
| 402 int progress = *options->progress; |
| 403 // Progress value should range from 0 to 100. |
| 404 if (progress < 0 || progress > 100) { |
| 405 SetError(kInvalidProgressValue); |
| 406 return false; |
| 407 } |
| 408 notification->set_progress(progress); |
| 409 } |
390 | 410 |
391 gfx::Image image; | 411 if (options->items.get() && options->items->size() > 0) { |
392 if (NotificationBitmapToGfxImage(options->image_bitmap.get(), &image)) { | 412 // We should have list items if and only if the type is a multiple type. |
393 // We should have an image if and only if the type is an image type. | 413 if (notification->type() != message_center::NOTIFICATION_TYPE_MULTIPLE) |
394 if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) | 414 return false; |
395 return false; | 415 |
396 notification->set_image(image); | 416 std::vector<message_center::NotificationItem> items; |
| 417 using api::notifications::NotificationItem; |
| 418 std::vector<linked_ptr<NotificationItem> >::iterator i; |
| 419 for (i = options->items->begin(); i != options->items->end(); ++i) { |
| 420 message_center::NotificationItem item( |
| 421 base::UTF8ToUTF16(i->get()->title), |
| 422 base::UTF8ToUTF16(i->get()->message)); |
| 423 items.push_back(item); |
397 } | 424 } |
398 | 425 notification->set_items(items); |
399 if (options->progress) { | |
400 // We should have progress if and only if the type is a progress type. | |
401 if (notification->type() != message_center::NOTIFICATION_TYPE_PROGRESS) { | |
402 SetError(kUnexpectedProgressValueForNonProgressType); | |
403 return false; | |
404 } | |
405 int progress = *options->progress; | |
406 // Progress value should range from 0 to 100. | |
407 if (progress < 0 || progress > 100) { | |
408 SetError(kInvalidProgressValue); | |
409 return false; | |
410 } | |
411 notification->set_progress(progress); | |
412 } | |
413 | |
414 if (options->items.get() && options->items->size() > 0) { | |
415 // We should have list items if and only if the type is a multiple type. | |
416 if (notification->type() != message_center::NOTIFICATION_TYPE_MULTIPLE) | |
417 return false; | |
418 | |
419 std::vector<message_center::NotificationItem> items; | |
420 using api::notifications::NotificationItem; | |
421 std::vector<linked_ptr<NotificationItem> >::iterator i; | |
422 for (i = options->items->begin(); i != options->items->end(); ++i) { | |
423 message_center::NotificationItem item( | |
424 base::UTF8ToUTF16(i->get()->title), | |
425 base::UTF8ToUTF16(i->get()->message)); | |
426 items.push_back(item); | |
427 } | |
428 notification->set_items(items); | |
429 } | |
430 } | 426 } |
431 | 427 |
432 // Then override if it's already set. | 428 // Then override if it's already set. |
433 if (options->is_clickable.get()) | 429 if (options->is_clickable.get()) |
434 notification->set_clickable(*options->is_clickable); | 430 notification->set_clickable(*options->is_clickable); |
435 | 431 |
436 g_browser_process->notification_ui_manager()->Update(*notification, | 432 g_browser_process->notification_ui_manager()->Update(*notification, |
437 GetProfile()); | 433 GetProfile()); |
438 return true; | 434 return true; |
439 } | 435 } |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 ? api::notifications::PERMISSION_LEVEL_GRANTED | 612 ? api::notifications::PERMISSION_LEVEL_GRANTED |
617 : api::notifications::PERMISSION_LEVEL_DENIED; | 613 : api::notifications::PERMISSION_LEVEL_DENIED; |
618 | 614 |
619 SetResult(new base::StringValue(api::notifications::ToString(result))); | 615 SetResult(new base::StringValue(api::notifications::ToString(result))); |
620 SendResponse(true); | 616 SendResponse(true); |
621 | 617 |
622 return true; | 618 return true; |
623 } | 619 } |
624 | 620 |
625 } // namespace extensions | 621 } // namespace extensions |
OLD | NEW |