Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / public / browser / notification_registrar.cc
blobf9e187b56b1fecca474d0d21da6f5d4324e64037
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.
5 #include "content/public/browser/notification_registrar.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "content/browser/notification_service_impl.h"
12 namespace content {
14 struct NotificationRegistrar::Record {
15 bool operator==(const Record& other) const;
17 NotificationObserver* observer;
18 int type;
19 NotificationSource source;
22 bool NotificationRegistrar::Record::operator==(const Record& other) const {
23 return observer == other.observer &&
24 type == other.type &&
25 source == other.source;
28 NotificationRegistrar::NotificationRegistrar() {
29 // Force the NotificationService to be constructed (if it isn't already).
30 // This ensures the NotificationService will be registered on the
31 // AtExitManager before any objects which access it via NotificationRegistrar.
32 // This in turn means it will be destroyed after these objects, so they will
33 // never try to access the NotificationService after it's been destroyed.
34 NotificationServiceImpl::current();
35 // It is OK to create a NotificationRegistrar instance on one thread and then
36 // use it (exclusively) on another, so we detach from the initial thread.
37 DetachFromThread();
40 NotificationRegistrar::~NotificationRegistrar() {
41 RemoveAll();
44 void NotificationRegistrar::Add(NotificationObserver* observer,
45 int type,
46 const NotificationSource& source) {
47 DCHECK(CalledOnValidThread());
48 DCHECK(!IsRegistered(observer, type, source)) << "Duplicate registration.";
50 Record record = { observer, type, source };
51 registered_.push_back(record);
53 NotificationServiceImpl::current()->AddObserver(observer, type, source);
56 void NotificationRegistrar::Remove(NotificationObserver* observer,
57 int type,
58 const NotificationSource& source) {
59 DCHECK(CalledOnValidThread());
61 Record record = { observer, type, source };
62 RecordVector::iterator found = std::find(
63 registered_.begin(), registered_.end(), record);
64 if (found == registered_.end()) {
65 NOTREACHED() << "Trying to remove unregistered observer of type " <<
66 type << " from list of size " << registered_.size() << ".";
67 return;
69 registered_.erase(found);
71 // This can be NULL if our owner outlives the NotificationService, e.g. if our
72 // owner is a Singleton.
73 NotificationServiceImpl* service = NotificationServiceImpl::current();
74 if (service)
75 service->RemoveObserver(observer, type, source);
78 void NotificationRegistrar::RemoveAll() {
79 CHECK(CalledOnValidThread());
80 // Early-exit if no registrations, to avoid calling
81 // NotificationService::current. If we've constructed an object with a
82 // NotificationRegistrar member, but haven't actually used the notification
83 // service, and we reach prgram exit, then calling current() below could try
84 // to initialize the service's lazy TLS pointer during exit, which throws
85 // wrenches at things.
86 if (registered_.empty())
87 return;
89 // This can be NULL if our owner outlives the NotificationService, e.g. if our
90 // owner is a Singleton.
91 NotificationServiceImpl* service = NotificationServiceImpl::current();
92 if (service) {
93 for (size_t i = 0; i < registered_.size(); i++) {
94 service->RemoveObserver(registered_[i].observer,
95 registered_[i].type,
96 registered_[i].source);
99 registered_.clear();
102 bool NotificationRegistrar::IsEmpty() const {
103 DCHECK(CalledOnValidThread());
104 return registered_.empty();
107 bool NotificationRegistrar::IsRegistered(NotificationObserver* observer,
108 int type,
109 const NotificationSource& source) {
110 DCHECK(CalledOnValidThread());
111 Record record = { observer, type, source };
112 return std::find(registered_.begin(), registered_.end(), record) !=
113 registered_.end();
116 } // namespace content