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