Home | History | Annotate | Download | only in browser
      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 CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_
      6 #define CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_
      7 
      8 #include <map>
      9 
     10 #include "base/observer_list.h"
     11 #include "content/common/content_export.h"
     12 #include "content/public/browser/notification_service.h"
     13 
     14 namespace content {
     15 
     16 class NotificationObserver;
     17 class NotificationRegistrar;
     18 
     19 class CONTENT_EXPORT NotificationServiceImpl : public NotificationService {
     20  public:
     21   static NotificationServiceImpl* current();
     22 
     23   // Normally instantiated when the thread is created.  Not all threads have
     24   // a NotificationService.  Only one instance should be created per thread.
     25   NotificationServiceImpl();
     26   virtual ~NotificationServiceImpl();
     27 
     28   // NotificationService:
     29   virtual void Notify(int type,
     30                       const NotificationSource& source,
     31                       const NotificationDetails& details) OVERRIDE;
     32 
     33  private:
     34   friend class NotificationRegistrar;
     35 
     36   typedef ObserverList<NotificationObserver> NotificationObserverList;
     37   typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap;
     38   typedef std::map<int, NotificationSourceMap> NotificationObserverMap;
     39   typedef std::map<int, int> NotificationObserverCount;
     40 
     41   // Convenience function to determine whether a source has a
     42   // NotificationObserverList in the given map;
     43   static bool HasKey(const NotificationSourceMap& map,
     44                      const NotificationSource& source);
     45 
     46   // NOTE: Rather than using this directly, you should use a
     47   // NotificationRegistrar.
     48   //
     49   // Registers a NotificationObserver to be called whenever a matching
     50   // notification is posted.  Observer is a pointer to an object subclassing
     51   // NotificationObserver to be notified when an event matching the other two
     52   // parameters is posted to this service.  Type is the type of events to be
     53   // notified about (or NOTIFICATION_ALL to receive events of all
     54   // types).
     55   // Source is a NotificationSource object (created using
     56   // "Source<classname>(pointer)"), if this observer only wants to
     57   // receive events from that object, or NotificationService::AllSources()
     58   // to receive events from all sources.
     59   //
     60   // A given observer can be registered only once for each combination of
     61   // type and source.  If the same object is registered more than once,
     62   // it must be removed for each of those combinations of type and source later.
     63   //
     64   // The caller retains ownership of the object pointed to by observer.
     65   void AddObserver(NotificationObserver* observer,
     66                    int type,
     67                    const NotificationSource& source);
     68 
     69   // NOTE: Rather than using this directly, you should use a
     70   // NotificationRegistrar.
     71   //
     72   // Removes the object pointed to by observer from receiving notifications
     73   // that match type and source.  If no object matching the parameters is
     74   // currently registered, this method is a no-op.
     75   void RemoveObserver(NotificationObserver* observer,
     76                       int type,
     77                       const NotificationSource& source);
     78 
     79   // Keeps track of the observers for each type of notification.
     80   // Until we get a prohibitively large number of notification types,
     81   // a simple array is probably the fastest way to dispatch.
     82   NotificationObserverMap observers_;
     83 
     84 #ifndef NDEBUG
     85   // Used to check to see that AddObserver and RemoveObserver calls are
     86   // balanced.
     87   NotificationObserverCount observer_counts_;
     88 #endif
     89 
     90   DISALLOW_COPY_AND_ASSIGN(NotificationServiceImpl);
     91 };
     92 
     93 }  // namespace content
     94 
     95 #endif  // CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_
     96