Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/desktop_notification_service.h" | 5 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/prefs/scoped_user_pref_update.h" | 9 #include "base/prefs/scoped_user_pref_update.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/browser/ui/browser.h" | 24 #include "chrome/browser/ui/browser.h" |
| 25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 26 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
| 27 #include "components/pref_registry/pref_registry_syncable.h" | 27 #include "components/pref_registry/pref_registry_syncable.h" |
| 28 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 29 #include "content/public/browser/desktop_notification_delegate.h" | 29 #include "content/public/browser/desktop_notification_delegate.h" |
| 30 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
| 31 #include "content/public/browser/render_frame_host.h" | 31 #include "content/public/browser/render_frame_host.h" |
| 32 #include "content/public/browser/render_process_host.h" | 32 #include "content/public/browser/render_process_host.h" |
| 33 #include "content/public/browser/render_view_host.h" | |
| 34 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
| 35 #include "content/public/common/show_desktop_notification_params.h" | 34 #include "content/public/common/show_desktop_notification_params.h" |
| 36 #include "ui/base/webui/web_ui_util.h" | 35 #include "ui/base/webui/web_ui_util.h" |
| 37 #include "ui/message_center/notifier_settings.h" | 36 #include "ui/message_center/notifier_settings.h" |
| 38 | 37 |
| 39 #if defined(ENABLE_EXTENSIONS) | 38 #if defined(ENABLE_EXTENSIONS) |
| 40 #include "chrome/browser/extensions/api/notifications/notifications_api.h" | 39 #include "chrome/browser/extensions/api/notifications/notifications_api.h" |
| 41 #include "chrome/browser/extensions/extension_service.h" | 40 #include "chrome/browser/extensions/extension_service.h" |
| 42 #include "extensions/browser/event_router.h" | 41 #include "extensions/browser/event_router.h" |
| 43 #include "extensions/browser/extension_registry.h" | 42 #include "extensions/browser/extension_registry.h" |
| 44 #include "extensions/browser/extension_system.h" | 43 #include "extensions/browser/extension_system.h" |
| 45 #include "extensions/browser/extension_util.h" | 44 #include "extensions/browser/extension_util.h" |
| 46 #include "extensions/browser/info_map.h" | 45 #include "extensions/browser/info_map.h" |
| 47 #include "extensions/common/constants.h" | 46 #include "extensions/common/constants.h" |
| 48 #include "extensions/common/extension.h" | 47 #include "extensions/common/extension.h" |
| 49 #include "extensions/common/extension_set.h" | 48 #include "extensions/common/extension_set.h" |
| 49 #include "extensions/common/permissions/permissions_data.h" | |
| 50 #endif | 50 #endif |
| 51 | 51 |
| 52 using blink::WebTextDirection; | 52 using blink::WebTextDirection; |
| 53 using content::BrowserThread; | 53 using content::BrowserThread; |
| 54 using content::RenderViewHost; | |
| 55 using content::WebContents; | 54 using content::WebContents; |
| 56 using message_center::NotifierId; | 55 using message_center::NotifierId; |
| 57 | 56 |
| 58 namespace { | 57 namespace { |
| 59 | 58 |
| 60 const char kChromeNowExtensionID[] = "pafkbggdmjlpgkdkcbjmhmfcdpncadgh"; | 59 const char kChromeNowExtensionID[] = "pafkbggdmjlpgkdkcbjmhmfcdpncadgh"; |
| 61 | 60 |
| 62 void CancelNotification(const std::string& id) { | 61 void CancelNotification(const std::string& id) { |
| 63 g_browser_process->notification_ui_manager()->CancelById(id); | 62 g_browser_process->notification_ui_manager()->CancelById(id); |
| 64 } | 63 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 request_id, | 139 request_id, |
| 141 requesting_frame, | 140 requesting_frame, |
| 142 user_gesture, | 141 user_gesture, |
| 143 base::Bind(&DesktopNotificationService::OnNotificationPermissionRequested, | 142 base::Bind(&DesktopNotificationService::OnNotificationPermissionRequested, |
| 144 weak_factory_.GetWeakPtr(), | 143 weak_factory_.GetWeakPtr(), |
| 145 callback)); | 144 callback)); |
| 146 } | 145 } |
| 147 | 146 |
| 148 void DesktopNotificationService::ShowDesktopNotification( | 147 void DesktopNotificationService::ShowDesktopNotification( |
| 149 const content::ShowDesktopNotificationHostMsgParams& params, | 148 const content::ShowDesktopNotificationHostMsgParams& params, |
| 150 content::RenderFrameHost* render_frame_host, | |
| 151 scoped_ptr<content::DesktopNotificationDelegate> delegate, | 149 scoped_ptr<content::DesktopNotificationDelegate> delegate, |
| 152 base::Closure* cancel_callback) { | 150 base::Closure* cancel_callback) { |
| 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 154 const GURL& origin = params.origin; | |
| 155 NotificationObjectProxy* proxy = | |
| 156 new NotificationObjectProxy(render_frame_host, delegate.Pass()); | |
| 157 | 152 |
| 158 base::string16 display_source = DisplayNameForOriginInProcessId( | 153 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); |
| 159 origin, render_frame_host->GetProcess()->GetID()); | 154 |
| 160 Notification notification(origin, params.icon_url, params.title, | 155 base::string16 display_source = DisplayNameForOrigin(params.origin); |
| 156 Notification notification(params.origin, params.icon_url, params.title, | |
| 161 params.body, params.direction, display_source, params.replace_id, | 157 params.body, params.direction, display_source, params.replace_id, |
| 162 proxy); | 158 proxy); |
| 163 | 159 |
| 164 // The webkit notification doesn't timeout. | 160 // Web Notifications never timeout. |
| 165 notification.set_never_timeout(true); | 161 notification.set_never_timeout(true); |
| 166 | 162 |
| 167 g_browser_process->notification_ui_manager()->Add(notification, profile_); | 163 g_browser_process->notification_ui_manager()->Add(notification, profile_); |
| 168 if (cancel_callback) | |
| 169 *cancel_callback = base::Bind(&CancelNotification, proxy->id()); | |
| 170 | 164 |
| 171 DesktopNotificationProfileUtil::UsePermission(profile_, origin); | 165 DCHECK(cancel_callback); |
| 166 *cancel_callback = base::Bind(&CancelNotification, proxy->id()); | |
| 167 | |
| 168 DesktopNotificationProfileUtil::UsePermission(profile_, params.origin); | |
| 172 } | 169 } |
| 173 | 170 |
| 174 base::string16 DesktopNotificationService::DisplayNameForOriginInProcessId( | 171 base::string16 DesktopNotificationService::DisplayNameForOrigin( |
|
Peter Beverloo
2014/09/17 16:24:08
This mimics the behavior of GetExtensionsWithAPIPe
dewittj
2014/09/17 16:58:48
+dcheng who wrote GetExtensionsWithAPIPermissionFo
dcheng
2014/10/03 18:40:06
It looks like I did this since it was suggested by
not at google - send to devlin
2014/10/17 16:12:27
Extension pages can end up being hosted in other p
| |
| 175 const GURL& origin, int process_id) { | 172 const GURL& origin) { |
| 176 #if defined(ENABLE_EXTENSIONS) | 173 #if defined(ENABLE_EXTENSIONS) |
| 177 // If the source is an extension, lookup the display name. | 174 // If the source is an extension, lookup the display name. |
| 178 if (origin.SchemeIs(extensions::kExtensionScheme)) { | 175 if (origin.SchemeIs(extensions::kExtensionScheme)) { |
| 179 extensions::InfoMap* extension_info_map = | 176 extensions::InfoMap* extension_info_map = |
| 180 extensions::ExtensionSystem::Get(profile_)->info_map(); | 177 extensions::ExtensionSystem::Get(profile_)->info_map(); |
| 181 if (extension_info_map) { | 178 if (extension_info_map) { |
| 182 extensions::ExtensionSet extensions; | 179 const std::string& extension_id = origin.host(); |
| 183 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( | 180 const extensions::Extension* extension = |
| 184 origin, | 181 extension_info_map->extensions().GetByID(extension_id); |
| 185 process_id, | 182 |
| 186 extensions::APIPermission::kNotifications, | 183 if (extension && |
| 187 &extensions); | 184 extension->permissions_data()->HasAPIPermission( |
| 188 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); | 185 extensions::APIPermission::kNotifications)) { |
| 189 iter != extensions.end(); ++iter) { | 186 NotifierId notifier_id(NotifierId::APPLICATION, extension_id); |
| 190 NotifierId notifier_id(NotifierId::APPLICATION, (*iter)->id()); | |
| 191 if (IsNotifierEnabled(notifier_id)) | 187 if (IsNotifierEnabled(notifier_id)) |
| 192 return base::UTF8ToUTF16((*iter)->name()); | 188 return base::UTF8ToUTF16(extension->name()); |
| 193 } | 189 } |
| 194 } | 190 } |
| 195 } | 191 } |
| 196 #endif | 192 #endif |
| 197 | 193 |
| 198 return base::UTF8ToUTF16(origin.host()); | 194 return base::UTF8ToUTF16(origin.host()); |
| 199 } | 195 } |
| 200 | 196 |
| 201 bool DesktopNotificationService::IsNotifierEnabled( | 197 bool DesktopNotificationService::IsNotifierEnabled( |
| 202 const NotifierId& notifier_id) { | 198 const NotifierId& notifier_id) { |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 335 // Tell the IO thread that this extension's permission for notifications | 331 // Tell the IO thread that this extension's permission for notifications |
| 336 // has changed. | 332 // has changed. |
| 337 extensions::InfoMap* extension_info_map = | 333 extensions::InfoMap* extension_info_map = |
| 338 extensions::ExtensionSystem::Get(profile_)->info_map(); | 334 extensions::ExtensionSystem::Get(profile_)->info_map(); |
| 339 BrowserThread::PostTask( | 335 BrowserThread::PostTask( |
| 340 BrowserThread::IO, FROM_HERE, | 336 BrowserThread::IO, FROM_HERE, |
| 341 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, | 337 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, |
| 342 extension_info_map, notifier_id.id, !enabled)); | 338 extension_info_map, notifier_id.id, !enabled)); |
| 343 #endif | 339 #endif |
| 344 } | 340 } |
| OLD | NEW |