| 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 23 matching lines...) Expand all Loading... |
| 34 #include "extensions/browser/info_map.h" | 34 #include "extensions/browser/info_map.h" |
| 35 #include "extensions/browser/suggest_permission_util.h" | 35 #include "extensions/browser/suggest_permission_util.h" |
| 36 #include "extensions/common/constants.h" | 36 #include "extensions/common/constants.h" |
| 37 #include "extensions/common/extension.h" | 37 #include "extensions/common/extension.h" |
| 38 #include "extensions/common/extension_set.h" | 38 #include "extensions/common/extension_set.h" |
| 39 #endif | 39 #endif |
| 40 | 40 |
| 41 using content::BrowserThread; | 41 using content::BrowserThread; |
| 42 using message_center::NotifierId; | 42 using message_center::NotifierId; |
| 43 | 43 |
| 44 namespace { | |
| 45 | |
| 46 void CancelNotification(const std::string& id, ProfileID profile_id) { | |
| 47 g_browser_process->notification_ui_manager()->CancelById(id, profile_id); | |
| 48 } | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 // DesktopNotificationService ------------------------------------------------- | 44 // DesktopNotificationService ------------------------------------------------- |
| 53 | 45 |
| 54 // static | 46 // static |
| 55 void DesktopNotificationService::RegisterProfilePrefs( | 47 void DesktopNotificationService::RegisterProfilePrefs( |
| 56 user_prefs::PrefRegistrySyncable* registry) { | 48 user_prefs::PrefRegistrySyncable* registry) { |
| 57 registry->RegisterListPref( | 49 registry->RegisterListPref( |
| 58 prefs::kMessageCenterDisabledExtensionIds, | 50 prefs::kMessageCenterDisabledExtensionIds, |
| 59 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 51 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 60 registry->RegisterListPref( | 52 registry->RegisterListPref( |
| 61 prefs::kMessageCenterDisabledSystemComponentIds, | 53 prefs::kMessageCenterDisabledSystemComponentIds, |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 } | 129 } |
| 138 #endif | 130 #endif |
| 139 | 131 |
| 140 RequestPermission(web_contents, | 132 RequestPermission(web_contents, |
| 141 request_id, | 133 request_id, |
| 142 requesting_origin, | 134 requesting_origin, |
| 143 user_gesture, | 135 user_gesture, |
| 144 result_callback); | 136 result_callback); |
| 145 } | 137 } |
| 146 | 138 |
| 147 void DesktopNotificationService::ShowDesktopNotification( | |
| 148 const content::ShowDesktopNotificationHostMsgParams& params, | |
| 149 int render_process_id, | |
| 150 scoped_ptr<content::DesktopNotificationDelegate> delegate, | |
| 151 base::Closure* cancel_callback) { | |
| 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 153 const GURL& origin = params.origin; | |
| 154 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); | |
| 155 | |
| 156 base::string16 display_source = DisplayNameForOriginInProcessId( | |
| 157 origin, render_process_id); | |
| 158 | |
| 159 // TODO(peter): Icons for Web Notifications are currently always requested for | |
| 160 // 1x scale, whereas the displays on which they can be displayed can have a | |
| 161 // different pixel density. Be smarter about this when the API gets updated | |
| 162 // with a way for developers to specify images of different resolutions. | |
| 163 Notification notification(origin, params.title, params.body, | |
| 164 gfx::Image::CreateFrom1xBitmap(params.icon), | |
| 165 display_source, params.replace_id, proxy); | |
| 166 | |
| 167 // The webkit notification doesn't timeout. | |
| 168 notification.set_never_timeout(true); | |
| 169 | |
| 170 g_browser_process->notification_ui_manager()->Add(notification, profile_); | |
| 171 if (cancel_callback) | |
| 172 *cancel_callback = | |
| 173 base::Bind(&CancelNotification, | |
| 174 proxy->id(), | |
| 175 NotificationUIManager::GetProfileID(profile_)); | |
| 176 | |
| 177 DesktopNotificationProfileUtil::UsePermission(profile_, origin); | |
| 178 } | |
| 179 | |
| 180 base::string16 DesktopNotificationService::DisplayNameForOriginInProcessId( | |
| 181 const GURL& origin, int process_id) { | |
| 182 #if defined(ENABLE_EXTENSIONS) | |
| 183 // If the source is an extension, lookup the display name. | |
| 184 if (origin.SchemeIs(extensions::kExtensionScheme)) { | |
| 185 extensions::InfoMap* extension_info_map = | |
| 186 extensions::ExtensionSystem::Get(profile_)->info_map(); | |
| 187 if (extension_info_map) { | |
| 188 extensions::ExtensionSet extensions; | |
| 189 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( | |
| 190 origin, | |
| 191 process_id, | |
| 192 extensions::APIPermission::kNotifications, | |
| 193 &extensions); | |
| 194 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); | |
| 195 iter != extensions.end(); ++iter) { | |
| 196 NotifierId notifier_id(NotifierId::APPLICATION, (*iter)->id()); | |
| 197 if (IsNotifierEnabled(notifier_id)) | |
| 198 return base::UTF8ToUTF16((*iter)->name()); | |
| 199 } | |
| 200 } | |
| 201 } | |
| 202 #endif | |
| 203 | |
| 204 return base::UTF8ToUTF16(origin.host()); | |
| 205 } | |
| 206 | |
| 207 bool DesktopNotificationService::IsNotifierEnabled( | 139 bool DesktopNotificationService::IsNotifierEnabled( |
| 208 const NotifierId& notifier_id) { | 140 const NotifierId& notifier_id) { |
| 209 switch (notifier_id.type) { | 141 switch (notifier_id.type) { |
| 210 case NotifierId::APPLICATION: | 142 case NotifierId::APPLICATION: |
| 211 return disabled_extension_ids_.find(notifier_id.id) == | 143 return disabled_extension_ids_.find(notifier_id.id) == |
| 212 disabled_extension_ids_.end(); | 144 disabled_extension_ids_.end(); |
| 213 case NotifierId::WEB_PAGE: | 145 case NotifierId::WEB_PAGE: |
| 214 return DesktopNotificationProfileUtil::GetContentSetting( | 146 return DesktopNotificationProfileUtil::GetContentSetting( |
| 215 profile_, notifier_id.url) == CONTENT_SETTING_ALLOW; | 147 profile_, notifier_id.url) == CONTENT_SETTING_ALLOW; |
| 216 case NotifierId::SYSTEM_COMPONENT: | 148 case NotifierId::SYSTEM_COMPONENT: |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 // Tell the IO thread that this extension's permission for notifications | 268 // Tell the IO thread that this extension's permission for notifications |
| 337 // has changed. | 269 // has changed. |
| 338 extensions::InfoMap* extension_info_map = | 270 extensions::InfoMap* extension_info_map = |
| 339 extensions::ExtensionSystem::Get(profile_)->info_map(); | 271 extensions::ExtensionSystem::Get(profile_)->info_map(); |
| 340 BrowserThread::PostTask( | 272 BrowserThread::PostTask( |
| 341 BrowserThread::IO, FROM_HERE, | 273 BrowserThread::IO, FROM_HERE, |
| 342 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, | 274 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, |
| 343 extension_info_map, notifier_id.id, !enabled)); | 275 extension_info_map, notifier_id.id, !enabled)); |
| 344 #endif | 276 #endif |
| 345 } | 277 } |
| OLD | NEW |