OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/public/browser/notification_registrar.h" | 5 #include "content/public/browser/notification_registrar.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
11 #include "content/common/notification_service.h" | 11 #include "content/browser/notification_service_impl.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 void CheckCalledOnValidThread(base::PlatformThreadId thread_id) { | 15 void CheckCalledOnValidThread(base::PlatformThreadId thread_id) { |
16 base::PlatformThreadId current_thread_id = base::PlatformThread::CurrentId(); | 16 base::PlatformThreadId current_thread_id = base::PlatformThread::CurrentId(); |
17 CHECK(current_thread_id == thread_id) << "called on invalid thread: " | 17 CHECK(current_thread_id == thread_id) << "called on invalid thread: " |
18 << thread_id << " vs. " | 18 << thread_id << " vs. " |
19 << current_thread_id; | 19 << current_thread_id; |
20 } | 20 } |
21 | 21 |
(...skipping 16 matching lines...) Expand all Loading... |
38 source == other.source; | 38 source == other.source; |
39 // thread_id is for debugging purpose and thus not compared here. | 39 // thread_id is for debugging purpose and thus not compared here. |
40 } | 40 } |
41 | 41 |
42 NotificationRegistrar::NotificationRegistrar() { | 42 NotificationRegistrar::NotificationRegistrar() { |
43 // Force the NotificationService to be constructed (if it isn't already). | 43 // Force the NotificationService to be constructed (if it isn't already). |
44 // This ensures the NotificationService will be registered on the | 44 // This ensures the NotificationService will be registered on the |
45 // AtExitManager before any objects which access it via NotificationRegistrar. | 45 // AtExitManager before any objects which access it via NotificationRegistrar. |
46 // This in turn means it will be destroyed after these objects, so they will | 46 // This in turn means it will be destroyed after these objects, so they will |
47 // never try to access the NotificationService after it's been destroyed. | 47 // never try to access the NotificationService after it's been destroyed. |
48 NotificationService::current(); | 48 NotificationServiceImpl::current(); |
49 } | 49 } |
50 | 50 |
51 NotificationRegistrar::~NotificationRegistrar() { | 51 NotificationRegistrar::~NotificationRegistrar() { |
52 RemoveAll(); | 52 RemoveAll(); |
53 } | 53 } |
54 | 54 |
55 void NotificationRegistrar::Add(NotificationObserver* observer, | 55 void NotificationRegistrar::Add(NotificationObserver* observer, |
56 int type, | 56 int type, |
57 const content::NotificationSource& source) { | 57 const content::NotificationSource& source) { |
58 DCHECK(!IsRegistered(observer, type, source)) << "Duplicate registration."; | 58 DCHECK(!IsRegistered(observer, type, source)) << "Duplicate registration."; |
59 | 59 |
60 Record record = { observer, type, source, base::PlatformThread::CurrentId() }; | 60 Record record = { observer, type, source, base::PlatformThread::CurrentId() }; |
61 registered_.push_back(record); | 61 registered_.push_back(record); |
62 | 62 |
63 NotificationService::current()->AddObserver(observer, type, source); | 63 NotificationServiceImpl::current()->AddObserver(observer, type, source); |
64 } | 64 } |
65 | 65 |
66 void NotificationRegistrar::Remove(NotificationObserver* observer, | 66 void NotificationRegistrar::Remove(NotificationObserver* observer, |
67 int type, | 67 int type, |
68 const content::NotificationSource& source) { | 68 const content::NotificationSource& source) { |
69 if (!IsRegistered(observer, type, source)) { | 69 if (!IsRegistered(observer, type, source)) { |
70 NOTREACHED() << "Trying to remove unregistered observer of type " << | 70 NOTREACHED() << "Trying to remove unregistered observer of type " << |
71 type << " from list of size " << registered_.size() << "."; | 71 type << " from list of size " << registered_.size() << "."; |
72 return; | 72 return; |
73 } | 73 } |
74 | 74 |
75 Record record = { observer, type, source }; | 75 Record record = { observer, type, source }; |
76 RecordVector::iterator found = std::find( | 76 RecordVector::iterator found = std::find( |
77 registered_.begin(), registered_.end(), record); | 77 registered_.begin(), registered_.end(), record); |
78 CheckCalledOnValidThread(found->thread_id); | 78 CheckCalledOnValidThread(found->thread_id); |
79 registered_.erase(found); | 79 registered_.erase(found); |
80 | 80 |
81 // This can be NULL if our owner outlives the NotificationService, e.g. if our | 81 // This can be NULL if our owner outlives the NotificationService, e.g. if our |
82 // owner is a Singleton. | 82 // owner is a Singleton. |
83 NotificationService* service = NotificationService::current(); | 83 NotificationServiceImpl* service = NotificationServiceImpl::current(); |
84 if (service) | 84 if (service) |
85 service->RemoveObserver(observer, type, source); | 85 service->RemoveObserver(observer, type, source); |
86 } | 86 } |
87 | 87 |
88 void NotificationRegistrar::RemoveAll() { | 88 void NotificationRegistrar::RemoveAll() { |
89 // Early-exit if no registrations, to avoid calling | 89 // Early-exit if no registrations, to avoid calling |
90 // NotificationService::current. If we've constructed an object with a | 90 // NotificationService::current. If we've constructed an object with a |
91 // NotificationRegistrar member, but haven't actually used the notification | 91 // NotificationRegistrar member, but haven't actually used the notification |
92 // service, and we reach prgram exit, then calling current() below could try | 92 // service, and we reach prgram exit, then calling current() below could try |
93 // to initialize the service's lazy TLS pointer during exit, which throws | 93 // to initialize the service's lazy TLS pointer during exit, which throws |
94 // wrenches at things. | 94 // wrenches at things. |
95 if (registered_.empty()) | 95 if (registered_.empty()) |
96 return; | 96 return; |
97 | 97 |
98 | 98 |
99 // This can be NULL if our owner outlives the NotificationService, e.g. if our | 99 // This can be NULL if our owner outlives the NotificationService, e.g. if our |
100 // owner is a Singleton. | 100 // owner is a Singleton. |
101 NotificationService* service = NotificationService::current(); | 101 NotificationServiceImpl* service = NotificationServiceImpl::current(); |
102 if (service) { | 102 if (service) { |
103 for (size_t i = 0; i < registered_.size(); i++) { | 103 for (size_t i = 0; i < registered_.size(); i++) { |
104 CheckCalledOnValidThread(registered_[i].thread_id); | 104 CheckCalledOnValidThread(registered_[i].thread_id); |
105 service->RemoveObserver(registered_[i].observer, | 105 service->RemoveObserver(registered_[i].observer, |
106 registered_[i].type, | 106 registered_[i].type, |
107 registered_[i].source); | 107 registered_[i].source); |
108 } | 108 } |
109 } | 109 } |
110 registered_.clear(); | 110 registered_.clear(); |
111 } | 111 } |
112 | 112 |
113 bool NotificationRegistrar::IsEmpty() const { | 113 bool NotificationRegistrar::IsEmpty() const { |
114 return registered_.empty(); | 114 return registered_.empty(); |
115 } | 115 } |
116 | 116 |
117 bool NotificationRegistrar::IsRegistered( | 117 bool NotificationRegistrar::IsRegistered( |
118 NotificationObserver* observer, | 118 NotificationObserver* observer, |
119 int type, | 119 int type, |
120 const content::NotificationSource& source) { | 120 const content::NotificationSource& source) { |
121 Record record = { observer, type, source }; | 121 Record record = { observer, type, source }; |
122 return std::find(registered_.begin(), registered_.end(), record) != | 122 return std::find(registered_.begin(), registered_.end(), record) != |
123 registered_.end(); | 123 registered_.end(); |
124 } | 124 } |
125 | 125 |
126 } // namespace content | 126 } // namespace content |
OLD | NEW |