OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef IPC_IPC_CHANNEL_WIN_H_ | |
6 #define IPC_IPC_CHANNEL_WIN_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <queue> | |
12 #include <string> | |
13 | |
14 #include "base/macros.h" | |
15 #include "base/memory/weak_ptr.h" | |
16 #include "base/message_loop/message_loop.h" | |
17 #include "base/win/scoped_handle.h" | |
18 #include "ipc/ipc_channel.h" | |
19 #include "ipc/ipc_channel_reader.h" | |
20 | |
21 namespace base { | |
22 class ThreadChecker; | |
23 } | |
24 | |
25 namespace IPC { | |
26 | |
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 ~ChannelWin() override; | |
37 | |
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; | |
45 | |
46 static bool IsNamedServerInitialized(const std::string& channel_id); | |
47 | |
48 | |
49 private: | |
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; | |
58 | |
59 static const base::string16 PipeName(const std::string& channel_id, | |
60 int32_t* secret); | |
61 bool CreatePipe(const IPC::ChannelHandle &channel_handle, Mode mode); | |
62 | |
63 bool ProcessConnection(); | |
64 bool ProcessOutgoingMessages(base::MessageLoopForIO::IOContext* context, | |
65 DWORD bytes_written); | |
66 | |
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 | |
70 // be re-entrant. | |
71 // Adds |message| to |output_queue_| and calls ProcessOutgoingMessages(). | |
72 bool ProcessMessageForDelivery(Message* message); | |
73 | |
74 // Moves all messages from |prelim_queue_| to |output_queue_| by calling | |
75 // ProcessMessageForDelivery(). | |
76 void FlushPrelimQueue(); | |
77 | |
78 // MessageLoop::IOHandler implementation. | |
79 void OnIOCompleted(base::MessageLoopForIO::IOContext* context, | |
80 DWORD bytes_transfered, | |
81 DWORD error) override; | |
82 | |
83 private: | |
84 struct State { | |
85 State(); | |
86 ~State(); | |
87 base::MessageLoopForIO::IOContext context; | |
88 bool is_pending = false; | |
89 }; | |
90 | |
91 State input_state_; | |
92 State output_state_; | |
93 | |
94 base::win::ScopedHandle pipe_; | |
95 | |
96 base::ProcessId peer_pid_; | |
97 | |
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 | |
103 // configured. | |
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_; | |
107 | |
108 // Messages to be sent are queued here. | |
109 std::queue<OutputElement*> output_queue_; | |
110 | |
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_; | |
115 | |
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_; | |
120 | |
121 // Determines if we should validate a client's secret on connection. | |
122 bool validate_client_; | |
123 | |
124 // This is a unique per-channel value used to authenticate the client end of | |
125 // a connection. If the value is non-zero, the client passes it in the hello | |
126 // and the host validates. (We don't send the zero value fto preserve IPC | |
127 // compatability with existing clients that don't validate the channel.) | |
128 int32_t client_secret_; | |
129 | |
130 std::unique_ptr<base::ThreadChecker> thread_check_; | |
131 | |
132 base::WeakPtrFactory<ChannelWin> weak_factory_; | |
133 DISALLOW_COPY_AND_ASSIGN(ChannelWin); | |
134 }; | |
135 | |
136 } // namespace IPC | |
137 | |
138 #endif // IPC_IPC_CHANNEL_WIN_H_ | |
OLD | NEW |