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

Side by Side Diff: chrome/browser/notifications/message_center_notification_manager.cc

Issue 441753002: Route newly created notifications to notification provider API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed comments, deleted browser tests for onUpdated and onCleared Created 6 years, 4 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 | Annotate | Revision Log
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/notifications/message_center_notification_manager.h" 5 #include "chrome/browser/notifications/message_center_notification_manager.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/pref_registry_simple.h" 9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "chrome/browser/chrome_notification_types.h" 12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/extensions/api/notification_provider/notification_provi der_api.h"
13 #include "chrome/browser/notifications/desktop_notification_service.h" 14 #include "chrome/browser/notifications/desktop_notification_service.h"
14 #include "chrome/browser/notifications/desktop_notification_service_factory.h" 15 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
15 #include "chrome/browser/notifications/fullscreen_notification_blocker.h" 16 #include "chrome/browser/notifications/fullscreen_notification_blocker.h"
16 #include "chrome/browser/notifications/message_center_settings_controller.h" 17 #include "chrome/browser/notifications/message_center_settings_controller.h"
17 #include "chrome/browser/notifications/notification.h" 18 #include "chrome/browser/notifications/notification.h"
19 #include "chrome/browser/notifications/notification_conversion_helper.h"
18 #include "chrome/browser/notifications/screen_lock_notification_blocker.h" 20 #include "chrome/browser/notifications/screen_lock_notification_blocker.h"
19 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/browser_finder.h" 22 #include "chrome/browser/ui/browser_finder.h"
21 #include "chrome/browser/ui/chrome_pages.h" 23 #include "chrome/browser/ui/chrome_pages.h"
22 #include "chrome/browser/ui/host_desktop.h" 24 #include "chrome/browser/ui/host_desktop.h"
25 #include "chrome/common/extensions/api/notification_provider.h"
23 #include "chrome/common/pref_names.h" 26 #include "chrome/common/pref_names.h"
24 #include "content/public/browser/notification_service.h" 27 #include "content/public/browser/notification_service.h"
25 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
26 #include "content/public/common/url_constants.h" 29 #include "content/public/common/url_constants.h"
30 #include "extensions/browser/extension_registry.h"
27 #include "extensions/browser/extension_system.h" 31 #include "extensions/browser/extension_system.h"
28 #include "extensions/browser/info_map.h" 32 #include "extensions/browser/info_map.h"
29 #include "extensions/common/extension_set.h" 33 #include "extensions/common/extension_set.h"
34 #include "extensions/common/permissions/permissions_data.h"
30 #include "ui/gfx/image/image_skia.h" 35 #include "ui/gfx/image/image_skia.h"
31 #include "ui/message_center/message_center_style.h" 36 #include "ui/message_center/message_center_style.h"
32 #include "ui/message_center/message_center_tray.h" 37 #include "ui/message_center/message_center_tray.h"
33 #include "ui/message_center/message_center_types.h" 38 #include "ui/message_center/message_center_types.h"
34 #include "ui/message_center/notifier_settings.h" 39 #include "ui/message_center/notifier_settings.h"
35 40
36 #if defined(OS_CHROMEOS) 41 #if defined(OS_CHROMEOS)
37 #include "chrome/browser/notifications/login_state_notification_blocker_chromeos .h" 42 #include "chrome/browser/notifications/login_state_notification_blocker_chromeos .h"
38 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" 43 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
39 #endif 44 #endif
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 // NotificationUIManager 115 // NotificationUIManager
111 116
112 void MessageCenterNotificationManager::Add(const Notification& notification, 117 void MessageCenterNotificationManager::Add(const Notification& notification,
113 Profile* profile) { 118 Profile* profile) {
114 if (Update(notification, profile)) 119 if (Update(notification, profile))
115 return; 120 return;
116 121
117 DesktopNotificationServiceFactory::GetForProfile(profile)-> 122 DesktopNotificationServiceFactory::GetForProfile(profile)->
118 ShowWelcomeNotificationIfNecessary(notification); 123 ShowWelcomeNotificationIfNecessary(notification);
119 124
120 // WARNING: You MUST use AddProfileNotification or update the message center 125 // WARNING: You MUST update the message center via the notification within a
121 // via the notification within a ProfileNotification object or the profile ID 126 // ProfileNotification object or the profile ID will not be correctly set for
122 // will not be correctly set for ChromeOS. 127 // ChromeOS.
123 AddProfileNotification( 128 ProfileNotification* profile_notification(
124 new ProfileNotification(profile, notification, message_center_)); 129 new ProfileNotification(profile, notification, message_center_));
130 AddProfileNotification(profile_notification);
131
132 // TODO(liyanhou): Change the logic to only send notifications to one party.
133 // Currently, if there is an app with notificationProvider permission,
134 // notifications will go to both message center and the app.
135 // Change it to send notifications to message center only when the user chose
136 // default message center (extension_id.empty()).
137
138 // If there exist apps/extensions that have notificationProvider permission,
139 // route notifications to one of the apps/extensions.
140 std::string extension_id = GetExtensionTakingOverNotifications(profile);
141 if (!extension_id.empty())
142 profile_notification->AddToAlternateProvider(extension_id);
143
144 message_center_->AddNotification(make_scoped_ptr(
145 new message_center::Notification(profile_notification->notification())));
146 profile_notification->StartDownloads();
125 } 147 }
126 148
127 bool MessageCenterNotificationManager::Update(const Notification& notification, 149 bool MessageCenterNotificationManager::Update(const Notification& notification,
128 Profile* profile) { 150 Profile* profile) {
129 const base::string16& replace_id = notification.replace_id(); 151 const base::string16& replace_id = notification.replace_id();
130 if (replace_id.empty()) 152 if (replace_id.empty())
131 return false; 153 return false;
132 154
133 const GURL origin_url = notification.origin_url(); 155 const GURL origin_url = notification.origin_url();
134 DCHECK(origin_url.is_valid()); 156 DCHECK(origin_url.is_valid());
(...skipping 13 matching lines...) Expand all
148 old_notification->notification().delegate_id(); 170 old_notification->notification().delegate_id();
149 171
150 // Add/remove notification in the local list but just update the same 172 // Add/remove notification in the local list but just update the same
151 // one in MessageCenter. 173 // one in MessageCenter.
152 delete old_notification; 174 delete old_notification;
153 profile_notifications_.erase(old_id); 175 profile_notifications_.erase(old_id);
154 ProfileNotification* new_notification = 176 ProfileNotification* new_notification =
155 new ProfileNotification(profile, notification, message_center_); 177 new ProfileNotification(profile, notification, message_center_);
156 profile_notifications_[notification.delegate_id()] = new_notification; 178 profile_notifications_[notification.delegate_id()] = new_notification;
157 179
180 // TODO(liyanhou): Add routing updated notifications to alternative
181 // providers.
182
158 // WARNING: You MUST use AddProfileNotification or update the message 183 // WARNING: You MUST use AddProfileNotification or update the message
159 // center via the notification within a ProfileNotification object or the 184 // center via the notification within a ProfileNotification object or the
160 // profile ID will not be correctly set for ChromeOS. 185 // profile ID will not be correctly set for ChromeOS.
161 message_center_->UpdateNotification( 186 message_center_->UpdateNotification(
162 old_id, 187 old_id,
163 make_scoped_ptr(new message_center::Notification( 188 make_scoped_ptr(new message_center::Notification(
164 new_notification->notification()))); 189 new_notification->notification())));
165 190
166 new_notification->StartDownloads(); 191 new_notification->StartDownloads();
167 return true; 192 return true;
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); 495 for (extensions::ExtensionSet::const_iterator iter = extensions.begin();
471 iter != extensions.end(); ++iter) { 496 iter != extensions.end(); ++iter) {
472 if (desktop_service->IsNotifierEnabled(message_center::NotifierId( 497 if (desktop_service->IsNotifierEnabled(message_center::NotifierId(
473 message_center::NotifierId::APPLICATION, (*iter)->id()))) { 498 message_center::NotifierId::APPLICATION, (*iter)->id()))) {
474 return (*iter)->id(); 499 return (*iter)->id();
475 } 500 }
476 } 501 }
477 return std::string(); 502 return std::string();
478 } 503 }
479 504
505 void
506 MessageCenterNotificationManager::ProfileNotification::AddToAlternateProvider(
507 const std::string extension_id) {
508 // Convert data from Notification type to NotificationOptions type.
509 extensions::api::notifications::NotificationOptions options;
510 NotificationConversionHelper::NotificationToNotificationOptions(notification_,
511 &options);
512
513 // Send the notification to the alternate provider extension/app.
514 extensions::NotificationProviderEventRouter event_router(profile_);
515 event_router.CreateNotification(extension_id,
516 notification_.notifier_id().id,
517 notification_.delegate_id(),
518 options);
519 }
520
480 //////////////////////////////////////////////////////////////////////////////// 521 ////////////////////////////////////////////////////////////////////////////////
481 // private 522 // private
482 523
483 void MessageCenterNotificationManager::AddProfileNotification( 524 void MessageCenterNotificationManager::AddProfileNotification(
484 ProfileNotification* profile_notification) { 525 ProfileNotification* profile_notification) {
485 const Notification& notification = profile_notification->notification(); 526 std::string id = profile_notification->notification().delegate_id();
486 std::string id = notification.delegate_id();
487 // Notification ids should be unique. 527 // Notification ids should be unique.
488 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); 528 DCHECK(profile_notifications_.find(id) == profile_notifications_.end());
489 profile_notifications_[id] = profile_notification; 529 profile_notifications_[id] = profile_notification;
490
491 // Create the copy for message center, and ensure the extension ID is correct.
492 scoped_ptr<message_center::Notification> message_center_notification(
493 new message_center::Notification(notification));
494 message_center_->AddNotification(message_center_notification.Pass());
495
496 profile_notification->StartDownloads();
497 } 530 }
498 531
499 void MessageCenterNotificationManager::RemoveProfileNotification( 532 void MessageCenterNotificationManager::RemoveProfileNotification(
500 ProfileNotification* profile_notification) { 533 ProfileNotification* profile_notification) {
501 std::string id = profile_notification->notification().delegate_id(); 534 std::string id = profile_notification->notification().delegate_id();
502 profile_notifications_.erase(id); 535 profile_notifications_.erase(id);
503 delete profile_notification; 536 delete profile_notification;
504 } 537 }
505 538
506 MessageCenterNotificationManager::ProfileNotification* 539 MessageCenterNotificationManager::ProfileNotification*
507 MessageCenterNotificationManager::FindProfileNotification( 540 MessageCenterNotificationManager::FindProfileNotification(
508 const std::string& id) const { 541 const std::string& id) const {
509 NotificationMap::const_iterator iter = profile_notifications_.find(id); 542 NotificationMap::const_iterator iter = profile_notifications_.find(id);
510 if (iter == profile_notifications_.end()) 543 if (iter == profile_notifications_.end())
511 return NULL; 544 return NULL;
512 545
513 return (*iter).second; 546 return (*iter).second;
514 } 547 }
548
549 std::string
550 MessageCenterNotificationManager::GetExtensionTakingOverNotifications(
551 Profile* profile) {
552 // TODO(liyanhou): When additional settings in Chrome Settings is implemented,
553 // change choosing the last app with permission to a user selected app.
554 extensions::ExtensionRegistry* registry =
555 extensions::ExtensionRegistry::Get(profile);
556 DCHECK(registry);
557 std::string extension_id;
558 for (extensions::ExtensionSet::const_iterator it =
559 registry->enabled_extensions().begin();
560 it != registry->enabled_extensions().end();
561 ++it) {
562 if ((*it->get()).permissions_data()->HasAPIPermission(
563 extensions::APIPermission::ID::kNotificationProvider)) {
564 extension_id = (*it->get()).id();
565 return extension_id;
566 }
567 }
568 return extension_id;
569 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698