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"
10 #include "googleurl/src/gurl.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 bool BalloonCollectionBase::DoesIdExist(const std::string
& id
) {
39 Balloons::iterator iter
;
40 for (iter
= balloons_
.begin(); iter
!= balloons_
.end(); ++iter
) {
41 if ((*iter
)->notification().notification_id() == id
)
47 bool BalloonCollectionBase::CloseById(const std::string
& id
) {
48 // Use a local list of balloons to close to avoid breaking
49 // iterator changes on each close.
51 Balloons::iterator iter
;
52 for (iter
= balloons_
.begin(); iter
!= balloons_
.end(); ++iter
) {
53 if ((*iter
)->notification().notification_id() == id
)
54 to_close
.push_back(*iter
);
56 for (iter
= to_close
.begin(); iter
!= to_close
.end(); ++iter
)
57 (*iter
)->CloseByScript();
59 return !to_close
.empty();
62 bool BalloonCollectionBase::CloseAllBySourceOrigin(
63 const GURL
& source_origin
) {
64 // Use a local list of balloons to close to avoid breaking
65 // iterator changes on each close.
67 Balloons::iterator iter
;
68 for (iter
= balloons_
.begin(); iter
!= balloons_
.end(); ++iter
) {
69 if ((*iter
)->notification().origin_url() == source_origin
)
70 to_close
.push_back(*iter
);
72 for (iter
= to_close
.begin(); iter
!= to_close
.end(); ++iter
)
73 (*iter
)->CloseByScript();
75 return !to_close
.empty();
78 bool BalloonCollectionBase::CloseAllByProfile(Profile
* profile
) {
79 // Use a local list of balloons to close to avoid breaking
80 // iterator changes on each close.
82 Balloons::iterator iter
;
83 for (iter
= balloons_
.begin(); iter
!= balloons_
.end(); ++iter
) {
84 if ((*iter
)->profile() == profile
)
85 to_close
.push_back(*iter
);
87 for (iter
= to_close
.begin(); iter
!= to_close
.end(); ++iter
)
88 (*iter
)->CloseByScript();
90 return !to_close
.empty();
93 void BalloonCollectionBase::CloseAll() {
94 // Use a local list of balloons to close to avoid breaking
95 // iterator changes on each close.
96 Balloons to_close
= balloons_
;
97 for (Balloons::iterator iter
= to_close
.begin();
98 iter
!= to_close
.end(); ++iter
)
99 (*iter
)->CloseByScript();
102 Balloon
* BalloonCollectionBase::FindBalloonById(
103 const std::string
& notification_id
) {
104 Balloons::iterator iter
;
105 for (iter
= balloons_
.begin(); iter
!= balloons_
.end(); ++iter
) {
106 if ((*iter
)->notification().notification_id() == notification_id
) {
113 Balloon
* BalloonCollectionBase::FindBalloon(const Notification
& notification
) {
114 return FindBalloonById(notification
.notification_id());