Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ipc / ipc_channel_win.h
blobd90529718204f79e99ff6305cfea0a81e723206d
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 <queue>
11 #include <string>
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/win/scoped_handle.h"
17 #include "ipc/ipc_channel_reader.h"
19 namespace base {
20 class ThreadChecker;
23 namespace IPC {
25 class ChannelWin : public Channel,
26 public internal::ChannelReader,
27 public base::MessageLoopForIO::IOHandler {
28 public:
29 // Mirror methods of Channel, see ipc_channel.h for description.
30 // |broker| must outlive the newly created object.
31 ChannelWin(const IPC::ChannelHandle& channel_handle,
32 Mode mode,
33 Listener* listener,
34 AttachmentBroker* broker);
35 ~ChannelWin() override;
37 // Channel implementation
38 bool Connect() override;
39 void Close() override;
40 bool Send(Message* message) override;
41 AttachmentBroker* GetAttachmentBroker() override;
42 base::ProcessId GetPeerPID() const override;
43 base::ProcessId GetSelfPID() const override;
45 static bool IsNamedServerInitialized(const std::string& channel_id);
48 private:
49 // ChannelReader implementation.
50 ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) override;
51 bool ShouldDispatchInputMessage(Message* msg) override;
52 bool GetNonBrokeredAttachments(Message* msg) override;
53 bool DidEmptyInputBuffers() override;
54 void HandleInternalMessage(const Message& msg) override;
55 base::ProcessId GetSenderPID() override;
56 bool IsAttachmentBrokerEndpoint() override;
58 static const base::string16 PipeName(const std::string& channel_id,
59 int32* secret);
60 bool CreatePipe(const IPC::ChannelHandle &channel_handle, Mode mode);
62 bool ProcessConnection();
63 bool ProcessOutgoingMessages(base::MessageLoopForIO::IOContext* context,
64 DWORD bytes_written);
66 // Returns |false| on channel error.
67 // If |message| has brokerable attachments, those attachments are passed to
68 // the AttachmentBroker (which in turn invokes Send()), so this method must
69 // be re-entrant.
70 // Adds |message| to |output_queue_| and calls ProcessOutgoingMessages().
71 bool ProcessMessageForDelivery(Message* message);
73 // Moves all messages from |prelim_queue_| to |output_queue_| by calling
74 // ProcessMessageForDelivery().
75 void FlushPrelimQueue();
77 // MessageLoop::IOHandler implementation.
78 void OnIOCompleted(base::MessageLoopForIO::IOContext* context,
79 DWORD bytes_transfered,
80 DWORD error) override;
82 private:
83 struct State {
84 explicit State(ChannelWin* channel);
85 ~State();
86 base::MessageLoopForIO::IOContext context;
87 bool is_pending;
90 State input_state_;
91 State output_state_;
93 base::win::ScopedHandle pipe_;
95 base::ProcessId peer_pid_;
97 // Messages not yet ready to be sent are queued here. Messages removed from
98 // this queue are placed in the output_queue_. The double queue is
99 // unfortunate, but is necessary because messages with brokerable attachments
100 // can generate multiple messages to be sent (possibly from other channels).
101 // Some of these generated messages cannot be sent until |peer_pid_| has been
102 // configured.
103 // As soon as |peer_pid| has been configured, there is no longer any need for
104 // |prelim_queue_|. All messages are flushed, and no new messages are added.
105 std::queue<Message*> prelim_queue_;
107 // Messages to be sent are queued here.
108 std::queue<Message*> output_queue_;
110 // In server-mode, we have to wait for the client to connect before we
111 // can begin reading. We make use of the input_state_ when performing
112 // the connect operation in overlapped mode.
113 bool waiting_connect_;
115 // This flag is set when processing incoming messages. It is used to
116 // avoid recursing through ProcessIncomingMessages, which could cause
117 // problems. TODO(darin): make this unnecessary
118 bool processing_incoming_;
120 // Determines if we should validate a client's secret on connection.
121 bool validate_client_;
123 // Tracks the lifetime of this object, for debugging purposes.
124 uint32 debug_flags_;
126 // This is a unique per-channel value used to authenticate the client end of
127 // a connection. If the value is non-zero, the client passes it in the hello
128 // and the host validates. (We don't send the zero value fto preserve IPC
129 // compatability with existing clients that don't validate the channel.)
130 int32 client_secret_;
132 scoped_ptr<base::ThreadChecker> thread_check_;
134 // |broker_| must outlive this instance.
135 AttachmentBroker* broker_;
137 base::WeakPtrFactory<ChannelWin> weak_factory_;
138 DISALLOW_COPY_AND_ASSIGN(ChannelWin);
141 } // namespace IPC
143 #endif // IPC_IPC_CHANNEL_WIN_H_