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 "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/notifications/notification.h" | |
10 #include "chrome/browser/notifications/notification_object_proxy.h" | 9 #include "chrome/browser/notifications/notification_object_proxy.h" |
11 #include "chrome/browser/notifications/notification_ui_manager.h" | 10 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 11 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/profiles/profile_io_data.h" | 13 #include "chrome/browser/profiles/profile_io_data.h" |
14 #include "components/content_settings/core/browser/host_content_settings_map.h" | 14 #include "components/content_settings/core/browser/host_content_settings_map.h" |
15 #include "components/content_settings/core/common/content_settings.h" | 15 #include "components/content_settings/core/common/content_settings.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/desktop_notification_delegate.h" | 17 #include "content/public/browser/desktop_notification_delegate.h" |
| 18 #include "content/public/browser/notification_event_dispatcher.h" |
18 #include "content/public/common/show_desktop_notification_params.h" | 19 #include "content/public/common/show_desktop_notification_params.h" |
19 #include "ui/message_center/notifier_settings.h" | 20 #include "ui/message_center/notifier_settings.h" |
20 | 21 |
21 #if defined(ENABLE_EXTENSIONS) | 22 #if defined(ENABLE_EXTENSIONS) |
22 #include "chrome/browser/notifications/desktop_notification_service.h" | 23 #include "chrome/browser/notifications/desktop_notification_service.h" |
23 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 24 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
24 #include "extensions/browser/extension_system.h" | 25 #include "extensions/browser/extension_system.h" |
25 #include "extensions/browser/info_map.h" | 26 #include "extensions/browser/info_map.h" |
26 #include "extensions/common/constants.h" | 27 #include "extensions/common/constants.h" |
27 #include "extensions/common/extension_set.h" | 28 #include "extensions/common/extension_set.h" |
(...skipping 16 matching lines...) Expand all Loading... |
44 PlatformNotificationServiceImpl* | 45 PlatformNotificationServiceImpl* |
45 PlatformNotificationServiceImpl::GetInstance() { | 46 PlatformNotificationServiceImpl::GetInstance() { |
46 return Singleton<PlatformNotificationServiceImpl>::get(); | 47 return Singleton<PlatformNotificationServiceImpl>::get(); |
47 } | 48 } |
48 | 49 |
49 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() | 50 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() |
50 : notification_ui_manager_for_tests_(nullptr) {} | 51 : notification_ui_manager_for_tests_(nullptr) {} |
51 | 52 |
52 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} | 53 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} |
53 | 54 |
| 55 void PlatformNotificationServiceImpl::OnPersistentNotificationClick( |
| 56 content::BrowserContext* browser_context, |
| 57 int64 service_worker_registration_id, |
| 58 const std::string& notification_id, |
| 59 const content::ShowDesktopNotificationHostMsgParams& params, |
| 60 const base::Callback<void(content::PersistentNotificationStatus)>& |
| 61 callback) const { |
| 62 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 63 content::NotificationEventDispatcher::GetInstance() |
| 64 ->DispatchNotificationClickEvent( |
| 65 browser_context, |
| 66 params.origin, |
| 67 service_worker_registration_id, |
| 68 notification_id, |
| 69 params, |
| 70 callback); |
| 71 } |
| 72 |
54 blink::WebNotificationPermission | 73 blink::WebNotificationPermission |
55 PlatformNotificationServiceImpl::CheckPermission( | 74 PlatformNotificationServiceImpl::CheckPermission( |
56 content::ResourceContext* resource_context, | 75 content::ResourceContext* resource_context, |
57 const GURL& origin, | 76 const GURL& origin, |
58 int render_process_id) { | 77 int render_process_id) { |
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
60 | 79 |
61 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context); | 80 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context); |
62 #if defined(ENABLE_EXTENSIONS) | 81 #if defined(ENABLE_EXTENSIONS) |
63 extensions::InfoMap* extension_info_map = io_data->GetExtensionInfoMap(); | 82 extensions::InfoMap* extension_info_map = io_data->GetExtensionInfoMap(); |
64 | 83 |
65 // We want to see if there is an extension that hasn't been manually disabled | 84 // We want to see if there is an extension that hasn't been manually disabled |
66 // that has the notifications permission and applies to this security origin. | 85 // that has the notifications permission and applies to this security origin. |
67 // First, get the list of extensions with permission for the origin. | 86 // First, get the list of extensions with permission for the origin. |
68 extensions::ExtensionSet extensions; | 87 extensions::ExtensionSet extensions; |
69 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( | 88 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( |
(...skipping 23 matching lines...) Expand all Loading... |
93 | 112 |
94 return blink::WebNotificationPermissionDefault; | 113 return blink::WebNotificationPermissionDefault; |
95 } | 114 } |
96 | 115 |
97 void PlatformNotificationServiceImpl::DisplayNotification( | 116 void PlatformNotificationServiceImpl::DisplayNotification( |
98 content::BrowserContext* browser_context, | 117 content::BrowserContext* browser_context, |
99 const content::ShowDesktopNotificationHostMsgParams& params, | 118 const content::ShowDesktopNotificationHostMsgParams& params, |
100 scoped_ptr<content::DesktopNotificationDelegate> delegate, | 119 scoped_ptr<content::DesktopNotificationDelegate> delegate, |
101 int render_process_id, | 120 int render_process_id, |
102 base::Closure* cancel_callback) { | 121 base::Closure* cancel_callback) { |
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 122 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
104 | 123 |
105 Profile* profile = Profile::FromBrowserContext(browser_context); | 124 Profile* profile = Profile::FromBrowserContext(browser_context); |
106 DCHECK(profile); | 125 DCHECK(profile); |
107 | 126 |
108 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); | 127 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); |
109 base::string16 display_source = DisplayNameForOriginInProcessId( | 128 Notification notification = CreateNotificationFromData( |
110 profile, params.origin, render_process_id); | 129 profile, params, proxy, render_process_id); |
111 | |
112 // TODO(peter): Icons for Web Notifications are currently always requested for | |
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 | 130 |
123 GetNotificationUIManager()->Add(notification, profile); | 131 GetNotificationUIManager()->Add(notification, profile); |
124 if (cancel_callback) | 132 if (cancel_callback) |
125 *cancel_callback = | 133 *cancel_callback = |
126 base::Bind(&CancelNotification, | 134 base::Bind(&CancelNotification, |
127 proxy->id(), | 135 notification.delegate_id(), |
128 NotificationUIManager::GetProfileID(profile)); | 136 NotificationUIManager::GetProfileID(profile)); |
129 | 137 |
130 profile->GetHostContentSettingsMap()->UpdateLastUsage( | 138 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
131 params.origin, params.origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 139 params.origin, params.origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
132 } | 140 } |
133 | 141 |
134 void PlatformNotificationServiceImpl::DisplayPersistentNotification( | 142 void PlatformNotificationServiceImpl::DisplayPersistentNotification( |
135 content::BrowserContext* browser_context, | 143 content::BrowserContext* browser_context, |
136 int64 service_worker_registration_id, | 144 int64 service_worker_registration_id, |
137 const content::ShowDesktopNotificationHostMsgParams& params, | 145 const content::ShowDesktopNotificationHostMsgParams& params, |
138 int render_process_id) { | 146 int render_process_id) { |
139 NOTIMPLEMENTED(); | 147 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 148 |
| 149 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 150 DCHECK(profile); |
| 151 |
| 152 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( |
| 153 browser_context, service_worker_registration_id, params); |
| 154 |
| 155 Notification notification = CreateNotificationFromData( |
| 156 profile, params, delegate, render_process_id); |
| 157 |
| 158 GetNotificationUIManager()->Add(notification, profile); |
| 159 |
| 160 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
| 161 params.origin, params.origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
140 } | 162 } |
141 | 163 |
142 void PlatformNotificationServiceImpl::ClosePersistentNotification( | 164 void PlatformNotificationServiceImpl::ClosePersistentNotification( |
143 content::BrowserContext* browser_context, | 165 content::BrowserContext* browser_context, |
144 const std::string& persistent_notification_id) { | 166 const std::string& persistent_notification_id) { |
145 NOTIMPLEMENTED(); | 167 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 168 |
| 169 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 170 DCHECK(profile); |
| 171 |
| 172 GetNotificationUIManager()->CancelById( |
| 173 persistent_notification_id, NotificationUIManager::GetProfileID(profile)); |
| 174 } |
| 175 |
| 176 Notification PlatformNotificationServiceImpl::CreateNotificationFromData( |
| 177 Profile* profile, |
| 178 const content::ShowDesktopNotificationHostMsgParams& params, |
| 179 NotificationDelegate* delegate, |
| 180 int render_process_id) const { |
| 181 base::string16 display_source = DisplayNameForOriginInProcessId( |
| 182 profile, params.origin, render_process_id); |
| 183 |
| 184 // TODO(peter): Icons for Web Notifications are currently always requested for |
| 185 // 1x scale, whereas the displays on which they can be displayed can have a |
| 186 // different pixel density. Be smarter about this when the API gets updated |
| 187 // with a way for developers to specify images of different resolutions. |
| 188 Notification notification(params.origin, params.title, params.body, |
| 189 gfx::Image::CreateFrom1xBitmap(params.icon), |
| 190 display_source, params.replace_id, delegate); |
| 191 |
| 192 // Web Notifications do not timeout. |
| 193 notification.set_never_timeout(true); |
| 194 |
| 195 return notification; |
146 } | 196 } |
147 | 197 |
148 NotificationUIManager* | 198 NotificationUIManager* |
149 PlatformNotificationServiceImpl::GetNotificationUIManager() const { | 199 PlatformNotificationServiceImpl::GetNotificationUIManager() const { |
150 if (notification_ui_manager_for_tests_) | 200 if (notification_ui_manager_for_tests_) |
151 return notification_ui_manager_for_tests_; | 201 return notification_ui_manager_for_tests_; |
152 | 202 |
153 return g_browser_process->notification_ui_manager(); | 203 return g_browser_process->notification_ui_manager(); |
154 } | 204 } |
155 | 205 |
156 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( | 206 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( |
157 NotificationUIManager* manager) { | 207 NotificationUIManager* manager) { |
158 notification_ui_manager_for_tests_ = manager; | 208 notification_ui_manager_for_tests_ = manager; |
159 } | 209 } |
160 | 210 |
161 base::string16 PlatformNotificationServiceImpl::DisplayNameForOriginInProcessId( | 211 base::string16 PlatformNotificationServiceImpl::DisplayNameForOriginInProcessId( |
162 Profile* profile, const GURL& origin, int process_id) { | 212 Profile* profile, const GURL& origin, int process_id) const { |
163 #if defined(ENABLE_EXTENSIONS) | 213 #if defined(ENABLE_EXTENSIONS) |
164 // If the source is an extension, lookup the display name. | 214 // If the source is an extension, lookup the display name. |
165 if (origin.SchemeIs(extensions::kExtensionScheme)) { | 215 if (origin.SchemeIs(extensions::kExtensionScheme)) { |
166 extensions::InfoMap* extension_info_map = | 216 extensions::InfoMap* extension_info_map = |
167 extensions::ExtensionSystem::Get(profile)->info_map(); | 217 extensions::ExtensionSystem::Get(profile)->info_map(); |
168 if (extension_info_map) { | 218 if (extension_info_map) { |
169 extensions::ExtensionSet extensions; | 219 extensions::ExtensionSet extensions; |
170 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( | 220 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( |
171 origin, | 221 origin, |
172 process_id, | 222 process_id, |
173 extensions::APIPermission::kNotifications, | 223 extensions::APIPermission::kNotifications, |
174 &extensions); | 224 &extensions); |
175 DesktopNotificationService* desktop_notification_service = | 225 DesktopNotificationService* desktop_notification_service = |
176 DesktopNotificationServiceFactory::GetForProfile(profile); | 226 DesktopNotificationServiceFactory::GetForProfile(profile); |
177 DCHECK(desktop_notification_service); | 227 DCHECK(desktop_notification_service); |
178 | 228 |
179 for (const auto& extension : extensions) { | 229 for (const auto& extension : extensions) { |
180 NotifierId notifier_id(NotifierId::APPLICATION, extension->id()); | 230 NotifierId notifier_id(NotifierId::APPLICATION, extension->id()); |
181 if (desktop_notification_service->IsNotifierEnabled(notifier_id)) | 231 if (desktop_notification_service->IsNotifierEnabled(notifier_id)) |
182 return base::UTF8ToUTF16(extension->name()); | 232 return base::UTF8ToUTF16(extension->name()); |
183 } | 233 } |
184 } | 234 } |
185 } | 235 } |
186 #endif | 236 #endif |
187 | 237 |
188 return base::UTF8ToUTF16(origin.host()); | 238 return base::UTF8ToUTF16(origin.host()); |
189 } | 239 } |
OLD | NEW |