summaryrefslogtreecommitdiffstats
path: root/ui/message_center/message_center.h
blob: c4009cc6fc30a4a155b51848f11ec4bb547b05ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2012 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 UI_MESSAGE_CENTER_MESSAGE_CENTER_H_
#define UI_MESSAGE_CENTER_MESSAGE_CENTER_H_

#include <string>

#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/message_center/message_center_export.h"
#include "ui/message_center/notification_list.h"
#include "ui/message_center/notification_types.h"

namespace base {
class DictionaryValue;
}

// Class for managing the NotificationList. The client (e.g. Chrome) calls
// [Add|Remove|Update]Notification to create and update notifications in the
// list. It can also implement Delegate to receive callbacks when a
// notification is removed (closed), or clicked on.
// If an Observer is provided, it will be informed when the notification list
// changes, and is expected to handle creating, showing, and hiding of any
// bubbles.

namespace message_center {

class MESSAGE_CENTER_EXPORT MessageCenter : public NotificationList::Delegate {
 public:
  // Class that hosts the message center.
  class MESSAGE_CENTER_EXPORT Observer {
   public:
    // Called when the notification list has changed. |new_notification| will
    // be true if a notification was added or updated.
    virtual void OnMessageCenterChanged(bool new_notification) = 0;
   protected:
    virtual ~Observer() {}
  };

  class MESSAGE_CENTER_EXPORT Delegate {
   public:
    // Called when the notification associated with |notification_id| is
    // removed (i.e. closed by the user).
    virtual void NotificationRemoved(const std::string& notification_id) = 0;

    // Request to disable the extension associated with |notification_id|.
    virtual void DisableExtension(const std::string& notification_id) = 0;

    // Request to disable notifications from the source of |notification_id|.
    virtual void DisableNotificationsFromSource(
        const std::string& notification_id) = 0;

    // Request to show the notification settings (|notification_id| is used
    // to identify the requesting browser context).
    virtual void ShowSettings(const std::string& notification_id) = 0;

    // Request to show the notification settings dialog. |context| is necessary
    // to create a new window.
    virtual void ShowSettingsDialog(gfx::NativeView context) = 0;

    // Called when the notification body is clicked on.
    virtual void OnClicked(const std::string& notification_id) = 0;

    // Called when a button in a notification is clicked. |button_index|
    // indicates which button was clicked, zero-indexed (button one is 0,
    // button two is 1).
    //
    // TODO(miket): consider providing default implementations for the pure
    // virtuals above, to avoid changing so many files in disparate parts of
    // the codebase each time we enhance this interface.
    virtual void OnButtonClicked(const std::string& id, int button_index);

   protected:
    virtual ~Delegate() {}
  };

  MessageCenter();
  virtual ~MessageCenter();

  // Called to set the delegate.  Generally called only once, except in tests.
  // Changing the delegate does not affect notifications in its
  // NotificationList.
  void SetDelegate(Delegate* delegate);

  // Management of the observer list.
  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Informs the notification list whether the message center is visible.
  // This affects whether or not a message has been "read".
  void SetMessageCenterVisible(bool visible);

  // Accessors to notification_list_
  size_t NotificationCount() const;
  size_t UnreadNotificationCount() const;
  bool HasPopupNotifications() const;

  // Adds a new notification. |id| is a unique identifier, used to update or
  // remove notifications. |title| and |meesage| describe the notification text.
  // Use SetNotificationIcon, SetNotificationImage, or SetNotificationButtonIcon
  // to set images. If |extension_id| is provided then 'Disable extension' will
  // appear in a dropdown menu and the id will be used to disable notifications
  // from the extension. Otherwise if |display_source| is provided, a menu item
  // showing the source and allowing notifications from that source to be
  // disabled will be shown. All actual disabling is handled by the Delegate.
  void AddNotification(NotificationType type,
                       const std::string& id,
                       const string16& title,
                       const string16& message,
                       const string16& display_source,
                       const std::string& extension_id,
                       const base::DictionaryValue* optional_fields);

  // Updates an existing notification with id = old_id and set its id to new_id.
  // |optional_fields| can be NULL in case of no updates on those fields.
  void UpdateNotification(const std::string& old_id,
                          const std::string& new_id,
                          const string16& title,
                          const string16& message,
                          const base::DictionaryValue* optional_fields);

  // Removes an existing notification.
  void RemoveNotification(const std::string& id);

  void SetNotificationIcon(const std::string& notification_id,
                           const gfx::ImageSkia& image);

  void SetNotificationImage(const std::string& notification_id,
                            const gfx::ImageSkia& image);

  void SetNotificationButtonIcon(const std::string& notification_id,
                                 int button_index,
                                 const gfx::ImageSkia& image);

  NotificationList* notification_list() { return notification_list_.get(); }
  bool quiet_mode() const { return notification_list_->quiet_mode(); }

  // Overridden from NotificationList::Delegate.
  virtual void SendRemoveNotification(const std::string& id) OVERRIDE;
  virtual void SendRemoveAllNotifications() OVERRIDE;
  virtual void DisableNotificationByExtension(const std::string& id) OVERRIDE;
  virtual void DisableNotificationByUrl(const std::string& id) OVERRIDE;
  virtual void ShowNotificationSettings(const std::string& id) OVERRIDE;
  virtual void ShowNotificationSettingsDialog(gfx::NativeView context) OVERRIDE;
  virtual void OnNotificationClicked(const std::string& id) OVERRIDE;
  virtual void OnQuietModeChanged(bool quiet_mode) OVERRIDE;
  virtual void OnButtonClicked(const std::string& id, int button_index)
      OVERRIDE;
  virtual NotificationList* GetNotificationList() OVERRIDE;

 private:
  // Calls OnMessageCenterChanged on each observer.
  void NotifyMessageCenterChanged(bool new_notification);

  scoped_ptr<NotificationList> notification_list_;
  ObserverList<Observer> observer_list_;
  Delegate* delegate_;

  DISALLOW_COPY_AND_ASSIGN(MessageCenter);
};

}  // namespace message_center

#endif  // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_