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/common/notification_registrar.h" | 5 #include "content/common/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/common/notification_service.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 |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 struct NotificationRegistrar::Record { | 24 struct NotificationRegistrar::Record { |
25 bool operator==(const Record& other) const; | 25 bool operator==(const Record& other) const; |
26 | 26 |
27 NotificationObserver* observer; | 27 NotificationObserver* observer; |
28 NotificationType type; | 28 int type; |
29 NotificationSource source; | 29 NotificationSource source; |
30 base::PlatformThreadId thread_id; | 30 base::PlatformThreadId thread_id; |
31 }; | 31 }; |
32 | 32 |
33 bool NotificationRegistrar::Record::operator==(const Record& other) const { | 33 bool NotificationRegistrar::Record::operator==(const Record& other) const { |
34 return observer == other.observer && | 34 return observer == other.observer && |
35 type == other.type && | 35 type == other.type && |
36 source == other.source; | 36 source == other.source; |
37 // thread_id is for debugging purpose and thus not compared here. | 37 // thread_id is for debugging purpose and thus not compared here. |
38 } | 38 } |
39 | 39 |
40 NotificationRegistrar::NotificationRegistrar() { | 40 NotificationRegistrar::NotificationRegistrar() { |
41 // Force the NotificationService to be constructed (if it isn't already). | 41 // Force the NotificationService to be constructed (if it isn't already). |
42 // This ensures the NotificationService will be registered on the | 42 // This ensures the NotificationService will be registered on the |
43 // AtExitManager before any objects which access it via NotificationRegistrar. | 43 // AtExitManager before any objects which access it via NotificationRegistrar. |
44 // This in turn means it will be destroyed after these objects, so they will | 44 // This in turn means it will be destroyed after these objects, so they will |
45 // never try to access the NotificationService after it's been destroyed. | 45 // never try to access the NotificationService after it's been destroyed. |
46 NotificationService::current(); | 46 NotificationService::current(); |
47 } | 47 } |
48 | 48 |
49 NotificationRegistrar::~NotificationRegistrar() { | 49 NotificationRegistrar::~NotificationRegistrar() { |
50 RemoveAll(); | 50 RemoveAll(); |
51 } | 51 } |
52 | 52 |
53 void NotificationRegistrar::Add(NotificationObserver* observer, | 53 void NotificationRegistrar::Add(NotificationObserver* observer, |
54 NotificationType type, | 54 int type, |
55 const NotificationSource& source) { | 55 const NotificationSource& source) { |
56 DCHECK(!IsRegistered(observer, type, source)) << "Duplicate registration."; | 56 DCHECK(!IsRegistered(observer, type, source)) << "Duplicate registration."; |
57 | 57 |
58 Record record = { observer, type, source, base::PlatformThread::CurrentId() }; | 58 Record record = { observer, type, source, base::PlatformThread::CurrentId() }; |
59 registered_.push_back(record); | 59 registered_.push_back(record); |
60 | 60 |
61 NotificationService::current()->AddObserver(observer, type, source); | 61 NotificationService::current()->AddObserver(observer, type, source); |
62 } | 62 } |
63 | 63 |
64 void NotificationRegistrar::Remove(NotificationObserver* observer, | 64 void NotificationRegistrar::Remove(NotificationObserver* observer, |
65 NotificationType type, | 65 int type, |
66 const NotificationSource& source) { | 66 const NotificationSource& source) { |
67 if (!IsRegistered(observer, type, source)) { | 67 if (!IsRegistered(observer, type, source)) { |
68 NOTREACHED() << "Trying to remove unregistered observer of type " << | 68 NOTREACHED() << "Trying to remove unregistered observer of type " << |
69 type.value << " from list of size " << registered_.size() << "."; | 69 type << " from list of size " << registered_.size() << "."; |
70 return; | 70 return; |
71 } | 71 } |
72 | 72 |
73 Record record = { observer, type, source }; | 73 Record record = { observer, type, source }; |
74 RecordVector::iterator found = std::find( | 74 RecordVector::iterator found = std::find( |
75 registered_.begin(), registered_.end(), record); | 75 registered_.begin(), registered_.end(), record); |
76 CheckCalledOnValidThread(found->thread_id); | 76 CheckCalledOnValidThread(found->thread_id); |
77 registered_.erase(found); | 77 registered_.erase(found); |
78 | 78 |
79 // This can be NULL if our owner outlives the NotificationService, e.g. if our | 79 // This can be NULL if our owner outlives the NotificationService, e.g. if our |
(...skipping 26 matching lines...) Expand all Loading... |
106 } | 106 } |
107 } | 107 } |
108 registered_.clear(); | 108 registered_.clear(); |
109 } | 109 } |
110 | 110 |
111 bool NotificationRegistrar::IsEmpty() const { | 111 bool NotificationRegistrar::IsEmpty() const { |
112 return registered_.empty(); | 112 return registered_.empty(); |
113 } | 113 } |
114 | 114 |
115 bool NotificationRegistrar::IsRegistered(NotificationObserver* observer, | 115 bool NotificationRegistrar::IsRegistered(NotificationObserver* observer, |
116 NotificationType type, | 116 int type, |
117 const NotificationSource& source) { | 117 const NotificationSource& source) { |
118 Record record = { observer, type, source }; | 118 Record record = { observer, type, source }; |
119 return std::find(registered_.begin(), registered_.end(), record) != | 119 return std::find(registered_.begin(), registered_.end(), record) != |
120 registered_.end(); | 120 registered_.end(); |
121 } | 121 } |
OLD | NEW |