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/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/prefs/pref_member.h"
16 #include "base/strings/string16.h"
17 #include "chrome/browser/content_settings/content_settings_provider.h"
18 #include "chrome/browser/notifications/extension_welcome_notification.h"
19 #include "chrome/common/content_settings.h"
20 #include "components/keyed_service/core/keyed_service.h"
21 #include "content/public/browser/notification_observer.h"
22 #include "content/public/browser/notification_registrar.h"
23 #include "third_party/WebKit/public/web/WebNotificationPresenter.h"
24 #include "third_party/WebKit/public/web/WebTextDirection.h"
25 #include "ui/message_center/notifier_settings.h"
28 class ContentSettingsPattern
;
30 class NotificationDelegate
;
31 class NotificationUIManager
;
36 struct ShowDesktopNotificationHostMsgParams
;
43 namespace user_prefs
{
44 class PrefRegistrySyncable
;
47 // The DesktopNotificationService is an object, owned by the Profile,
48 // which provides the creation of desktop "toasts" to web pages and workers.
49 class DesktopNotificationService
: public KeyedService
,
50 public content::NotificationObserver
{
52 enum DesktopNotificationSource
{
57 // Register profile-specific prefs of notifications.
58 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* prefs
);
60 DesktopNotificationService(Profile
* profile
,
61 NotificationUIManager
* ui_manager
);
62 virtual ~DesktopNotificationService();
64 // Requests permission (using an info-bar) for a given origin.
65 // |callback_context| contains an opaque value to pass back to the
66 // requesting process when the info-bar finishes.
67 void RequestPermission(const GURL
& origin
,
71 content::WebContents
* tab
);
73 // ShowNotification is called on the UI thread handling IPCs from a child
74 // process, identified by |process_id| and |route_id|. |source| indicates
75 // whether the script is in a worker or page. |params| contains all the
76 // other parameters supplied by the worker or page.
77 bool ShowDesktopNotification(
78 const content::ShowDesktopNotificationHostMsgParams
& params
,
81 DesktopNotificationSource source
);
83 // Cancels a notification. If it has already been shown, it will be
84 // removed from the screen. If it hasn't been shown yet, it won't be
86 bool CancelDesktopNotification(int process_id
,
90 // Methods to setup and modify permission preferences.
91 void GrantPermission(const GURL
& origin
);
92 void DenyPermission(const GURL
& origin
);
94 // Creates a data:xxxx URL which contains the full HTML for a notification
95 // using supplied icon, title, and text, run through a template which contains
96 // the standard formatting for notifications.
97 static base::string16
CreateDataUrl(const GURL
& icon_url
,
98 const base::string16
& title
,
99 const base::string16
& body
,
100 blink::WebTextDirection dir
);
102 // Creates a data:xxxx URL which contains the full HTML for a notification
103 // using resource template which contains the standard formatting for
105 static base::string16
CreateDataUrl(int resource
,
106 const std::vector
<std::string
>& subst
);
108 // Add a desktop notification. On non-Ash platforms this will generate a HTML
109 // notification from the input parameters. On Ash it will generate a normal
110 // ash notification. Returns the notification id.
111 // TODO(mukai): remove these methods. HTML notifications are no longer
113 static std::string
AddNotification(const GURL
& origin_url
,
114 const base::string16
& title
,
115 const base::string16
& message
,
116 const GURL
& icon_url
,
117 const base::string16
& replace_id
,
118 NotificationDelegate
* delegate
,
121 // Same as above, but takes a gfx::Image for the icon instead.
122 static std::string
AddIconNotification(const GURL
& origin_url
,
123 const base::string16
& title
,
124 const base::string16
& message
,
125 const gfx::Image
& icon
,
126 const base::string16
& replace_id
,
127 NotificationDelegate
* delegate
,
130 // Remove any active notification corresponding to |notification_id|.
131 static void RemoveNotification(const std::string
& notification_id
);
133 // The default content setting determines how to handle origins that haven't
134 // been allowed or denied yet. If |provider_id| is not NULL, the id of the
135 // provider which provided the default setting is assigned to it.
136 ContentSetting
GetDefaultContentSetting(std::string
* provider_id
);
137 void SetDefaultContentSetting(ContentSetting setting
);
139 // NOTE: This should only be called on the UI thread.
140 void ResetToDefaultContentSetting();
142 // Returns all notifications settings. |settings| is cleared before
143 // notifications setting are passed to it.
144 void GetNotificationsSettings(ContentSettingsForOneType
* settings
);
146 // Clears the notifications setting for the given pattern.
147 void ClearSetting(const ContentSettingsPattern
& pattern
);
149 // Clears the sets of explicitly allowed and denied origins.
150 void ResetAllOrigins();
152 ContentSetting
GetContentSetting(const GURL
& origin
);
154 // Returns true if the notifier with |notifier_id| is allowed to send
156 bool IsNotifierEnabled(const message_center::NotifierId
& notifier_id
);
158 // Updates the availability of the notifier.
159 void SetNotifierEnabled(const message_center::NotifierId
& notifier_id
,
162 // Adds in a the welcome notification if required for components built
163 // into Chrome that show notifications like Chrome Now.
164 void ShowWelcomeNotificationIfNecessary(const Notification
& notification
);
167 // Takes a notification object and shows it in the UI.
168 void ShowNotification(const Notification
& notification
);
170 // Returns a display name for an origin in the process id, to be used in
171 // permission infobar or on the frame of the notification toast. Different
172 // from the origin itself when dealing with extensions.
173 base::string16
DisplayNameForOriginInProcessId(const GURL
& origin
,
176 // Notifies the observers when permissions settings change.
177 void NotifySettingsChange();
179 NotificationUIManager
* GetUIManager();
181 // Called when the string list pref has been changed.
182 void OnStringListPrefChanged(
183 const char* pref_name
, std::set
<std::string
>* ids_field
);
185 // Called when the disabled_extension_id pref has been changed.
186 void OnDisabledExtensionIdsChanged();
188 // Called when the disabled_system_component_id pref has been changed.
189 void OnDisabledSystemComponentIdsChanged();
191 // Called when the enabled_sync_notifier_id pref has been changed.
192 void OnEnabledSyncNotifierIdsChanged();
194 void FirePermissionLevelChangedEvent(
195 const message_center::NotifierId
& notifier_id
,
198 // content::NotificationObserver:
199 virtual void Observe(int type
,
200 const content::NotificationSource
& source
,
201 const content::NotificationDetails
& details
) OVERRIDE
;
203 // The profile which owns this object.
206 // Non-owned pointer to the notification manager which manages the
207 // UI for desktop toasts.
208 NotificationUIManager
* ui_manager_
;
210 // Prefs listener for disabled_extension_id.
211 StringListPrefMember disabled_extension_id_pref_
;
213 // Prefs listener for disabled_system_component_id.
214 StringListPrefMember disabled_system_component_id_pref_
;
216 // Prefs listener for enabled_sync_notifier_id.
217 StringListPrefMember enabled_sync_notifier_id_pref_
;
219 // On-memory data for the availability of extensions.
220 std::set
<std::string
> disabled_extension_ids_
;
222 // On-memory data for the availability of system_component.
223 std::set
<std::string
> disabled_system_component_ids_
;
225 // On-memory data for the availability of sync notifiers.
226 std::set
<std::string
> enabled_sync_notifier_ids_
;
228 // Registrar for the other kind of notifications (event signaling).
229 content::NotificationRegistrar registrar_
;
231 // Welcome Notification
232 scoped_ptr
<ExtensionWelcomeNotification
> chrome_now_welcome_notification_
;
234 DISALLOW_COPY_AND_ASSIGN(DesktopNotificationService
);
237 #endif // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_