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/content_settings_provider.h"
21 #include "chrome/browser/content_settings/permission_context_base.h"
22 #include "chrome/browser/notifications/extension_welcome_notification.h"
23 #include "chrome/common/content_settings.h"
24 #include "components/keyed_service/core/keyed_service.h"
25 #include "extensions/browser/extension_registry_observer.h"
26 #include "third_party/WebKit/public/platform/WebNotificationPermission.h"
27 #include "third_party/WebKit/public/web/WebTextDirection.h"
28 #include "ui/message_center/notifier_settings.h"
31 class ContentSettingsPattern
;
33 class NotificationDelegate
;
34 class NotificationUIManager
;
38 class DesktopNotificationDelegate
;
39 class RenderFrameHost
;
40 struct ShowDesktopNotificationHostMsgParams
;
43 namespace extensions
{
44 class ExtensionRegistry
;
51 namespace user_prefs
{
52 class PrefRegistrySyncable
;
55 // Callback to be invoked when the result of a permission request is known.
56 typedef base::Callback
<void(blink::WebNotificationPermission
)>
57 NotificationPermissionCallback
;
59 // The DesktopNotificationService is an object, owned by the Profile,
60 // which provides the creation of desktop "toasts" to web pages and workers.
61 class DesktopNotificationService
62 : public PermissionContextBase
,
63 public extensions::ExtensionRegistryObserver
{
65 // Register profile-specific prefs of notifications.
66 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* prefs
);
68 DesktopNotificationService(Profile
* profile
,
69 NotificationUIManager
* ui_manager
);
70 virtual ~DesktopNotificationService();
72 // Requests Web Notification permission for |requesting_frame|. The |callback|
73 // will be invoked after the user has made a decision.
74 void RequestNotificationPermission(
75 content::WebContents
* web_contents
,
76 const PermissionRequestID
& request_id
,
77 const GURL
& requesting_frame
,
79 const NotificationPermissionCallback
& callback
);
81 // Show a desktop notification. If |cancel_callback| is non-null, it's set to
82 // a callback which can be used to cancel the notification.
83 void ShowDesktopNotification(
84 const content::ShowDesktopNotificationHostMsgParams
& params
,
85 content::RenderFrameHost
* render_frame_host
,
86 scoped_ptr
<content::DesktopNotificationDelegate
> delegate
,
87 base::Closure
* cancel_callback
);
89 // Creates a data:xxxx URL which contains the full HTML for a notification
90 // using supplied icon, title, and text, run through a template which contains
91 // the standard formatting for notifications.
92 static base::string16
CreateDataUrl(const GURL
& icon_url
,
93 const base::string16
& title
,
94 const base::string16
& body
,
95 blink::WebTextDirection dir
);
97 // Creates a data:xxxx URL which contains the full HTML for a notification
98 // using resource template which contains the standard formatting for
100 static base::string16
CreateDataUrl(int resource
,
101 const std::vector
<std::string
>& subst
);
103 // Add a desktop notification.
104 static std::string
AddIconNotification(const GURL
& origin_url
,
105 const base::string16
& title
,
106 const base::string16
& message
,
107 const gfx::Image
& icon
,
108 const base::string16
& replace_id
,
109 NotificationDelegate
* delegate
,
112 // Returns true if the notifier with |notifier_id| is allowed to send
114 bool IsNotifierEnabled(const message_center::NotifierId
& notifier_id
);
116 // Updates the availability of the notifier.
117 void SetNotifierEnabled(const message_center::NotifierId
& notifier_id
,
120 // Adds in a the welcome notification if required for components built
121 // into Chrome that show notifications like Chrome Now.
122 void ShowWelcomeNotificationIfNecessary(const Notification
& notification
);
125 // Returns a display name for an origin in the process id, to be used in
126 // permission infobar or on the frame of the notification toast. Different
127 // from the origin itself when dealing with extensions.
128 base::string16
DisplayNameForOriginInProcessId(const GURL
& origin
,
130 NotificationUIManager
* GetUIManager();
132 // Called when the string list pref has been changed.
133 void OnStringListPrefChanged(
134 const char* pref_name
, std::set
<std::string
>* ids_field
);
136 // Called when the disabled_extension_id pref has been changed.
137 void OnDisabledExtensionIdsChanged();
139 // Used as a callback once a permission has been decided to convert |allowed|
140 // to one of the blink::WebNotificationPermission values.
141 void OnNotificationPermissionRequested(
142 const base::Callback
<void(blink::WebNotificationPermission
)>& callback
,
145 void FirePermissionLevelChangedEvent(
146 const message_center::NotifierId
& notifier_id
,
149 // extensions::ExtensionRegistryObserver:
150 virtual void OnExtensionUninstalled(
151 content::BrowserContext
* browser_context
,
152 const extensions::Extension
* extension
,
153 extensions::UninstallReason reason
) OVERRIDE
;
155 // The profile which owns this object.
158 // Non-owned pointer to the notification manager which manages the
159 // UI for desktop toasts.
160 NotificationUIManager
* ui_manager_
;
162 // Prefs listener for disabled_extension_id.
163 StringListPrefMember disabled_extension_id_pref_
;
165 // Prefs listener for disabled_system_component_id.
166 StringListPrefMember disabled_system_component_id_pref_
;
168 // On-memory data for the availability of extensions.
169 std::set
<std::string
> disabled_extension_ids_
;
171 // On-memory data for the availability of system_component.
172 std::set
<std::string
> disabled_system_component_ids_
;
174 // An observer to listen when extension is uninstalled.
175 ScopedObserver
<extensions::ExtensionRegistry
,
176 extensions::ExtensionRegistryObserver
>
177 extension_registry_observer_
;
179 // Welcome Notification
180 scoped_ptr
<ExtensionWelcomeNotification
> chrome_now_welcome_notification_
;
182 base::WeakPtrFactory
<DesktopNotificationService
> weak_factory_
;
184 DISALLOW_COPY_AND_ASSIGN(DesktopNotificationService
);
187 #endif // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_