summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/util/channel.h
blob: 88ddfc4a6b6c8ddb435a6eb61da8489bf0abf0cf (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// 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.

#ifndef CHROME_BROWSER_SYNC_UTIL_CHANNEL_H_
#define CHROME_BROWSER_SYNC_UTIL_CHANNEL_H_
#pragma once

///////////////////////////////////////////////////////////////////////////////
//
// OVERVIEW:
//
//   A threadsafe container for a list of observers.  Observers are able to
//   remove themselves during iteration, and can be added on any thread.  This
//   allows observers to safely remove themselves during notifications.  It
//   also provides a handler when an observer is added that will remove the
//   observer on destruction.
//
//   It is expected that all observers are removed before destruction.
//   The channel owner should notify all observers to disconnect on shutdown if
//   needed to ensure this.
//
// TYPICAL USAGE:
//
//   class MyWidget {
//    public:
//     ...
//
//     class Observer : public ChannelEventHandler<FooEvent> {
//      public:
//       virtual void HandleChannelEvent(const FooEvent& w) = 0;
//     };
//
//     ChannelHookup<MyEvent>* AddObserver(Observer* obs) {
//       return channel_.AddObserver(obs);
//     }
//
//     void RemoveObserver(Observer* obs) {
//       channel_.RemoveObserver(obs);
//     }
//
//     void NotifyFoo(FooEvent& event) {
//       channel_.Notify(event);
//     }
//
//    private:
//     Channel<FooEvent> channel_;
//   };
//
//
///////////////////////////////////////////////////////////////////////////////

#include "base/lock.h"
#include "base/observer_list.h"
#include "base/threading/platform_thread.h"

namespace browser_sync {

template <typename EventType>
class Channel;

class EventHandler {
};

template <typename EventType>
class ChannelEventHandler : public EventHandler {
 public:
  virtual void HandleChannelEvent(const EventType& event) = 0;

 protected:
  virtual ~ChannelEventHandler() {}
};

// This class manages a connection to a channel.  When it is destroyed, it
// will remove the listener from the channel observer list.
template <typename EventType>
class ChannelHookup {
 public:
  ChannelHookup(Channel<EventType>* channel,
                browser_sync::ChannelEventHandler<EventType>* handler)
      : channel_(channel),
        handler_(handler) {}
  ~ChannelHookup() {
    channel_->RemoveObserver(handler_);
  }

 private:
  Channel<EventType>* channel_;
  browser_sync::ChannelEventHandler<EventType>* handler_;
};

template <typename EventType>
class Channel {
 public:
  typedef ObserverListBase<EventHandler> ChannelObserverList;

  Channel() : locking_thread_(0) {}

  ChannelHookup<EventType>* AddObserver(
      ChannelEventHandler<EventType>* observer) {
    AutoLock scoped_lock(event_handlers_mutex_);
    event_handlers_.AddObserver(observer);
    return new ChannelHookup<EventType>(this, observer);
  }

  void RemoveObserver(ChannelEventHandler<EventType>* observer) {
    // This can be called in response to a notification, so we may already have
    // locked this channel on this thread.
    bool need_lock = (locking_thread_ != base::PlatformThread::CurrentId());
    if (need_lock)
      event_handlers_mutex_.Acquire();

    event_handlers_mutex_.AssertAcquired();
    event_handlers_.RemoveObserver(observer);
    if (need_lock)
      event_handlers_mutex_.Release();
  }

  void Notify(const EventType& event) {
    AutoLock scoped_lock(event_handlers_mutex_);

    // This may result in an observer trying to remove itself, so keep track
    // of the thread we're locked on.
    locking_thread_ = base::PlatformThread::CurrentId();

    ChannelObserverList::Iterator it(event_handlers_);
    EventHandler* obs;
    while ((obs = it.GetNext()) != NULL) {
      static_cast<ChannelEventHandler<EventType>* >(obs)->
          HandleChannelEvent(event);
    }

    // Set back to an invalid thread id.
    locking_thread_ = 0;
  }

 private:
  Lock event_handlers_mutex_;
  base::PlatformThreadId locking_thread_;
  ObserverList<EventHandler> event_handlers_;
};

}  // namespace browser_sync

#endif  // CHROME_BROWSER_SYNC_UTIL_CHANNEL_H_