Update V8 to version 4.6.61.
[chromium-blink-merge.git] / content / public / browser / notification_registrar.cc
blobaf82710b81e6197f84918c75749e6c74e0d53c7c
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 DCHECK(found != registered_.end());
66 registered_.erase(found);
68 // This can be nullptr if our owner outlives the NotificationService, e.g. if
69 // our owner is a Singleton.
70 NotificationServiceImpl* service = NotificationServiceImpl::current();
71 if (service)
72 service->RemoveObserver(observer, type, source);
75 void NotificationRegistrar::RemoveAll() {
76 CHECK(CalledOnValidThread());
77 // Early-exit if no registrations, to avoid calling
78 // NotificationService::current. If we've constructed an object with a
79 // NotificationRegistrar member, but haven't actually used the notification
80 // service, and we reach prgram exit, then calling current() below could try
81 // to initialize the service's lazy TLS pointer during exit, which throws
82 // wrenches at things.
83 if (registered_.empty())
84 return;
86 // This can be nullptr if our owner outlives the NotificationService, e.g. if
87 // our owner is a Singleton.
88 NotificationServiceImpl* service = NotificationServiceImpl::current();
89 if (service) {
90 for (size_t i = 0; i < registered_.size(); i++) {
91 service->RemoveObserver(registered_[i].observer,
92 registered_[i].type,
93 registered_[i].source);
96 registered_.clear();
99 bool NotificationRegistrar::IsEmpty() const {
100 DCHECK(CalledOnValidThread());
101 return registered_.empty();
104 bool NotificationRegistrar::IsRegistered(NotificationObserver* observer,
105 int type,
106 const NotificationSource& source) {
107 DCHECK(CalledOnValidThread());
108 Record record = { observer, type, source };
109 return std::find(registered_.begin(), registered_.end(), record) !=
110 registered_.end();
113 } // namespace content