ipc: Convert k(u)int*max types from basictypes.h to std::numeric_limits variants.
[chromium-blink-merge.git] / ipc / ipc_channel_win.h
blob579f7f7b2adbf428e6b86da04367a2cdc26b67b0
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"
10 #include <stdint.h>
12 #include <queue>
13 #include <string>
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"
21 namespace base {
22 class ThreadChecker;
25 namespace IPC {
27 class ChannelWin : public Channel,
28 public internal::ChannelReader,
29 public base::MessageLoopForIO::IOHandler {
30 public:
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,
34 Mode mode,
35 Listener* listener,
36 AttachmentBroker* broker);
37 ~ChannelWin() override;
39 // Channel implementation
40 bool Connect() override;
41 void Close() override;
42 bool Send(Message* message) override;
43 AttachmentBroker* GetAttachmentBroker() override;
44 base::ProcessId GetPeerPID() const override;
45 base::ProcessId GetSelfPID() const override;
47 static bool IsNamedServerInitialized(const std::string& channel_id);
50 private:
51 // ChannelReader implementation.
52 ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) override;
53 bool ShouldDispatchInputMessage(Message* msg) override;
54 bool GetNonBrokeredAttachments(Message* msg) override;
55 bool DidEmptyInputBuffers() override;
56 void HandleInternalMessage(const Message& msg) override;
57 base::ProcessId GetSenderPID() override;
58 bool IsAttachmentBrokerEndpoint() override;
60 static const base::string16 PipeName(const std::string& channel_id,
61 int32_t* secret);
62 bool CreatePipe(const IPC::ChannelHandle &channel_handle, Mode mode);
64 bool ProcessConnection();
65 bool ProcessOutgoingMessages(base::MessageLoopForIO::IOContext* context,
66 DWORD bytes_written);
68 // Returns |false| on channel error.
69 // If |message| has brokerable attachments, those attachments are passed to
70 // the AttachmentBroker (which in turn invokes Send()), so this method must
71 // be re-entrant.
72 // Adds |message| to |output_queue_| and calls ProcessOutgoingMessages().
73 bool ProcessMessageForDelivery(Message* message);
75 // Moves all messages from |prelim_queue_| to |output_queue_| by calling
76 // ProcessMessageForDelivery().
77 void FlushPrelimQueue();
79 // MessageLoop::IOHandler implementation.
80 void OnIOCompleted(base::MessageLoopForIO::IOContext* context,
81 DWORD bytes_transfered,
82 DWORD error) override;
84 private:
85 struct State {
86 explicit State(ChannelWin* channel);
87 ~State();
88 base::MessageLoopForIO::IOContext context;
89 bool is_pending;
92 State input_state_;
93 State output_state_;
95 base::win::ScopedHandle pipe_;
97 base::ProcessId peer_pid_;
99 // Messages not yet ready to be sent are queued here. Messages removed from
100 // this queue are placed in the output_queue_. The double queue is
101 // unfortunate, but is necessary because messages with brokerable attachments
102 // can generate multiple messages to be sent (possibly from other channels).
103 // Some of these generated messages cannot be sent until |peer_pid_| has been
104 // configured.
105 // As soon as |peer_pid| has been configured, there is no longer any need for
106 // |prelim_queue_|. All messages are flushed, and no new messages are added.
107 std::queue<Message*> prelim_queue_;
109 // Messages to be sent are queued here.
110 std::queue<OutputElement*> output_queue_;
112 // In server-mode, we have to wait for the client to connect before we
113 // can begin reading. We make use of the input_state_ when performing
114 // the connect operation in overlapped mode.
115 bool waiting_connect_;
117 // This flag is set when processing incoming messages. It is used to
118 // avoid recursing through ProcessIncomingMessages, which could cause
119 // problems. TODO(darin): make this unnecessary
120 bool processing_incoming_;
122 // Determines if we should validate a client's secret on connection.
123 bool validate_client_;
125 // Tracks the lifetime of this object, for debugging purposes.
126 uint32_t debug_flags_;
128 // This is a unique per-channel value used to authenticate the client end of
129 // a connection. If the value is non-zero, the client passes it in the hello
130 // and the host validates. (We don't send the zero value fto preserve IPC
131 // compatability with existing clients that don't validate the channel.)
132 int32_t client_secret_;
134 scoped_ptr<base::ThreadChecker> thread_check_;
136 // |broker_| must outlive this instance.
137 AttachmentBroker* broker_;
139 base::WeakPtrFactory<ChannelWin> weak_factory_;
140 DISALLOW_COPY_AND_ASSIGN(ChannelWin);
143 } // namespace IPC
145 #endif // IPC_IPC_CHANNEL_WIN_H_