Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/notifications/balloon_collection_impl.h" | 5 #include "chrome/browser/chromeos/notifications/balloon_collection_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util-inl.h" | |
| 11 #include "chrome/browser/browser_list.h" | 10 #include "chrome/browser/browser_list.h" |
| 12 #include "chrome/browser/chromeos/notifications/balloon_view.h" | 11 #include "chrome/browser/chromeos/notifications/balloon_view.h" |
| 13 #include "chrome/browser/chromeos/notifications/notification_panel.h" | 12 #include "chrome/browser/chromeos/notifications/notification_panel.h" |
| 14 #include "chrome/browser/notifications/balloon.h" | 13 #include "chrome/browser/notifications/balloon.h" |
| 14 #include "chrome/browser/notifications/balloon_collection_util.h" | |
| 15 #include "chrome/browser/notifications/notification.h" | 15 #include "chrome/browser/notifications/notification.h" |
| 16 #include "chrome/browser/window_sizer.h" | 16 #include "chrome/browser/window_sizer.h" |
| 17 #include "chrome/common/notification_service.h" | 17 #include "chrome/common/notification_service.h" |
| 18 #include "gfx/rect.h" | 18 #include "gfx/rect.h" |
| 19 #include "gfx/size.h" | 19 #include "gfx/size.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Margin from the edge of the work area | 23 // Margin from the edge of the work area |
| 24 const int kVerticalEdgeMargin = 5; | 24 const int kVerticalEdgeMargin = 5; |
| 25 const int kHorizontalEdgeMargin = 5; | 25 const int kHorizontalEdgeMargin = 5; |
| 26 | 26 |
| 27 class NotificationMatcher { | |
| 28 public: | |
| 29 explicit NotificationMatcher(const Notification& notification) | |
| 30 : notification_(notification) {} | |
| 31 bool operator()(const Balloon* b) const { | |
| 32 return notification_.IsSame(b->notification()); | |
| 33 } | |
| 34 private: | |
| 35 Notification notification_; | |
| 36 }; | |
| 37 | |
| 38 } // namespace | 27 } // namespace |
| 39 | 28 |
| 40 namespace chromeos { | 29 namespace chromeos { |
| 41 | 30 |
| 42 BalloonCollectionImpl::BalloonCollectionImpl() | 31 BalloonCollectionImpl::BalloonCollectionImpl() |
| 43 : notification_ui_(new NotificationPanel()) { | 32 : notification_ui_(new NotificationPanel()) { |
| 44 registrar_.Add(this, NotificationType::BROWSER_CLOSED, | 33 registrar_.Add(this, NotificationType::BROWSER_CLOSED, |
| 45 NotificationService::AllSources()); | 34 NotificationService::AllSources()); |
| 46 } | 35 } |
| 47 | 36 |
| 48 BalloonCollectionImpl::~BalloonCollectionImpl() { | 37 BalloonCollectionImpl::~BalloonCollectionImpl() { |
| 49 Shutdown(); | 38 Shutdown(); |
| 50 } | 39 } |
| 51 | 40 |
| 52 void BalloonCollectionImpl::Add(const Notification& notification, | 41 void BalloonCollectionImpl::Add(const Notification& notification, |
| 53 Profile* profile) { | 42 Profile* profile) { |
| 54 Balloon* new_balloon = MakeBalloon(notification, profile); | 43 Balloon* new_balloon = MakeBalloon(notification, profile); |
| 55 balloons_.push_back(new_balloon); | 44 base_.Add(new_balloon) |
| 56 new_balloon->Show(); | 45 new_balloon->Show(); |
| 57 notification_ui_->Add(new_balloon); | 46 notification_ui_->Add(new_balloon); |
| 58 | 47 |
| 59 // There may be no listener in a unit test. | 48 // There may be no listener in a unit test. |
| 60 if (space_change_listener_) | 49 if (space_change_listener_) |
| 61 space_change_listener_->OnBalloonSpaceChanged(); | 50 space_change_listener_->OnBalloonSpaceChanged(); |
| 62 } | 51 } |
| 63 | 52 |
| 64 bool BalloonCollectionImpl::AddDOMUIMessageCallback( | 53 bool BalloonCollectionImpl::AddDOMUIMessageCallback( |
| 65 const Notification& notification, | 54 const Notification& notification, |
| 66 const std::string& message, | 55 const std::string& message, |
| 67 MessageCallback* callback) { | 56 MessageCallback* callback) { |
| 68 Balloons::iterator iter = FindBalloon(notification); | 57 Balloon* balloon = base_.FindBalloon(notification); |
| 69 if (iter == balloons_.end()) { | 58 if (!balloon) { |
| 70 delete callback; | 59 delete callback; |
| 71 return false; | 60 return false; |
| 72 } | 61 } |
| 73 BalloonViewHost* host = | 62 BalloonViewHost* host = |
| 74 static_cast<BalloonViewHost*>((*iter)->view()->GetHost()); | 63 static_cast<BalloonViewHost*>(balloon->view()->GetHost()); |
| 75 return host->AddDOMUIMessageCallback(message, callback); | 64 return host->AddDOMUIMessageCallback(message, callback); |
| 76 } | 65 } |
| 77 | 66 |
| 78 void BalloonCollectionImpl::AddSystemNotification( | 67 void BalloonCollectionImpl::AddSystemNotification( |
| 79 const Notification& notification, | 68 const Notification& notification, |
| 80 Profile* profile, | 69 Profile* profile, |
| 81 bool sticky, | 70 bool sticky, |
| 82 bool control) { | 71 bool control) { |
| 72 | |
| 83 Balloon* new_balloon = new Balloon(notification, profile, this); | 73 Balloon* new_balloon = new Balloon(notification, profile, this); |
| 84 new_balloon->set_view( | 74 new_balloon->set_view( |
| 85 new chromeos::BalloonViewImpl(sticky, control, true)); | 75 new chromeos::BalloonViewImpl(sticky, control, true)); |
| 86 balloons_.push_back(new_balloon); | 76 base_.Add(new_balloon); |
| 87 new_balloon->Show(); | 77 new_balloon->Show(); |
| 88 notification_ui_->Add(new_balloon); | 78 notification_ui_->Add(new_balloon); |
| 89 | 79 |
| 90 // There may be no listener in a unit test. | 80 // There may be no listener in a unit test. |
| 91 if (space_change_listener_) | 81 if (space_change_listener_) |
| 92 space_change_listener_->OnBalloonSpaceChanged(); | 82 space_change_listener_->OnBalloonSpaceChanged(); |
| 93 } | 83 } |
| 94 | 84 |
| 95 bool BalloonCollectionImpl::UpdateNotification( | 85 bool BalloonCollectionImpl::UpdateNotification( |
| 96 const Notification& notification) { | 86 const Notification& notification) { |
| 97 Balloons::iterator iter = FindBalloon(notification); | 87 Balloon* balloon = base_.FindBalloon(notification); |
| 98 if (iter == balloons_.end()) | 88 if (!balloon) |
| 99 return false; | 89 return false; |
| 100 Balloon* balloon = *iter; | |
| 101 balloon->Update(notification); | 90 balloon->Update(notification); |
| 102 notification_ui_->Update(balloon); | 91 notification_ui_->Update(balloon); |
| 103 return true; | 92 return true; |
| 104 } | 93 } |
| 105 | 94 |
| 106 bool BalloonCollectionImpl::UpdateAndShowNotification( | 95 bool BalloonCollectionImpl::UpdateAndShowNotification( |
| 107 const Notification& notification) { | 96 const Notification& notification) { |
| 108 Balloons::iterator iter = FindBalloon(notification); | 97 Balloon* balloon = base_.FindBalloon(notification); |
| 109 if (iter == balloons_.end()) | 98 if (!balloon) |
| 110 return false; | 99 return false; |
| 111 Balloon* balloon = *iter; | 100 Balloon* balloon = *iter; |
| 112 balloon->Update(notification); | 101 balloon->Update(notification); |
| 113 bool updated = notification_ui_->Update(balloon); | 102 bool updated = notification_ui_->Update(balloon); |
| 114 DCHECK(updated); | 103 DCHECK(updated); |
| 115 notification_ui_->Show(balloon); | 104 notification_ui_->Show(balloon); |
| 116 return true; | 105 return true; |
| 117 } | 106 } |
| 118 | 107 |
| 119 bool BalloonCollectionImpl::Remove(const Notification& notification) { | 108 bool BalloonCollectionImpl::RemoveById(const std::string& id) { |
| 120 Balloons::iterator iter = FindBalloon(notification); | 109 return base_.RemoveById(id); |
| 121 if (iter != balloons_.end()) { | 110 } |
| 122 // Balloon.CloseByScript() will cause OnBalloonClosed() to be called on | 111 |
| 123 // this object, which will remove it from the collection and free it. | 112 bool BalloonCollectionImpl::RemoveBySourceOrigin(const GURL& origin) { |
| 124 (*iter)->CloseByScript(); | 113 return base_.RemoveAllBySourceOrigin(origin); |
| 125 return true; | |
| 126 } | |
| 127 return false; | |
| 128 } | 114 } |
| 129 | 115 |
| 130 bool BalloonCollectionImpl::HasSpace() const { | 116 bool BalloonCollectionImpl::HasSpace() const { |
| 131 return true; | 117 return true; |
| 132 } | 118 } |
| 133 | 119 |
| 134 void BalloonCollectionImpl::ResizeBalloon(Balloon* balloon, | 120 void BalloonCollectionImpl::ResizeBalloon(Balloon* balloon, |
| 135 const gfx::Size& size) { | 121 const gfx::Size& size) { |
| 136 notification_ui_->ResizeNotification(balloon, size); | 122 notification_ui_->ResizeNotification(balloon, size); |
| 137 } | 123 } |
| 138 | 124 |
| 139 void BalloonCollectionImpl::OnBalloonClosed(Balloon* source) { | 125 void BalloonCollectionImpl::OnBalloonClosed(Balloon* source) { |
| 140 // We want to free the balloon when finished. | 126 base_.Remove(source); |
|
Andrew T Wilson (Slow)
2010/11/11 01:07:44
Previously this routine was freeing |source| - who
John Gregg
2010/11/11 18:13:51
It is freed by base_.Remove(), so I reordered them
| |
| 141 scoped_ptr<Balloon> closed(source); | |
| 142 | |
| 143 notification_ui_->Remove(source); | 127 notification_ui_->Remove(source); |
| 144 | 128 |
| 145 Balloons::iterator iter = FindBalloon(source->notification()); | |
| 146 if (iter != balloons_.end()) { | |
| 147 balloons_.erase(iter); | |
| 148 } | |
| 149 // There may be no listener in a unit test. | 129 // There may be no listener in a unit test. |
| 150 if (space_change_listener_) | 130 if (space_change_listener_) |
| 151 space_change_listener_->OnBalloonSpaceChanged(); | 131 space_change_listener_->OnBalloonSpaceChanged(); |
| 152 } | 132 } |
| 153 | 133 |
| 154 void BalloonCollectionImpl::Observe(NotificationType type, | 134 void BalloonCollectionImpl::Observe(NotificationType type, |
| 155 const NotificationSource& source, | 135 const NotificationSource& source, |
| 156 const NotificationDetails& details) { | 136 const NotificationDetails& details) { |
| 157 DCHECK(type == NotificationType::BROWSER_CLOSED); | 137 DCHECK(type == NotificationType::BROWSER_CLOSED); |
| 158 bool app_closing = *Details<bool>(details).ptr(); | 138 bool app_closing = *Details<bool>(details).ptr(); |
| 159 // When exitting, we need to shutdown all renderers in | 139 // When exitting, we need to shutdown all renderers in |
| 160 // BalloonViewImpl before IO thread gets deleted in the | 140 // BalloonViewImpl before IO thread gets deleted in the |
| 161 // BrowserProcessImpl's destructor. See http://crbug.com/40810 | 141 // BrowserProcessImpl's destructor. See http://crbug.com/40810 |
| 162 // for details. | 142 // for details. |
| 163 if (app_closing) | 143 if (app_closing) |
| 164 Shutdown(); | 144 Shutdown(); |
| 165 } | 145 } |
| 166 | 146 |
| 167 void BalloonCollectionImpl::Shutdown() { | 147 void BalloonCollectionImpl::Shutdown() { |
| 168 // We need to remove the panel first because deleting | 148 // We need to remove the panel first because deleting |
| 169 // views that are not owned by parent will not remove | 149 // views that are not owned by parent will not remove |
| 170 // themselves from the parent. | 150 // themselves from the parent. |
| 171 DVLOG(1) << "Shutting down notification UI"; | 151 DVLOG(1) << "Shutting down notification UI"; |
| 172 notification_ui_.reset(); | 152 notification_ui_.reset(); |
| 173 STLDeleteElements(&balloons_); | |
| 174 } | 153 } |
| 175 | 154 |
| 176 Balloon* BalloonCollectionImpl::MakeBalloon(const Notification& notification, | 155 Balloon* BalloonCollectionImpl::MakeBalloon(const Notification& notification, |
| 177 Profile* profile) { | 156 Profile* profile) { |
| 178 Balloon* new_balloon = new Balloon(notification, profile, this); | 157 Balloon* new_balloon = new Balloon(notification, profile, this); |
| 179 new_balloon->set_view(new chromeos::BalloonViewImpl(false, true, false)); | 158 new_balloon->set_view(new chromeos::BalloonViewImpl(false, true, false)); |
| 180 return new_balloon; | 159 return new_balloon; |
| 181 } | 160 } |
| 182 | 161 |
| 183 std::deque<Balloon*>::iterator BalloonCollectionImpl::FindBalloon( | |
| 184 const Notification& notification) { | |
| 185 return std::find_if(balloons_.begin(), | |
| 186 balloons_.end(), | |
| 187 NotificationMatcher(notification)); | |
| 188 } | |
| 189 | |
| 190 } // namespace chromeos | 162 } // namespace chromeos |
| 191 | 163 |
| 192 // static | 164 // static |
| 193 BalloonCollection* BalloonCollection::Create() { | 165 BalloonCollection* BalloonCollection::Create() { |
| 194 return new chromeos::BalloonCollectionImpl(); | 166 return new chromeos::BalloonCollectionImpl(); |
| 195 } | 167 } |
| OLD | NEW |