| 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), | 101 ui_manager_(NULL), |
| 162 extension_registry_observer_(this), | 102 extension_registry_observer_(this), |
| 163 weak_factory_(this) { | 103 weak_factory_(this) { |
| 164 OnStringListPrefChanged( | 104 OnStringListPrefChanged( |
| 165 prefs::kMessageCenterDisabledExtensionIds, &disabled_extension_ids_); | 105 prefs::kMessageCenterDisabledExtensionIds, &disabled_extension_ids_); |
| 166 OnStringListPrefChanged( | 106 OnStringListPrefChanged( |
| 167 prefs::kMessageCenterDisabledSystemComponentIds, | 107 prefs::kMessageCenterDisabledSystemComponentIds, |
| 168 &disabled_system_component_ids_); | 108 &disabled_system_component_ids_); |
| 169 disabled_extension_id_pref_.Init( | 109 disabled_extension_id_pref_.Init( |
| 170 prefs::kMessageCenterDisabledExtensionIds, | 110 prefs::kMessageCenterDisabledExtensionIds, |
| 171 profile_->GetPrefs(), | 111 profile_->GetPrefs(), |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 // Tell the IO thread that this extension's permission for notifications | 344 // Tell the IO thread that this extension's permission for notifications |
| 405 // has changed. | 345 // has changed. |
| 406 extensions::InfoMap* extension_info_map = | 346 extensions::InfoMap* extension_info_map = |
| 407 extensions::ExtensionSystem::Get(profile_)->info_map(); | 347 extensions::ExtensionSystem::Get(profile_)->info_map(); |
| 408 BrowserThread::PostTask( | 348 BrowserThread::PostTask( |
| 409 BrowserThread::IO, FROM_HERE, | 349 BrowserThread::IO, FROM_HERE, |
| 410 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, | 350 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, |
| 411 extension_info_map, notifier_id.id, !enabled)); | 351 extension_info_map, notifier_id.id, !enabled)); |
| 412 #endif | 352 #endif |
| 413 } | 353 } |
| OLD | NEW |