OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 IPC_IPC_CHANNEL_WIN_H_ | 5 #ifndef IPC_IPC_CHANNEL_WIN_H_ |
6 #define IPC_IPC_CHANNEL_WIN_H_ | 6 #define IPC_IPC_CHANNEL_WIN_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "ipc/ipc_channel.h" | 9 #include "ipc/ipc_channel.h" |
10 | 10 |
11 #include <queue> | 11 #include <queue> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
| 17 #include "ipc/ipc_channel_reader.h" |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 class NonThreadSafe; | 20 class NonThreadSafe; |
20 } | 21 } |
21 | 22 |
22 namespace IPC { | 23 namespace IPC { |
23 | 24 |
24 class Channel::ChannelImpl : public MessageLoopForIO::IOHandler { | 25 class Channel::ChannelImpl : public internal::ChannelReader, |
| 26 public MessageLoopForIO::IOHandler { |
25 public: | 27 public: |
26 // Mirror methods of Channel, see ipc_channel.h for description. | 28 // Mirror methods of Channel, see ipc_channel.h for description. |
27 ChannelImpl(const IPC::ChannelHandle &channel_handle, Mode mode, | 29 ChannelImpl(const IPC::ChannelHandle &channel_handle, Mode mode, |
28 Listener* listener); | 30 Listener* listener); |
29 ~ChannelImpl(); | 31 ~ChannelImpl(); |
30 bool Connect(); | 32 bool Connect(); |
31 void Close(); | 33 void Close(); |
32 void set_listener(Listener* listener) { listener_ = listener; } | |
33 bool Send(Message* message); | 34 bool Send(Message* message); |
34 static bool IsNamedServerInitialized(const std::string& channel_id); | 35 static bool IsNamedServerInitialized(const std::string& channel_id); |
35 | 36 |
36 private: | 37 private: |
37 enum ReadState { READ_SUCCEEDED, READ_FAILED, READ_PENDING }; | 38 // ChannelReader implementation. |
38 | 39 virtual ReadState ReadData(char* buffer, |
39 // This will become the virtual interface implemented by this class to | 40 int buffer_len, |
40 // handle platform-specific reading. | 41 int* bytes_read) OVERRIDE; |
41 // TODO(brettw) finish refactoring. | 42 virtual bool WillDispatchInputMessage(Message* msg) OVERRIDE; |
42 ReadState ReadData(char* buffer, int buffer_len, int* bytes_read); | 43 bool DidEmptyInputBuffers() OVERRIDE; |
43 bool WillDispatchInputMessage(Message* msg); | 44 virtual void HandleHelloMessage(const Message& msg) OVERRIDE; |
44 void HandleHelloMessage(const Message& msg); | |
45 bool DidEmptyInputBuffers(); | |
46 | |
47 bool DispatchInputData(const char* input_data, int input_data_len); | |
48 | |
49 // Returns true if the given message is the hello message. | |
50 bool IsHelloMessage(const Message& m) const; | |
51 | |
52 // Handles asynchronously read data. | |
53 // | |
54 // Optionally call this after returning READ_PENDING from ReadData to | |
55 // indicate that buffer was filled with the given number of bytes of | |
56 // data. See ReadData for more. | |
57 bool AsyncReadComplete(int bytes_read); | |
58 | 45 |
59 static const std::wstring PipeName(const std::string& channel_id); | 46 static const std::wstring PipeName(const std::string& channel_id); |
60 bool CreatePipe(const IPC::ChannelHandle &channel_handle, Mode mode); | 47 bool CreatePipe(const IPC::ChannelHandle &channel_handle, Mode mode); |
61 | 48 |
62 bool ProcessConnection(); | 49 bool ProcessConnection(); |
63 bool ProcessIncomingMessages(); | |
64 bool ProcessOutgoingMessages(MessageLoopForIO::IOContext* context, | 50 bool ProcessOutgoingMessages(MessageLoopForIO::IOContext* context, |
65 DWORD bytes_written); | 51 DWORD bytes_written); |
66 | 52 |
67 // MessageLoop::IOHandler implementation. | 53 // MessageLoop::IOHandler implementation. |
68 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 54 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
69 DWORD bytes_transfered, DWORD error); | 55 DWORD bytes_transfered, DWORD error); |
70 private: | 56 private: |
71 struct State { | 57 struct State { |
72 explicit State(ChannelImpl* channel); | 58 explicit State(ChannelImpl* channel); |
73 ~State(); | 59 ~State(); |
74 MessageLoopForIO::IOContext context; | 60 MessageLoopForIO::IOContext context; |
75 bool is_pending; | 61 bool is_pending; |
76 }; | 62 }; |
77 | 63 |
78 State input_state_; | 64 State input_state_; |
79 State output_state_; | 65 State output_state_; |
80 | 66 |
81 HANDLE pipe_; | 67 HANDLE pipe_; |
82 | 68 |
83 Listener* listener_; | |
84 | |
85 // Messages to be sent are queued here. | 69 // Messages to be sent are queued here. |
86 std::queue<Message*> output_queue_; | 70 std::queue<Message*> output_queue_; |
87 | 71 |
88 // We read from the pipe into this buffer | |
89 char input_buf_[Channel::kReadBufferSize]; | |
90 | |
91 // Large messages that span multiple pipe buffers, get built-up using | |
92 // this buffer. | |
93 std::string input_overflow_buf_; | |
94 | |
95 // In server-mode, we have to wait for the client to connect before we | 72 // In server-mode, we have to wait for the client to connect before we |
96 // can begin reading. We make use of the input_state_ when performing | 73 // can begin reading. We make use of the input_state_ when performing |
97 // the connect operation in overlapped mode. | 74 // the connect operation in overlapped mode. |
98 bool waiting_connect_; | 75 bool waiting_connect_; |
99 | 76 |
100 // This flag is set when processing incoming messages. It is used to | 77 // This flag is set when processing incoming messages. It is used to |
101 // avoid recursing through ProcessIncomingMessages, which could cause | 78 // avoid recursing through ProcessIncomingMessages, which could cause |
102 // problems. TODO(darin): make this unnecessary | 79 // problems. TODO(darin): make this unnecessary |
103 bool processing_incoming_; | 80 bool processing_incoming_; |
104 | 81 |
105 base::WeakPtrFactory<ChannelImpl> weak_factory_; | 82 base::WeakPtrFactory<ChannelImpl> weak_factory_; |
106 | 83 |
107 scoped_ptr<base::NonThreadSafe> thread_check_; | 84 scoped_ptr<base::NonThreadSafe> thread_check_; |
108 | 85 |
109 DISALLOW_COPY_AND_ASSIGN(ChannelImpl); | 86 DISALLOW_COPY_AND_ASSIGN(ChannelImpl); |
110 }; | 87 }; |
111 | 88 |
112 } // namespace IPC | 89 } // namespace IPC |
113 | 90 |
114 #endif // IPC_IPC_CHANNEL_WIN_H_ | 91 #endif // IPC_IPC_CHANNEL_WIN_H_ |
OLD | NEW |