ipc/chromium/src/chrome/common/notification_service.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/chrome/common/notification_service.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +// This file describes a central switchboard for notifications that might
     1.9 +// happen in various parts of the application, and allows users to register
    1.10 +// observers for various classes of events that they're interested in.
    1.11 +
    1.12 +#ifndef CHROME_COMMON_NOTIFICATION_SERVICE_H_
    1.13 +#define CHROME_COMMON_NOTIFICATION_SERVICE_H_
    1.14 +
    1.15 +#include <map>
    1.16 +
    1.17 +#include "base/observer_list.h"
    1.18 +#include "chrome/common/notification_details.h"
    1.19 +#include "chrome/common/notification_observer.h"
    1.20 +#include "chrome/common/notification_source.h"
    1.21 +#include "chrome/common/notification_type.h"
    1.22 +
    1.23 +class NotificationObserver;
    1.24 +
    1.25 +class NotificationService {
    1.26 + public:
    1.27 +  // Returns the NotificationService object for the current thread, or NULL if
    1.28 +  // none.
    1.29 +  static NotificationService* current();
    1.30 +
    1.31 +  // Normally instantiated when the thread is created.  Not all threads have
    1.32 +  // a NotificationService.  Only one instance should be created per thread.
    1.33 +  NotificationService();
    1.34 +  ~NotificationService();
    1.35 +
    1.36 +  // Registers a NotificationObserver to be called whenever a matching
    1.37 +  // notification is posted.  Observer is a pointer to an object subclassing
    1.38 +  // NotificationObserver to be notified when an event matching the other two
    1.39 +  // parameters is posted to this service.  Type is the type of events to
    1.40 +  // be notified about (or NOTIFY_ALL to receive events of all types).
    1.41 +  // Source is a NotificationSource object (created using
    1.42 +  // "Source<classname>(pointer)"), if this observer only wants to
    1.43 +  // receive events from that object, or NotificationService::AllSources()
    1.44 +  // to receive events from all sources.
    1.45 +  //
    1.46 +  // A given observer can be registered only once for each combination of
    1.47 +  // type and source.  If the same object is registered more than once,
    1.48 +  // it must be removed for each of those combinations of type and source later.
    1.49 +  //
    1.50 +  // The caller retains ownership of the object pointed to by observer.
    1.51 +  void AddObserver(NotificationObserver* observer,
    1.52 +                   NotificationType type, const NotificationSource& source);
    1.53 +
    1.54 +  // Removes the object pointed to by observer from receiving notifications
    1.55 +  // that match type and source.  If no object matching the parameters is
    1.56 +  // currently registered, this method is a no-op.
    1.57 +  void RemoveObserver(NotificationObserver* observer,
    1.58 +                      NotificationType type, const NotificationSource& source);
    1.59 +
    1.60 +  // Synchronously posts a notification to all interested observers.
    1.61 +  // Source is a reference to a NotificationSource object representing
    1.62 +  // the object originating the notification (can be
    1.63 +  // NotificationService::AllSources(), in which case
    1.64 +  // only observers interested in all sources will be notified).
    1.65 +  // Details is a reference to an object containing additional data about
    1.66 +  // the notification.  If no additional data is needed, NoDetails() is used.
    1.67 +  // There is no particular order in which the observers will be notified.
    1.68 +  void Notify(NotificationType type,
    1.69 +              const NotificationSource& source,
    1.70 +              const NotificationDetails& details);
    1.71 +
    1.72 +  // Returns a NotificationSource that represents all notification sources
    1.73 +  // (for the purpose of registering an observer for events from all sources).
    1.74 +  static Source<void> AllSources() { return Source<void>(NULL); }
    1.75 +
    1.76 +  // Returns a NotificationDetails object that represents a lack of details
    1.77 +  // associated with a notification.  (This is effectively a null pointer.)
    1.78 +  static Details<void> NoDetails() { return Details<void>(NULL); }
    1.79 +
    1.80 + private:
    1.81 +  typedef base::ObserverList<NotificationObserver> NotificationObserverList;
    1.82 +  typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap;
    1.83 +
    1.84 +  // Convenience function to determine whether a source has a
    1.85 +  // NotificationObserverList in the given map;
    1.86 +  static bool HasKey(const NotificationSourceMap& map,
    1.87 +                     const NotificationSource& source);
    1.88 +
    1.89 +  // Keeps track of the observers for each type of notification.
    1.90 +  // Until we get a prohibitively large number of notification types,
    1.91 +  // a simple array is probably the fastest way to dispatch.
    1.92 +  NotificationSourceMap observers_[NotificationType::NOTIFICATION_TYPE_COUNT];
    1.93 +
    1.94 +#ifndef NDEBUG
    1.95 +  // Used to check to see that AddObserver and RemoveObserver calls are
    1.96 +  // balanced.
    1.97 +  int observer_counts_[NotificationType::NOTIFICATION_TYPE_COUNT];
    1.98 +#endif
    1.99 +
   1.100 +  DISALLOW_COPY_AND_ASSIGN(NotificationService);
   1.101 +};
   1.102 +
   1.103 +#endif  // CHROME_COMMON_NOTIFICATION_SERVICE_H_

mercurial