summaryrefslogtreecommitdiffstats
path: root/chrome/common/notification_registrar.cc
blob: b9ea3d7685321e3bfe8e878726b9929cbfeedcf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/notification_registrar.h"

#include <algorithm>

#include "base/logging.h"
#include "chrome/common/notification_service.h"

struct NotificationRegistrar::Record {
  bool operator==(const Record& other) const;

  NotificationObserver* observer;
  NotificationType type;
  NotificationSource source;
};

bool NotificationRegistrar::Record::operator==(const Record& other) const {
  return observer == other.observer &&
         type == other.type &&
         source == other.source;
}

NotificationRegistrar::NotificationRegistrar() {
}

NotificationRegistrar::~NotificationRegistrar() {
  RemoveAll();
}

void NotificationRegistrar::Add(NotificationObserver* observer,
                                NotificationType type,
                                const NotificationSource& source) {
  Record record = { observer, type, source };
  DCHECK(std::find(registered_.begin(), registered_.end(), record) ==
         registered_.end()) << "Duplicate registration.";
  registered_.push_back(record);

  NotificationService::current()->AddObserver(observer, type, source);
}

void NotificationRegistrar::Remove(NotificationObserver* observer,
                                   NotificationType type,
                                   const NotificationSource& source) {
  Record record = { observer, type, source };
  RecordVector::iterator found = std::find(
      registered_.begin(), registered_.end(), record);
  if (found != registered_.end()) {
    registered_.erase(found);
  } else {
    // Fall through to passing the removal through to the notification service.
    // If it really isn't registered, it will also assert and do nothing, but
    // we might as well catch the case where the class is trying to unregister
    // for something they registered without going through us.
    NOTREACHED();
  }

  NotificationService::current()->RemoveObserver(observer, type, source);
}

void NotificationRegistrar::RemoveAll() {
  NotificationService* service = NotificationService::current();
  for (size_t i = 0; i < registered_.size(); i++) {
    service->RemoveObserver(registered_[i].observer,
                            registered_[i].type,
                            registered_[i].source);
  }
  registered_.clear();
}