| 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 15 matching lines...) Expand all Loading... |
| 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" | 33 #include "content/public/browser/render_view_host.h" |
| 34 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
| 35 #include "content/public/common/show_desktop_notification_params.h" | 35 #include "content/public/common/show_desktop_notification_params.h" |
| 36 #include "grit/browser_resources.h" | |
| 37 #include "net/base/escape.h" | |
| 38 #include "ui/base/resource/resource_bundle.h" | |
| 39 #include "ui/base/webui/web_ui_util.h" | 36 #include "ui/base/webui/web_ui_util.h" |
| 40 #include "ui/message_center/notifier_settings.h" | 37 #include "ui/message_center/notifier_settings.h" |
| 41 | 38 |
| 42 #if defined(ENABLE_EXTENSIONS) | 39 #if defined(ENABLE_EXTENSIONS) |
| 43 #include "chrome/browser/extensions/api/notifications/notifications_api.h" | 40 #include "chrome/browser/extensions/api/notifications/notifications_api.h" |
| 44 #include "chrome/browser/extensions/extension_service.h" | 41 #include "chrome/browser/extensions/extension_service.h" |
| 45 #include "extensions/browser/event_router.h" | 42 #include "extensions/browser/event_router.h" |
| 46 #include "extensions/browser/extension_registry.h" | 43 #include "extensions/browser/extension_registry.h" |
| 47 #include "extensions/browser/extension_system.h" | 44 #include "extensions/browser/extension_system.h" |
| 48 #include "extensions/browser/extension_util.h" | 45 #include "extensions/browser/extension_util.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 61 namespace { | 58 namespace { |
| 62 | 59 |
| 63 const char kChromeNowExtensionID[] = "pafkbggdmjlpgkdkcbjmhmfcdpncadgh"; | 60 const char kChromeNowExtensionID[] = "pafkbggdmjlpgkdkcbjmhmfcdpncadgh"; |
| 64 | 61 |
| 65 void CancelNotification(const std::string& id) { | 62 void CancelNotification(const std::string& id) { |
| 66 g_browser_process->notification_ui_manager()->CancelById(id); | 63 g_browser_process->notification_ui_manager()->CancelById(id); |
| 67 } | 64 } |
| 68 | 65 |
| 69 } // namespace | 66 } // namespace |
| 70 | 67 |
| 71 | |
| 72 // DesktopNotificationService ------------------------------------------------- | 68 // DesktopNotificationService ------------------------------------------------- |
| 73 | 69 |
| 74 // static | 70 // static |
| 75 void DesktopNotificationService::RegisterProfilePrefs( | 71 void DesktopNotificationService::RegisterProfilePrefs( |
| 76 user_prefs::PrefRegistrySyncable* registry) { | 72 user_prefs::PrefRegistrySyncable* registry) { |
| 77 registry->RegisterListPref( | 73 registry->RegisterListPref( |
| 78 prefs::kMessageCenterDisabledExtensionIds, | 74 prefs::kMessageCenterDisabledExtensionIds, |
| 79 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 75 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 80 registry->RegisterListPref( | 76 registry->RegisterListPref( |
| 81 prefs::kMessageCenterDisabledSystemComponentIds, | 77 prefs::kMessageCenterDisabledSystemComponentIds, |
| 82 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 78 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 83 ExtensionWelcomeNotification::RegisterProfilePrefs(registry); | 79 ExtensionWelcomeNotification::RegisterProfilePrefs(registry); |
| 84 } | 80 } |
| 85 | 81 |
| 86 // static | 82 // static |
| 87 base::string16 DesktopNotificationService::CreateDataUrl( | |
| 88 const GURL& icon_url, | |
| 89 const base::string16& title, | |
| 90 const base::string16& body, | |
| 91 WebTextDirection dir) { | |
| 92 int resource; | |
| 93 std::vector<std::string> subst; | |
| 94 if (icon_url.is_valid()) { | |
| 95 resource = IDR_NOTIFICATION_ICON_HTML; | |
| 96 subst.push_back(icon_url.spec()); | |
| 97 subst.push_back(net::EscapeForHTML(base::UTF16ToUTF8(title))); | |
| 98 subst.push_back(net::EscapeForHTML(base::UTF16ToUTF8(body))); | |
| 99 // icon float position | |
| 100 subst.push_back(dir == blink::WebTextDirectionRightToLeft ? | |
| 101 "right" : "left"); | |
| 102 } else if (title.empty() || body.empty()) { | |
| 103 resource = IDR_NOTIFICATION_1LINE_HTML; | |
| 104 base::string16 line = title.empty() ? body : title; | |
| 105 // Strings are div names in the template file. | |
| 106 base::string16 line_name = | |
| 107 title.empty() ? base::ASCIIToUTF16("description") | |
| 108 : base::ASCIIToUTF16("title"); | |
| 109 subst.push_back(net::EscapeForHTML(base::UTF16ToUTF8(line_name))); | |
| 110 subst.push_back(net::EscapeForHTML(base::UTF16ToUTF8(line))); | |
| 111 } else { | |
| 112 resource = IDR_NOTIFICATION_2LINE_HTML; | |
| 113 subst.push_back(net::EscapeForHTML(base::UTF16ToUTF8(title))); | |
| 114 subst.push_back(net::EscapeForHTML(base::UTF16ToUTF8(body))); | |
| 115 } | |
| 116 // body text direction | |
| 117 subst.push_back(dir == blink::WebTextDirectionRightToLeft ? | |
| 118 "rtl" : "ltr"); | |
| 119 | |
| 120 return CreateDataUrl(resource, subst); | |
| 121 } | |
| 122 | |
| 123 // static | |
| 124 base::string16 DesktopNotificationService::CreateDataUrl( | |
| 125 int resource, const std::vector<std::string>& subst) { | |
| 126 const base::StringPiece template_html( | |
| 127 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 128 resource)); | |
| 129 | |
| 130 if (template_html.empty()) { | |
| 131 NOTREACHED() << "unable to load template. ID: " << resource; | |
| 132 return base::string16(); | |
| 133 } | |
| 134 | |
| 135 std::string data = ReplaceStringPlaceholders(template_html, subst, NULL); | |
| 136 return base::UTF8ToUTF16("data:text/html;charset=utf-8," + | |
| 137 net::EscapeQueryParamValue(data, false)); | |
| 138 } | |
| 139 | |
| 140 // static | |
| 141 std::string DesktopNotificationService::AddIconNotification( | 83 std::string DesktopNotificationService::AddIconNotification( |
| 142 const GURL& origin_url, | 84 const GURL& origin_url, |
| 143 const base::string16& title, | 85 const base::string16& title, |
| 144 const base::string16& message, | 86 const base::string16& message, |
| 145 const gfx::Image& icon, | 87 const gfx::Image& icon, |
| 146 const base::string16& replace_id, | 88 const base::string16& replace_id, |
| 147 NotificationDelegate* delegate, | 89 NotificationDelegate* delegate, |
| 148 Profile* profile) { | 90 Profile* profile) { |
| 149 Notification notification(origin_url, icon, title, message, | 91 Notification notification(origin_url, icon, title, message, |
| 150 blink::WebTextDirectionDefault, | 92 blink::WebTextDirectionDefault, |
| 151 base::string16(), replace_id, delegate); | 93 base::string16(), replace_id, delegate); |
| 152 g_browser_process->notification_ui_manager()->Add(notification, profile); | 94 g_browser_process->notification_ui_manager()->Add(notification, profile); |
| 153 return notification.delegate_id(); | 95 return notification.delegate_id(); |
| 154 } | 96 } |
| 155 | 97 |
| 156 DesktopNotificationService::DesktopNotificationService( | 98 DesktopNotificationService::DesktopNotificationService(Profile* profile) |
| 157 Profile* profile, | |
| 158 NotificationUIManager* ui_manager) | |
| 159 : PermissionContextBase(profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS), | 99 : PermissionContextBase(profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS), |
| 160 profile_(profile), | 100 profile_(profile), |
| 161 ui_manager_(ui_manager), | |
| 162 extension_registry_observer_(this), | 101 extension_registry_observer_(this), |
| 163 weak_factory_(this) { | 102 weak_factory_(this) { |
| 164 OnStringListPrefChanged( | 103 OnStringListPrefChanged( |
| 165 prefs::kMessageCenterDisabledExtensionIds, &disabled_extension_ids_); | 104 prefs::kMessageCenterDisabledExtensionIds, &disabled_extension_ids_); |
| 166 OnStringListPrefChanged( | 105 OnStringListPrefChanged( |
| 167 prefs::kMessageCenterDisabledSystemComponentIds, | 106 prefs::kMessageCenterDisabledSystemComponentIds, |
| 168 &disabled_system_component_ids_); | 107 &disabled_system_component_ids_); |
| 169 disabled_extension_id_pref_.Init( | 108 disabled_extension_id_pref_.Init( |
| 170 prefs::kMessageCenterDisabledExtensionIds, | 109 prefs::kMessageCenterDisabledExtensionIds, |
| 171 profile_->GetPrefs(), | 110 profile_->GetPrefs(), |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 | 157 |
| 219 base::string16 display_source = DisplayNameForOriginInProcessId( | 158 base::string16 display_source = DisplayNameForOriginInProcessId( |
| 220 origin, render_frame_host->GetProcess()->GetID()); | 159 origin, render_frame_host->GetProcess()->GetID()); |
| 221 Notification notification(origin, params.icon_url, params.title, | 160 Notification notification(origin, params.icon_url, params.title, |
| 222 params.body, params.direction, display_source, params.replace_id, | 161 params.body, params.direction, display_source, params.replace_id, |
| 223 proxy); | 162 proxy); |
| 224 | 163 |
| 225 // The webkit notification doesn't timeout. | 164 // The webkit notification doesn't timeout. |
| 226 notification.set_never_timeout(true); | 165 notification.set_never_timeout(true); |
| 227 | 166 |
| 228 GetUIManager()->Add(notification, profile_); | 167 g_browser_process->notification_ui_manager()->Add(notification, profile_); |
| 229 if (cancel_callback) | 168 if (cancel_callback) |
| 230 *cancel_callback = base::Bind(&CancelNotification, proxy->id()); | 169 *cancel_callback = base::Bind(&CancelNotification, proxy->id()); |
| 231 | 170 |
| 232 DesktopNotificationProfileUtil::UsePermission(profile_, origin); | 171 DesktopNotificationProfileUtil::UsePermission(profile_, origin); |
| 233 } | 172 } |
| 234 | 173 |
| 235 base::string16 DesktopNotificationService::DisplayNameForOriginInProcessId( | 174 base::string16 DesktopNotificationService::DisplayNameForOriginInProcessId( |
| 236 const GURL& origin, int process_id) { | 175 const GURL& origin, int process_id) { |
| 237 #if defined(ENABLE_EXTENSIONS) | 176 #if defined(ENABLE_EXTENSIONS) |
| 238 // If the source is an extension, lookup the display name. | 177 // If the source is an extension, lookup the display name. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 252 if (IsNotifierEnabled(notifier_id)) | 191 if (IsNotifierEnabled(notifier_id)) |
| 253 return base::UTF8ToUTF16((*iter)->name()); | 192 return base::UTF8ToUTF16((*iter)->name()); |
| 254 } | 193 } |
| 255 } | 194 } |
| 256 } | 195 } |
| 257 #endif | 196 #endif |
| 258 | 197 |
| 259 return base::UTF8ToUTF16(origin.host()); | 198 return base::UTF8ToUTF16(origin.host()); |
| 260 } | 199 } |
| 261 | 200 |
| 262 NotificationUIManager* DesktopNotificationService::GetUIManager() { | |
| 263 // We defer setting ui_manager_ to the global singleton until we need it | |
| 264 // in order to avoid UI dependent construction during startup. | |
| 265 if (!ui_manager_) | |
| 266 ui_manager_ = g_browser_process->notification_ui_manager(); | |
| 267 return ui_manager_; | |
| 268 } | |
| 269 | |
| 270 bool DesktopNotificationService::IsNotifierEnabled( | 201 bool DesktopNotificationService::IsNotifierEnabled( |
| 271 const NotifierId& notifier_id) { | 202 const NotifierId& notifier_id) { |
| 272 switch (notifier_id.type) { | 203 switch (notifier_id.type) { |
| 273 case NotifierId::APPLICATION: | 204 case NotifierId::APPLICATION: |
| 274 return disabled_extension_ids_.find(notifier_id.id) == | 205 return disabled_extension_ids_.find(notifier_id.id) == |
| 275 disabled_extension_ids_.end(); | 206 disabled_extension_ids_.end(); |
| 276 case NotifierId::WEB_PAGE: | 207 case NotifierId::WEB_PAGE: |
| 277 return DesktopNotificationProfileUtil::GetContentSetting( | 208 return DesktopNotificationProfileUtil::GetContentSetting( |
| 278 profile_, notifier_id.url) == CONTENT_SETTING_ALLOW; | 209 profile_, notifier_id.url) == CONTENT_SETTING_ALLOW; |
| 279 case NotifierId::SYSTEM_COMPONENT: | 210 case NotifierId::SYSTEM_COMPONENT: |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 // Tell the IO thread that this extension's permission for notifications | 335 // Tell the IO thread that this extension's permission for notifications |
| 405 // has changed. | 336 // has changed. |
| 406 extensions::InfoMap* extension_info_map = | 337 extensions::InfoMap* extension_info_map = |
| 407 extensions::ExtensionSystem::Get(profile_)->info_map(); | 338 extensions::ExtensionSystem::Get(profile_)->info_map(); |
| 408 BrowserThread::PostTask( | 339 BrowserThread::PostTask( |
| 409 BrowserThread::IO, FROM_HERE, | 340 BrowserThread::IO, FROM_HERE, |
| 410 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, | 341 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, |
| 411 extension_info_map, notifier_id.id, !enabled)); | 342 extension_info_map, notifier_id.id, !enabled)); |
| 412 #endif | 343 #endif |
| 413 } | 344 } |
| OLD | NEW |