|
1 // Copyright (c) 2008 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 #ifndef CHROME_COMMON_IPC_CHANNEL_WIN_H_ |
|
6 #define CHROME_COMMON_IPC_CHANNEL_WIN_H_ |
|
7 |
|
8 #include "chrome/common/ipc_channel.h" |
|
9 |
|
10 #include <queue> |
|
11 #include <string> |
|
12 |
|
13 #include "base/message_loop.h" |
|
14 |
|
15 class NonThreadSafe; |
|
16 |
|
17 namespace IPC { |
|
18 |
|
19 class Channel::ChannelImpl : public MessageLoopForIO::IOHandler { |
|
20 public: |
|
21 // Mirror methods of Channel, see ipc_channel.h for description. |
|
22 ChannelImpl(const std::wstring& channel_id, Mode mode, Listener* listener); |
|
23 ChannelImpl(const std::wstring& channel_id, HANDLE server_pipe, |
|
24 Mode mode, Listener* listener); |
|
25 ~ChannelImpl() { |
|
26 if (pipe_ != INVALID_HANDLE_VALUE) { |
|
27 Close(); |
|
28 } |
|
29 } |
|
30 bool Connect(); |
|
31 void Close(); |
|
32 HANDLE GetServerPipeHandle() const; |
|
33 Listener* set_listener(Listener* listener) { |
|
34 Listener* old = listener_; |
|
35 listener_ = listener; |
|
36 return old; |
|
37 } |
|
38 bool Send(Message* message); |
|
39 |
|
40 // See the comment in ipc_channel.h for info on Unsound_IsClosed() and |
|
41 // Unsound_NumQueuedMessages(). |
|
42 bool Unsound_IsClosed() const; |
|
43 uint32_t Unsound_NumQueuedMessages() const; |
|
44 |
|
45 private: |
|
46 void Init(Mode mode, Listener* listener); |
|
47 |
|
48 void OutputQueuePush(Message* msg); |
|
49 void OutputQueuePop(); |
|
50 |
|
51 const std::wstring PipeName(const std::wstring& channel_id) const; |
|
52 bool CreatePipe(const std::wstring& channel_id, Mode mode); |
|
53 bool EnqueueHelloMessage(); |
|
54 |
|
55 bool ProcessConnection(); |
|
56 bool ProcessIncomingMessages(MessageLoopForIO::IOContext* context, |
|
57 DWORD bytes_read); |
|
58 bool ProcessOutgoingMessages(MessageLoopForIO::IOContext* context, |
|
59 DWORD bytes_written); |
|
60 |
|
61 // MessageLoop::IOHandler implementation. |
|
62 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
|
63 DWORD bytes_transfered, DWORD error); |
|
64 private: |
|
65 struct State { |
|
66 explicit State(ChannelImpl* channel); |
|
67 ~State(); |
|
68 MessageLoopForIO::IOContext context; |
|
69 bool is_pending; |
|
70 }; |
|
71 |
|
72 State input_state_; |
|
73 State output_state_; |
|
74 |
|
75 HANDLE pipe_; |
|
76 |
|
77 Listener* listener_; |
|
78 |
|
79 // Messages to be sent are queued here. |
|
80 std::queue<Message*> output_queue_; |
|
81 |
|
82 // We read from the pipe into this buffer |
|
83 char input_buf_[Channel::kReadBufferSize]; |
|
84 |
|
85 // Large messages that span multiple pipe buffers, get built-up using |
|
86 // this buffer. |
|
87 std::string input_overflow_buf_; |
|
88 |
|
89 // In server-mode, we have to wait for the client to connect before we |
|
90 // can begin reading. We make use of the input_state_ when performing |
|
91 // the connect operation in overlapped mode. |
|
92 bool waiting_connect_; |
|
93 |
|
94 // This flag is set when processing incoming messages. It is used to |
|
95 // avoid recursing through ProcessIncomingMessages, which could cause |
|
96 // problems. TODO(darin): make this unnecessary |
|
97 bool processing_incoming_; |
|
98 |
|
99 // This flag is set after Close() is run on the channel. |
|
100 bool closed_; |
|
101 |
|
102 // This variable is updated so it matches output_queue_.size(), except we can |
|
103 // read output_queue_length_ from any thread (if we're OK getting an |
|
104 // occasional out-of-date or bogus value). We use output_queue_length_ to |
|
105 // implement Unsound_NumQueuedMessages. |
|
106 size_t output_queue_length_; |
|
107 |
|
108 ScopedRunnableMethodFactory<ChannelImpl> factory_; |
|
109 |
|
110 scoped_ptr<NonThreadSafe> thread_check_; |
|
111 |
|
112 DISALLOW_COPY_AND_ASSIGN(ChannelImpl); |
|
113 }; |
|
114 |
|
115 } // namespace IPC |
|
116 |
|
117 #endif // CHROME_COMMON_IPC_CHANNEL_WIN_H_ |