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

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

Issue 2659533003: Remove the notificationProvider extension API (Closed)
Patch Set: Remove the notificationProvider extension API Created 3 years, 10 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/notifications/message_center_notification_manager.h" 5 #include "chrome/browser/notifications/message_center_notification_manager.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "build/build_config.h" 12 #include "build/build_config.h"
13 #include "chrome/browser/extensions/api/notification_provider/notification_provi der_api.h"
14 #include "chrome/browser/notifications/extension_welcome_notification.h" 13 #include "chrome/browser/notifications/extension_welcome_notification.h"
15 #include "chrome/browser/notifications/extension_welcome_notification_factory.h" 14 #include "chrome/browser/notifications/extension_welcome_notification_factory.h"
16 #include "chrome/browser/notifications/fullscreen_notification_blocker.h" 15 #include "chrome/browser/notifications/fullscreen_notification_blocker.h"
17 #include "chrome/browser/notifications/message_center_settings_controller.h" 16 #include "chrome/browser/notifications/message_center_settings_controller.h"
18 #include "chrome/browser/notifications/notification.h" 17 #include "chrome/browser/notifications/notification.h"
19 #include "chrome/browser/notifications/notification_conversion_helper.h"
20 #include "chrome/browser/notifications/profile_notification.h" 18 #include "chrome/browser/notifications/profile_notification.h"
21 #include "chrome/browser/notifications/screen_lock_notification_blocker.h" 19 #include "chrome/browser/notifications/screen_lock_notification_blocker.h"
22 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/common/extensions/api/notification_provider.h"
24 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/url_constants.h" 22 #include "content/public/common/url_constants.h"
26 #include "extensions/browser/extension_registry.h" 23 #include "extensions/browser/extension_registry.h"
27 #include "extensions/common/extension_set.h" 24 #include "extensions/common/extension_set.h"
28 #include "extensions/common/permissions/permissions_data.h" 25 #include "extensions/common/permissions/permissions_data.h"
29 #include "ui/gfx/image/image_skia.h" 26 #include "ui/gfx/image/image_skia.h"
30 #include "ui/message_center/message_center_style.h" 27 #include "ui/message_center/message_center_style.h"
31 #include "ui/message_center/message_center_tray.h" 28 #include "ui/message_center/message_center_tray.h"
32 #include "ui/message_center/message_center_types.h" 29 #include "ui/message_center/message_center_types.h"
33 #include "ui/message_center/notifier_settings.h" 30 #include "ui/message_center/notifier_settings.h"
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 90
94 ExtensionWelcomeNotificationFactory::GetForBrowserContext(profile)-> 91 ExtensionWelcomeNotificationFactory::GetForBrowserContext(profile)->
95 ShowWelcomeNotificationIfNecessary(profile_notification->notification()); 92 ShowWelcomeNotificationIfNecessary(profile_notification->notification());
96 93
97 // WARNING: You MUST use AddProfileNotification or update the message center 94 // WARNING: You MUST use AddProfileNotification or update the message center
98 // via the notification within a ProfileNotification object or the profile ID 95 // via the notification within a ProfileNotification object or the profile ID
99 // will not be correctly set for ChromeOS. 96 // will not be correctly set for ChromeOS.
100 // Takes ownership of profile_notification. 97 // Takes ownership of profile_notification.
101 AddProfileNotification(std::move(profile_notification_ptr)); 98 AddProfileNotification(std::move(profile_notification_ptr));
102 99
103 // TODO(liyanhou): Change the logic to only send notifications to one party.
104 // Currently, if there is an app with notificationProvider permission,
105 // notifications will go to both message center and the app.
106 // Change it to send notifications to message center only when the user chose
107 // default message center (extension_id.empty()).
108
109 // If there exist apps/extensions that have notificationProvider permission,
110 // route notifications to one of the apps/extensions.
111 std::string extension_id = GetExtensionTakingOverNotifications(profile);
112 if (!extension_id.empty())
113 AddNotificationToAlternateProvider(profile_notification->notification(),
114 profile, extension_id);
115
116 message_center_->AddNotification( 100 message_center_->AddNotification(
117 base::MakeUnique<message_center::Notification>( 101 base::MakeUnique<message_center::Notification>(
118 profile_notification->notification())); 102 profile_notification->notification()));
119 } 103 }
120 104
121 bool MessageCenterNotificationManager::Update(const Notification& notification, 105 bool MessageCenterNotificationManager::Update(const Notification& notification,
122 Profile* profile) { 106 Profile* profile) {
123 const std::string& tag = notification.tag(); 107 const std::string& tag = notification.tag();
124 if (tag.empty()) 108 if (tag.empty())
125 return false; 109 return false;
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 MessageCenterNotificationManager::GetMessageCenterNotificationIdForTest( 299 MessageCenterNotificationManager::GetMessageCenterNotificationIdForTest(
316 const std::string& delegate_id, 300 const std::string& delegate_id,
317 Profile* profile) { 301 Profile* profile) {
318 return ProfileNotification::GetProfileNotificationId(delegate_id, 302 return ProfileNotification::GetProfileNotificationId(delegate_id,
319 GetProfileID(profile)); 303 GetProfileID(profile));
320 } 304 }
321 305
322 //////////////////////////////////////////////////////////////////////////////// 306 ////////////////////////////////////////////////////////////////////////////////
323 // private 307 // private
324 308
325 void MessageCenterNotificationManager::AddNotificationToAlternateProvider(
326 const Notification& notification,
327 Profile* profile,
328 const std::string& extension_id) const {
329 // Convert data from Notification type to NotificationOptions type.
330 extensions::api::notifications::NotificationOptions options;
331 NotificationConversionHelper::NotificationToNotificationOptions(notification,
332 &options);
333
334 // Send the notification to the alternate provider extension/app.
335 extensions::NotificationProviderEventRouter event_router(profile);
336 event_router.CreateNotification(extension_id,
337 notification.notifier_id().id,
338 notification.delegate_id(),
339 options);
340 }
341
342 void MessageCenterNotificationManager::AddProfileNotification( 309 void MessageCenterNotificationManager::AddProfileNotification(
343 std::unique_ptr<ProfileNotification> profile_notification) { 310 std::unique_ptr<ProfileNotification> profile_notification) {
344 const Notification& notification = profile_notification->notification(); 311 const Notification& notification = profile_notification->notification();
345 std::string id = notification.id(); 312 std::string id = notification.id();
346 // Notification ids should be unique. 313 // Notification ids should be unique.
347 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); 314 DCHECK(profile_notifications_.find(id) == profile_notifications_.end());
348 profile_notifications_[id] = std::move(profile_notification); 315 profile_notifications_[id] = std::move(profile_notification);
349 } 316 }
350 317
351 void MessageCenterNotificationManager::RemoveProfileNotification( 318 void MessageCenterNotificationManager::RemoveProfileNotification(
(...skipping 17 matching lines...) Expand all
369 } 336 }
370 337
371 ProfileNotification* MessageCenterNotificationManager::FindProfileNotification( 338 ProfileNotification* MessageCenterNotificationManager::FindProfileNotification(
372 const std::string& id) const { 339 const std::string& id) const {
373 auto iter = profile_notifications_.find(id); 340 auto iter = profile_notifications_.find(id);
374 if (iter == profile_notifications_.end()) 341 if (iter == profile_notifications_.end())
375 return nullptr; 342 return nullptr;
376 343
377 return (*iter).second.get(); 344 return (*iter).second.get();
378 } 345 }
379
380 std::string
381 MessageCenterNotificationManager::GetExtensionTakingOverNotifications(
382 Profile* profile) {
383 // TODO(liyanhou): When additional settings in Chrome Settings is implemented,
384 // change choosing the last app with permission to a user selected app.
385 extensions::ExtensionRegistry* registry =
386 extensions::ExtensionRegistry::Get(profile);
387 DCHECK(registry);
388 std::string extension_id;
389 for (extensions::ExtensionSet::const_iterator it =
390 registry->enabled_extensions().begin();
391 it != registry->enabled_extensions().end();
392 ++it) {
393 if ((*it->get()).permissions_data()->HasAPIPermission(
394 extensions::APIPermission::ID::kNotificationProvider)) {
395 extension_id = (*it->get()).id();
396 return extension_id;
397 }
398 }
399 return extension_id;
400 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698