Initialize Tizen 2.3
[external/chromium.git] / ipc / ipc_channel_win.h
1 // Copyright (c) 2011 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 IPC_IPC_CHANNEL_WIN_H_
6 #define IPC_IPC_CHANNEL_WIN_H_
7 #pragma once
8
9 #include "ipc/ipc_channel.h"
10
11 #include <queue>
12 #include <string>
13
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop.h"
16
17 namespace base {
18 class NonThreadSafe;
19 }
20
21 namespace IPC {
22
23 class Channel::ChannelImpl : public MessageLoopForIO::IOHandler {
24  public:
25   // Mirror methods of Channel, see ipc_channel.h for description.
26   ChannelImpl(const IPC::ChannelHandle &channel_handle, Mode mode,
27               Listener* listener);
28   ~ChannelImpl();
29   bool Connect();
30   void Close();
31   void set_listener(Listener* listener) { listener_ = listener; }
32   bool Send(Message* message);
33   static bool IsNamedServerInitialized(const std::string& channel_id);
34  private:
35   static const std::wstring PipeName(const std::string& channel_id);
36   bool CreatePipe(const IPC::ChannelHandle &channel_handle, Mode mode);
37
38   bool ProcessConnection();
39   bool ProcessIncomingMessages(MessageLoopForIO::IOContext* context,
40                                DWORD bytes_read);
41   bool ProcessOutgoingMessages(MessageLoopForIO::IOContext* context,
42                                DWORD bytes_written);
43
44   // MessageLoop::IOHandler implementation.
45   virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
46                              DWORD bytes_transfered, DWORD error);
47  private:
48   struct State {
49     explicit State(ChannelImpl* channel);
50     ~State();
51     MessageLoopForIO::IOContext context;
52     bool is_pending;
53   };
54
55   State input_state_;
56   State output_state_;
57
58   HANDLE pipe_;
59
60   Listener* listener_;
61
62   // Messages to be sent are queued here.
63   std::queue<Message*> output_queue_;
64
65   // We read from the pipe into this buffer
66   char input_buf_[Channel::kReadBufferSize];
67
68   // Large messages that span multiple pipe buffers, get built-up using
69   // this buffer.
70   std::string input_overflow_buf_;
71
72   // In server-mode, we have to wait for the client to connect before we
73   // can begin reading.  We make use of the input_state_ when performing
74   // the connect operation in overlapped mode.
75   bool waiting_connect_;
76
77   // This flag is set when processing incoming messages.  It is used to
78   // avoid recursing through ProcessIncomingMessages, which could cause
79   // problems.  TODO(darin): make this unnecessary
80   bool processing_incoming_;
81
82   ScopedRunnableMethodFactory<ChannelImpl> factory_;
83
84   scoped_ptr<base::NonThreadSafe> thread_check_;
85
86   DISALLOW_COPY_AND_ASSIGN(ChannelImpl);
87 };
88
89 }  // namespace IPC
90
91 #endif  // IPC_IPC_CHANNEL_WIN_H_