15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <set>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/callback_forward.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/memory/weak_ptr.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_member.h"
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/scoped_observer.h"
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/content_settings/permission_context_base.h"
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/content_settings/core/common/content_settings.h"
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/keyed_service.h"
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "third_party/WebKit/public/platform/WebNotificationPermission.h"
247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "third_party/WebKit/public/web/WebTextDirection.h"
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/message_center/notifier_settings.h"
26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(ENABLE_EXTENSIONS)
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/extension_registry_observer.h"
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Notification;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NotificationDelegate;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NotificationUIManager;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Profile;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass DesktopNotificationDelegate;
390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass RenderFrameHost;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct ShowDesktopNotificationHostMsgParams;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(ENABLE_EXTENSIONS)
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace extensions {
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class ExtensionRegistry;
465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace gfx {
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Image;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace user_prefs {
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class PrefRegistrySyncable;
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Callback to be invoked when the result of a permission request is known.
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)typedef base::Callback<void(blink::WebNotificationPermission)>
595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    NotificationPermissionCallback;
605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The DesktopNotificationService is an object, owned by the Profile,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// which provides the creation of desktop "toasts" to web pages and workers.
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass DesktopNotificationService : public PermissionContextBase
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(ENABLE_EXTENSIONS)
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                   ,
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                   public extensions::ExtensionRegistryObserver
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                   {
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Register profile-specific prefs of notifications.
717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* prefs);
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // Add a desktop notification.
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static std::string AddIconNotification(const GURL& origin_url,
751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                         const base::string16& title,
761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                         const base::string16& message,
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                         const gfx::Image& icon,
781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                         const base::string16& replace_id,
791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                         NotificationDelegate* delegate,
801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                         Profile* profile);
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  explicit DesktopNotificationService(Profile* profile);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~DesktopNotificationService();
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Requests Web Notification permission for |requesting_frame|. The |callback|
865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // will be invoked after the user has made a decision.
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void RequestNotificationPermission(
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      content::WebContents* web_contents,
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      const PermissionRequestID& request_id,
905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      const GURL& requesting_frame,
915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      bool user_gesture,
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      const NotificationPermissionCallback& callback);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Show a desktop notification. If |cancel_callback| is non-null, it's set to
950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // a callback which can be used to cancel the notification.
960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void ShowDesktopNotification(
970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      const content::ShowDesktopNotificationHostMsgParams& params,
980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      content::RenderFrameHost* render_frame_host,
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      scoped_ptr<content::DesktopNotificationDelegate> delegate,
1000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      base::Closure* cancel_callback);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Returns true if the notifier with |notifier_id| is allowed to send
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // notifications.
104eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  bool IsNotifierEnabled(const message_center::NotifierId& notifier_id);
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Updates the availability of the notifier.
107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  void SetNotifierEnabled(const message_center::NotifierId& notifier_id,
108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                          bool enabled);
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns a display name for an origin in the process id, to be used in
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // permission infobar or on the frame of the notification toast.  Different
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // from the origin itself when dealing with extensions.
114a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 DisplayNameForOriginInProcessId(const GURL& origin,
115a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                                 int process_id);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
117eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Called when the string list pref has been changed.
118eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  void OnStringListPrefChanged(
119eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const char* pref_name, std::set<std::string>* ids_field);
120eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called when the disabled_extension_id pref has been changed.
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnDisabledExtensionIdsChanged();
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Used as a callback once a permission has been decided to convert |allowed|
1255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // to one of the blink::WebNotificationPermission values.
1265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void OnNotificationPermissionRequested(
1275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      const base::Callback<void(blink::WebNotificationPermission)>& callback,
1285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      bool allowed);
129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void FirePermissionLevelChangedEvent(
1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      const message_center::NotifierId& notifier_id,
1321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      bool enabled);
1331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(ENABLE_EXTENSIONS)
1355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // extensions::ExtensionRegistryObserver:
1365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void OnExtensionUninstalled(
1375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      content::BrowserContext* browser_context,
1385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      const extensions::Extension* extension,
1395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      extensions::UninstallReason reason) OVERRIDE;
1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // PermissionContextBase:
1431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void UpdateContentSetting(const GURL& requesting_origin,
1441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                    const GURL& embedder_origin,
1451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                    bool allowed) OVERRIDE;
14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The profile which owns this object.
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile_;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Prefs listener for disabled_extension_id.
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StringListPrefMember disabled_extension_id_pref_;
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
153eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Prefs listener for disabled_system_component_id.
154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  StringListPrefMember disabled_system_component_id_pref_;
155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // On-memory data for the availability of extensions.
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::set<std::string> disabled_extension_ids_;
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // On-memory data for the availability of system_component.
160c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::set<std::string> disabled_system_component_ids_;
161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(ENABLE_EXTENSIONS)
1635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // An observer to listen when extension is uninstalled.
1645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ScopedObserver<extensions::ExtensionRegistry,
1655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 extensions::ExtensionRegistryObserver>
1665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      extension_registry_observer_;
1671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
1688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  base::WeakPtrFactory<DesktopNotificationService> weak_factory_;
1705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DesktopNotificationService);
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_H_
175