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

Side by Side Diff: chrome/browser/extensions/api/notifications/notifications_api.cc

Issue 2703213004: Migrate extension notifications to the new NotificationDisplayService (Closed)
Patch Set: Migrate extension notifications to the new NotificationDisplayService Created 3 years, 8 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 unified diff | Download patch
OLDNEW
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/feature_list.h" 12 #include "base/feature_list.h"
13 #include "base/guid.h" 13 #include "base/guid.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/metrics/histogram_macros.h" 16 #include "base/metrics/histogram_macros.h"
17 #include "base/rand_util.h" 17 #include "base/rand_util.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
20 #include "base/time/time.h" 20 #include "base/time/time.h"
21 #include "build/build_config.h" 21 #include "build/build_config.h"
22 #include "chrome/browser/browser_process.h" 22 #include "chrome/browser/extensions/api/notifications/extension_notification_dis play_helper.h"
23 #include "chrome/browser/extensions/api/notifications/extension_notification_dis play_helper_factory.h"
23 #include "chrome/browser/notifications/notification.h" 24 #include "chrome/browser/notifications/notification.h"
24 #include "chrome/browser/notifications/notification_ui_manager.h"
25 #include "chrome/browser/notifications/notifier_state_tracker.h" 25 #include "chrome/browser/notifications/notifier_state_tracker.h"
26 #include "chrome/browser/notifications/notifier_state_tracker_factory.h" 26 #include "chrome/browser/notifications/notifier_state_tracker_factory.h"
27 #include "chrome/browser/profiles/profile.h" 27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/common/extensions/api/notifications/notification_style.h" 28 #include "chrome/common/extensions/api/notifications/notification_style.h"
29 #include "components/keyed_service/content/browser_context_keyed_service_shutdow n_notifier_factory.h" 29 #include "components/keyed_service/content/browser_context_keyed_service_shutdow n_notifier_factory.h"
30 #include "components/keyed_service/core/keyed_service_shutdown_notifier.h" 30 #include "components/keyed_service/core/keyed_service_shutdown_notifier.h"
31 #include "content/public/browser/render_process_host.h" 31 #include "content/public/browser/render_process_host.h"
32 #include "content/public/browser/render_view_host.h" 32 #include "content/public/browser/render_view_host.h"
33 #include "content/public/browser/web_contents.h" 33 #include "content/public/browser/web_contents.h"
34 #include "extensions/browser/app_window/app_window.h" 34 #include "extensions/browser/app_window/app_window.h"
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 }; 205 };
206 206
207 class NotificationsApiDelegate : public NotificationDelegate { 207 class NotificationsApiDelegate : public NotificationDelegate {
208 public: 208 public:
209 NotificationsApiDelegate(ChromeAsyncExtensionFunction* api_function, 209 NotificationsApiDelegate(ChromeAsyncExtensionFunction* api_function,
210 Profile* profile, 210 Profile* profile,
211 const std::string& extension_id, 211 const std::string& extension_id,
212 const std::string& id) 212 const std::string& id)
213 : api_function_(api_function), 213 : api_function_(api_function),
214 event_router_(EventRouter::Get(profile)), 214 event_router_(EventRouter::Get(profile)),
215 display_helper_(
216 ExtensionNotificationDisplayHelperFactory::GetForProfile(profile)),
215 extension_id_(extension_id), 217 extension_id_(extension_id),
216 id_(id), 218 id_(id),
217 scoped_id_(CreateScopedIdentifier(extension_id, id)) { 219 scoped_id_(CreateScopedIdentifier(extension_id, id)) {
218 DCHECK(api_function_); 220 DCHECK(api_function_);
221 DCHECK(display_helper_);
222
219 shutdown_notifier_subscription_ = 223 shutdown_notifier_subscription_ =
220 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe( 224 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe(
221 base::Bind(&NotificationsApiDelegate::Shutdown, 225 base::Bind(&NotificationsApiDelegate::Shutdown,
222 base::Unretained(this))); 226 base::Unretained(this)));
223 } 227 }
224 228
225 void Close(bool by_user) override { 229 void Close(bool by_user) override {
226 EventRouter::UserGestureState gesture = 230 EventRouter::UserGestureState gesture =
227 by_user ? EventRouter::USER_GESTURE_ENABLED 231 by_user ? EventRouter::USER_GESTURE_ENABLED
228 : EventRouter::USER_GESTURE_NOT_ENABLED; 232 : EventRouter::USER_GESTURE_NOT_ENABLED;
229 std::unique_ptr<base::ListValue> args(CreateBaseEventArgs()); 233 std::unique_ptr<base::ListValue> args(CreateBaseEventArgs());
230 args->AppendBoolean(by_user); 234 args->AppendBoolean(by_user);
231 SendEvent(events::NOTIFICATIONS_ON_CLOSED, 235 SendEvent(events::NOTIFICATIONS_ON_CLOSED,
232 notifications::OnClosed::kEventName, gesture, std::move(args)); 236 notifications::OnClosed::kEventName, gesture, std::move(args));
237
238 DCHECK(display_helper_);
239 display_helper_->EraseDataForNotificationId(scoped_id_);
233 } 240 }
234 241
235 void Click() override { 242 void Click() override {
236 std::unique_ptr<base::ListValue> args(CreateBaseEventArgs()); 243 std::unique_ptr<base::ListValue> args(CreateBaseEventArgs());
237 SendEvent(events::NOTIFICATIONS_ON_CLICKED, 244 SendEvent(events::NOTIFICATIONS_ON_CLICKED,
238 notifications::OnClicked::kEventName, 245 notifications::OnClicked::kEventName,
239 EventRouter::USER_GESTURE_ENABLED, std::move(args)); 246 EventRouter::USER_GESTURE_ENABLED, std::move(args));
240 } 247 }
241 248
242 bool HasClickedListener() override { 249 bool HasClickedListener() override {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 if (!event_router_) 302 if (!event_router_)
296 return; 303 return;
297 304
298 std::unique_ptr<Event> event( 305 std::unique_ptr<Event> event(
299 new Event(histogram_value, name, std::move(args))); 306 new Event(histogram_value, name, std::move(args)));
300 event->user_gesture = user_gesture; 307 event->user_gesture = user_gesture;
301 event_router_->DispatchEventToExtension(extension_id_, std::move(event)); 308 event_router_->DispatchEventToExtension(extension_id_, std::move(event));
302 } 309 }
303 310
304 void Shutdown() { 311 void Shutdown() {
312 shutdown_notifier_subscription_.reset();
305 event_router_ = nullptr; 313 event_router_ = nullptr;
306 shutdown_notifier_subscription_.reset(); 314 display_helper_ = nullptr;
307 } 315 }
308 316
309 std::unique_ptr<base::ListValue> CreateBaseEventArgs() { 317 std::unique_ptr<base::ListValue> CreateBaseEventArgs() {
310 std::unique_ptr<base::ListValue> args(new base::ListValue()); 318 std::unique_ptr<base::ListValue> args(new base::ListValue());
311 args->AppendString(id_); 319 args->AppendString(id_);
312 return args; 320 return args;
313 } 321 }
314 322
315 scoped_refptr<ChromeAsyncExtensionFunction> api_function_; 323 scoped_refptr<ChromeAsyncExtensionFunction> api_function_;
316 324
317 // Since this class is refcounted it may outlive the profile. We listen for 325 // Since this class is refcounted it may outlive the profile. We listen for
318 // profile-keyed service shutdown events and reset to nullptr at that time, 326 // profile-keyed service shutdown events and reset to nullptr at that time,
319 // so make sure to check for a valid pointer before use. 327 // so make sure to check for a valid pointer before use.
320 EventRouter* event_router_; 328 EventRouter* event_router_;
329 ExtensionNotificationDisplayHelper* display_helper_;
321 330
322 const std::string extension_id_; 331 const std::string extension_id_;
323 const std::string id_; 332 const std::string id_;
324 const std::string scoped_id_; 333 const std::string scoped_id_;
325 334
326 std::unique_ptr<KeyedServiceShutdownNotifier::Subscription> 335 std::unique_ptr<KeyedServiceShutdownNotifier::Subscription>
327 shutdown_notifier_subscription_; 336 shutdown_notifier_subscription_;
328 337
329 DISALLOW_COPY_AND_ASSIGN(NotificationsApiDelegate); 338 DISALLOW_COPY_AND_ASSIGN(NotificationsApiDelegate);
330 }; 339 };
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 type, title, message, icon, 498 type, title, message, icon,
490 message_center::NotifierId(message_center::NotifierId::APPLICATION, 499 message_center::NotifierId(message_center::NotifierId::APPLICATION,
491 extension_->id()), 500 extension_->id()),
492 base::UTF8ToUTF16(extension_->name()), extension_->url(), 501 base::UTF8ToUTF16(extension_->name()), extension_->url(),
493 api_delegate->id(), optional_fields, api_delegate); 502 api_delegate->id(), optional_fields, api_delegate);
494 503
495 // Apply the "requireInteraction" flag. The value defaults to false. 504 // Apply the "requireInteraction" flag. The value defaults to false.
496 notification.set_never_timeout(options->require_interaction && 505 notification.set_never_timeout(options->require_interaction &&
497 *options->require_interaction); 506 *options->require_interaction);
498 507
499 g_browser_process->notification_ui_manager()->Add(notification, GetProfile()); 508 GetDisplayHelper()->Display(notification);
500 return true; 509 return true;
501 } 510 }
502 511
503 bool NotificationsApiFunction::UpdateNotification( 512 bool NotificationsApiFunction::UpdateNotification(
504 const std::string& id, 513 const std::string& id,
505 api::notifications::NotificationOptions* options, 514 api::notifications::NotificationOptions* options,
506 Notification* notification) { 515 Notification* notification) {
507 #if !defined(OS_CHROMEOS) 516 #if !defined(OS_CHROMEOS)
508 if (options->priority && 517 if (options->priority &&
509 *options->priority < message_center::DEFAULT_PRIORITY) { 518 *options->priority < message_center::DEFAULT_PRIORITY) {
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 base::UTF8ToUTF16(api_item.title), 632 base::UTF8ToUTF16(api_item.title),
624 base::UTF8ToUTF16(api_item.message))); 633 base::UTF8ToUTF16(api_item.message)));
625 } 634 }
626 notification->set_items(items); 635 notification->set_items(items);
627 } 636 }
628 637
629 // Then override if it's already set. 638 // Then override if it's already set.
630 if (options->is_clickable.get()) 639 if (options->is_clickable.get())
631 notification->set_clickable(*options->is_clickable); 640 notification->set_clickable(*options->is_clickable);
632 641
633 g_browser_process->notification_ui_manager()->Update(*notification, 642 // It's safe to follow the regular path for adding a new notification as it's
634 GetProfile()); 643 // already been verified that there is a notification that can be updated.
644 GetDisplayHelper()->Display(*notification);
645
635 return true; 646 return true;
636 } 647 }
637 648
638 bool NotificationsApiFunction::AreExtensionNotificationsAllowed() const { 649 bool NotificationsApiFunction::AreExtensionNotificationsAllowed() const {
639 NotifierStateTracker* notifier_state_tracker = 650 NotifierStateTracker* notifier_state_tracker =
640 NotifierStateTrackerFactory::GetForProfile(GetProfile()); 651 NotifierStateTrackerFactory::GetForProfile(GetProfile());
641 652
642 return notifier_state_tracker->IsNotifierEnabled( 653 return notifier_state_tracker->IsNotifierEnabled(
643 message_center::NotifierId(message_center::NotifierId::APPLICATION, 654 message_center::NotifierId(message_center::NotifierId::APPLICATION,
644 extension_->id())); 655 extension_->id()));
645 } 656 }
646 657
647 bool NotificationsApiFunction::IsNotificationsApiEnabled() const { 658 bool NotificationsApiFunction::IsNotificationsApiEnabled() const {
648 return CanRunWhileDisabled() || AreExtensionNotificationsAllowed(); 659 return CanRunWhileDisabled() || AreExtensionNotificationsAllowed();
649 } 660 }
650 661
651 bool NotificationsApiFunction::CanRunWhileDisabled() const { 662 bool NotificationsApiFunction::CanRunWhileDisabled() const {
652 return false; 663 return false;
653 } 664 }
654 665
666 ExtensionNotificationDisplayHelper* NotificationsApiFunction::GetDisplayHelper()
667 const {
668 return ExtensionNotificationDisplayHelperFactory::GetForProfile(GetProfile());
669 }
670
655 bool NotificationsApiFunction::RunAsync() { 671 bool NotificationsApiFunction::RunAsync() {
656 if (IsNotificationsApiAvailable() && IsNotificationsApiEnabled()) { 672 if (IsNotificationsApiAvailable() && IsNotificationsApiEnabled()) {
657 return RunNotificationsApi(); 673 return RunNotificationsApi();
658 } else { 674 } else {
659 SendResponse(false); 675 SendResponse(false);
660 return true; 676 return true;
661 } 677 }
662 } 678 }
663 679
664 message_center::NotificationType 680 message_center::NotificationType
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 NotificationsUpdateFunction::~NotificationsUpdateFunction() { 737 NotificationsUpdateFunction::~NotificationsUpdateFunction() {
722 } 738 }
723 739
724 bool NotificationsUpdateFunction::RunNotificationsApi() { 740 bool NotificationsUpdateFunction::RunNotificationsApi() {
725 params_ = api::notifications::Update::Params::Create(*args_); 741 params_ = api::notifications::Update::Params::Create(*args_);
726 EXTENSION_FUNCTION_VALIDATE(params_.get()); 742 EXTENSION_FUNCTION_VALIDATE(params_.get());
727 743
728 // We are in update. If the ID doesn't exist, succeed but call the callback 744 // We are in update. If the ID doesn't exist, succeed but call the callback
729 // with "false". 745 // with "false".
730 const Notification* matched_notification = 746 const Notification* matched_notification =
731 g_browser_process->notification_ui_manager()->FindById( 747 GetDisplayHelper()->GetByNotificationId(
732 CreateScopedIdentifier(extension_->id(), params_->notification_id), 748 CreateScopedIdentifier(extension_->id(), params_->notification_id));
733 NotificationUIManager::GetProfileID(GetProfile())); 749
734 if (!matched_notification) { 750 if (!matched_notification) {
735 SetResult(base::MakeUnique<base::Value>(false)); 751 SetResult(base::MakeUnique<base::Value>(false));
736 SendResponse(true); 752 SendResponse(true);
737 return true; 753 return true;
738 } 754 }
739 755
740 // Copy the existing notification to get a writable version of it. 756 // Copy the existing notification to get a writable version of it.
741 Notification notification = *matched_notification; 757 Notification notification = *matched_notification;
742 758
743 // If we have trouble updating the notification (could be improper use of API 759 // If we have trouble updating the notification (could be improper use of API
(...skipping 15 matching lines...) Expand all
759 NotificationsClearFunction::NotificationsClearFunction() { 775 NotificationsClearFunction::NotificationsClearFunction() {
760 } 776 }
761 777
762 NotificationsClearFunction::~NotificationsClearFunction() { 778 NotificationsClearFunction::~NotificationsClearFunction() {
763 } 779 }
764 780
765 bool NotificationsClearFunction::RunNotificationsApi() { 781 bool NotificationsClearFunction::RunNotificationsApi() {
766 params_ = api::notifications::Clear::Params::Create(*args_); 782 params_ = api::notifications::Clear::Params::Create(*args_);
767 EXTENSION_FUNCTION_VALIDATE(params_.get()); 783 EXTENSION_FUNCTION_VALIDATE(params_.get());
768 784
769 bool cancel_result = g_browser_process->notification_ui_manager()->CancelById( 785 bool cancel_result = GetDisplayHelper()->Close(
770 CreateScopedIdentifier(extension_->id(), params_->notification_id), 786 CreateScopedIdentifier(extension_->id(), params_->notification_id));
771 NotificationUIManager::GetProfileID(GetProfile()));
772 787
773 SetResult(base::MakeUnique<base::Value>(cancel_result)); 788 SetResult(base::MakeUnique<base::Value>(cancel_result));
774 SendResponse(true); 789 SendResponse(true);
775 790
776 return true; 791 return true;
777 } 792 }
778 793
779 NotificationsGetAllFunction::NotificationsGetAllFunction() {} 794 NotificationsGetAllFunction::NotificationsGetAllFunction() {}
780 795
781 NotificationsGetAllFunction::~NotificationsGetAllFunction() {} 796 NotificationsGetAllFunction::~NotificationsGetAllFunction() {}
782 797
783 bool NotificationsGetAllFunction::RunNotificationsApi() { 798 bool NotificationsGetAllFunction::RunNotificationsApi() {
784 NotificationUIManager* notification_ui_manager =
785 g_browser_process->notification_ui_manager();
786 std::set<std::string> notification_ids = 799 std::set<std::string> notification_ids =
787 notification_ui_manager->GetAllIdsByProfileAndSourceOrigin( 800 GetDisplayHelper()->GetNotificationIdsForExtension(extension_->url());
788 NotificationUIManager::GetProfileID(GetProfile()), extension_->url());
789 801
790 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); 802 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
791 803
792 for (std::set<std::string>::iterator iter = notification_ids.begin(); 804 for (std::set<std::string>::iterator iter = notification_ids.begin();
793 iter != notification_ids.end(); iter++) { 805 iter != notification_ids.end(); iter++) {
794 result->SetBooleanWithoutPathExpansion( 806 result->SetBooleanWithoutPathExpansion(
795 StripScopeFromIdentifier(extension_->id(), *iter), true); 807 StripScopeFromIdentifier(extension_->id(), *iter), true);
796 } 808 }
797 809
798 SetResult(std::move(result)); 810 SetResult(std::move(result));
(...skipping 19 matching lines...) Expand all
818 : api::notifications::PERMISSION_LEVEL_DENIED; 830 : api::notifications::PERMISSION_LEVEL_DENIED;
819 831
820 SetResult( 832 SetResult(
821 base::MakeUnique<base::Value>(api::notifications::ToString(result))); 833 base::MakeUnique<base::Value>(api::notifications::ToString(result)));
822 SendResponse(true); 834 SendResponse(true);
823 835
824 return true; 836 return true;
825 } 837 }
826 838
827 } // namespace extensions 839 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698