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