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