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.
5 #ifndef CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_
6 #define CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_
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"
28 #if defined(ENABLE_EXTENSIONS)
29 #include "extensions/browser/extension_registry_observer.h"
33 class NotificationDelegate
;
34 class NotificationUIManager
;
38 class DesktopNotificationDelegate
;
39 class RenderFrameHost
;
40 struct ShowDesktopNotificationHostMsgParams
;
43 #if defined(ENABLE_EXTENSIONS)
44 namespace extensions
{
45 class ExtensionRegistry
;
53 namespace user_prefs
{
54 class PrefRegistrySyncable
;
57 // Callback to be invoked when the result of a permission request is known.
58 typedef base::Callback
<void(blink::WebNotificationPermission
)>
59 NotificationPermissionCallback
;
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)
66 public extensions::ExtensionRegistryObserver
70 // Register profile-specific prefs of notifications.
71 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* prefs
);
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
,
82 explicit DesktopNotificationService(Profile
* profile
);
83 virtual ~DesktopNotificationService();
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_origin
,
92 const NotificationPermissionCallback
& callback
);
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
);
102 // Returns true if the notifier with |notifier_id| is allowed to send
104 bool IsNotifierEnabled(const message_center::NotifierId
& notifier_id
);
106 // Updates the availability of the notifier.
107 void SetNotifierEnabled(const message_center::NotifierId
& notifier_id
,
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
,
117 // Called when the string list pref has been changed.
118 void OnStringListPrefChanged(
119 const char* pref_name
, std::set
<std::string
>* ids_field
);
121 // Called when the disabled_extension_id pref has been changed.
122 void OnDisabledExtensionIdsChanged();
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
,
130 void FirePermissionLevelChangedEvent(
131 const message_center::NotifierId
& notifier_id
,
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
;
142 // PermissionContextBase:
143 virtual void UpdateContentSetting(const GURL
& requesting_origin
,
144 const GURL
& embedder_origin
,
145 bool allowed
) override
;
147 // The profile which owns this object.
150 // Prefs listener for disabled_extension_id.
151 StringListPrefMember disabled_extension_id_pref_
;
153 // Prefs listener for disabled_system_component_id.
154 StringListPrefMember disabled_system_component_id_pref_
;
156 // On-memory data for the availability of extensions.
157 std::set
<std::string
> disabled_extension_ids_
;
159 // On-memory data for the availability of system_component.
160 std::set
<std::string
> disabled_system_component_ids_
;
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_
;
169 base::WeakPtrFactory
<DesktopNotificationService
> weak_factory_
;
171 DISALLOW_COPY_AND_ASSIGN(DesktopNotificationService
);
174 #endif // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_