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.
5 #include "chrome/browser/notifications/balloon_collection_base.h"
7 #include "base/stl_util.h"
8 #include "chrome/browser/notifications/balloon.h"
9 #include "chrome/browser/notifications/notification.h"
12 BalloonCollectionBase::BalloonCollectionBase() {
15 BalloonCollectionBase::~BalloonCollectionBase() {
16 STLDeleteElements(&balloons_
);
19 void BalloonCollectionBase::Add(Balloon
* balloon
, bool add_to_front
) {
21 balloons_
.push_front(balloon
);
23 balloons_
.push_back(balloon
);
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
);
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());
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.
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
);
58 for (iter
= to_close
.begin(); iter
!= to_close
.end(); ++iter
)
59 (*iter
)->CloseByScript();
61 return !to_close
.empty();
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.
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
);
74 for (iter
= to_close
.begin(); iter
!= to_close
.end(); ++iter
)
75 (*iter
)->CloseByScript();
77 return !to_close
.empty();
80 bool BalloonCollectionBase::CloseAllByProfile(Profile
* profile
) {
81 // Use a local list of balloons to close to avoid breaking
82 // iterator changes on each 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
);
89 for (iter
= to_close
.begin(); iter
!= to_close
.end(); ++iter
)
90 (*iter
)->CloseByScript();
92 return !to_close
.empty();
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();
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
) {
115 Balloon
* BalloonCollectionBase::FindBalloon(const Notification
& notification
) {
116 return FindBalloonById(notification
.notification_id());