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
3 // found in the LICENSE file.
5 #ifndef IPC_IPC_CHANNEL_WIN_H_
6 #define IPC_IPC_CHANNEL_WIN_H_
8 #include "ipc/ipc_channel.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/win/scoped_handle.h"
19 #include "ipc/ipc_channel_reader.h"
27 class ChannelWin
: public Channel
,
28 public internal::ChannelReader
,
29 public base::MessageLoopForIO::IOHandler
{
31 // Mirror methods of Channel, see ipc_channel.h for description.
32 // |broker| must outlive the newly created object.
33 ChannelWin(const IPC::ChannelHandle
& channel_handle
,
36 ~ChannelWin() override
;
38 // Channel implementation
39 bool Connect() override
;
40 void Close() override
;
41 bool Send(Message
* message
) override
;
42 AttachmentBroker
* GetAttachmentBroker() override
;
43 base::ProcessId
GetPeerPID() const override
;
44 base::ProcessId
GetSelfPID() const override
;
46 static bool IsNamedServerInitialized(const std::string
& channel_id
);
50 // ChannelReader implementation.
51 ReadState
ReadData(char* buffer
, int buffer_len
, int* bytes_read
) override
;
52 bool ShouldDispatchInputMessage(Message
* msg
) override
;
53 bool GetNonBrokeredAttachments(Message
* msg
) override
;
54 bool DidEmptyInputBuffers() override
;
55 void HandleInternalMessage(const Message
& msg
) override
;
56 base::ProcessId
GetSenderPID() override
;
57 bool IsAttachmentBrokerEndpoint() override
;
59 static const base::string16
PipeName(const std::string
& channel_id
,
61 bool CreatePipe(const IPC::ChannelHandle
&channel_handle
, Mode mode
);
63 bool ProcessConnection();
64 bool ProcessOutgoingMessages(base::MessageLoopForIO::IOContext
* context
,
67 // Returns |false| on channel error.
68 // If |message| has brokerable attachments, those attachments are passed to
69 // the AttachmentBroker (which in turn invokes Send()), so this method must
71 // Adds |message| to |output_queue_| and calls ProcessOutgoingMessages().
72 bool ProcessMessageForDelivery(Message
* message
);
74 // Moves all messages from |prelim_queue_| to |output_queue_| by calling
75 // ProcessMessageForDelivery().
76 void FlushPrelimQueue();
78 // MessageLoop::IOHandler implementation.
79 void OnIOCompleted(base::MessageLoopForIO::IOContext
* context
,
80 DWORD bytes_transfered
,
81 DWORD error
) override
;
85 explicit State(ChannelWin
* channel
);
87 base::MessageLoopForIO::IOContext context
;
94 base::win::ScopedHandle pipe_
;
96 base::ProcessId peer_pid_
;
98 // Messages not yet ready to be sent are queued here. Messages removed from
99 // this queue are placed in the output_queue_. The double queue is
100 // unfortunate, but is necessary because messages with brokerable attachments
101 // can generate multiple messages to be sent (possibly from other channels).
102 // Some of these generated messages cannot be sent until |peer_pid_| has been
104 // As soon as |peer_pid| has been configured, there is no longer any need for
105 // |prelim_queue_|. All messages are flushed, and no new messages are added.
106 std::queue
<Message
*> prelim_queue_
;
108 // Messages to be sent are queued here.
109 std::queue
<OutputElement
*> output_queue_
;
111 // In server-mode, we have to wait for the client to connect before we
112 // can begin reading. We make use of the input_state_ when performing
113 // the connect operation in overlapped mode.
114 bool waiting_connect_
;
116 // This flag is set when processing incoming messages. It is used to
117 // avoid recursing through ProcessIncomingMessages, which could cause
118 // problems. TODO(darin): make this unnecessary
119 bool processing_incoming_
;
121 // Determines if we should validate a client's secret on connection.
122 bool validate_client_
;
124 // Tracks the lifetime of this object, for debugging purposes.
125 uint32_t debug_flags_
;
127 // This is a unique per-channel value used to authenticate the client end of
128 // a connection. If the value is non-zero, the client passes it in the hello
129 // and the host validates. (We don't send the zero value fto preserve IPC
130 // compatability with existing clients that don't validate the channel.)
131 int32_t client_secret_
;
133 scoped_ptr
<base::ThreadChecker
> thread_check_
;
135 base::WeakPtrFactory
<ChannelWin
> weak_factory_
;
136 DISALLOW_COPY_AND_ASSIGN(ChannelWin
);
141 #endif // IPC_IPC_CHANNEL_WIN_H_