Home | History | Annotate | Download | only in notifications
      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/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/welcome_notification.h"
     19 #include "chrome/common/content_settings.h"
     20 #include "components/browser_context_keyed_service/browser_context_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"
     26 #include "url/gurl.h"
     27 
     28 class ContentSettingsPattern;
     29 class Notification;
     30 class NotificationDelegate;
     31 class NotificationUIManager;
     32 class Profile;
     33 
     34 namespace content {
     35 class WebContents;
     36 struct ShowDesktopNotificationHostMsgParams;
     37 }
     38 
     39 namespace gfx {
     40 class Image;
     41 }
     42 
     43 namespace user_prefs {
     44 class PrefRegistrySyncable;
     45 }
     46 
     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 BrowserContextKeyedService,
     50                                    public content::NotificationObserver {
     51  public:
     52   enum DesktopNotificationSource {
     53     PageNotification,
     54     WorkerNotification
     55   };
     56 
     57   // Register profile-specific prefs of notifications.
     58   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* prefs);
     59 
     60   DesktopNotificationService(Profile* profile,
     61                              NotificationUIManager* ui_manager);
     62   virtual ~DesktopNotificationService();
     63 
     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,
     68                          int process_id,
     69                          int route_id,
     70                          int callback_context,
     71                          content::WebContents* tab);
     72 
     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,
     79       int process_id,
     80       int route_id,
     81       DesktopNotificationSource source);
     82 
     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
     85   // shown.
     86   bool CancelDesktopNotification(int process_id,
     87                                  int route_id,
     88                                  int notification_id);
     89 
     90   // Methods to setup and modify permission preferences.
     91   void GrantPermission(const GURL& origin);
     92   void DenyPermission(const GURL& origin);
     93 
     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);
    101 
    102   // Creates a data:xxxx URL which contains the full HTML for a notification
    103   // using resource template which contains the standard formatting for
    104   // notifications.
    105   static base::string16 CreateDataUrl(int resource,
    106                                 const std::vector<std::string>& subst);
    107 
    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
    112   // supported.
    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,
    119                                      Profile* profile);
    120 
    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,
    128                                          Profile* profile);
    129 
    130   // Remove any active notification corresponding to |notification_id|.
    131   static void RemoveNotification(const std::string& notification_id);
    132 
    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);
    138 
    139   // NOTE: This should only be called on the UI thread.
    140   void ResetToDefaultContentSetting();
    141 
    142   // Returns all notifications settings. |settings| is cleared before
    143   // notifications setting are passed to it.
    144   void GetNotificationsSettings(ContentSettingsForOneType* settings);
    145 
    146   // Clears the notifications setting for the given pattern.
    147   void ClearSetting(const ContentSettingsPattern& pattern);
    148 
    149   // Clears the sets of explicitly allowed and denied origins.
    150   void ResetAllOrigins();
    151 
    152   ContentSetting GetContentSetting(const GURL& origin);
    153 
    154   // Returns true if the notifier with |notifier_id| is allowed to send
    155   // notifications.
    156   bool IsNotifierEnabled(const message_center::NotifierId& notifier_id);
    157 
    158   // Updates the availability of the notifier.
    159   void SetNotifierEnabled(const message_center::NotifierId& notifier_id,
    160                           bool enabled);
    161 
    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);
    165 
    166  private:
    167   // Takes a notification object and shows it in the UI.
    168   void ShowNotification(const Notification& notification);
    169 
    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,
    174                                                  int process_id);
    175 
    176   // Notifies the observers when permissions settings change.
    177   void NotifySettingsChange();
    178 
    179   NotificationUIManager* GetUIManager();
    180 
    181   // Called when the string list pref has been changed.
    182   void OnStringListPrefChanged(
    183       const char* pref_name, std::set<std::string>* ids_field);
    184 
    185   // Called when the disabled_extension_id pref has been changed.
    186   void OnDisabledExtensionIdsChanged();
    187 
    188   // Called when the disabled_system_component_id pref has been changed.
    189   void OnDisabledSystemComponentIdsChanged();
    190 
    191   // Called when the enabled_sync_notifier_id pref has been changed.
    192   void OnEnabledSyncNotifierIdsChanged();
    193 
    194   void FirePermissionLevelChangedEvent(
    195       const message_center::NotifierId& notifier_id,
    196       bool enabled);
    197 
    198   // content::NotificationObserver:
    199   virtual void Observe(int type,
    200                        const content::NotificationSource& source,
    201                        const content::NotificationDetails& details) OVERRIDE;
    202 
    203   // The profile which owns this object.
    204   Profile* profile_;
    205 
    206   // Non-owned pointer to the notification manager which manages the
    207   // UI for desktop toasts.
    208   NotificationUIManager* ui_manager_;
    209 
    210   // Prefs listener for disabled_extension_id.
    211   StringListPrefMember disabled_extension_id_pref_;
    212 
    213   // Prefs listener for disabled_system_component_id.
    214   StringListPrefMember disabled_system_component_id_pref_;
    215 
    216   // Prefs listener for enabled_sync_notifier_id.
    217   StringListPrefMember enabled_sync_notifier_id_pref_;
    218 
    219   // On-memory data for the availability of extensions.
    220   std::set<std::string> disabled_extension_ids_;
    221 
    222   // On-memory data for the availability of system_component.
    223   std::set<std::string> disabled_system_component_ids_;
    224 
    225   // On-memory data for the availability of sync notifiers.
    226   std::set<std::string> enabled_sync_notifier_ids_;
    227 
    228   // Registrar for the other kind of notifications (event signaling).
    229   content::NotificationRegistrar registrar_;
    230 
    231   // Welcome Notification
    232   scoped_ptr<WelcomeNotification> welcome_notification;
    233 
    234   DISALLOW_COPY_AND_ASSIGN(DesktopNotificationService);
    235 };
    236 
    237 #endif  // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_
    238