Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / desktop_notification_service.h
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/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"
29 #include "url/gurl.h"
30
31 class ContentSettingsPattern;
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 namespace extensions {
44 class ExtensionRegistry;
45 }
46
47 namespace gfx {
48 class Image;
49 }
50
51 namespace user_prefs {
52 class PrefRegistrySyncable;
53 }
54
55 // Callback to be invoked when the result of a permission request is known.
56 typedef base::Callback<void(blink::WebNotificationPermission)>
57     NotificationPermissionCallback;
58
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 {
64  public:
65   // Register profile-specific prefs of notifications.
66   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* prefs);
67
68   DesktopNotificationService(Profile* profile,
69                              NotificationUIManager* ui_manager);
70   virtual ~DesktopNotificationService();
71
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,
78       bool user_gesture,
79       const NotificationPermissionCallback& callback);
80
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);
88
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);
96
97   // Creates a data:xxxx URL which contains the full HTML for a notification
98   // using resource template which contains the standard formatting for
99   // notifications.
100   static base::string16 CreateDataUrl(int resource,
101                                 const std::vector<std::string>& subst);
102
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,
110                                          Profile* profile);
111
112   // Returns true if the notifier with |notifier_id| is allowed to send
113   // notifications.
114   bool IsNotifierEnabled(const message_center::NotifierId& notifier_id);
115
116   // Updates the availability of the notifier.
117   void SetNotifierEnabled(const message_center::NotifierId& notifier_id,
118                           bool enabled);
119
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);
123
124  private:
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,
129                                                  int process_id);
130   NotificationUIManager* GetUIManager();
131
132   // Called when the string list pref has been changed.
133   void OnStringListPrefChanged(
134       const char* pref_name, std::set<std::string>* ids_field);
135
136   // Called when the disabled_extension_id pref has been changed.
137   void OnDisabledExtensionIdsChanged();
138
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,
143       bool allowed);
144
145   void FirePermissionLevelChangedEvent(
146       const message_center::NotifierId& notifier_id,
147       bool enabled);
148
149   // extensions::ExtensionRegistryObserver:
150   virtual void OnExtensionUninstalled(
151       content::BrowserContext* browser_context,
152       const extensions::Extension* extension,
153       extensions::UninstallReason reason) OVERRIDE;
154
155   // PermissionContextBase:
156   virtual void UpdateContentSetting(const GURL& requesting_origin,
157                                     const GURL& embedder_origin,
158                                     bool allowed) OVERRIDE;
159
160   // The profile which owns this object.
161   Profile* profile_;
162
163   // Non-owned pointer to the notification manager which manages the
164   // UI for desktop toasts.
165   NotificationUIManager* ui_manager_;
166
167   // Prefs listener for disabled_extension_id.
168   StringListPrefMember disabled_extension_id_pref_;
169
170   // Prefs listener for disabled_system_component_id.
171   StringListPrefMember disabled_system_component_id_pref_;
172
173   // On-memory data for the availability of extensions.
174   std::set<std::string> disabled_extension_ids_;
175
176   // On-memory data for the availability of system_component.
177   std::set<std::string> disabled_system_component_ids_;
178
179   // An observer to listen when extension is uninstalled.
180   ScopedObserver<extensions::ExtensionRegistry,
181                  extensions::ExtensionRegistryObserver>
182       extension_registry_observer_;
183
184   // Welcome Notification
185   scoped_ptr<ExtensionWelcomeNotification> chrome_now_welcome_notification_;
186
187   base::WeakPtrFactory<DesktopNotificationService> weak_factory_;
188
189   DISALLOW_COPY_AND_ASSIGN(DesktopNotificationService);
190 };
191
192 #endif  // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_