Overview
Notification Service provides
notification delivery mechanism from producer to observer in the same
thread. This class is created and used in browser process’s sub
threads.
Refer
to src/content/browser/browser_process_sub_thread.cc
Components
a. NotificationService
It handles notification delivery and
manages observer list.
This service provides method that
can add or remove notification for observer.
Also, Notify method is provided for
notification provider.
To get the NotificationService
object, content::NotificationService::current() is used.
It could be used in that thread
only. It means calling content::NotifcationService::current()
returns different object in UI
thread and IO thread.
b. Notification Observer:
This component is responsible for
registration its preferred notification type to
NotificationService.
For receiving notification, Observer
must implement NotificationObserver class.
c. Notification Provider:
It is a notification producer. This
component sends notification to NotificationService.
How
To Register Notification to NotificationService
a. Add Notification
For registration,
NotificationRegistrar class is used instead of using
NotificationService
directly. It is a helper class for
registering notification and ensures that all registered
notifications are unregistered when
the class is destroyed.
Refer to
src/content/public/browser/notification_registrar.h
b. Implement Notification Handler
For implementing handler, Observer
should implement NotificationObserver class as a base
class. It has only one function,
observe(). When registered notification is occured,
NotificationService calls this
function.
Refer to
src/content/public/browser/notification_observer.h
How
to Notify to NotificationService
NotificationService provides Notify()
for synchronous notify mechanism. Notification provider should
deliver notification type, source that originates this notification
and details that contains additional information to
NotificationService via Notify().
Refer
to src/content/browser/notification_service_impl.cc