notification_registrar.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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_REGISTRAR_H_ 6#define CONTENT_PUBLIC_BROWSER_NOTIFICATION_REGISTRAR_H_ 7 8#include <vector> 9 10#include "base/basictypes.h" 11#include "base/threading/non_thread_safe.h" 12#include "content/common/content_export.h" 13 14namespace content { 15 16class NotificationObserver; 17class NotificationSource; 18 19// Aids in registering for notifications and ensures that all registered 20// notifications are unregistered when the class is destroyed. 21// 22// The intended use is that you make a NotificationRegistrar member in your 23// class and use it to register your notifications instead of going through the 24// notification service directly. It will automatically unregister them for 25// you. 26class CONTENT_EXPORT NotificationRegistrar : 27 NON_EXPORTED_BASE(public base::NonThreadSafe) { 28 public: 29 // This class must not be derived from (we don't have a virtual destructor so 30 // it won't work). Instead, use it as a member in your class. 31 NotificationRegistrar(); 32 ~NotificationRegistrar(); 33 34 // Wrappers around NotificationService::[Add|Remove]Observer. 35 void Add(NotificationObserver* observer, 36 int type, 37 const NotificationSource& source); 38 void Remove(NotificationObserver* observer, 39 int type, 40 const NotificationSource& source); 41 42 // Unregisters all notifications. 43 void RemoveAll(); 44 45 // Returns true if no notifications are registered. 46 bool IsEmpty() const; 47 48 // Returns true if there is already a registered notification with the 49 // specified details. 50 bool IsRegistered(NotificationObserver* observer, 51 int type, 52 const NotificationSource& source); 53 54 private: 55 struct Record; 56 57 // We keep registered notifications in a simple vector. This means we'll do 58 // brute-force searches when removing them individually, but individual 59 // removal is uncommon, and there will typically only be a couple of 60 // notifications anyway. 61 typedef std::vector<Record> RecordVector; 62 63 // Lists all notifications we're currently registered for. 64 RecordVector registered_; 65 66 DISALLOW_COPY_AND_ASSIGN(NotificationRegistrar); 67}; 68 69} // namespace content 70 71#endif // CONTENT_PUBLIC_BROWSER_NOTIFICATION_REGISTRAR_H_ 72