1// Copyright (c) 2011 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#ifndef CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_
6#define CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_
7#pragma once
8
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/memory/scoped_ptr.h"
13#include "chrome/browser/chromeos/notifications/balloon_view_host.h"
14#include "chrome/browser/notifications/balloon_collection.h"
15#include "chrome/browser/notifications/balloon_collection_base.h"
16#include "content/common/notification_registrar.h"
17#include "ui/gfx/point.h"
18#include "ui/gfx/rect.h"
19
20namespace gfx {
21class Size;
22}  // namespace gfx
23
24namespace chromeos {
25
26class BalloonViewImpl;
27
28// A balloon collection represents a set of notification balloons being
29// shown in the chromeos notification panel. Unlike other platforms,
30// chromeos shows the all notifications in the notification panel, and
31// this class does not manage the location of balloons.
32class BalloonCollectionImpl : public BalloonCollection,
33                              public NotificationObserver {
34 public:
35  // An interface to display balloons on the screen.
36  // This is used for unit tests to inject a mock ui implementation.
37  class NotificationUI {
38   public:
39    NotificationUI() {}
40    virtual ~NotificationUI() {}
41
42    // Add, remove, resize and show the balloon.
43    virtual void Add(Balloon* balloon) = 0;
44    virtual bool Update(Balloon* balloon) = 0;
45    virtual void Remove(Balloon* balloon) = 0;
46    virtual void Show(Balloon* balloon) = 0;
47
48    // Resize notification from webkit.
49    virtual void ResizeNotification(Balloon* balloon,
50                                    const gfx::Size& size) = 0;
51
52    // Sets the active view.
53    virtual void SetActiveView(BalloonViewImpl* view) = 0;
54   private:
55    DISALLOW_COPY_AND_ASSIGN(NotificationUI);
56  };
57
58  BalloonCollectionImpl();
59  virtual ~BalloonCollectionImpl();
60
61  // BalloonCollectionInterface overrides
62  virtual void Add(const Notification& notification,
63                   Profile* profile);
64  virtual bool RemoveById(const std::string& id);
65  virtual bool RemoveBySourceOrigin(const GURL& origin);
66  virtual void RemoveAll();
67  virtual bool HasSpace() const;
68  virtual void ResizeBalloon(Balloon* balloon, const gfx::Size& size);
69  virtual void SetPositionPreference(PositionPreference position) {}
70  virtual void DisplayChanged() {}
71  virtual void OnBalloonClosed(Balloon* source);
72  virtual const Balloons& GetActiveBalloons() { return base_.balloons(); }
73
74  // NotificationObserver overrides:
75  virtual void Observe(NotificationType type,
76                       const NotificationSource& source,
77                       const NotificationDetails& details);
78
79  // Adds a callback for WebUI message. Returns true if the callback
80  // is succssfully registered, or false otherwise. It fails to add if
81  // there is no notification that matches NotificationDelegate::id(),
82  // or a callback for given message already exists. The callback
83  // object is owned and deleted by callee.
84  bool AddWebUIMessageCallback(const Notification& notification,
85                               const std::string& message,
86                               MessageCallback* callback);
87
88  // Adds new system notification.
89  // |sticky| is used to indicate that the notification
90  // is sticky and cannot be dismissed by a user. |controls| turns on/off
91  // info label and option/dismiss buttons.
92  void AddSystemNotification(const Notification& notification,
93                             Profile* profile, bool sticky, bool controls);
94
95  // Updates the notification's content. It uses
96  // NotificationDelegate::id() to check the equality of notifications.
97  // Returns true if the notification has been updated. False if
98  // no corresponding notification is found. This will not change the
99  // visibility of the notification.
100  bool UpdateNotification(const Notification& notification);
101
102  // Updates and shows the notification. It will open the notification panel
103  // if it's closed or minimized, and scroll the viewport so that
104  // the updated notification is visible.
105  bool UpdateAndShowNotification(const Notification& notification);
106
107  // Injects notification ui. Used to inject a mock implementation in tests.
108  void set_notification_ui(NotificationUI* ui) {
109    notification_ui_.reset(ui);
110  }
111
112  NotificationUI* notification_ui() {
113    return notification_ui_.get();
114  }
115
116 protected:
117  // Creates a new balloon. Overridable by unit tests.  The caller is
118  // responsible for freeing the pointer returned.
119  virtual Balloon* MakeBalloon(const Notification& notification,
120                               Profile* profile);
121
122  // Base implementation for the collection of active balloons.
123  BalloonCollectionBase base_;
124
125 private:
126  friend class NotificationPanelTester;
127
128  // Shutdown the notification ui.
129  void Shutdown();
130
131  Balloon* FindBalloon(const Notification& notification) {
132    return base_.FindBalloon(notification);
133  }
134
135  scoped_ptr<NotificationUI> notification_ui_;
136
137  NotificationRegistrar registrar_;
138
139  DISALLOW_COPY_AND_ASSIGN(BalloonCollectionImpl);
140};
141
142}  // namespace chromeos
143
144#endif  // CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_
145