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 #include "content/public/browser/notification_registrar.h"
6 
7 #include <stddef.h>
8 
9 #include <algorithm>
10 
11 #include "base/logging.h"
12 #include "base/stl_util.h"
13 #include "content/browser/notification_service_impl.h"
14 
15 namespace content {
16 
17 struct NotificationRegistrar::Record {
18   bool operator==(const Record& other) const;
19 
20   NotificationObserver* observer;
21   int type;
22   NotificationSource source;
23 };
24 
operator ==(const Record & other) const25 bool NotificationRegistrar::Record::operator==(const Record& other) const {
26   return observer == other.observer &&
27          type == other.type &&
28          source == other.source;
29 }
30 
NotificationRegistrar()31 NotificationRegistrar::NotificationRegistrar() {
32   // Force the NotificationService to be constructed (if it isn't already).
33   // This ensures the NotificationService will be registered on the
34   // AtExitManager before any objects which access it via NotificationRegistrar.
35   // This in turn means it will be destroyed after these objects, so they will
36   // never try to access the NotificationService after it's been destroyed.
37   NotificationServiceImpl::current();
38   // It is OK to create a NotificationRegistrar instance on one thread and then
39   // use it (exclusively) on another, so we detach from the initial thread.
40   DETACH_FROM_SEQUENCE(sequence_checker_);
41 }
42 
~NotificationRegistrar()43 NotificationRegistrar::~NotificationRegistrar() {
44   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
45   RemoveAll();
46 }
47 
Add(NotificationObserver * observer,int type,const NotificationSource & source)48 void NotificationRegistrar::Add(NotificationObserver* observer,
49                                 int type,
50                                 const NotificationSource& source) {
51   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
52   DCHECK(!IsRegistered(observer, type, source)) << "Duplicate registration.";
53   DCHECK(NotificationServiceImpl::current());
54 
55   Record record = { observer, type, source };
56   registered_.push_back(record);
57 
58   NotificationServiceImpl::current()->AddObserver(observer, type, source);
59 }
60 
Remove(NotificationObserver * observer,int type,const NotificationSource & source)61 void NotificationRegistrar::Remove(NotificationObserver* observer,
62                                    int type,
63                                    const NotificationSource& source) {
64   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
65 
66   Record record = { observer, type, source };
67   RecordVector::iterator found =
68       std::find(registered_.begin(), registered_.end(), record);
69   DCHECK(found != registered_.end());
70 
71   registered_.erase(found);
72 
73   // This can be nullptr if our owner outlives the NotificationService, e.g. if
74   // our owner is a Singleton.
75   NotificationServiceImpl* service = NotificationServiceImpl::current();
76   if (service)
77     service->RemoveObserver(observer, type, source);
78 }
79 
RemoveAll()80 void NotificationRegistrar::RemoveAll() {
81   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
82   // Early-exit if no registrations, to avoid calling
83   // NotificationService::current.  If we've constructed an object with a
84   // NotificationRegistrar member, but haven't actually used the notification
85   // service, and we reach prgram exit, then calling current() below could try
86   // to initialize the service's lazy TLS pointer during exit, which throws
87   // wrenches at things.
88   if (registered_.empty())
89     return;
90 
91   // This can be nullptr if our owner outlives the NotificationService, e.g. if
92   // our owner is a Singleton.
93   NotificationServiceImpl* service = NotificationServiceImpl::current();
94   if (service) {
95     for (size_t i = 0; i < registered_.size(); i++) {
96       service->RemoveObserver(registered_[i].observer,
97                               registered_[i].type,
98                               registered_[i].source);
99     }
100   }
101   registered_.clear();
102 }
103 
IsEmpty() const104 bool NotificationRegistrar::IsEmpty() const {
105   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
106   return registered_.empty();
107 }
108 
IsRegistered(NotificationObserver * observer,int type,const NotificationSource & source)109 bool NotificationRegistrar::IsRegistered(NotificationObserver* observer,
110                                          int type,
111                                          const NotificationSource& source) {
112   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
113   Record record = { observer, type, source };
114   return base::Contains(registered_, record);
115 }
116 
117 }  // namespace content
118