~ubuntu-branches/ubuntu/utopic/rlvm/utopic-proposed

« back to all changes in this revision

Viewing changes to src/base/notification_registrar.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ying-Chun Liu (PaulLiu), Ying-Chun Liu (PaulLiu), Elliot Glaysher
  • Date: 2011-05-19 00:28:44 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110519002844-qszwmj7oiixww0eg
Tags: 0.12-1
[ Ying-Chun Liu (PaulLiu) <paulliu@debian.org> ]
* New upstream release

[ Elliot Glaysher <glaysher@umich.edu> ]
* New GTK+ interface with desktop integration and UI refinements
* Partial Japanese localizations
* Fix graphics corruption in in-game dialogs when a dialog is brought
  up, and then fullscreen mode activated
* Smooth the output of text in rlBabel using games
* Don't play voice samples while fast forwarding

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "base/notification_registrar.h"
 
6
 
 
7
#include <algorithm>
 
8
 
 
9
#include "base/notification_service.h"
 
10
 
 
11
struct NotificationRegistrar::Record {
 
12
  bool operator==(const Record& other) const;
 
13
 
 
14
  NotificationObserver* observer;
 
15
  NotificationType type;
 
16
  NotificationSource source;
 
17
};
 
18
 
 
19
bool NotificationRegistrar::Record::operator==(const Record& other) const {
 
20
  return observer == other.observer &&
 
21
         type == other.type &&
 
22
         source == other.source;
 
23
}
 
24
 
 
25
NotificationRegistrar::NotificationRegistrar() {
 
26
}
 
27
 
 
28
NotificationRegistrar::~NotificationRegistrar() {
 
29
  RemoveAll();
 
30
}
 
31
 
 
32
void NotificationRegistrar::Add(NotificationObserver* observer,
 
33
                                NotificationType type,
 
34
                                const NotificationSource& source) {
 
35
  Record record = { observer, type, source };
 
36
  registered_.push_back(record);
 
37
 
 
38
  NotificationService::current()->AddObserver(observer, type, source);
 
39
}
 
40
 
 
41
void NotificationRegistrar::Remove(NotificationObserver* observer,
 
42
                                   NotificationType type,
 
43
                                   const NotificationSource& source) {
 
44
  if (!IsRegistered(observer, type, source)) {
 
45
    return;
 
46
  }
 
47
 
 
48
  Record record = { observer, type, source };
 
49
  RecordVector::iterator found = std::find(
 
50
      registered_.begin(), registered_.end(), record);
 
51
  registered_.erase(found);
 
52
 
 
53
  // This can be NULL if our owner outlives the NotificationService, e.g. if our
 
54
  // owner is a Singleton.
 
55
  NotificationService* service = NotificationService::current();
 
56
  if (service)
 
57
    service->RemoveObserver(observer, type, source);
 
58
}
 
59
 
 
60
void NotificationRegistrar::RemoveAll() {
 
61
  // Early-exit if no registrations, to avoid calling
 
62
  // NotificationService::current.  If we've constructed an object with a
 
63
  // NotificationRegistrar member, but haven't actually used the notification
 
64
  // service, and we reach prgram exit, then calling current() below could try
 
65
  // to initialize the service's lazy TLS pointer during exit, which throws
 
66
  // wrenches at things.
 
67
  if (registered_.empty())
 
68
    return;
 
69
 
 
70
  // This can be NULL if our owner outlives the NotificationService, e.g. if our
 
71
  // owner is a Singleton.
 
72
  NotificationService* service = NotificationService::current();
 
73
  if (service) {
 
74
    for (size_t i = 0; i < registered_.size(); i++) {
 
75
      service->RemoveObserver(registered_[i].observer,
 
76
                              registered_[i].type,
 
77
                              registered_[i].source);
 
78
    }
 
79
  }
 
80
  registered_.clear();
 
81
}
 
82
 
 
83
bool NotificationRegistrar::IsEmpty() const {
 
84
  return registered_.empty();
 
85
}
 
86
 
 
87
bool NotificationRegistrar::IsRegistered(NotificationObserver* observer,
 
88
                                         NotificationType type,
 
89
                                         const NotificationSource& source) {
 
90
  Record record = { observer, type, source };
 
91
  return std::find(registered_.begin(), registered_.end(), record) !=
 
92
      registered_.end();
 
93
}