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