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#include "chrome/browser/notifications/balloon_collection_base.h" 6 7#include "base/stl_util-inl.h" 8#include "chrome/browser/notifications/balloon.h" 9#include "chrome/browser/notifications/notification.h" 10#include "googleurl/src/gurl.h" 11 12BalloonCollectionBase::BalloonCollectionBase() { 13} 14 15BalloonCollectionBase::~BalloonCollectionBase() { 16 STLDeleteElements(&balloons_); 17} 18 19void BalloonCollectionBase::Add(Balloon* balloon) { 20 balloons_.push_back(balloon); 21} 22 23void BalloonCollectionBase::Remove(Balloon* balloon) { 24 // Free after removing. 25 scoped_ptr<Balloon> to_delete(balloon); 26 Balloons::iterator iter; 27 for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) { 28 if ((*iter) == balloon) { 29 balloons_.erase(iter); 30 return; 31 } 32 } 33} 34 35bool BalloonCollectionBase::CloseById(const std::string& id) { 36 // Use a local list of balloons to close to avoid breaking 37 // iterator changes on each close. 38 Balloons to_close; 39 Balloons::iterator iter; 40 for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) { 41 if ((*iter)->notification().notification_id() == id) 42 to_close.push_back(*iter); 43 } 44 for (iter = to_close.begin(); iter != to_close.end(); ++iter) 45 (*iter)->CloseByScript(); 46 47 return !to_close.empty(); 48} 49 50bool BalloonCollectionBase::CloseAllBySourceOrigin( 51 const GURL& source_origin) { 52 // Use a local list of balloons to close to avoid breaking 53 // iterator changes on each close. 54 Balloons to_close; 55 Balloons::iterator iter; 56 for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) { 57 if ((*iter)->notification().origin_url() == source_origin) 58 to_close.push_back(*iter); 59 } 60 for (iter = to_close.begin(); iter != to_close.end(); ++iter) 61 (*iter)->CloseByScript(); 62 63 return !to_close.empty(); 64} 65 66void BalloonCollectionBase::CloseAll() { 67 // Use a local list of balloons to close to avoid breaking 68 // iterator changes on each close. 69 Balloons to_close = balloons_; 70 for (Balloons::iterator iter = to_close.begin(); 71 iter != to_close.end(); ++iter) 72 (*iter)->CloseByScript(); 73} 74 75Balloon* BalloonCollectionBase::FindBalloon( 76 const Notification& notification) { 77 Balloons::iterator iter; 78 for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) { 79 if ((*iter)->notification().notification_id() == 80 notification.notification_id()) { 81 return *iter; 82 } 83 } 84 return NULL; 85} 86