blob: 2a601e1910b1662c8afd84ff6b5c6a9c05e2d880 [file] [log] [blame]
[email protected]514411fc2008-12-10 22:28:111// 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
[email protected]c1e4bff32009-01-29 00:07:0615class NonThreadSafe;
16
[email protected]514411fc2008-12-10 22:28:1117namespace IPC {
18
19class 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() { Close(); }
24 bool Connect();
25 void Close();
26 void set_listener(Listener* listener) { listener_ = listener; }
27 bool Send(Message* message);
28 private:
29 const std::wstring PipeName(const std::wstring& channel_id) const;
30 bool CreatePipe(const std::wstring& channel_id, Mode mode);
31
32 bool ProcessConnection();
33 bool ProcessIncomingMessages(MessageLoopForIO::IOContext* context,
34 DWORD bytes_read);
35 bool ProcessOutgoingMessages(MessageLoopForIO::IOContext* context,
36 DWORD bytes_written);
37
38 // MessageLoop::IOHandler implementation.
39 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
40 DWORD bytes_transfered, DWORD error);
41 private:
42 struct State {
43 explicit State(ChannelImpl* channel);
44 ~State();
45 MessageLoopForIO::IOContext context;
46 bool is_pending;
47 };
48
49 State input_state_;
50 State output_state_;
51
52 HANDLE pipe_;
53
54 Listener* listener_;
55
56 // Messages to be sent are queued here.
57 std::queue<Message*> output_queue_;
58
59 // We read from the pipe into this buffer
60 char input_buf_[Channel::kReadBufferSize];
61
62 // Large messages that span multiple pipe buffers, get built-up using
63 // this buffer.
64 std::string input_overflow_buf_;
65
66 // In server-mode, we have to wait for the client to connect before we
67 // can begin reading. We make use of the input_state_ when performing
68 // the connect operation in overlapped mode.
69 bool waiting_connect_;
70
71 // This flag is set when processing incoming messages. It is used to
72 // avoid recursing through ProcessIncomingMessages, which could cause
73 // problems. TODO(darin): make this unnecessary
74 bool processing_incoming_;
75
76 ScopedRunnableMethodFactory<ChannelImpl> factory_;
77
[email protected]c1e4bff32009-01-29 00:07:0678 scoped_ptr<NonThreadSafe> thread_check_;
79
[email protected]514411fc2008-12-10 22:28:1180 DISALLOW_COPY_AND_ASSIGN(ChannelImpl);
81};
82
83} // namespace IPC
84
85#endif // CHROME_COMMON_IPC_CHANNEL_WIN_H_