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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/guid.h" | 12 #include "base/guid.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" |
14 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
15 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
19 #include "build/build_config.h" | 20 #include "build/build_config.h" |
20 #include "chrome/browser/browser_process.h" | 21 #include "chrome/browser/browser_process.h" |
21 #include "chrome/browser/notifications/notification.h" | 22 #include "chrome/browser/notifications/notification.h" |
22 #include "chrome/browser/notifications/notification_conversion_helper.h" | 23 #include "chrome/browser/notifications/notification_conversion_helper.h" |
23 #include "chrome/browser/notifications/notification_ui_manager.h" | 24 #include "chrome/browser/notifications/notification_ui_manager.h" |
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 // If the caller provided a notificationId, use that. | 600 // If the caller provided a notificationId, use that. |
600 notification_id = *params_->notification_id; | 601 notification_id = *params_->notification_id; |
601 } else { | 602 } else { |
602 // Otherwise, use a randomly created GUID. In case that GenerateGUID returns | 603 // Otherwise, use a randomly created GUID. In case that GenerateGUID returns |
603 // the empty string, simply generate a random string. | 604 // the empty string, simply generate a random string. |
604 notification_id = base::GenerateGUID(); | 605 notification_id = base::GenerateGUID(); |
605 if (notification_id.empty()) | 606 if (notification_id.empty()) |
606 notification_id = base::RandBytesAsString(16); | 607 notification_id = base::RandBytesAsString(16); |
607 } | 608 } |
608 | 609 |
609 SetResult(new base::StringValue(notification_id)); | 610 SetResult(base::MakeUnique<base::StringValue>(notification_id)); |
610 | 611 |
611 // TODO(dewittj): Add more human-readable error strings if this fails. | 612 // TODO(dewittj): Add more human-readable error strings if this fails. |
612 if (!CreateNotification(notification_id, ¶ms_->options)) | 613 if (!CreateNotification(notification_id, ¶ms_->options)) |
613 return false; | 614 return false; |
614 | 615 |
615 SendResponse(true); | 616 SendResponse(true); |
616 | 617 |
617 return true; | 618 return true; |
618 } | 619 } |
619 | 620 |
620 NotificationsUpdateFunction::NotificationsUpdateFunction() { | 621 NotificationsUpdateFunction::NotificationsUpdateFunction() { |
621 } | 622 } |
622 | 623 |
623 NotificationsUpdateFunction::~NotificationsUpdateFunction() { | 624 NotificationsUpdateFunction::~NotificationsUpdateFunction() { |
624 } | 625 } |
625 | 626 |
626 bool NotificationsUpdateFunction::RunNotificationsApi() { | 627 bool NotificationsUpdateFunction::RunNotificationsApi() { |
627 params_ = api::notifications::Update::Params::Create(*args_); | 628 params_ = api::notifications::Update::Params::Create(*args_); |
628 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 629 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
629 | 630 |
630 // We are in update. If the ID doesn't exist, succeed but call the callback | 631 // We are in update. If the ID doesn't exist, succeed but call the callback |
631 // with "false". | 632 // with "false". |
632 const Notification* matched_notification = | 633 const Notification* matched_notification = |
633 g_browser_process->notification_ui_manager()->FindById( | 634 g_browser_process->notification_ui_manager()->FindById( |
634 CreateScopedIdentifier(extension_->id(), params_->notification_id), | 635 CreateScopedIdentifier(extension_->id(), params_->notification_id), |
635 NotificationUIManager::GetProfileID(GetProfile())); | 636 NotificationUIManager::GetProfileID(GetProfile())); |
636 if (!matched_notification) { | 637 if (!matched_notification) { |
637 SetResult(new base::FundamentalValue(false)); | 638 SetResult(base::MakeUnique<base::FundamentalValue>(false)); |
638 SendResponse(true); | 639 SendResponse(true); |
639 return true; | 640 return true; |
640 } | 641 } |
641 | 642 |
642 // Copy the existing notification to get a writable version of it. | 643 // Copy the existing notification to get a writable version of it. |
643 Notification notification = *matched_notification; | 644 Notification notification = *matched_notification; |
644 | 645 |
645 // If we have trouble updating the notification (could be improper use of API | 646 // If we have trouble updating the notification (could be improper use of API |
646 // or some other reason), mark the function as failed, calling the callback | 647 // or some other reason), mark the function as failed, calling the callback |
647 // with false. | 648 // with false. |
648 // TODO(dewittj): Add more human-readable error strings if this fails. | 649 // TODO(dewittj): Add more human-readable error strings if this fails. |
649 bool could_update_notification = UpdateNotification( | 650 bool could_update_notification = UpdateNotification( |
650 params_->notification_id, ¶ms_->options, ¬ification); | 651 params_->notification_id, ¶ms_->options, ¬ification); |
651 SetResult(new base::FundamentalValue(could_update_notification)); | 652 SetResult( |
| 653 base::MakeUnique<base::FundamentalValue>(could_update_notification)); |
652 if (!could_update_notification) | 654 if (!could_update_notification) |
653 return false; | 655 return false; |
654 | 656 |
655 // No trouble, created the notification, send true to the callback and | 657 // No trouble, created the notification, send true to the callback and |
656 // succeed. | 658 // succeed. |
657 SendResponse(true); | 659 SendResponse(true); |
658 return true; | 660 return true; |
659 } | 661 } |
660 | 662 |
661 NotificationsClearFunction::NotificationsClearFunction() { | 663 NotificationsClearFunction::NotificationsClearFunction() { |
662 } | 664 } |
663 | 665 |
664 NotificationsClearFunction::~NotificationsClearFunction() { | 666 NotificationsClearFunction::~NotificationsClearFunction() { |
665 } | 667 } |
666 | 668 |
667 bool NotificationsClearFunction::RunNotificationsApi() { | 669 bool NotificationsClearFunction::RunNotificationsApi() { |
668 params_ = api::notifications::Clear::Params::Create(*args_); | 670 params_ = api::notifications::Clear::Params::Create(*args_); |
669 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 671 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
670 | 672 |
671 bool cancel_result = g_browser_process->notification_ui_manager()->CancelById( | 673 bool cancel_result = g_browser_process->notification_ui_manager()->CancelById( |
672 CreateScopedIdentifier(extension_->id(), params_->notification_id), | 674 CreateScopedIdentifier(extension_->id(), params_->notification_id), |
673 NotificationUIManager::GetProfileID(GetProfile())); | 675 NotificationUIManager::GetProfileID(GetProfile())); |
674 | 676 |
675 SetResult(new base::FundamentalValue(cancel_result)); | 677 SetResult(base::MakeUnique<base::FundamentalValue>(cancel_result)); |
676 SendResponse(true); | 678 SendResponse(true); |
677 | 679 |
678 return true; | 680 return true; |
679 } | 681 } |
680 | 682 |
681 NotificationsGetAllFunction::NotificationsGetAllFunction() {} | 683 NotificationsGetAllFunction::NotificationsGetAllFunction() {} |
682 | 684 |
683 NotificationsGetAllFunction::~NotificationsGetAllFunction() {} | 685 NotificationsGetAllFunction::~NotificationsGetAllFunction() {} |
684 | 686 |
685 bool NotificationsGetAllFunction::RunNotificationsApi() { | 687 bool NotificationsGetAllFunction::RunNotificationsApi() { |
686 NotificationUIManager* notification_ui_manager = | 688 NotificationUIManager* notification_ui_manager = |
687 g_browser_process->notification_ui_manager(); | 689 g_browser_process->notification_ui_manager(); |
688 std::set<std::string> notification_ids = | 690 std::set<std::string> notification_ids = |
689 notification_ui_manager->GetAllIdsByProfileAndSourceOrigin( | 691 notification_ui_manager->GetAllIdsByProfileAndSourceOrigin( |
690 NotificationUIManager::GetProfileID(GetProfile()), extension_->url()); | 692 NotificationUIManager::GetProfileID(GetProfile()), extension_->url()); |
691 | 693 |
692 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 694 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
693 | 695 |
694 for (std::set<std::string>::iterator iter = notification_ids.begin(); | 696 for (std::set<std::string>::iterator iter = notification_ids.begin(); |
695 iter != notification_ids.end(); iter++) { | 697 iter != notification_ids.end(); iter++) { |
696 result->SetBooleanWithoutPathExpansion( | 698 result->SetBooleanWithoutPathExpansion( |
697 StripScopeFromIdentifier(extension_->id(), *iter), true); | 699 StripScopeFromIdentifier(extension_->id(), *iter), true); |
698 } | 700 } |
699 | 701 |
700 SetResult(result.release()); | 702 SetResult(std::move(result)); |
701 SendResponse(true); | 703 SendResponse(true); |
702 | 704 |
703 return true; | 705 return true; |
704 } | 706 } |
705 | 707 |
706 NotificationsGetPermissionLevelFunction:: | 708 NotificationsGetPermissionLevelFunction:: |
707 NotificationsGetPermissionLevelFunction() {} | 709 NotificationsGetPermissionLevelFunction() {} |
708 | 710 |
709 NotificationsGetPermissionLevelFunction:: | 711 NotificationsGetPermissionLevelFunction:: |
710 ~NotificationsGetPermissionLevelFunction() {} | 712 ~NotificationsGetPermissionLevelFunction() {} |
711 | 713 |
712 bool NotificationsGetPermissionLevelFunction::CanRunWhileDisabled() const { | 714 bool NotificationsGetPermissionLevelFunction::CanRunWhileDisabled() const { |
713 return true; | 715 return true; |
714 } | 716 } |
715 | 717 |
716 bool NotificationsGetPermissionLevelFunction::RunNotificationsApi() { | 718 bool NotificationsGetPermissionLevelFunction::RunNotificationsApi() { |
717 api::notifications::PermissionLevel result = | 719 api::notifications::PermissionLevel result = |
718 AreExtensionNotificationsAllowed() | 720 AreExtensionNotificationsAllowed() |
719 ? api::notifications::PERMISSION_LEVEL_GRANTED | 721 ? api::notifications::PERMISSION_LEVEL_GRANTED |
720 : api::notifications::PERMISSION_LEVEL_DENIED; | 722 : api::notifications::PERMISSION_LEVEL_DENIED; |
721 | 723 |
722 SetResult(new base::StringValue(api::notifications::ToString(result))); | 724 SetResult(base::MakeUnique<base::StringValue>( |
| 725 api::notifications::ToString(result))); |
723 SendResponse(true); | 726 SendResponse(true); |
724 | 727 |
725 return true; | 728 return true; |
726 } | 729 } |
727 | 730 |
728 } // namespace extensions | 731 } // namespace extensions |
OLD | NEW |