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