// Copyright (c) 2012 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/desktop_notification_service.h" #include "base/metrics/histogram.h" #include "base/strings/utf_string_conversions.h" #include "base/threading/thread.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/content_settings/content_settings_details.h" #include "chrome/browser/content_settings/content_settings_provider.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/extensions/extension_info_map.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_system.h" #include "chrome/browser/infobars/confirm_infobar_delegate.h" #include "chrome/browser/infobars/infobar_service.h" #include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notification_object_proxy.h" #include "chrome/browser/notifications/notification_ui_manager.h" #include "chrome/browser/prefs/scoped_user_pref_update.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/content_settings.h" #include "chrome/common/content_settings_pattern.h" #include "chrome/common/pref_names.h" #include "chrome/common/url_constants.h" #include "components/user_prefs/pref_registry_syncable.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/render_view_host.h" #include "content/public/browser/web_contents.h" #include "content/public/common/show_desktop_notification_params.h" #include "extensions/common/constants.h" #include "grit/browser_resources.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" #include "grit/theme_resources.h" #include "net/base/escape.h" #include "third_party/WebKit/public/web/WebSecurityOrigin.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" #include "ui/message_center/message_center_util.h" #include "ui/message_center/notifier_settings.h" #include "ui/webui/web_ui_util.h" using content::BrowserThread; using content::RenderViewHost; using content::WebContents; using WebKit::WebNotificationPresenter; using WebKit::WebTextDirection; using WebKit::WebSecurityOrigin; const ContentSetting kDefaultSetting = CONTENT_SETTING_ASK; namespace { void ToggleListPrefItem(PrefService* prefs, const char* key, const std::string& item, bool flag) { ListPrefUpdate update(prefs, key); base::ListValue* const list = update.Get(); if (flag) { // AppendIfNotPresent will delete |adding_value| when the same value // already exists. base::StringValue* const adding_value = new base::StringValue(item); list->AppendIfNotPresent(adding_value); } else { base::StringValue removed_value(item); list->Remove(removed_value, NULL); } } void CopySetFromPrefToMemory(PrefService* prefs, const char* key, std::set* dst) { dst->clear(); const base::ListValue* pref_list = prefs->GetList(key); for (size_t i = 0; i < pref_list->GetSize(); ++i) { std::string element; if (!pref_list->GetString(i, &element) && element.empty()) { LOG(WARNING) << i << "-th element is not a string for " << key; continue; } dst->insert(element); } } } // namespace // NotificationPermissionInfoBarDelegate -------------------------------------- // The delegate for the infobar shown when an origin requests notification // permissions. class NotificationPermissionInfoBarDelegate : public ConfirmInfoBarDelegate { public: // Creates a notification permission delegate and adds it to // |infobar_service|. static void Create(InfoBarService* infobar_service, DesktopNotificationService* notification_service, const GURL& origin, const string16& display_name, int process_id, int route_id, int callback_context); private: NotificationPermissionInfoBarDelegate( InfoBarService* infobar_service, DesktopNotificationService* notification_service, const GURL& origin, const string16& display_name, int process_id, int route_id, int callback_context); virtual ~NotificationPermissionInfoBarDelegate(); // ConfirmInfoBarDelegate: virtual int GetIconID() const OVERRIDE; virtual Type GetInfoBarType() const OVERRIDE; virtual string16 GetMessageText() const OVERRIDE; virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; virtual bool Accept() OVERRIDE; virtual bool Cancel() OVERRIDE; // The origin we are asking for permissions on. GURL origin_; // The display name for the origin to be displayed. Will be different from // origin_ for extensions. string16 display_name_; // The notification service to be used. DesktopNotificationService* notification_service_; // The callback information that tells us how to respond to javascript via // the correct RenderView. int process_id_; int route_id_; int callback_context_; // Whether the user clicked one of the buttons. bool action_taken_; DISALLOW_COPY_AND_ASSIGN(NotificationPermissionInfoBarDelegate); }; // static void NotificationPermissionInfoBarDelegate::Create( InfoBarService* infobar_service, DesktopNotificationService* notification_service, const GURL& origin, const string16& display_name, int process_id, int route_id, int callback_context) { infobar_service->AddInfoBar(scoped_ptr( new NotificationPermissionInfoBarDelegate( infobar_service, notification_service, origin, display_name, process_id, route_id, callback_context))); } NotificationPermissionInfoBarDelegate::NotificationPermissionInfoBarDelegate( InfoBarService* infobar_service, DesktopNotificationService* notification_service, const GURL& origin, const string16& display_name, int process_id, int route_id, int callback_context) : ConfirmInfoBarDelegate(infobar_service), origin_(origin), display_name_(display_name), notification_service_(notification_service), process_id_(process_id), route_id_(route_id), callback_context_(callback_context), action_taken_(false) { } NotificationPermissionInfoBarDelegate:: ~NotificationPermissionInfoBarDelegate() { if (!action_taken_) UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Ignored", 1); RenderViewHost* host = RenderViewHost::FromID(process_id_, route_id_); if (host) host->DesktopNotificationPermissionRequestDone(callback_context_); } int NotificationPermissionInfoBarDelegate::GetIconID() const { return IDR_INFOBAR_DESKTOP_NOTIFICATIONS; } InfoBarDelegate::Type NotificationPermissionInfoBarDelegate::GetInfoBarType() const { return PAGE_ACTION_TYPE; } string16 NotificationPermissionInfoBarDelegate::GetMessageText() const { return l10n_util::GetStringFUTF16(IDS_NOTIFICATION_PERMISSIONS, display_name_); } string16 NotificationPermissionInfoBarDelegate::GetButtonLabel( InfoBarButton button) const { return l10n_util::GetStringUTF16((button == BUTTON_OK) ? IDS_NOTIFICATION_PERMISSION_YES : IDS_NOTIFICATION_PERMISSION_NO); } bool NotificationPermissionInfoBarDelegate::Accept() { UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Allowed", 1); notification_service_->GrantPermission(origin_); action_taken_ = true; return true; } bool NotificationPermissionInfoBarDelegate::Cancel() { UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Denied", 1); notification_service_->DenyPermission(origin_); action_taken_ = true; return true; } // DesktopNotificationService ------------------------------------------------- // static void DesktopNotificationService::RegisterUserPrefs( user_prefs::PrefRegistrySyncable* registry) { registry->RegisterListPref(prefs::kMessageCenterDisabledExtensionIds, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); registry->RegisterListPref(prefs::kMessageCenterDisabledSystemComponentIds, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); } // static string16 DesktopNotificationService::CreateDataUrl( const GURL& icon_url, const string16& title, const string16& body, WebTextDirection dir) { int resource; std::vector subst; if (icon_url.is_valid()) { resource = IDR_NOTIFICATION_ICON_HTML; subst.push_back(icon_url.spec()); subst.push_back(net::EscapeForHTML(UTF16ToUTF8(title))); subst.push_back(net::EscapeForHTML(UTF16ToUTF8(body))); // icon float position subst.push_back(dir == WebKit::WebTextDirectionRightToLeft ? "right" : "left"); } else if (title.empty() || body.empty()) { resource = IDR_NOTIFICATION_1LINE_HTML; string16 line = title.empty() ? body : title; // Strings are div names in the template file. string16 line_name = title.empty() ? ASCIIToUTF16("description") : ASCIIToUTF16("title"); subst.push_back(net::EscapeForHTML(UTF16ToUTF8(line_name))); subst.push_back(net::EscapeForHTML(UTF16ToUTF8(line))); } else { resource = IDR_NOTIFICATION_2LINE_HTML; subst.push_back(net::EscapeForHTML(UTF16ToUTF8(title))); subst.push_back(net::EscapeForHTML(UTF16ToUTF8(body))); } // body text direction subst.push_back(dir == WebKit::WebTextDirectionRightToLeft ? "rtl" : "ltr"); return CreateDataUrl(resource, subst); } // static string16 DesktopNotificationService::CreateDataUrl( int resource, const std::vector& subst) { const base::StringPiece template_html( ResourceBundle::GetSharedInstance().GetRawDataResource( resource)); if (template_html.empty()) { NOTREACHED() << "unable to load template. ID: " << resource; return string16(); } std::string data = ReplaceStringPlaceholders(template_html, subst, NULL); return UTF8ToUTF16("data:text/html;charset=utf-8," + net::EscapeQueryParamValue(data, false)); } // static std::string DesktopNotificationService::AddNotification( const GURL& origin_url, const string16& title, const string16& message, const GURL& icon_url, const string16& replace_id, NotificationDelegate* delegate, Profile* profile) { if (message_center::IsRichNotificationEnabled()) { // For message center create a non-HTML notification with |icon_url|. Notification notification(origin_url, icon_url, title, message, WebKit::WebTextDirectionDefault, string16(), replace_id, delegate); g_browser_process->notification_ui_manager()->Add(notification, profile); return notification.notification_id(); } // Generate a data URL embedding the icon URL, title, and message. GURL content_url(CreateDataUrl( icon_url, title, message, WebKit::WebTextDirectionDefault)); Notification notification( GURL(), content_url, string16(), replace_id, delegate); g_browser_process->notification_ui_manager()->Add(notification, profile); return notification.notification_id(); } // static std::string DesktopNotificationService::AddIconNotification( const GURL& origin_url, const string16& title, const string16& message, const gfx::Image& icon, const string16& replace_id, NotificationDelegate* delegate, Profile* profile) { if (message_center::IsRichNotificationEnabled()) { // For message center create a non-HTML notification with |icon|. Notification notification(origin_url, icon, title, message, WebKit::WebTextDirectionDefault, string16(), replace_id, delegate); g_browser_process->notification_ui_manager()->Add(notification, profile); return notification.notification_id(); } GURL icon_url; if (!icon.IsEmpty()) icon_url = GURL(webui::GetBitmapDataUrl(*icon.ToSkBitmap())); return AddNotification( origin_url, title, message, icon_url, replace_id, delegate, profile); } // static void DesktopNotificationService::RemoveNotification( const std::string& notification_id) { g_browser_process->notification_ui_manager()->CancelById(notification_id); } DesktopNotificationService::DesktopNotificationService( Profile* profile, NotificationUIManager* ui_manager) : profile_(profile), ui_manager_(ui_manager) { OnDisabledExtensionIdsChanged(); OnDisabledSystemComponentIdsChanged(); disabled_extension_id_pref_.Init( prefs::kMessageCenterDisabledExtensionIds, profile_->GetPrefs(), base::Bind( &DesktopNotificationService::OnDisabledExtensionIdsChanged, base::Unretained(this))); disabled_system_component_id_pref_.Init( prefs::kMessageCenterDisabledSystemComponentIds, profile_->GetPrefs(), base::Bind( &DesktopNotificationService::OnDisabledSystemComponentIdsChanged, base::Unretained(this))); registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, content::Source(profile_)); } DesktopNotificationService::~DesktopNotificationService() { } void DesktopNotificationService::GrantPermission(const GURL& origin) { ContentSettingsPattern primary_pattern = ContentSettingsPattern::FromURLNoWildcard(origin); profile_->GetHostContentSettingsMap()->SetContentSetting( primary_pattern, ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NO_RESOURCE_IDENTIFIER, CONTENT_SETTING_ALLOW); } void DesktopNotificationService::DenyPermission(const GURL& origin) { ContentSettingsPattern primary_pattern = ContentSettingsPattern::FromURLNoWildcard(origin); profile_->GetHostContentSettingsMap()->SetContentSetting( primary_pattern, ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NO_RESOURCE_IDENTIFIER, CONTENT_SETTING_BLOCK); } ContentSetting DesktopNotificationService::GetDefaultContentSetting( std::string* provider_id) { return profile_->GetHostContentSettingsMap()->GetDefaultContentSetting( CONTENT_SETTINGS_TYPE_NOTIFICATIONS, provider_id); } void DesktopNotificationService::SetDefaultContentSetting( ContentSetting setting) { profile_->GetHostContentSettingsMap()->SetDefaultContentSetting( CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting); } void DesktopNotificationService::ResetToDefaultContentSetting() { profile_->GetHostContentSettingsMap()->SetDefaultContentSetting( CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_DEFAULT); } void DesktopNotificationService::GetNotificationsSettings( ContentSettingsForOneType* settings) { profile_->GetHostContentSettingsMap()->GetSettingsForOneType( CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NO_RESOURCE_IDENTIFIER, settings); } void DesktopNotificationService::ClearSetting( const ContentSettingsPattern& pattern) { profile_->GetHostContentSettingsMap()->SetContentSetting( pattern, ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NO_RESOURCE_IDENTIFIER, CONTENT_SETTING_DEFAULT); } void DesktopNotificationService::ResetAllOrigins() { profile_->GetHostContentSettingsMap()->ClearSettingsForOneType( CONTENT_SETTINGS_TYPE_NOTIFICATIONS); } ContentSetting DesktopNotificationService::GetContentSetting( const GURL& origin) { return profile_->GetHostContentSettingsMap()->GetContentSetting( origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NO_RESOURCE_IDENTIFIER); } void DesktopNotificationService::RequestPermission( const GURL& origin, int process_id, int route_id, int callback_context, WebContents* contents) { // If |origin| hasn't been seen before and the default content setting for // notifications is "ask", show an infobar. // The cache can only answer queries on the IO thread once it's initialized, // so don't ask the cache. ContentSetting setting = GetContentSetting(origin); if (setting == CONTENT_SETTING_ASK) { // Show an info bar requesting permission. InfoBarService* infobar_service = InfoBarService::FromWebContents(contents); // |infobar_service| may be NULL, e.g., if this request originated in a // browser action popup, extension background page, or any HTML that runs // outside of a tab. if (infobar_service) { NotificationPermissionInfoBarDelegate::Create( infobar_service, DesktopNotificationServiceFactory::GetForProfile( Profile::FromBrowserContext(contents->GetBrowserContext())), origin, DisplayNameForOriginInProcessId(origin, process_id), process_id, route_id, callback_context); return; } } // Notify renderer immediately. RenderViewHost* host = RenderViewHost::FromID(process_id, route_id); if (host) host->DesktopNotificationPermissionRequestDone(callback_context); } #if !defined(OS_WIN) void DesktopNotificationService::ShowNotification( const Notification& notification) { GetUIManager()->Add(notification, profile_); } bool DesktopNotificationService::CancelDesktopNotification( int process_id, int route_id, int notification_id) { scoped_refptr proxy( new NotificationObjectProxy(process_id, route_id, notification_id, false)); return GetUIManager()->CancelById(proxy->id()); } #endif // OS_WIN bool DesktopNotificationService::ShowDesktopNotification( const content::ShowDesktopNotificationHostMsgParams& params, int process_id, int route_id, DesktopNotificationSource source) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); const GURL& origin = params.origin; NotificationObjectProxy* proxy = new NotificationObjectProxy(process_id, route_id, params.notification_id, source == WorkerNotification); string16 display_source = DisplayNameForOriginInProcessId(origin, process_id); if (params.is_html) { ShowNotification(Notification(origin, params.contents_url, display_source, params.replace_id, proxy)); } else { Notification notification(origin, params.icon_url, params.title, params.body, params.direction, display_source, params.replace_id, proxy); // The webkit notification doesn't timeout. notification.set_never_timeout(true); ShowNotification(notification); } return true; } string16 DesktopNotificationService::DisplayNameForOriginInProcessId( const GURL& origin, int process_id) { // If the source is an extension, lookup the display name. // Message center prefers to use extension name if the notification // is allowed by an extension. if (NotificationUIManager::DelegatesToMessageCenter() || origin.SchemeIs(extensions::kExtensionScheme)) { ExtensionInfoMap* extension_info_map = extensions::ExtensionSystem::Get(profile_)->info_map(); if (extension_info_map) { ExtensionSet extensions; extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( origin, process_id, extensions::APIPermission::kNotification, &extensions); for (ExtensionSet::const_iterator iter = extensions.begin(); iter != extensions.end(); ++iter) { if (IsExtensionEnabled((*iter)->id())) return UTF8ToUTF16((*iter)->name()); } } } return UTF8ToUTF16(origin.host()); } void DesktopNotificationService::NotifySettingsChange() { content::NotificationService::current()->Notify( chrome::NOTIFICATION_DESKTOP_NOTIFICATION_SETTINGS_CHANGED, content::Source(this), content::NotificationService::NoDetails()); } NotificationUIManager* DesktopNotificationService::GetUIManager() { // We defer setting ui_manager_ to the global singleton until we need it // in order to avoid UI dependent construction during startup. if (!ui_manager_) ui_manager_ = g_browser_process->notification_ui_manager(); return ui_manager_; } bool DesktopNotificationService::IsExtensionEnabled(const std::string& id) { return disabled_extension_ids_.find(id) == disabled_extension_ids_.end(); } void DesktopNotificationService::SetExtensionEnabled( const std::string& id, bool enabled) { // Do not touch |disabled_extension_ids_|. It will be updated at // OnDisabledExtensionIdsChanged() which will be called when the pref changes. ToggleListPrefItem( profile_->GetPrefs(), prefs::kMessageCenterDisabledExtensionIds, id, !enabled); } void DesktopNotificationService::OnDisabledExtensionIdsChanged() { CopySetFromPrefToMemory(profile_->GetPrefs(), prefs::kMessageCenterDisabledExtensionIds, &disabled_extension_ids_); } bool DesktopNotificationService::IsSystemComponentEnabled( message_center::Notifier::SystemComponentNotifierType type) { return disabled_system_component_ids_.find(message_center::ToString(type)) == disabled_system_component_ids_.end(); } void DesktopNotificationService::SetSystemComponentEnabled( message_center::Notifier::SystemComponentNotifierType type, bool enabled) { // Do not touch |disabled_extension_ids_|. It will be updated at // OnDisabledExtensionIdsChanged() which will be called when the pref changes. ToggleListPrefItem( profile_->GetPrefs(), prefs::kMessageCenterDisabledSystemComponentIds, message_center::ToString(type), !enabled); } void DesktopNotificationService::OnDisabledSystemComponentIdsChanged() { disabled_system_component_ids_.clear(); CopySetFromPrefToMemory(profile_->GetPrefs(), prefs::kMessageCenterDisabledSystemComponentIds, &disabled_system_component_ids_); } WebKit::WebNotificationPresenter::Permission DesktopNotificationService::HasPermission(const GURL& origin) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); HostContentSettingsMap* host_content_settings_map = profile_->GetHostContentSettingsMap(); ContentSetting setting = host_content_settings_map->GetContentSetting( origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NO_RESOURCE_IDENTIFIER); if (setting == CONTENT_SETTING_ALLOW) return WebKit::WebNotificationPresenter::PermissionAllowed; if (setting == CONTENT_SETTING_BLOCK) return WebKit::WebNotificationPresenter::PermissionDenied; if (setting == CONTENT_SETTING_ASK) return WebKit::WebNotificationPresenter::PermissionNotAllowed; NOTREACHED() << "Invalid notifications settings value: " << setting; return WebKit::WebNotificationPresenter::PermissionNotAllowed; } void DesktopNotificationService::Observe( int type, const content::NotificationSource& source, const content::NotificationDetails& details) { DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNINSTALLED, type); extensions::Extension* extension = content::Details(details).ptr(); if (IsExtensionEnabled(extension->id())) return; SetExtensionEnabled(extension->id(), true); }