summaryrefslogtreecommitdiffstats
path: root/chrome/browser/notifications/balloon_collection_base.cc
blob: 4e9ab50fdb74b93bd302436a71e73e4fed1907ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/notifications/balloon_collection_base.h"

#include "base/stl_util.h"
#include "chrome/browser/notifications/balloon.h"
#include "chrome/browser/notifications/notification.h"
#include "googleurl/src/gurl.h"

BalloonCollectionBase::BalloonCollectionBase() {
}

BalloonCollectionBase::~BalloonCollectionBase() {
  STLDeleteElements(&balloons_);
}

void BalloonCollectionBase::Add(Balloon* balloon) {
  balloons_.push_back(balloon);
}

void BalloonCollectionBase::Remove(Balloon* balloon) {
  // Free after removing.
  scoped_ptr<Balloon> to_delete(balloon);
  Balloons::iterator iter;
  for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) {
    if ((*iter) == balloon) {
      balloons_.erase(iter);
      return;
    }
  }
}

bool BalloonCollectionBase::CloseById(const std::string& id) {
  // Use a local list of balloons to close to avoid breaking
  // iterator changes on each close.
  Balloons to_close;
  Balloons::iterator iter;
  for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) {
    if ((*iter)->notification().notification_id() == id)
      to_close.push_back(*iter);
  }
  for (iter = to_close.begin(); iter != to_close.end(); ++iter)
    (*iter)->CloseByScript();

  return !to_close.empty();
}

bool BalloonCollectionBase::CloseAllBySourceOrigin(
    const GURL& source_origin) {
  // Use a local list of balloons to close to avoid breaking
  // iterator changes on each close.
  Balloons to_close;
  Balloons::iterator iter;
  for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) {
    if ((*iter)->notification().origin_url() == source_origin)
      to_close.push_back(*iter);
  }
  for (iter = to_close.begin(); iter != to_close.end(); ++iter)
    (*iter)->CloseByScript();

  return !to_close.empty();
}

void BalloonCollectionBase::CloseAll() {
  // Use a local list of balloons to close to avoid breaking
  // iterator changes on each close.
  Balloons to_close = balloons_;
  for (Balloons::iterator iter = to_close.begin();
       iter != to_close.end(); ++iter)
    (*iter)->CloseByScript();
}

Balloon* BalloonCollectionBase::FindBalloon(
    const Notification& notification) {
  Balloons::iterator iter;
  for (iter = balloons_.begin(); iter != balloons_.end(); ++iter) {
    if ((*iter)->notification().notification_id() ==
        notification.notification_id()) {
      return *iter;
    }
  }
  return NULL;
}