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_NOTIFICATION_UI_MANAGER_MAC_H_
      6 #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_MAC_H_
      7 
      8 #import <AppKit/AppKit.h>
      9 
     10 #include <map>
     11 #include <set>
     12 
     13 #include "base/basictypes.h"
     14 #include "base/mac/scoped_nsobject.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/strings/string16.h"
     17 #include "chrome/browser/notifications/balloon_notification_ui_manager.h"
     18 
     19 @protocol CrUserNotification;
     20 class Notification;
     21 @class NotificationCenterDelegate;
     22 class PrefService;
     23 class Profile;
     24 
     25 // This class is an implementation of BalloonNotificationUIManager that will
     26 // send text-only (non-HTML) notifications to Notification Center on 10.8. This
     27 // class is only instantiated on 10.8 and above. For HTML notifications,
     28 // this class delegates to the base class.
     29 class NotificationUIManagerMac : public BalloonNotificationUIManager {
     30  public:
     31   explicit NotificationUIManagerMac(PrefService* local_state);
     32   virtual ~NotificationUIManagerMac();
     33 
     34   // NotificationUIManager:
     35   virtual void Add(const Notification& notification,
     36                    Profile* profile) OVERRIDE;
     37   virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
     38       Profile* profile, const GURL& source_origin) OVERRIDE;
     39   virtual bool CancelById(const std::string& notification_id) OVERRIDE;
     40   virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE;
     41   virtual bool CancelAllByProfile(Profile* profile) OVERRIDE;
     42   virtual void CancelAll() OVERRIDE;
     43 
     44   // Returns the corresponding C++ object for the Cocoa notification object,
     45   // or NULL if it could not be found.
     46   const Notification* FindNotificationWithCocoaNotification(
     47       id<CrUserNotification> notification) const;
     48 
     49  private:
     50   // A ControllerNotification links the Cocoa (view) notification to the C++
     51   // (model) notification. This assumes ownership (i.e. does not retain a_view)
     52   // of both objects and will delete them on destruction.
     53   struct ControllerNotification {
     54     ControllerNotification(Profile* a_profile,
     55                            id<CrUserNotification> a_view,
     56                            Notification* a_model);
     57     ~ControllerNotification();
     58 
     59     Profile* profile;
     60     id<CrUserNotification> view;
     61     Notification* model;
     62 
     63    private:
     64     DISALLOW_COPY_AND_ASSIGN(ControllerNotification);
     65   };
     66   typedef std::map<std::string, ControllerNotification*> NotificationMap;
     67 
     68   // Erases a Cocoa notification from both the application and Notification
     69   // Center, and deletes the corresponding C++ object.
     70   bool RemoveNotification(id<CrUserNotification> notification);
     71 
     72   // Finds a notification with a given replacement id.
     73   id<CrUserNotification> FindNotificationWithReplacementId(
     74       const base::string16& replacement_id) const;
     75 
     76   // Cocoa class that receives callbacks from the NSUserNotificationCenter.
     77   base::scoped_nsobject<NotificationCenterDelegate> delegate_;
     78 
     79   // Maps notification_ids to ControllerNotifications. The map owns the value,
     80   // so it must be deleted when remvoed from the map. This is typically handled
     81   // in RemoveNotification.
     82   NotificationMap notification_map_;
     83 
     84   DISALLOW_COPY_AND_ASSIGN(NotificationUIManagerMac);
     85 };
     86 
     87 #endif  // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_MAC_H_
     88