1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file describes a central switchboard for notifications that might
6// happen in various parts of the application, and allows users to register
7// observers for various classes of events that they're interested in.
8
9#ifndef CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_H_
10#define CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_H_
11
12#include "content/common/content_export.h"
13#include "content/public/browser/notification_details.h"
14#include "content/public/browser/notification_source.h"
15
16namespace content {
17
18class CONTENT_EXPORT NotificationService {
19 public:
20  // Returns the NotificationService object for the current thread, or NULL if
21  // none.
22  static NotificationService* current();
23
24  static NotificationService* Create();
25
26  virtual ~NotificationService() {}
27
28  // Synchronously posts a notification to all interested observers.
29  // Source is a reference to a NotificationSource object representing
30  // the object originating the notification (can be
31  // NotificationService::AllSources(), in which case
32  // only observers interested in all sources will be notified).
33  // Details is a reference to an object containing additional data about
34  // the notification.  If no additional data is needed, NoDetails() is used.
35  // There is no particular order in which the observers will be notified.
36  virtual void Notify(int type,
37                      const NotificationSource& source,
38                      const NotificationDetails& details) = 0;
39
40  // Returns a NotificationSource that represents all notification sources
41  // (for the purpose of registering an observer for events from all sources).
42  static Source<void> AllSources() { return Source<void>(NULL); }
43
44  // Returns the same value as AllSources(). This function has semantic
45  // differences to the programmer: We have checked that this AllSources()
46  // usage is safe in the face of multiple profiles. Objects that were
47  // singletons now will always have multiple instances, one per browser
48  // context.
49  //
50  // Some usage is safe, where the Source is checked to see if it's a member of
51  // a container before use. But, we want the number of AllSources() calls to
52  // drop to almost nothing, because most usages are not multiprofile safe and
53  // were done because it was easier to listen to everything.
54  static Source<void> AllBrowserContextsAndSources() {
55    return Source<void>(NULL);
56  }
57
58  // Returns a NotificationDetails object that represents a lack of details
59  // associated with a notification.  (This is effectively a null pointer.)
60  static Details<void> NoDetails() { return Details<void>(NULL); }
61};
62
63}  // namespace content
64
65#endif  // CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_H_
66