diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-19 23:17:07 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-19 23:17:07 +0000 |
commit | ad50def5e1165d0cc74b98f988bbd5962587d9f4 (patch) | |
tree | 3a751abaed2cc056ca60b8b1e3ff54532a6f4d3f /content/browser/notification_service_impl.h | |
parent | 75c920505ddfb2d49c194c76bde64edd6b3f91f2 (diff) | |
download | chromium_src-ad50def5e1165d0cc74b98f988bbd5962587d9f4.zip chromium_src-ad50def5e1165d0cc74b98f988bbd5962587d9f4.tar.gz chromium_src-ad50def5e1165d0cc74b98f988bbd5962587d9f4.tar.bz2 |
Make NotificationService an interface in the content namespace, and switch callers to use it. Move the implementation to content/browser. Stop creating it in all child processes since it's only used in the browser.
BUG=98716
Review URL: http://codereview.chromium.org/8342048
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@106403 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/notification_service_impl.h')
-rw-r--r-- | content/browser/notification_service_impl.h | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/content/browser/notification_service_impl.h b/content/browser/notification_service_impl.h new file mode 100644 index 0000000..0898234 --- /dev/null +++ b/content/browser/notification_service_impl.h @@ -0,0 +1,92 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ +#define CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ +#pragma once + +#include <map> + +#include "base/observer_list.h" +#include "content/common/content_export.h" +#include "content/public/browser/notification_service.h" + +namespace content { +class NotificationObserver; +class NotificationRegistrar; +} + +class NotificationServiceImpl : public content::NotificationService { + public: + static NotificationServiceImpl* current(); + + // Normally instantiated when the thread is created. Not all threads have + // a NotificationService. Only one instance should be created per thread. + NotificationServiceImpl(); + virtual ~NotificationServiceImpl(); + + // content::NotificationService + virtual void Notify(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details); + + private: + friend class content::NotificationRegistrar; + + typedef ObserverList<content::NotificationObserver> NotificationObserverList; + typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap; + typedef std::map<int, NotificationSourceMap> NotificationObserverMap; + typedef std::map<int, int> NotificationObserverCount; + + // Convenience function to determine whether a source has a + // NotificationObserverList in the given map; + static bool HasKey(const NotificationSourceMap& map, + const content::NotificationSource& source); + + // NOTE: Rather than using this directly, you should use a + // NotificationRegistrar. + // + // Registers a NotificationObserver to be called whenever a matching + // 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 + // types). + // Source is a NotificationSource object (created using + // "Source<classname>(pointer)"), if this observer only wants to + // receive events from that object, or NotificationService::AllSources() + // to receive events from all sources. + // + // A given observer can be registered only once for each combination of + // type and source. If the same object is registered more than once, + // 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); + + // NOTE: Rather than using this directly, you should use a + // NotificationRegistrar. + // + // 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); + + // Keeps track of the observers for each type of notification. + // Until we get a prohibitively large number of notification types, + // a simple array is probably the fastest way to dispatch. + NotificationObserverMap observers_; + +#ifndef NDEBUG + // Used to check to see that AddObserver and RemoveObserver calls are + // balanced. + NotificationObserverCount observer_counts_; +#endif + + DISALLOW_COPY_AND_ASSIGN(NotificationServiceImpl); +}; + +#endif // CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ |