15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CONTENT_PUBLIC_BROWSER_NOTIFICATION_REGISTRAR_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_PUBLIC_BROWSER_NOTIFICATION_REGISTRAR_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/non_thread_safe.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/content_export.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NotificationObserver;
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NotificationSource;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Aids in registering for notifications and ensures that all registered
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notifications are unregistered when the class is destroyed.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The intended use is that you make a NotificationRegistrar member in your
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// class and use it to register your notifications instead of going through the
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notification service directly. It will automatically unregister them for
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// you.
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CONTENT_EXPORT NotificationRegistrar :
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NON_EXPORTED_BASE(public base::NonThreadSafe) {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This class must not be derived from (we don't have a virtual destructor so
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it won't work). Instead, use it as a member in your class.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NotificationRegistrar();
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~NotificationRegistrar();
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wrappers around NotificationService::[Add|Remove]Observer.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Add(NotificationObserver* observer,
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           int type,
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           const NotificationSource& source);
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Remove(NotificationObserver* observer,
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              int type,
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              const NotificationSource& source);
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unregisters all notifications.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveAll();
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if no notifications are registered.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsEmpty() const;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if there is already a registered notification with the
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // specified details.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsRegistered(NotificationObserver* observer,
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    int type,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    const NotificationSource& source);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct Record;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We keep registered notifications in a simple vector. This means we'll do
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // brute-force searches when removing them individually, but individual
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // removal is uncommon, and there will typically only be a couple of
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // notifications anyway.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::vector<Record> RecordVector;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Lists all notifications we're currently registered for.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RecordVector registered_;
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(NotificationRegistrar);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CONTENT_PUBLIC_BROWSER_NOTIFICATION_REGISTRAR_H_
72