ipc/chromium/src/chrome/common/notification_service.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "chrome/common/notification_service.h"
michael@0 6
michael@0 7 #include "base/lazy_instance.h"
michael@0 8 #include "base/thread_local.h"
michael@0 9
michael@0 10 static base::LazyInstance<base::ThreadLocalPointer<NotificationService> >
michael@0 11 lazy_tls_ptr(base::LINKER_INITIALIZED);
michael@0 12
michael@0 13 // static
michael@0 14 NotificationService* NotificationService::current() {
michael@0 15 return lazy_tls_ptr.Pointer()->Get();
michael@0 16 }
michael@0 17
michael@0 18 // static
michael@0 19 bool NotificationService::HasKey(const NotificationSourceMap& map,
michael@0 20 const NotificationSource& source) {
michael@0 21 return map.find(source.map_key()) != map.end();
michael@0 22 }
michael@0 23
michael@0 24 NotificationService::NotificationService() {
michael@0 25 DCHECK(current() == NULL);
michael@0 26 #ifndef NDEBUG
michael@0 27 memset(observer_counts_, 0, sizeof(observer_counts_));
michael@0 28 #endif
michael@0 29
michael@0 30 lazy_tls_ptr.Pointer()->Set(this);
michael@0 31 }
michael@0 32
michael@0 33 void NotificationService::AddObserver(NotificationObserver* observer,
michael@0 34 NotificationType type,
michael@0 35 const NotificationSource& source) {
michael@0 36 DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT);
michael@0 37
michael@0 38 // We have gotten some crashes where the observer pointer is NULL. The problem
michael@0 39 // is that this happens when we actually execute a notification, so have no
michael@0 40 // way of knowing who the bad observer was. We want to know when this happens
michael@0 41 // in release mode so we know what code to blame the crash on (since this is
michael@0 42 // guaranteed to crash later).
michael@0 43 CHECK(observer);
michael@0 44
michael@0 45 NotificationObserverList* observer_list;
michael@0 46 if (HasKey(observers_[type.value], source)) {
michael@0 47 observer_list = observers_[type.value][source.map_key()];
michael@0 48 } else {
michael@0 49 observer_list = new NotificationObserverList;
michael@0 50 observers_[type.value][source.map_key()] = observer_list;
michael@0 51 }
michael@0 52
michael@0 53 observer_list->AddObserver(observer);
michael@0 54 #ifndef NDEBUG
michael@0 55 ++observer_counts_[type.value];
michael@0 56 #endif
michael@0 57 }
michael@0 58
michael@0 59 void NotificationService::RemoveObserver(NotificationObserver* observer,
michael@0 60 NotificationType type,
michael@0 61 const NotificationSource& source) {
michael@0 62 DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT);
michael@0 63 DCHECK(HasKey(observers_[type.value], source));
michael@0 64
michael@0 65 NotificationObserverList* observer_list =
michael@0 66 observers_[type.value][source.map_key()];
michael@0 67 if (observer_list) {
michael@0 68 observer_list->RemoveObserver(observer);
michael@0 69 #ifndef NDEBUG
michael@0 70 --observer_counts_[type.value];
michael@0 71 #endif
michael@0 72 }
michael@0 73
michael@0 74 // TODO(jhughes): Remove observer list from map if empty?
michael@0 75 }
michael@0 76
michael@0 77 void NotificationService::Notify(NotificationType type,
michael@0 78 const NotificationSource& source,
michael@0 79 const NotificationDetails& details) {
michael@0 80 DCHECK(type.value > NotificationType::ALL) <<
michael@0 81 "Allowed for observing, but not posting.";
michael@0 82 DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT);
michael@0 83
michael@0 84 // There's no particular reason for the order in which the different
michael@0 85 // classes of observers get notified here.
michael@0 86
michael@0 87 // Notify observers of all types and all sources
michael@0 88 if (HasKey(observers_[NotificationType::ALL], AllSources()) &&
michael@0 89 source != AllSources()) {
michael@0 90 FOR_EACH_OBSERVER(NotificationObserver,
michael@0 91 *observers_[NotificationType::ALL][AllSources().map_key()],
michael@0 92 Observe(type, source, details));
michael@0 93 }
michael@0 94
michael@0 95 // Notify observers of all types and the given source
michael@0 96 if (HasKey(observers_[NotificationType::ALL], source)) {
michael@0 97 FOR_EACH_OBSERVER(NotificationObserver,
michael@0 98 *observers_[NotificationType::ALL][source.map_key()],
michael@0 99 Observe(type, source, details));
michael@0 100 }
michael@0 101
michael@0 102 // Notify observers of the given type and all sources
michael@0 103 if (HasKey(observers_[type.value], AllSources()) &&
michael@0 104 source != AllSources()) {
michael@0 105 FOR_EACH_OBSERVER(NotificationObserver,
michael@0 106 *observers_[type.value][AllSources().map_key()],
michael@0 107 Observe(type, source, details));
michael@0 108 }
michael@0 109
michael@0 110 // Notify observers of the given type and the given source
michael@0 111 if (HasKey(observers_[type.value], source)) {
michael@0 112 FOR_EACH_OBSERVER(NotificationObserver,
michael@0 113 *observers_[type.value][source.map_key()],
michael@0 114 Observe(type, source, details));
michael@0 115 }
michael@0 116 }
michael@0 117
michael@0 118
michael@0 119 NotificationService::~NotificationService() {
michael@0 120 lazy_tls_ptr.Pointer()->Set(NULL);
michael@0 121
michael@0 122 #ifndef NDEBUG
michael@0 123 for (int i = 0; i < NotificationType::NOTIFICATION_TYPE_COUNT; i++) {
michael@0 124 if (observer_counts_[i] > 0) {
michael@0 125 // This may not be completely fixable -- see
michael@0 126 // http://code.google.com/p/chromium/issues/detail?id=11010 .
michael@0 127 // But any new leaks should be fixed.
michael@0 128 CHROMIUM_LOG(WARNING) << observer_counts_[i] << " notification observer(s) leaked"
michael@0 129 << " of notification type " << i;
michael@0 130 }
michael@0 131 }
michael@0 132 #endif
michael@0 133
michael@0 134 for (int i = 0; i < NotificationType::NOTIFICATION_TYPE_COUNT; i++) {
michael@0 135 NotificationSourceMap omap = observers_[i];
michael@0 136 for (NotificationSourceMap::iterator it = omap.begin();
michael@0 137 it != omap.end(); ++it) {
michael@0 138 delete it->second;
michael@0 139 }
michael@0 140 }
michael@0 141 }
michael@0 142
michael@0 143 NotificationObserver::~NotificationObserver() {}

mercurial