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/memory/weak_ptr.h" 17 #include "base/prefs/pref_member.h" 18 #include "base/scoped_observer.h" 19 #include "base/strings/string16.h" 20 #include "chrome/browser/content_settings/permission_context_base.h" 21 #include "components/content_settings/core/common/content_settings.h" 22 #include "components/keyed_service/core/keyed_service.h" 23 #include "third_party/WebKit/public/platform/WebNotificationPermission.h" 24 #include "third_party/WebKit/public/web/WebTextDirection.h" 25 #include "ui/message_center/notifier_settings.h" 26 #include "url/gurl.h" 27 28 #if defined(ENABLE_EXTENSIONS) 29 #include "extensions/browser/extension_registry_observer.h" 30 #endif 31 32 class Notification; 33 class NotificationDelegate; 34 class NotificationUIManager; 35 class Profile; 36 37 namespace content { 38 class DesktopNotificationDelegate; 39 class RenderFrameHost; 40 struct ShowDesktopNotificationHostMsgParams; 41 } 42 43 #if defined(ENABLE_EXTENSIONS) 44 namespace extensions { 45 class ExtensionRegistry; 46 } 47 #endif 48 49 namespace gfx { 50 class Image; 51 } 52 53 namespace user_prefs { 54 class PrefRegistrySyncable; 55 } 56 57 // Callback to be invoked when the result of a permission request is known. 58 typedef base::Callback<void(blink::WebNotificationPermission)> 59 NotificationPermissionCallback; 60 61 // The DesktopNotificationService is an object, owned by the Profile, 62 // which provides the creation of desktop "toasts" to web pages and workers. 63 class DesktopNotificationService : public PermissionContextBase 64 #if defined(ENABLE_EXTENSIONS) 65 , 66 public extensions::ExtensionRegistryObserver 67 #endif 68 { 69 public: 70 // Register profile-specific prefs of notifications. 71 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* prefs); 72 73 // Add a desktop notification. 74 static std::string AddIconNotification(const GURL& origin_url, 75 const base::string16& title, 76 const base::string16& message, 77 const gfx::Image& icon, 78 const base::string16& replace_id, 79 NotificationDelegate* delegate, 80 Profile* profile); 81 82 explicit DesktopNotificationService(Profile* profile); 83 virtual ~DesktopNotificationService(); 84 85 // Requests Web Notification permission for |requesting_frame|. The |callback| 86 // will be invoked after the user has made a decision. 87 void RequestNotificationPermission( 88 content::WebContents* web_contents, 89 const PermissionRequestID& request_id, 90 const GURL& requesting_frame, 91 bool user_gesture, 92 const NotificationPermissionCallback& callback); 93 94 // Show a desktop notification. If |cancel_callback| is non-null, it's set to 95 // a callback which can be used to cancel the notification. 96 void ShowDesktopNotification( 97 const content::ShowDesktopNotificationHostMsgParams& params, 98 content::RenderFrameHost* render_frame_host, 99 scoped_ptr<content::DesktopNotificationDelegate> delegate, 100 base::Closure* cancel_callback); 101 102 // Returns true if the notifier with |notifier_id| is allowed to send 103 // notifications. 104 bool IsNotifierEnabled(const message_center::NotifierId& notifier_id); 105 106 // Updates the availability of the notifier. 107 void SetNotifierEnabled(const message_center::NotifierId& notifier_id, 108 bool enabled); 109 110 private: 111 // Returns a display name for an origin in the process id, to be used in 112 // permission infobar or on the frame of the notification toast. Different 113 // from the origin itself when dealing with extensions. 114 base::string16 DisplayNameForOriginInProcessId(const GURL& origin, 115 int process_id); 116 117 // Called when the string list pref has been changed. 118 void OnStringListPrefChanged( 119 const char* pref_name, std::set<std::string>* ids_field); 120 121 // Called when the disabled_extension_id pref has been changed. 122 void OnDisabledExtensionIdsChanged(); 123 124 // Used as a callback once a permission has been decided to convert |allowed| 125 // to one of the blink::WebNotificationPermission values. 126 void OnNotificationPermissionRequested( 127 const base::Callback<void(blink::WebNotificationPermission)>& callback, 128 bool allowed); 129 130 void FirePermissionLevelChangedEvent( 131 const message_center::NotifierId& notifier_id, 132 bool enabled); 133 134 #if defined(ENABLE_EXTENSIONS) 135 // extensions::ExtensionRegistryObserver: 136 virtual void OnExtensionUninstalled( 137 content::BrowserContext* browser_context, 138 const extensions::Extension* extension, 139 extensions::UninstallReason reason) OVERRIDE; 140 #endif 141 142 // PermissionContextBase: 143 virtual void UpdateContentSetting(const GURL& requesting_origin, 144 const GURL& embedder_origin, 145 bool allowed) OVERRIDE; 146 147 // The profile which owns this object. 148 Profile* profile_; 149 150 // Prefs listener for disabled_extension_id. 151 StringListPrefMember disabled_extension_id_pref_; 152 153 // Prefs listener for disabled_system_component_id. 154 StringListPrefMember disabled_system_component_id_pref_; 155 156 // On-memory data for the availability of extensions. 157 std::set<std::string> disabled_extension_ids_; 158 159 // On-memory data for the availability of system_component. 160 std::set<std::string> disabled_system_component_ids_; 161 162 #if defined(ENABLE_EXTENSIONS) 163 // An observer to listen when extension is uninstalled. 164 ScopedObserver<extensions::ExtensionRegistry, 165 extensions::ExtensionRegistryObserver> 166 extension_registry_observer_; 167 #endif 168 169 base::WeakPtrFactory<DesktopNotificationService> weak_factory_; 170 171 DISALLOW_COPY_AND_ASSIGN(DesktopNotificationService); 172 }; 173 174 #endif // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_ 175