ipc/chromium/src/chrome/common/notification_registrar.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_registrar.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,53 @@
     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 +#ifndef CHROME_COMMON_NOTIFICATION_REGISTRAR_H_
     1.9 +#define CHROME_COMMON_NOTIFICATION_REGISTRAR_H_
    1.10 +
    1.11 +#include <vector>
    1.12 +
    1.13 +#include "base/basictypes.h"
    1.14 +#include "chrome/common/notification_observer.h"
    1.15 +
    1.16 +// Aids in registering for notifications and ensures that all registered
    1.17 +// notifications are unregistered when the class is destroyed.
    1.18 +//
    1.19 +// The intended use is that you make a NotificationRegistrar member in your
    1.20 +// class and use it to register your notifications instead of going through the
    1.21 +// notification service directly. It will automatically unregister them for
    1.22 +// you.
    1.23 +class NotificationRegistrar {
    1.24 + public:
    1.25 +  // This class must not be derived from (we don't have a virtual destructor so
    1.26 +  // it won't work). Instead, use it as a member in your class.
    1.27 +  NotificationRegistrar();
    1.28 +  ~NotificationRegistrar();
    1.29 +
    1.30 +  // Wrappers around NotificationService::[Add|Remove]Observer.
    1.31 +  void Add(NotificationObserver* observer,
    1.32 +           NotificationType type,
    1.33 +           const NotificationSource& source);
    1.34 +  void Remove(NotificationObserver* observer,
    1.35 +              NotificationType type,
    1.36 +              const NotificationSource& source);
    1.37 +
    1.38 +  // Unregisters all notifications.
    1.39 +  void RemoveAll();
    1.40 +
    1.41 + private:
    1.42 +  struct Record;
    1.43 +
    1.44 +  // We keep registered notifications in a simple vector. This means we'll do
    1.45 +  // brute-force searches when removing them individually, but individual
    1.46 +  // removal is uncommon, and there will typically only be a couple of
    1.47 +  // notifications anyway.
    1.48 +  typedef std::vector<Record> RecordVector;
    1.49 +
    1.50 +  // Lists all notifications we're currently registered for.
    1.51 +  RecordVector registered_;
    1.52 +
    1.53 +  DISALLOW_COPY_AND_ASSIGN(NotificationRegistrar);
    1.54 +};
    1.55 +
    1.56 +#endif  // CHROME_COMMON_NOTIFICATION_REGISTRAR_H_

mercurial