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