balloon_collection_impl.h revision 201ade2fbba22bfb27ae029f4d23fca6ded109a0
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/browser/notifications/balloon_collection_base.h" 16#include "chrome/common/notification_registrar.h" 17#include "gfx/point.h" 18#include "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 DisplayChanged() {} 70 virtual void OnBalloonClosed(Balloon* source); 71 virtual const Balloons& GetActiveBalloons() { return base_.balloons(); } 72 73 // NotificationObserver overrides: 74 virtual void Observe(NotificationType type, 75 const NotificationSource& source, 76 const NotificationDetails& details); 77 78 // Adds a callback for DOMUI message. Returns true if the callback 79 // is succssfully registered, or false otherwise. It fails to add if 80 // there is no notification that matches NotificationDelegate::id(), 81 // or a callback for given message already exists. The callback 82 // object is owned and deleted by callee. 83 bool AddDOMUIMessageCallback(const Notification& notification, 84 const std::string& message, 85 MessageCallback* callback); 86 87 // Adds new system notification. 88 // |sticky| is used to indicate that the notification 89 // is sticky and cannot be dismissed by a user. |controls| turns on/off 90 // info label and option/dismiss buttons. 91 void AddSystemNotification(const Notification& notification, 92 Profile* profile, bool sticky, bool controls); 93 94 // Updates the notification's content. It uses 95 // NotificationDelegate::id() to check the equality of notifications. 96 // Returns true if the notification has been updated. False if 97 // no corresponding notification is found. This will not change the 98 // visibility of the notification. 99 bool UpdateNotification(const Notification& notification); 100 101 // Updates and shows the notification. It will open the notification panel 102 // if it's closed or minimized, and scroll the viewport so that 103 // the updated notification is visible. 104 bool UpdateAndShowNotification(const Notification& notification); 105 106 // Injects notification ui. Used to inject a mock implementation in tests. 107 void set_notification_ui(NotificationUI* ui) { 108 notification_ui_.reset(ui); 109 } 110 111 NotificationUI* notification_ui() { 112 return notification_ui_.get(); 113 } 114 115 protected: 116 // Creates a new balloon. Overridable by unit tests. The caller is 117 // responsible for freeing the pointer returned. 118 virtual Balloon* MakeBalloon(const Notification& notification, 119 Profile* profile); 120 121 // Base implementation for the collection of active balloons. 122 BalloonCollectionBase base_; 123 124 private: 125 friend class NotificationPanelTester; 126 127 // Shutdown the notification ui. 128 void Shutdown(); 129 130 Balloon* FindBalloon(const Notification& notification) { 131 return base_.FindBalloon(notification); 132 } 133 134 scoped_ptr<NotificationUI> notification_ui_; 135 136 NotificationRegistrar registrar_; 137 138 DISALLOW_COPY_AND_ASSIGN(BalloonCollectionImpl); 139}; 140 141} // namespace chromeos 142 143#endif // CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_ 144