diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-30 00:30:09 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-30 00:30:09 +0000 |
commit | 1a8163936437805fef3519520bae43c48dd723b1 (patch) | |
tree | 30389a290ef075e58b902cfd928bdf1fd2fe5a5f | |
parent | cc5d7f4feebf230546893234449242d22eab524a (diff) | |
download | chromium_src-1a8163936437805fef3519520bae43c48dd723b1.zip chromium_src-1a8163936437805fef3519520bae43c48dd723b1.tar.gz chromium_src-1a8163936437805fef3519520bae43c48dd723b1.tar.bz2 |
content/browser: Move notification_service_impl into content namespace.
R=jam@chromium.org
Review URL: https://codereview.chromium.org/11340018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@164793 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | content/browser/browser_main_runner.cc | 1 | ||||
-rw-r--r-- | content/browser/notification_service_impl.cc | 59 | ||||
-rw-r--r-- | content/browser/notification_service_impl.h | 31 | ||||
-rw-r--r-- | content/browser/notification_service_impl_unittest.cc | 129 | ||||
-rw-r--r-- | content/public/test/test_content_client_initializer.h | 3 |
5 files changed, 117 insertions, 106 deletions
diff --git a/content/browser/browser_main_runner.cc b/content/browser/browser_main_runner.cc index 061c7d4..319a59f 100644 --- a/content/browser/browser_main_runner.cc +++ b/content/browser/browser_main_runner.cc @@ -30,6 +30,7 @@ bool g_exited_main_message_loop = false; using content::ChildProcess; +using content::NotificationServiceImpl; namespace { diff --git a/content/browser/notification_service_impl.cc b/content/browser/notification_service_impl.cc index 2c6fa06..4913288 100644 --- a/content/browser/notification_service_impl.cc +++ b/content/browser/notification_service_impl.cc @@ -9,27 +9,33 @@ #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_types.h" -static base::LazyInstance<base::ThreadLocalPointer<NotificationServiceImpl> > +namespace content { + +namespace { + +base::LazyInstance<base::ThreadLocalPointer<NotificationServiceImpl> > lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; +} // namespace + // static NotificationServiceImpl* NotificationServiceImpl::current() { return lazy_tls_ptr.Pointer()->Get(); } // static -content::NotificationService* content::NotificationService::current() { +NotificationService* NotificationService::current() { return NotificationServiceImpl::current(); } // static -content::NotificationService* content::NotificationService::Create() { +NotificationService* NotificationService::Create() { return new NotificationServiceImpl; } // static bool NotificationServiceImpl::HasKey(const NotificationSourceMap& map, - const content::NotificationSource& source) { + const NotificationSource& source) { return map.find(source.map_key()) != map.end(); } @@ -38,10 +44,9 @@ NotificationServiceImpl::NotificationServiceImpl() { lazy_tls_ptr.Pointer()->Set(this); } -void NotificationServiceImpl::AddObserver( - content::NotificationObserver* observer, - int type, - const content::NotificationSource& source) { +void NotificationServiceImpl::AddObserver(NotificationObserver* observer, + int type, + const NotificationSource& source) { // We have gotten some crashes where the observer pointer is NULL. The problem // is that this happens when we actually execute a notification, so have no // way of knowing who the bad observer was. We want to know when this happens @@ -63,10 +68,9 @@ void NotificationServiceImpl::AddObserver( #endif } -void NotificationServiceImpl::RemoveObserver( - content::NotificationObserver* observer, - int type, - const content::NotificationSource& source) { +void NotificationServiceImpl::RemoveObserver(NotificationObserver* observer, + int type, + const NotificationSource& source) { // This is a very serious bug. An object is most likely being deleted on // the wrong thread, and as a result another thread's NotificationServiceImpl // has its deleted pointer in its map. A garbge object will be called in the @@ -89,42 +93,41 @@ void NotificationServiceImpl::RemoveObserver( } } -void NotificationServiceImpl::Notify( - int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) { - DCHECK(type > content::NOTIFICATION_ALL) << +void NotificationServiceImpl::Notify(int type, + const NotificationSource& source, + const NotificationDetails& details) { + DCHECK_GT(type, NOTIFICATION_ALL) << "Allowed for observing, but not posting."; // There's no particular reason for the order in which the different // classes of observers get notified here. // Notify observers of all types and all sources - if (HasKey(observers_[content::NOTIFICATION_ALL], AllSources()) && + if (HasKey(observers_[NOTIFICATION_ALL], AllSources()) && source != AllSources()) { - FOR_EACH_OBSERVER(content::NotificationObserver, - *observers_[content::NOTIFICATION_ALL][AllSources().map_key()], - Observe(type, source, details)); + FOR_EACH_OBSERVER(NotificationObserver, + *observers_[NOTIFICATION_ALL][AllSources().map_key()], + Observe(type, source, details)); } // Notify observers of all types and the given source - if (HasKey(observers_[content::NOTIFICATION_ALL], source)) { - FOR_EACH_OBSERVER(content::NotificationObserver, - *observers_[content::NOTIFICATION_ALL][source.map_key()], - Observe(type, source, details)); + if (HasKey(observers_[NOTIFICATION_ALL], source)) { + FOR_EACH_OBSERVER(NotificationObserver, + *observers_[NOTIFICATION_ALL][source.map_key()], + Observe(type, source, details)); } // Notify observers of the given type and all sources if (HasKey(observers_[type], AllSources()) && source != AllSources()) { - FOR_EACH_OBSERVER(content::NotificationObserver, + FOR_EACH_OBSERVER(NotificationObserver, *observers_[type][AllSources().map_key()], Observe(type, source, details)); } // Notify observers of the given type and the given source if (HasKey(observers_[type], source)) { - FOR_EACH_OBSERVER(content::NotificationObserver, + FOR_EACH_OBSERVER(NotificationObserver, *observers_[type][source.map_key()], Observe(type, source, details)); } @@ -152,3 +155,5 @@ NotificationServiceImpl::~NotificationServiceImpl() { delete it->second; } } + +} // namespace content diff --git a/content/browser/notification_service_impl.h b/content/browser/notification_service_impl.h index e7c135d..c9dce92 100644 --- a/content/browser/notification_service_impl.h +++ b/content/browser/notification_service_impl.h @@ -12,12 +12,11 @@ #include "content/public/browser/notification_service.h" namespace content { + class NotificationObserver; class NotificationRegistrar; -} -class CONTENT_EXPORT NotificationServiceImpl - : public content::NotificationService { +class CONTENT_EXPORT NotificationServiceImpl : public NotificationService { public: static NotificationServiceImpl* current(); @@ -26,15 +25,15 @@ class CONTENT_EXPORT NotificationServiceImpl NotificationServiceImpl(); virtual ~NotificationServiceImpl(); - // content::NotificationService + // NotificationService: virtual void Notify(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) OVERRIDE; + const NotificationSource& source, + const NotificationDetails& details) OVERRIDE; private: - friend class content::NotificationRegistrar; + friend class NotificationRegistrar; - typedef ObserverList<content::NotificationObserver> NotificationObserverList; + typedef ObserverList<NotificationObserver> NotificationObserverList; typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap; typedef std::map<int, NotificationSourceMap> NotificationObserverMap; typedef std::map<int, int> NotificationObserverCount; @@ -42,7 +41,7 @@ class CONTENT_EXPORT NotificationServiceImpl // Convenience function to determine whether a source has a // NotificationObserverList in the given map; static bool HasKey(const NotificationSourceMap& map, - const content::NotificationSource& source); + const NotificationSource& source); // NOTE: Rather than using this directly, you should use a // NotificationRegistrar. @@ -51,7 +50,7 @@ class CONTENT_EXPORT NotificationServiceImpl // notification is posted. Observer is a pointer to an object subclassing // NotificationObserver to be notified when an event matching the other two // parameters is posted to this service. Type is the type of events to be - // notified about (or content::NOTIFICATION_ALL to receive events of all + // notified about (or NOTIFICATION_ALL to receive events of all // types). // Source is a NotificationSource object (created using // "Source<classname>(pointer)"), if this observer only wants to @@ -63,8 +62,9 @@ class CONTENT_EXPORT NotificationServiceImpl // it must be removed for each of those combinations of type and source later. // // The caller retains ownership of the object pointed to by observer. - void AddObserver(content::NotificationObserver* observer, - int type, const content::NotificationSource& source); + void AddObserver(NotificationObserver* observer, + int type, + const NotificationSource& source); // NOTE: Rather than using this directly, you should use a // NotificationRegistrar. @@ -72,8 +72,9 @@ class CONTENT_EXPORT NotificationServiceImpl // Removes the object pointed to by observer from receiving notifications // that match type and source. If no object matching the parameters is // currently registered, this method is a no-op. - void RemoveObserver(content::NotificationObserver* observer, - int type, const content::NotificationSource& source); + void RemoveObserver(NotificationObserver* observer, + int type, + const NotificationSource& source); // Keeps track of the observers for each type of notification. // Until we get a prohibitively large number of notification types, @@ -89,4 +90,6 @@ class CONTENT_EXPORT NotificationServiceImpl DISALLOW_COPY_AND_ASSIGN(NotificationServiceImpl); }; +} // namespace content + #endif // CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ diff --git a/content/browser/notification_service_impl_unittest.cc b/content/browser/notification_service_impl_unittest.cc index 62a0e51..2b91216 100644 --- a/content/browser/notification_service_impl_unittest.cc +++ b/content/browser/notification_service_impl_unittest.cc @@ -3,29 +3,32 @@ // found in the LICENSE file. #include "content/browser/notification_service_impl.h" + #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_registrar.h" #include "content/public/browser/notification_types.h" #include "testing/gtest/include/gtest/gtest.h" +namespace content { + namespace { // Bogus class to act as a NotificationSource for the messages. class TestSource {}; -class TestObserver : public content::NotificationObserver { -public: +class TestObserver : public NotificationObserver { + public: TestObserver() : notification_count_(0) {} - int notification_count() { return notification_count_; } + int notification_count() const { return notification_count_; } - void Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) { + virtual void Observe(int type, + const NotificationSource& source, + const NotificationDetails& details) OVERRIDE { ++notification_count_; } -private: + private: int notification_count_; }; @@ -34,7 +37,7 @@ private: class NotificationServiceImplTest : public testing::Test { protected: - content::NotificationRegistrar registrar_; + NotificationRegistrar registrar_; }; TEST_F(NotificationServiceImplTest, Basic) { @@ -42,10 +45,10 @@ TEST_F(NotificationServiceImplTest, Basic) { TestSource other_source; // Check the equality operators defined for NotificationSource - EXPECT_TRUE(content::Source<TestSource>(&test_source) == - content::Source<TestSource>(&test_source)); - EXPECT_TRUE(content::Source<TestSource>(&test_source) != - content::Source<TestSource>(&other_source)); + EXPECT_TRUE(Source<TestSource>(&test_source) == + Source<TestSource>(&test_source)); + EXPECT_TRUE(Source<TestSource>(&test_source) != + Source<TestSource>(&other_source)); TestObserver all_types_all_sources; TestObserver idle_all_sources; @@ -53,56 +56,55 @@ TEST_F(NotificationServiceImplTest, Basic) { TestObserver idle_test_source; // Make sure it doesn't freak out when there are no observers. - content::NotificationService* service = - content::NotificationService::current(); - service->Notify(content::NOTIFICATION_IDLE, - content::Source<TestSource>(&test_source), - content::NotificationService::NoDetails()); - - registrar_.Add(&all_types_all_sources, content::NOTIFICATION_ALL, - content::NotificationService::AllSources()); - registrar_.Add(&idle_all_sources, content::NOTIFICATION_IDLE, - content::NotificationService::AllSources()); - registrar_.Add(&all_types_test_source, content::NOTIFICATION_ALL, - content::Source<TestSource>(&test_source)); - registrar_.Add(&idle_test_source, content::NOTIFICATION_IDLE, - content::Source<TestSource>(&test_source)); + NotificationService* service = NotificationService::current(); + service->Notify(NOTIFICATION_IDLE, + Source<TestSource>(&test_source), + NotificationService::NoDetails()); + + registrar_.Add(&all_types_all_sources, NOTIFICATION_ALL, + NotificationService::AllSources()); + registrar_.Add(&idle_all_sources, NOTIFICATION_IDLE, + NotificationService::AllSources()); + registrar_.Add(&all_types_test_source, NOTIFICATION_ALL, + Source<TestSource>(&test_source)); + registrar_.Add(&idle_test_source, NOTIFICATION_IDLE, + Source<TestSource>(&test_source)); EXPECT_EQ(0, all_types_all_sources.notification_count()); EXPECT_EQ(0, idle_all_sources.notification_count()); EXPECT_EQ(0, all_types_test_source.notification_count()); EXPECT_EQ(0, idle_test_source.notification_count()); - service->Notify(content::NOTIFICATION_IDLE, - content::Source<TestSource>(&test_source), - content::NotificationService::NoDetails()); + service->Notify(NOTIFICATION_IDLE, + Source<TestSource>(&test_source), + NotificationService::NoDetails()); EXPECT_EQ(1, all_types_all_sources.notification_count()); EXPECT_EQ(1, idle_all_sources.notification_count()); EXPECT_EQ(1, all_types_test_source.notification_count()); EXPECT_EQ(1, idle_test_source.notification_count()); - service->Notify(content::NOTIFICATION_BUSY, - content::Source<TestSource>(&test_source), - content::NotificationService::NoDetails()); + service->Notify(NOTIFICATION_BUSY, + Source<TestSource>(&test_source), + NotificationService::NoDetails()); EXPECT_EQ(2, all_types_all_sources.notification_count()); EXPECT_EQ(1, idle_all_sources.notification_count()); EXPECT_EQ(2, all_types_test_source.notification_count()); EXPECT_EQ(1, idle_test_source.notification_count()); - service->Notify(content::NOTIFICATION_IDLE, - content::Source<TestSource>(&other_source), - content::NotificationService::NoDetails()); + service->Notify(NOTIFICATION_IDLE, + Source<TestSource>(&other_source), + NotificationService::NoDetails()); EXPECT_EQ(3, all_types_all_sources.notification_count()); EXPECT_EQ(2, idle_all_sources.notification_count()); EXPECT_EQ(2, all_types_test_source.notification_count()); EXPECT_EQ(1, idle_test_source.notification_count()); - service->Notify(content::NOTIFICATION_BUSY, - content::Source<TestSource>(&other_source), - content::NotificationService::NoDetails()); + service->Notify(NOTIFICATION_BUSY, + Source<TestSource>(&other_source), + NotificationService::NoDetails()); EXPECT_EQ(4, all_types_all_sources.notification_count()); EXPECT_EQ(2, idle_all_sources.notification_count()); @@ -110,9 +112,9 @@ TEST_F(NotificationServiceImplTest, Basic) { EXPECT_EQ(1, idle_test_source.notification_count()); // Try send with NULL source. - service->Notify(content::NOTIFICATION_IDLE, - content::NotificationService::AllSources(), - content::NotificationService::NoDetails()); + service->Notify(NOTIFICATION_IDLE, + NotificationService::AllSources(), + NotificationService::NoDetails()); EXPECT_EQ(5, all_types_all_sources.notification_count()); EXPECT_EQ(3, idle_all_sources.notification_count()); @@ -121,9 +123,9 @@ TEST_F(NotificationServiceImplTest, Basic) { registrar_.RemoveAll(); - service->Notify(content::NOTIFICATION_IDLE, - content::Source<TestSource>(&test_source), - content::NotificationService::NoDetails()); + service->Notify(NOTIFICATION_IDLE, + Source<TestSource>(&test_source), + NotificationService::NoDetails()); EXPECT_EQ(5, all_types_all_sources.notification_count()); EXPECT_EQ(3, idle_all_sources.notification_count()); @@ -136,32 +138,33 @@ TEST_F(NotificationServiceImplTest, MultipleRegistration) { TestObserver idle_test_source; - content::NotificationService* service = - content::NotificationService::current(); + NotificationService* service = NotificationService::current(); - registrar_.Add(&idle_test_source, content::NOTIFICATION_IDLE, - content::Source<TestSource>(&test_source)); - registrar_.Add(&idle_test_source, content::NOTIFICATION_ALL, - content::Source<TestSource>(&test_source)); + registrar_.Add(&idle_test_source, NOTIFICATION_IDLE, + Source<TestSource>(&test_source)); + registrar_.Add(&idle_test_source, NOTIFICATION_ALL, + Source<TestSource>(&test_source)); - service->Notify(content::NOTIFICATION_IDLE, - content::Source<TestSource>(&test_source), - content::NotificationService::NoDetails()); + service->Notify(NOTIFICATION_IDLE, + Source<TestSource>(&test_source), + NotificationService::NoDetails()); EXPECT_EQ(2, idle_test_source.notification_count()); - registrar_.Remove(&idle_test_source, content::NOTIFICATION_IDLE, - content::Source<TestSource>(&test_source)); + registrar_.Remove(&idle_test_source, NOTIFICATION_IDLE, + Source<TestSource>(&test_source)); - service->Notify(content::NOTIFICATION_IDLE, - content::Source<TestSource>(&test_source), - content::NotificationService::NoDetails()); + service->Notify(NOTIFICATION_IDLE, + Source<TestSource>(&test_source), + NotificationService::NoDetails()); EXPECT_EQ(3, idle_test_source.notification_count()); - registrar_.Remove(&idle_test_source, content::NOTIFICATION_ALL, - content::Source<TestSource>(&test_source)); + registrar_.Remove(&idle_test_source, NOTIFICATION_ALL, + Source<TestSource>(&test_source)); - service->Notify(content::NOTIFICATION_IDLE, - content::Source<TestSource>(&test_source), - content::NotificationService::NoDetails()); + service->Notify(NOTIFICATION_IDLE, + Source<TestSource>(&test_source), + NotificationService::NoDetails()); EXPECT_EQ(3, idle_test_source.notification_count()); } + +} // namespace content diff --git a/content/public/test/test_content_client_initializer.h b/content/public/test/test_content_client_initializer.h index 22a69a2..eb9b6f0 100644 --- a/content/public/test/test_content_client_initializer.h +++ b/content/public/test/test_content_client_initializer.h @@ -8,12 +8,11 @@ #include "base/basictypes.h" #include "base/memory/scoped_ptr.h" -class NotificationServiceImpl; - namespace content { class ContentClient; class MockRenderProcessHostFactory; +class NotificationServiceImpl; class TestContentBrowserClient; class TestRenderViewHostFactory; |