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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_ 6 #define CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_ 7 8 #include <set> 9 #include <string> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/callback_forward.h" 14 #include "base/memory/ref_counted.h" 15 #include "base/memory/scoped_ptr.h" 16 #include "base/prefs/pref_member.h" 17 #include "base/strings/string16.h" 18 #include "chrome/browser/content_settings/content_settings_provider.h" 19 #include "chrome/browser/notifications/extension_welcome_notification.h" 20 #include "chrome/common/content_settings.h" 21 #include "components/keyed_service/core/keyed_service.h" 22 #include "content/public/browser/notification_observer.h" 23 #include "content/public/browser/notification_registrar.h" 24 #include "third_party/WebKit/public/web/WebNotificationPresenter.h" 25 #include "third_party/WebKit/public/web/WebTextDirection.h" 26 #include "ui/message_center/notifier_settings.h" 27 #include "url/gurl.h" 28 29 class ContentSettingsPattern; 30 class Notification; 31 class NotificationDelegate; 32 class NotificationUIManager; 33 class Profile; 34 35 namespace content { 36 class DesktopNotificationDelegate; 37 class RenderFrameHost; 38 struct ShowDesktopNotificationHostMsgParams; 39 } 40 41 namespace gfx { 42 class Image; 43 } 44 45 namespace user_prefs { 46 class PrefRegistrySyncable; 47 } 48 49 // The DesktopNotificationService is an object, owned by the Profile, 50 // which provides the creation of desktop "toasts" to web pages and workers. 51 class DesktopNotificationService : public KeyedService, 52 public content::NotificationObserver { 53 public: 54 // Register profile-specific prefs of notifications. 55 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* prefs); 56 57 DesktopNotificationService(Profile* profile, 58 NotificationUIManager* ui_manager); 59 virtual ~DesktopNotificationService(); 60 61 // Requests permission for a given origin. |callback| is run when the UI 62 // finishes. 63 void RequestPermission(const GURL& origin, 64 content::RenderFrameHost* render_frame_host, 65 const base::Closure& callback); 66 67 // Show a desktop notification. If |cancel_callback| is non-null, it's set to 68 // a callback which can be used to cancel the notification. 69 void ShowDesktopNotification( 70 const content::ShowDesktopNotificationHostMsgParams& params, 71 content::RenderFrameHost* render_frame_host, 72 content::DesktopNotificationDelegate* delegate, 73 base::Closure* cancel_callback); 74 75 // Methods to setup and modify permission preferences. 76 void GrantPermission(const GURL& origin); 77 void DenyPermission(const GURL& origin); 78 79 // Creates a data:xxxx URL which contains the full HTML for a notification 80 // using supplied icon, title, and text, run through a template which contains 81 // the standard formatting for notifications. 82 static base::string16 CreateDataUrl(const GURL& icon_url, 83 const base::string16& title, 84 const base::string16& body, 85 blink::WebTextDirection dir); 86 87 // Creates a data:xxxx URL which contains the full HTML for a notification 88 // using resource template which contains the standard formatting for 89 // notifications. 90 static base::string16 CreateDataUrl(int resource, 91 const std::vector<std::string>& subst); 92 93 // Add a desktop notification. 94 static std::string AddIconNotification(const GURL& origin_url, 95 const base::string16& title, 96 const base::string16& message, 97 const gfx::Image& icon, 98 const base::string16& replace_id, 99 NotificationDelegate* delegate, 100 Profile* profile); 101 102 // The default content setting determines how to handle origins that haven't 103 // been allowed or denied yet. If |provider_id| is not NULL, the id of the 104 // provider which provided the default setting is assigned to it. 105 ContentSetting GetDefaultContentSetting(std::string* provider_id); 106 void SetDefaultContentSetting(ContentSetting setting); 107 108 // NOTE: This should only be called on the UI thread. 109 void ResetToDefaultContentSetting(); 110 111 // Returns all notifications settings. |settings| is cleared before 112 // notifications setting are passed to it. 113 void GetNotificationsSettings(ContentSettingsForOneType* settings); 114 115 // Clears the notifications setting for the given pattern. 116 void ClearSetting(const ContentSettingsPattern& pattern); 117 118 // Clears the sets of explicitly allowed and denied origins. 119 void ResetAllOrigins(); 120 121 ContentSetting GetContentSetting(const GURL& origin); 122 123 // Returns true if the notifier with |notifier_id| is allowed to send 124 // notifications. 125 bool IsNotifierEnabled(const message_center::NotifierId& notifier_id); 126 127 // Updates the availability of the notifier. 128 void SetNotifierEnabled(const message_center::NotifierId& notifier_id, 129 bool enabled); 130 131 // Adds in a the welcome notification if required for components built 132 // into Chrome that show notifications like Chrome Now. 133 void ShowWelcomeNotificationIfNecessary(const Notification& notification); 134 135 private: 136 // Returns a display name for an origin in the process id, to be used in 137 // permission infobar or on the frame of the notification toast. Different 138 // from the origin itself when dealing with extensions. 139 base::string16 DisplayNameForOriginInProcessId(const GURL& origin, 140 int process_id); 141 142 // Notifies the observers when permissions settings change. 143 void NotifySettingsChange(); 144 145 NotificationUIManager* GetUIManager(); 146 147 // Called when the string list pref has been changed. 148 void OnStringListPrefChanged( 149 const char* pref_name, std::set<std::string>* ids_field); 150 151 // Called when the disabled_extension_id pref has been changed. 152 void OnDisabledExtensionIdsChanged(); 153 154 // Called when the disabled_system_component_id pref has been changed. 155 void OnDisabledSystemComponentIdsChanged(); 156 157 // Called when the enabled_sync_notifier_id pref has been changed. 158 void OnEnabledSyncNotifierIdsChanged(); 159 160 void FirePermissionLevelChangedEvent( 161 const message_center::NotifierId& notifier_id, 162 bool enabled); 163 164 // content::NotificationObserver: 165 virtual void Observe(int type, 166 const content::NotificationSource& source, 167 const content::NotificationDetails& details) OVERRIDE; 168 169 // The profile which owns this object. 170 Profile* profile_; 171 172 // Non-owned pointer to the notification manager which manages the 173 // UI for desktop toasts. 174 NotificationUIManager* ui_manager_; 175 176 // Prefs listener for disabled_extension_id. 177 StringListPrefMember disabled_extension_id_pref_; 178 179 // Prefs listener for disabled_system_component_id. 180 StringListPrefMember disabled_system_component_id_pref_; 181 182 // Prefs listener for enabled_sync_notifier_id. 183 StringListPrefMember enabled_sync_notifier_id_pref_; 184 185 // On-memory data for the availability of extensions. 186 std::set<std::string> disabled_extension_ids_; 187 188 // On-memory data for the availability of system_component. 189 std::set<std::string> disabled_system_component_ids_; 190 191 // On-memory data for the availability of sync notifiers. 192 std::set<std::string> enabled_sync_notifier_ids_; 193 194 // Registrar for the other kind of notifications (event signaling). 195 content::NotificationRegistrar registrar_; 196 197 // Welcome Notification 198 scoped_ptr<ExtensionWelcomeNotification> chrome_now_welcome_notification_; 199 200 DISALLOW_COPY_AND_ASSIGN(DesktopNotificationService); 201 }; 202 203 #endif // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_ 204