• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 #pragma once
8 
9 #include <string>
10 #include <vector>
11 
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/string16.h"
16 #include "chrome/browser/content_settings/content_settings_notification_provider.h"
17 #include "chrome/browser/content_settings/content_settings_provider.h"
18 #include "chrome/browser/prefs/pref_change_registrar.h"
19 #include "chrome/browser/profiles/profile_keyed_service.h"
20 #include "chrome/common/content_settings.h"
21 #include "content/common/notification_observer.h"
22 #include "content/common/notification_registrar.h"
23 #include "googleurl/src/gurl.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextDirection.h"
25 
26 class Notification;
27 class NotificationUIManager;
28 class NotificationsPrefsCache;
29 class PrefService;
30 class Profile;
31 class TabContents;
32 struct DesktopNotificationHostMsg_Show_Params;
33 
34 // The DesktopNotificationService is an object, owned by the Profile,
35 // which provides the creation of desktop "toasts" to web pages and workers.
36 class DesktopNotificationService : public NotificationObserver,
37                                    public ProfileKeyedService {
38  public:
39   enum DesktopNotificationSource {
40     PageNotification,
41     WorkerNotification
42   };
43 
44   DesktopNotificationService(Profile* profile,
45                              NotificationUIManager* ui_manager);
46   virtual ~DesktopNotificationService();
47 
48   // Requests permission (using an info-bar) for a given origin.
49   // |callback_context| contains an opaque value to pass back to the
50   // requesting process when the info-bar finishes.  A NULL tab can be given if
51   // none exist (i.e. background tab), in which case the currently selected tab
52   // will be used.
53   void RequestPermission(const GURL& origin,
54                          int process_id,
55                          int route_id,
56                          int callback_context,
57                          TabContents* tab);
58 
59   // ShowNotification is called on the UI thread handling IPCs from a child
60   // process, identified by |process_id| and |route_id|.  |source| indicates
61   // whether the script is in a worker or page. |params| contains all the
62   // other parameters supplied by the worker or page.
63   bool ShowDesktopNotification(
64       const DesktopNotificationHostMsg_Show_Params& params,
65       int process_id, int route_id, DesktopNotificationSource source);
66 
67   // Cancels a notification.  If it has already been shown, it will be
68   // removed from the screen.  If it hasn't been shown yet, it won't be
69   // shown.
70   bool CancelDesktopNotification(int process_id,
71                                  int route_id,
72                                  int notification_id);
73 
74   // Methods to setup and modify permission preferences.
75   void GrantPermission(const GURL& origin);
76   void DenyPermission(const GURL& origin);
77 
78   // NotificationObserver implementation.
79   virtual void Observe(NotificationType type,
80                        const NotificationSource& source,
81                        const NotificationDetails& details);
82 
prefs_cache()83   NotificationsPrefsCache* prefs_cache() { return prefs_cache_; }
84 
85   // Creates a data:xxxx URL which contains the full HTML for a notification
86   // using supplied icon, title, and text, run through a template which contains
87   // the standard formatting for notifications.
88   static string16 CreateDataUrl(const GURL& icon_url,
89                                 const string16& title,
90                                 const string16& body,
91                                 WebKit::WebTextDirection dir);
92 
93   // Creates a data:xxxx URL which contains the full HTML for a notification
94   // using resource template which contains the standard formatting for
95   // notifications.
96   static string16 CreateDataUrl(int resource,
97                                 const std::vector<std::string>& subst);
98 
99   // The default content setting determines how to handle origins that haven't
100   // been allowed or denied yet.
101   ContentSetting GetDefaultContentSetting();
102   void SetDefaultContentSetting(ContentSetting setting);
103   bool IsDefaultContentSettingManaged() const;
104 
105   // NOTE: This should only be called on the UI thread.
106   void ResetToDefaultContentSetting();
107 
108   // Returns all origins that explicitly have been allowed.
109   std::vector<GURL> GetAllowedOrigins();
110 
111   // Returns all origins that explicitly have been denied.
112   std::vector<GURL> GetBlockedOrigins();
113 
114   // Removes an origin from the "explicitly allowed" set.
115   void ResetAllowedOrigin(const GURL& origin);
116 
117   // Removes an origin from the "explicitly denied" set.
118   void ResetBlockedOrigin(const GURL& origin);
119 
120   // Clears the sets of explicitly allowed and denied origins.
121   void ResetAllOrigins();
122 
123   static void RegisterUserPrefs(PrefService* user_prefs);
124 
125   ContentSetting GetContentSetting(const GURL& origin);
126 
127  private:
128   void InitPrefs();
129   void StartObserving();
130   void StopObserving();
131 
132   void OnPrefsChanged(const std::string& pref_name);
133 
134   // Takes a notification object and shows it in the UI.
135   void ShowNotification(const Notification& notification);
136 
137   // Returns a display name for an origin, to be used in permission infobar
138   // or on the frame of the notification toast.  Different from the origin
139   // itself when dealing with extensions.
140   string16 DisplayNameForOrigin(const GURL& origin);
141 
142   // Notifies the observers when permissions settings change.
143   void NotifySettingsChange();
144 
145   // The profile which owns this object.
146   Profile* profile_;
147 
148   // A cache of preferences which is accessible only on the IO thread
149   // to service synchronous IPCs.
150   scoped_refptr<NotificationsPrefsCache> prefs_cache_;
151 
152   // Non-owned pointer to the notification manager which manages the
153   // UI for desktop toasts.
154   NotificationUIManager* ui_manager_;
155 
156   scoped_ptr<content_settings::NotificationProvider> provider_;
157 
158   PrefChangeRegistrar prefs_registrar_;
159   NotificationRegistrar notification_registrar_;
160 
161   DISALLOW_COPY_AND_ASSIGN(DesktopNotificationService);
162 };
163 
164 #endif  // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_
165