1 // Copyright (c) 2012 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 #include "chrome/browser/notifications/balloon_collection_base.h" 6 7 #include "base/stl_util.h" 8 #include "chrome/browser/notifications/balloon.h" 9 #include "chrome/browser/notifications/notification.h" 10 #include "url/gurl.h" 11 12 BalloonCollectionBase::BalloonCollectionBase() { 13 } 14 15 BalloonCollectionBase::~BalloonCollectionBase() { 16 STLDeleteElements(&balloons_); 17 } 18 19 void BalloonCollectionBase::Add(Balloon* balloon, bool add_to_front) { 20 if (add_to_front) 21 balloons_.push_front(balloon); 22 else 23 balloons_.push_back(balloon); 24 } 25 26 void BalloonCollectionBase::Remove(Balloon* balloon) { 27 // Free after removing. 28 scoped_ptr<Balloon> to_delete(balloon); 29 Balloons::iterator iter; 30 for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) { 31 if ((*iter) == balloon) { 32 balloons_.erase(iter); 33 return; 34 } 35 } 36 } 37 38 const Notification* BalloonCollectionBase::FindById( 39 const std::string& id) const { 40 Balloons::const_iterator iter; 41 for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) { 42 if ((*iter)->notification().notification_id() == id) { 43 return &((*iter)->notification()); 44 } 45 } 46 return NULL; 47 } 48 49 bool BalloonCollectionBase::CloseById(const std::string& id) { 50 // Use a local list of balloons to close to avoid breaking 51 // iterator changes on each close. 52 Balloons to_close; 53 Balloons::iterator iter; 54 for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) { 55 if ((*iter)->notification().notification_id() == id) 56 to_close.push_back(*iter); 57 } 58 for (iter = to_close.begin(); iter != to_close.end(); ++iter) 59 (*iter)->CloseByScript(); 60 61 return !to_close.empty(); 62 } 63 64 bool BalloonCollectionBase::CloseAllBySourceOrigin( 65 const GURL& source_origin) { 66 // Use a local list of balloons to close to avoid breaking 67 // iterator changes on each close. 68 Balloons to_close; 69 Balloons::iterator iter; 70 for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) { 71 if ((*iter)->notification().origin_url() == source_origin) 72 to_close.push_back(*iter); 73 } 74 for (iter = to_close.begin(); iter != to_close.end(); ++iter) 75 (*iter)->CloseByScript(); 76 77 return !to_close.empty(); 78 } 79 80 bool BalloonCollectionBase::CloseAllByProfile(Profile* profile) { 81 // Use a local list of balloons to close to avoid breaking 82 // iterator changes on each close. 83 Balloons to_close; 84 Balloons::iterator iter; 85 for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) { 86 if ((*iter)->profile() == profile) 87 to_close.push_back(*iter); 88 } 89 for (iter = to_close.begin(); iter != to_close.end(); ++iter) 90 (*iter)->CloseByScript(); 91 92 return !to_close.empty(); 93 } 94 95 void BalloonCollectionBase::CloseAll() { 96 // Use a local list of balloons to close to avoid breaking 97 // iterator changes on each close. 98 Balloons to_close = balloons_; 99 for (Balloons::iterator iter = to_close.begin(); 100 iter != to_close.end(); ++iter) 101 (*iter)->CloseByScript(); 102 } 103 104 Balloon* BalloonCollectionBase::FindBalloonById( 105 const std::string& notification_id) { 106 Balloons::iterator iter; 107 for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) { 108 if ((*iter)->notification().notification_id() == notification_id) { 109 return *iter; 110 } 111 } 112 return NULL; 113 } 114 115 Balloon* BalloonCollectionBase::FindBalloon(const Notification& notification) { 116 return FindBalloonById(notification.notification_id()); 117 } 118