OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_COMMON_IPC_SYNC_SENDER_H__ | 5 #ifndef CHROME_COMMON_IPC_SYNC_SENDER_H__ |
6 #define CHROME_COMMON_IPC_SYNC_SENDER_H__ | 6 #define CHROME_COMMON_IPC_SYNC_SENDER_H__ |
7 | 7 |
8 #include <windows.h> | |
9 #include <string> | 8 #include <string> |
10 #include <deque> | 9 #include <deque> |
11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
12 #include "base/lock.h" | 11 #include "base/lock.h" |
13 #include "base/object_watcher.h" | |
14 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
15 #include "base/scoped_handle.h" | 13 #include "base/scoped_handle.h" |
| 14 #include "base/waitable_event.h" |
| 15 #include "base/waitable_event_watcher.h" |
16 #include "chrome/common/ipc_channel_proxy.h" | 16 #include "chrome/common/ipc_channel_proxy.h" |
17 | 17 |
18 namespace IPC { | 18 namespace IPC { |
19 | 19 |
20 class SyncMessage; | 20 class SyncMessage; |
| 21 class MessageReplyDeserializer; |
21 | 22 |
22 // This is similar to IPC::ChannelProxy, with the added feature of supporting | 23 // This is similar to IPC::ChannelProxy, with the added feature of supporting |
23 // sending synchronous messages. | 24 // sending synchronous messages. |
24 // Note that care must be taken that the lifetime of the ipc_thread argument | 25 // Note that care must be taken that the lifetime of the ipc_thread argument |
25 // is more than this object. If the message loop goes away while this object | 26 // is more than this object. If the message loop goes away while this object |
26 // is running and it's used to send a message, then it will use the invalid | 27 // is running and it's used to send a message, then it will use the invalid |
27 // message loop pointer to proxy it to the ipc thread. | 28 // message loop pointer to proxy it to the ipc thread. |
28 class SyncChannel : public ChannelProxy, | 29 class SyncChannel : public ChannelProxy, |
29 public base::ObjectWatcher::Delegate { | 30 public base::WaitableEventWatcher::Delegate { |
30 public: | 31 public: |
31 SyncChannel(const std::wstring& channel_id, Channel::Mode mode, | 32 SyncChannel(const std::wstring& channel_id, Channel::Mode mode, |
32 Channel::Listener* listener, MessageFilter* filter, | 33 Channel::Listener* listener, MessageFilter* filter, |
33 MessageLoop* ipc_message_loop, bool create_pipe_now, | 34 MessageLoop* ipc_message_loop, bool create_pipe_now, |
34 HANDLE shutdown_event); | 35 base::WaitableEvent* shutdown_event); |
35 ~SyncChannel(); | 36 ~SyncChannel(); |
36 | 37 |
37 virtual bool Send(Message* message); | 38 virtual bool Send(Message* message); |
38 virtual bool SendWithTimeout(Message* message, int timeout_ms); | 39 virtual bool SendWithTimeout(Message* message, int timeout_ms); |
39 | 40 |
40 // Whether we allow sending messages with no time-out. | 41 // Whether we allow sending messages with no time-out. |
41 void set_sync_messages_with_no_timeout_allowed(bool value) { | 42 void set_sync_messages_with_no_timeout_allowed(bool value) { |
42 sync_messages_with_no_timeout_allowed_ = value; | 43 sync_messages_with_no_timeout_allowed_ = value; |
43 } | 44 } |
44 | 45 |
45 protected: | 46 protected: |
46 class ReceivedSyncMsgQueue; | 47 class ReceivedSyncMsgQueue; |
47 friend class ReceivedSyncMsgQueue; | 48 friend class ReceivedSyncMsgQueue; |
48 | 49 |
49 // SyncContext holds the per object data for SyncChannel, so that SyncChannel | 50 // SyncContext holds the per object data for SyncChannel, so that SyncChannel |
50 // can be deleted while it's being used in a different thread. See | 51 // can be deleted while it's being used in a different thread. See |
51 // ChannelProxy::Context for more information. | 52 // ChannelProxy::Context for more information. |
52 class SyncContext : public Context, | 53 class SyncContext : public Context, |
53 public base::ObjectWatcher::Delegate { | 54 public base::WaitableEventWatcher::Delegate { |
54 public: | 55 public: |
55 SyncContext(Channel::Listener* listener, | 56 SyncContext(Channel::Listener* listener, |
56 MessageFilter* filter, | 57 MessageFilter* filter, |
57 MessageLoop* ipc_thread, | 58 MessageLoop* ipc_thread, |
58 HANDLE shutdown_event); | 59 base::WaitableEvent* shutdown_event); |
59 | 60 |
60 ~SyncContext(); | 61 ~SyncContext(); |
61 | 62 |
62 // Adds information about an outgoing sync message to the context so that | 63 // Adds information about an outgoing sync message to the context so that |
63 // we know how to deserialize the reply. | 64 // we know how to deserialize the reply. |
64 void Push(IPC::SyncMessage* sync_msg); | 65 void Push(IPC::SyncMessage* sync_msg); |
65 | 66 |
66 // Cleanly remove the top deserializer (and throw it away). Returns the | 67 // Cleanly remove the top deserializer (and throw it away). Returns the |
67 // result of the Send call for that message. | 68 // result of the Send call for that message. |
68 bool Pop(); | 69 bool Pop(); |
69 | 70 |
70 // Returns an event that's set when the send is complete, timed out or the | 71 // Returns an event that's set when the send is complete, timed out or the |
71 // process shut down. | 72 // process shut down. |
72 HANDLE GetSendDoneEvent(); | 73 base::WaitableEvent* GetSendDoneEvent(); |
73 | 74 |
74 // Returns an event that's set when an incoming message that's not the reply | 75 // Returns an event that's set when an incoming message that's not the reply |
75 // needs to get dispatched (by calling SyncContext::DispatchMessages). | 76 // needs to get dispatched (by calling SyncContext::DispatchMessages). |
76 HANDLE GetDispatchEvent(); | 77 base::WaitableEvent* GetDispatchEvent(); |
77 | 78 |
78 void DispatchMessages(); | 79 void DispatchMessages(); |
79 | 80 |
80 // Checks if the given message is blocking the listener thread because of a | 81 // Checks if the given message is blocking the listener thread because of a |
81 // synchronous send. If it is, the thread is unblocked and true is returned
. | 82 // synchronous send. If it is, the thread is unblocked and true is returned
. |
82 // Otherwise the function returns false. | 83 // Otherwise the function returns false. |
83 bool TryToUnblockListener(const Message* msg); | 84 bool TryToUnblockListener(const Message* msg); |
84 | 85 |
85 // Called on the IPC thread when a sync send that runs a nested message loop | 86 // Called on the IPC thread when a sync send that runs a nested message loop |
86 // times out. | 87 // times out. |
87 void OnSendTimeout(int message_id); | 88 void OnSendTimeout(int message_id); |
88 | 89 |
89 HANDLE shutdown_event() { return shutdown_event_; } | 90 base::WaitableEvent* shutdown_event() { return shutdown_event_; } |
90 | 91 |
91 private: | 92 private: |
92 // IPC::ChannelProxy methods that we override. | 93 // IPC::ChannelProxy methods that we override. |
93 | 94 |
94 // Called on the listener thread. | 95 // Called on the listener thread. |
95 virtual void Clear(); | 96 virtual void Clear(); |
96 | 97 |
97 // Called on the IPC thread. | 98 // Called on the IPC thread. |
98 virtual void OnMessageReceived(const Message& msg); | 99 virtual void OnMessageReceived(const Message& msg); |
99 virtual void OnChannelError(); | 100 virtual void OnChannelError(); |
100 virtual void OnChannelOpened(); | 101 virtual void OnChannelOpened(); |
101 virtual void OnChannelClosed(); | 102 virtual void OnChannelClosed(); |
102 | 103 |
103 // Cancels all pending Send calls. | 104 // Cancels all pending Send calls. |
104 void CancelPendingSends(); | 105 void CancelPendingSends(); |
105 | 106 |
106 // ObjectWatcher::Delegate implementation. | 107 // WaitableEventWatcher::Delegate implementation. |
107 virtual void OnObjectSignaled(HANDLE object); | 108 virtual void OnWaitableEventSignaled(base::WaitableEvent* arg); |
108 | 109 |
109 // When sending a synchronous message, this structure contains an object tha
t | 110 // When sending a synchronous message, this structure contains an object tha
t |
110 // knows how to deserialize the response. | 111 // knows how to deserialize the response. |
111 struct PendingSyncMsg { | 112 struct PendingSyncMsg { |
112 PendingSyncMsg(int id, IPC::MessageReplyDeserializer* d, HANDLE e) : | 113 PendingSyncMsg(int id, IPC::MessageReplyDeserializer* d, |
| 114 base::WaitableEvent* e) : |
113 id(id), deserializer(d), done_event(e), send_result(false) { } | 115 id(id), deserializer(d), done_event(e), send_result(false) { } |
114 int id; | 116 int id; |
115 IPC::MessageReplyDeserializer* deserializer; | 117 IPC::MessageReplyDeserializer* deserializer; |
116 HANDLE done_event; | 118 base::WaitableEvent* done_event; |
117 bool send_result; | 119 bool send_result; |
118 }; | 120 }; |
119 | 121 |
120 typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue; | 122 typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue; |
121 PendingSyncMessageQueue deserializers_; | 123 PendingSyncMessageQueue deserializers_; |
122 Lock deserializers_lock_; | 124 Lock deserializers_lock_; |
123 | 125 |
124 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; | 126 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; |
125 | 127 |
126 HANDLE shutdown_event_; | 128 base::WaitableEvent* shutdown_event_; |
127 base::ObjectWatcher shutdown_watcher_; | 129 base::WaitableEventWatcher shutdown_watcher_; |
128 }; | 130 }; |
129 | 131 |
130 private: | 132 private: |
131 // ObjectWatcher::Delegate implementation. | 133 // WaitableEventWatcher::Delegate implementation. |
132 virtual void OnObjectSignaled(HANDLE object); | 134 virtual void OnWaitableEventSignaled(base::WaitableEvent* arg); |
133 | 135 |
134 SyncContext* sync_context() { return reinterpret_cast<SyncContext*>(context())
; } | 136 SyncContext* sync_context() { return reinterpret_cast<SyncContext*>(context())
; } |
135 | 137 |
136 // Both these functions wait for a reply, timeout or process shutdown. The | 138 // Both these functions wait for a reply, timeout or process shutdown. The |
137 // latter one also runs a nested message loop in the meantime. | 139 // latter one also runs a nested message loop in the meantime. |
138 void WaitForReply(HANDLE pump_messages_event); | 140 void WaitForReply(base::WaitableEvent* pump_messages_event); |
139 | 141 |
140 // Runs a nested message loop until a reply arrives, times out, or the process | 142 // Runs a nested message loop until a reply arrives, times out, or the process |
141 // shuts down. | 143 // shuts down. |
142 void WaitForReplyWithNestedMessageLoop(); | 144 void WaitForReplyWithNestedMessageLoop(); |
143 | 145 |
144 bool sync_messages_with_no_timeout_allowed_; | 146 bool sync_messages_with_no_timeout_allowed_; |
145 | 147 |
146 // Used to signal events between the IPC and listener threads. | 148 // Used to signal events between the IPC and listener threads. |
147 base::ObjectWatcher send_done_watcher_; | 149 base::WaitableEventWatcher send_done_watcher_; |
148 base::ObjectWatcher dispatch_watcher_; | 150 base::WaitableEventWatcher dispatch_watcher_; |
149 | 151 |
150 DISALLOW_EVIL_CONSTRUCTORS(SyncChannel); | 152 DISALLOW_EVIL_CONSTRUCTORS(SyncChannel); |
151 }; | 153 }; |
152 | 154 |
153 } // namespace IPC | 155 } // namespace IPC |
154 | 156 |
155 #endif // CHROME_COMMON_IPC_SYNC_SENDER_H__ | 157 #endif // CHROME_COMMON_IPC_SYNC_SENDER_H__ |
156 | 158 |
OLD | NEW |