blob: 933a95eea0ee420b24d09fb88b5541cdb8a753de [file] [log] [blame]
rsleevi@chromium.org997c1d42012-04-28 11:12:00 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
jabdelmalek@google.comeb921652010-04-07 05:33:36 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef IPC_IPC_SYNC_MESSAGE_FILTER_H_
6#define IPC_IPC_SYNC_MESSAGE_FILTER_H_
7
thestig@chromium.org519c9ac2011-11-15 09:29:48 +09008#include <set>
9
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090010#include "base/basictypes.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090011#include "base/memory/ref_counted.h"
brettw@chromium.orgabe477a2011-01-21 13:55:52 +090012#include "base/synchronization/lock.h"
dmichael@chromium.orgc0c370e2014-04-25 09:07:30 +090013#include "ipc/ipc_sender.h"
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090014#include "ipc/ipc_sync_message.h"
dmichael@chromium.orgc0c370e2014-04-25 09:07:30 +090015#include "ipc/message_filter.h"
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090016
thestig@chromium.org613caba2010-08-06 15:22:15 +090017namespace base {
sievers@chromium.org7ac134c2011-09-16 18:38:38 +090018class MessageLoopProxy;
thestig@chromium.org613caba2010-08-06 15:22:15 +090019class WaitableEvent;
20}
21
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090022namespace IPC {
23
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090024// This MessageFilter allows sending synchronous IPC messages from a thread
25// other than the listener thread associated with the SyncChannel. It does not
26// support fancy features that SyncChannel does, such as handling recursion or
27// receiving messages while waiting for a response. Note that this object can
28// be used to send simultaneous synchronous messages from different threads.
dmichael@chromium.orgc0c370e2014-04-25 09:07:30 +090029class IPC_EXPORT SyncMessageFilter : public MessageFilter, public Sender {
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090030 public:
31 explicit SyncMessageFilter(base::WaitableEvent* shutdown_event);
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090032
brettw@chromium.orgf947ed02012-06-12 07:35:26 +090033 // MessageSender implementation.
avi@chromium.org362c8a82011-11-18 01:09:44 +090034 virtual bool Send(Message* message) OVERRIDE;
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090035
dmichael@chromium.orgc0c370e2014-04-25 09:07:30 +090036 // MessageFilter implementation.
morrita@chromium.org631fb8e2014-06-13 15:07:14 +090037 virtual void OnFilterAdded(Sender* sender) OVERRIDE;
avi@chromium.org362c8a82011-11-18 01:09:44 +090038 virtual void OnChannelError() OVERRIDE;
39 virtual void OnChannelClosing() OVERRIDE;
40 virtual bool OnMessageReceived(const Message& message) OVERRIDE;
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090041
rsleevi@chromium.org997c1d42012-04-28 11:12:00 +090042 protected:
43 virtual ~SyncMessageFilter();
44
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090045 private:
46 void SendOnIOThread(Message* message);
47 // Signal all the pending sends as done, used in an error condition.
48 void SignalAllEvents();
49
50 // The channel to which this filter was added.
morrita@chromium.org631fb8e2014-06-13 15:07:14 +090051 Sender* sender_;
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090052
sievers@chromium.org7ac134c2011-09-16 18:38:38 +090053 // The process's main thread.
54 scoped_refptr<base::MessageLoopProxy> listener_loop_;
55
56 // The message loop where the Channel lives.
57 scoped_refptr<base::MessageLoopProxy> io_loop_;
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090058
dumi@chromium.org229db062010-05-29 16:00:47 +090059 typedef std::set<PendingSyncMsg*> PendingSyncMessages;
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090060 PendingSyncMessages pending_sync_messages_;
61
62 // Locks data members above.
brettw@chromium.orgabe477a2011-01-21 13:55:52 +090063 base::Lock lock_;
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090064
65 base::WaitableEvent* shutdown_event_;
66
67 DISALLOW_COPY_AND_ASSIGN(SyncMessageFilter);
68};
69
70} // namespace IPC
71
72#endif // IPC_IPC_SYNC_MESSAGE_FILTER_H_