| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser/notification_service_impl.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 |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 namespace content { | 24 namespace content { |
| 25 | 25 |
| 26 struct NotificationRegistrar::Record { | 26 struct NotificationRegistrar::Record { |
| 27 bool operator==(const Record& other) const; | 27 bool operator==(const Record& other) const; |
| 28 | 28 |
| 29 NotificationObserver* observer; | 29 NotificationObserver* observer; |
| 30 int type; | 30 int type; |
| 31 content::NotificationSource source; | 31 NotificationSource source; |
| 32 base::PlatformThreadId thread_id; | 32 base::PlatformThreadId thread_id; |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 bool NotificationRegistrar::Record::operator==(const Record& other) const { | 35 bool NotificationRegistrar::Record::operator==(const Record& other) const { |
| 36 return observer == other.observer && | 36 return observer == other.observer && |
| 37 type == other.type && | 37 type == other.type && |
| 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 NotificationServiceImpl::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 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 NotificationServiceImpl::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 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); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 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(NotificationObserver* observer, |
| 118 NotificationObserver* observer, | 118 int type, |
| 119 int type, | 119 const NotificationSource& source) { |
| 120 const content::NotificationSource& source) { | |
| 121 Record record = { observer, type, source }; | 120 Record record = { observer, type, source }; |
| 122 return std::find(registered_.begin(), registered_.end(), record) != | 121 return std::find(registered_.begin(), registered_.end(), record) != |
| 123 registered_.end(); | 122 registered_.end(); |
| 124 } | 123 } |
| 125 | 124 |
| 126 } // namespace content | 125 } // namespace content |
| OLD | NEW |