OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/platform_notification_service_impl.h" | 5 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
6 | 6 |
7 #include "chrome/browser/notifications/desktop_notification_service.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/notifications/notification.h" |
| 10 #include "chrome/browser/notifications/notification_object_proxy.h" |
| 11 #include "chrome/browser/notifications/notification_ui_manager.h" |
9 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/profiles/profile_io_data.h" | 13 #include "chrome/browser/profiles/profile_io_data.h" |
11 #include "components/content_settings/core/browser/host_content_settings_map.h" | 14 #include "components/content_settings/core/browser/host_content_settings_map.h" |
12 #include "components/content_settings/core/common/content_settings.h" | 15 #include "components/content_settings/core/common/content_settings.h" |
13 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/desktop_notification_delegate.h" | 17 #include "content/public/browser/desktop_notification_delegate.h" |
15 #include "content/public/common/show_desktop_notification_params.h" | 18 #include "content/public/common/show_desktop_notification_params.h" |
| 19 #include "ui/message_center/notifier_settings.h" |
16 | 20 |
17 #if defined(ENABLE_EXTENSIONS) | 21 #if defined(ENABLE_EXTENSIONS) |
| 22 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 23 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
| 24 #include "extensions/browser/extension_system.h" |
18 #include "extensions/browser/info_map.h" | 25 #include "extensions/browser/info_map.h" |
| 26 #include "extensions/common/constants.h" |
19 #include "extensions/common/extension_set.h" | 27 #include "extensions/common/extension_set.h" |
20 #include "extensions/common/permissions/api_permission.h" | 28 #include "extensions/common/permissions/api_permission.h" |
21 #endif | 29 #endif |
22 | 30 |
23 using content::BrowserThread; | 31 using content::BrowserThread; |
| 32 using message_center::NotifierId; |
| 33 |
| 34 namespace { |
| 35 |
| 36 void CancelNotification(const std::string& id, ProfileID profile_id) { |
| 37 PlatformNotificationServiceImpl::GetInstance() |
| 38 ->GetNotificationUIManager()->CancelById(id, profile_id); |
| 39 } |
| 40 |
| 41 } // namespace |
24 | 42 |
25 // static | 43 // static |
26 PlatformNotificationServiceImpl* | 44 PlatformNotificationServiceImpl* |
27 PlatformNotificationServiceImpl::GetInstance() { | 45 PlatformNotificationServiceImpl::GetInstance() { |
28 return Singleton<PlatformNotificationServiceImpl>::get(); | 46 return Singleton<PlatformNotificationServiceImpl>::get(); |
29 } | 47 } |
30 | 48 |
31 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() {} | 49 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() |
| 50 : notification_ui_manager_for_tests_(nullptr) {} |
32 | 51 |
33 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} | 52 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} |
34 | 53 |
35 blink::WebNotificationPermission | 54 blink::WebNotificationPermission |
36 PlatformNotificationServiceImpl::CheckPermission( | 55 PlatformNotificationServiceImpl::CheckPermission( |
37 content::ResourceContext* resource_context, | 56 content::ResourceContext* resource_context, |
38 const GURL& origin, | 57 const GURL& origin, |
39 int render_process_id) { | 58 int render_process_id) { |
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
41 | 60 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 content::BrowserContext* browser_context, | 98 content::BrowserContext* browser_context, |
80 const content::ShowDesktopNotificationHostMsgParams& params, | 99 const content::ShowDesktopNotificationHostMsgParams& params, |
81 scoped_ptr<content::DesktopNotificationDelegate> delegate, | 100 scoped_ptr<content::DesktopNotificationDelegate> delegate, |
82 int render_process_id, | 101 int render_process_id, |
83 base::Closure* cancel_callback) { | 102 base::Closure* cancel_callback) { |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
85 | 104 |
86 Profile* profile = Profile::FromBrowserContext(browser_context); | 105 Profile* profile = Profile::FromBrowserContext(browser_context); |
87 DCHECK(profile); | 106 DCHECK(profile); |
88 | 107 |
89 DesktopNotificationService* service = | 108 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); |
90 DesktopNotificationServiceFactory::GetForProfile(profile); | 109 base::string16 display_source = DisplayNameForOriginInProcessId( |
91 DCHECK(service); | 110 profile, params.origin, render_process_id); |
92 | 111 |
93 service->ShowDesktopNotification( | 112 // TODO(peter): Icons for Web Notifications are currently always requested for |
94 params, render_process_id, delegate.Pass(), cancel_callback); | 113 // 1x scale, whereas the displays on which they can be displayed can have a |
| 114 // different pixel density. Be smarter about this when the API gets updated |
| 115 // with a way for developers to specify images of different resolutions. |
| 116 Notification notification(params.origin, params.title, params.body, |
| 117 gfx::Image::CreateFrom1xBitmap(params.icon), |
| 118 display_source, params.replace_id, proxy); |
| 119 |
| 120 // Web Notifications do not timeout. |
| 121 notification.set_never_timeout(true); |
| 122 |
| 123 GetNotificationUIManager()->Add(notification, profile); |
| 124 if (cancel_callback) |
| 125 *cancel_callback = |
| 126 base::Bind(&CancelNotification, |
| 127 proxy->id(), |
| 128 NotificationUIManager::GetProfileID(profile)); |
| 129 |
95 profile->GetHostContentSettingsMap()->UpdateLastUsage( | 130 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
96 params.origin, params.origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 131 params.origin, params.origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
97 } | 132 } |
98 | 133 |
99 void PlatformNotificationServiceImpl::DisplayPersistentNotification( | 134 void PlatformNotificationServiceImpl::DisplayPersistentNotification( |
100 content::BrowserContext* browser_context, | 135 content::BrowserContext* browser_context, |
101 int64 service_worker_registration_id, | 136 int64 service_worker_registration_id, |
102 const content::ShowDesktopNotificationHostMsgParams& params, | 137 const content::ShowDesktopNotificationHostMsgParams& params, |
103 int render_process_id) { | 138 int render_process_id) { |
104 NOTIMPLEMENTED(); | 139 NOTIMPLEMENTED(); |
105 } | 140 } |
106 | 141 |
107 void PlatformNotificationServiceImpl::ClosePersistentNotification( | 142 void PlatformNotificationServiceImpl::ClosePersistentNotification( |
108 content::BrowserContext* browser_context, | 143 content::BrowserContext* browser_context, |
109 const std::string& persistent_notification_id) { | 144 const std::string& persistent_notification_id) { |
110 NOTIMPLEMENTED(); | 145 NOTIMPLEMENTED(); |
111 } | 146 } |
| 147 |
| 148 NotificationUIManager* |
| 149 PlatformNotificationServiceImpl::GetNotificationUIManager() const { |
| 150 if (notification_ui_manager_for_tests_) |
| 151 return notification_ui_manager_for_tests_; |
| 152 |
| 153 return g_browser_process->notification_ui_manager(); |
| 154 } |
| 155 |
| 156 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( |
| 157 NotificationUIManager* manager) { |
| 158 notification_ui_manager_for_tests_ = manager; |
| 159 } |
| 160 |
| 161 base::string16 PlatformNotificationServiceImpl::DisplayNameForOriginInProcessId( |
| 162 Profile* profile, const GURL& origin, int process_id) { |
| 163 #if defined(ENABLE_EXTENSIONS) |
| 164 // If the source is an extension, lookup the display name. |
| 165 if (origin.SchemeIs(extensions::kExtensionScheme)) { |
| 166 extensions::InfoMap* extension_info_map = |
| 167 extensions::ExtensionSystem::Get(profile)->info_map(); |
| 168 if (extension_info_map) { |
| 169 extensions::ExtensionSet extensions; |
| 170 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( |
| 171 origin, |
| 172 process_id, |
| 173 extensions::APIPermission::kNotifications, |
| 174 &extensions); |
| 175 DesktopNotificationService* desktop_notification_service = |
| 176 DesktopNotificationServiceFactory::GetForProfile(profile); |
| 177 DCHECK(desktop_notification_service); |
| 178 |
| 179 for (const auto& extension : extensions) { |
| 180 NotifierId notifier_id(NotifierId::APPLICATION, extension->id()); |
| 181 if (desktop_notification_service->IsNotifierEnabled(notifier_id)) |
| 182 return base::UTF8ToUTF16(extension->name()); |
| 183 } |
| 184 } |
| 185 } |
| 186 #endif |
| 187 |
| 188 return base::UTF8ToUTF16(origin.host()); |
| 189 } |
OLD | NEW |