[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #include "ipc/ipc_channel_win.h" |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 6 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | #include <windows.h> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | |
[email protected] | 5be7da24 | 2009-11-20 23:16:26 | [diff] [blame] | 11 | #include "base/auto_reset.h" |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "base/logging.h" |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 16 | #include "base/pickle.h" |
[email protected] | e66ef60 | 2013-07-24 05:15:24 | [diff] [blame] | 17 | #include "base/process/process_handle.h" |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 18 | #include "base/rand_util.h" |
[email protected] | 4aa794a1 | 2013-06-11 06:32:18 | [diff] [blame] | 19 | #include "base/strings/string_number_conversions.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 20 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 327e52b | 2012-06-25 21:11:36 | [diff] [blame] | 21 | #include "base/threading/thread_checker.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 22 | #include "base/win/scoped_handle.h" |
erikchen | 5708aae | 2015-09-14 17:45:12 | [diff] [blame] | 23 | #include "ipc/attachment_broker.h" |
[email protected] | 0ee99068 | 2012-11-17 19:20:05 | [diff] [blame] | 24 | #include "ipc/ipc_listener.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 25 | #include "ipc/ipc_logging.h" |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 26 | #include "ipc/ipc_message_attachment_set.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 27 | #include "ipc/ipc_message_utils.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | namespace IPC { |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 30 | |
fdoray | 324a072 | 2016-05-11 20:00:35 | [diff] [blame] | 31 | ChannelWin::State::State() = default; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 33 | ChannelWin::State::~State() { |
anujk.sharma | 5a7ffe2f | 2015-01-22 05:39:37 | [diff] [blame] | 34 | static_assert(offsetof(ChannelWin::State, context) == 0, |
| 35 | "ChannelWin::State should have context as its first data" |
| 36 | "member."); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | } |
| 38 | |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 39 | ChannelWin::ChannelWin(const IPC::ChannelHandle& channel_handle, |
| 40 | Mode mode, |
erikchen | 5708aae | 2015-09-14 17:45:12 | [diff] [blame] | 41 | Listener* listener) |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 42 | : ChannelReader(listener), |
[email protected] | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 43 | peer_pid_(base::kNullProcessId), |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 44 | waiting_connect_(mode & MODE_SERVER_FLAG), |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 45 | processing_incoming_(false), |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 46 | validate_client_(false), |
[email protected] | 65727eb | 2014-07-31 12:24:29 | [diff] [blame] | 47 | client_secret_(0), |
erikchen | f195382 | 2015-06-16 23:16:04 | [diff] [blame] | 48 | weak_factory_(this) { |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 49 | CreatePipe(channel_handle, mode); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 52 | ChannelWin::~ChannelWin() { |
erikchen | 53d919d | 2015-09-11 17:33:34 | [diff] [blame] | 53 | CleanUp(); |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 54 | Close(); |
| 55 | } |
| 56 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 57 | void ChannelWin::Close() { |
rvargas | cd742bcb | 2014-11-14 19:27:31 | [diff] [blame] | 58 | if (thread_check_.get()) |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 59 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 60 | |
[email protected] | 74f87acf2 | 2009-10-14 22:10:40 | [diff] [blame] | 61 | if (input_state_.is_pending || output_state_.is_pending) |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 62 | CancelIo(pipe_.Get()); |
[email protected] | ee78622d | 2008-10-13 21:25:50 | [diff] [blame] | 63 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 64 | // Closing the handle at this point prevents us from issuing more requests |
| 65 | // form OnIOCompleted(). |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 66 | if (pipe_.IsValid()) |
| 67 | pipe_.Close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 69 | // Make sure all IO has completed. |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 70 | while (input_state_.is_pending || output_state_.is_pending) { |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 71 | base::MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 72 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 73 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | while (!output_queue_.empty()) { |
erikchen | 5142dc7 | 2015-09-10 21:00:18 | [diff] [blame] | 75 | OutputElement* element = output_queue_.front(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | output_queue_.pop(); |
erikchen | 5142dc7 | 2015-09-10 21:00:18 | [diff] [blame] | 77 | delete element; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 81 | bool ChannelWin::Send(Message* message) { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 82 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 83 | DVLOG(2) << "sending message @" << message << " on channel @" << this |
| 84 | << " with type " << message->type() |
| 85 | << " (" << output_queue_.size() << " in queue)"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 87 | if (!prelim_queue_.empty()) { |
| 88 | prelim_queue_.push(message); |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | if (message->HasBrokerableAttachments() && |
| 93 | peer_pid_ == base::kNullProcessId) { |
| 94 | prelim_queue_.push(message); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | return ProcessMessageForDelivery(message); |
| 99 | } |
| 100 | |
| 101 | bool ChannelWin::ProcessMessageForDelivery(Message* message) { |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 102 | // Sending a brokerable attachment requires a call to Channel::Send(), so |
erikchen | 24e44d30 | 2015-10-21 22:28:54 | [diff] [blame] | 103 | // both Send() and ProcessMessageForDelivery() may be re-entrant. |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 104 | if (message->HasBrokerableAttachments()) { |
erikchen | 5708aae | 2015-09-14 17:45:12 | [diff] [blame] | 105 | DCHECK(GetAttachmentBroker()); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 106 | DCHECK(peer_pid_ != base::kNullProcessId); |
erikchen | a03dde6f | 2015-10-29 22:37:04 | [diff] [blame] | 107 | for (const scoped_refptr<IPC::BrokerableAttachment>& attachment : |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 108 | message->attachment_set()->GetBrokerableAttachments()) { |
erikchen | 5708aae | 2015-09-14 17:45:12 | [diff] [blame] | 109 | if (!GetAttachmentBroker()->SendAttachmentToProcess(attachment, |
| 110 | peer_pid_)) { |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 111 | delete message; |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 112 | return false; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 113 | } |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | #ifdef IPC_MESSAGE_LOG_ENABLED |
[email protected] | 8e8bb6d | 2010-12-13 08:18:55 | [diff] [blame] | 118 | Logging::GetInstance()->OnSendMessage(message, ""); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | #endif |
| 120 | |
yuhaoz | 9b8157d | 2015-08-18 22:21:35 | [diff] [blame] | 121 | TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), |
| 122 | "ChannelWin::ProcessMessageForDelivery", |
| 123 | message->flags(), |
| 124 | TRACE_EVENT_FLAG_FLOW_OUT); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 125 | |
| 126 | // |output_queue_| takes ownership of |message|. |
erikchen | 5142dc7 | 2015-09-10 21:00:18 | [diff] [blame] | 127 | OutputElement* element = new OutputElement(message); |
| 128 | output_queue_.push(element); |
| 129 | |
erikchen | 3b32506 | 2015-09-22 21:59:10 | [diff] [blame] | 130 | #if USE_ATTACHMENT_BROKER |
| 131 | if (message->HasBrokerableAttachments()) { |
| 132 | // |output_queue_| takes ownership of |ids.buffer|. |
| 133 | Message::SerializedAttachmentIds ids = |
| 134 | message->SerializedIdsOfBrokerableAttachments(); |
| 135 | output_queue_.push(new OutputElement(ids.buffer, ids.size)); |
| 136 | } |
| 137 | #endif |
erikchen | 5142dc7 | 2015-09-10 21:00:18 | [diff] [blame] | 138 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | // ensure waiting to write |
| 140 | if (!waiting_connect_) { |
| 141 | if (!output_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 142 | if (!ProcessOutgoingMessages(NULL, 0)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 150 | void ChannelWin::FlushPrelimQueue() { |
| 151 | DCHECK_NE(peer_pid_, base::kNullProcessId); |
| 152 | |
| 153 | // Due to the possibly re-entrant nature of ProcessMessageForDelivery(), it |
| 154 | // is critical that |prelim_queue_| appears empty. |
| 155 | std::queue<Message*> prelim_queue; |
| 156 | prelim_queue_.swap(prelim_queue); |
| 157 | |
| 158 | while (!prelim_queue.empty()) { |
| 159 | Message* m = prelim_queue.front(); |
erikchen | f81b10a | 2015-10-06 21:46:37 | [diff] [blame] | 160 | bool success = ProcessMessageForDelivery(m); |
| 161 | prelim_queue.pop(); |
| 162 | |
| 163 | if (!success) |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | // Delete any unprocessed messages. |
| 168 | while (!prelim_queue.empty()) { |
| 169 | Message* m = prelim_queue.front(); |
| 170 | delete m; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 171 | prelim_queue.pop(); |
| 172 | } |
| 173 | } |
| 174 | |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 175 | AttachmentBroker* ChannelWin::GetAttachmentBroker() { |
erikchen | 5708aae | 2015-09-14 17:45:12 | [diff] [blame] | 176 | return AttachmentBroker::GetGlobal(); |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 179 | base::ProcessId ChannelWin::GetPeerPID() const { |
| 180 | return peer_pid_; |
| 181 | } |
| 182 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 183 | base::ProcessId ChannelWin::GetSelfPID() const { |
| 184 | return GetCurrentProcessId(); |
| 185 | } |
| 186 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 187 | // static |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 188 | bool ChannelWin::IsNamedServerInitialized( |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 189 | const std::string& channel_id) { |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 190 | if (WaitNamedPipe(PipeName(channel_id, NULL).c_str(), 1)) |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 191 | return true; |
| 192 | // If ERROR_SEM_TIMEOUT occurred, the pipe exists but is handling another |
| 193 | // connection. |
| 194 | return GetLastError() == ERROR_SEM_TIMEOUT; |
| 195 | } |
| 196 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 197 | ChannelWin::ReadState ChannelWin::ReadData( |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 198 | char* buffer, |
| 199 | int buffer_len, |
| 200 | int* /* bytes_read */) { |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 201 | if (!pipe_.IsValid()) |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 202 | return READ_FAILED; |
| 203 | |
| 204 | DWORD bytes_read = 0; |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 205 | BOOL ok = ReadFile(pipe_.Get(), buffer, buffer_len, |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 206 | &bytes_read, &input_state_.context.overlapped); |
| 207 | if (!ok) { |
| 208 | DWORD err = GetLastError(); |
| 209 | if (err == ERROR_IO_PENDING) { |
| 210 | input_state_.is_pending = true; |
| 211 | return READ_PENDING; |
| 212 | } |
| 213 | LOG(ERROR) << "pipe error: " << err; |
| 214 | return READ_FAILED; |
| 215 | } |
| 216 | |
| 217 | // We could return READ_SUCCEEDED here. But the way that this code is |
| 218 | // structured we instead go back to the message loop. Our completion port |
| 219 | // will be signalled even in the "synchronously completed" state. |
| 220 | // |
| 221 | // This allows us to potentially process some outgoing messages and |
| 222 | // interleave other work on this thread when we're getting hammered with |
| 223 | // input messages. Potentially, this could be tuned to be more efficient |
| 224 | // with some testing. |
| 225 | input_state_.is_pending = true; |
| 226 | return READ_PENDING; |
| 227 | } |
| 228 | |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 229 | bool ChannelWin::ShouldDispatchInputMessage(Message* msg) { |
[email protected] | 0590878 | 2012-04-03 08:49:43 | [diff] [blame] | 230 | // Make sure we get a hello when client validation is required. |
| 231 | if (validate_client_) |
| 232 | return IsHelloMessage(*msg); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 236 | bool ChannelWin::GetNonBrokeredAttachments(Message* msg) { |
| 237 | return true; |
| 238 | } |
| 239 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 240 | void ChannelWin::HandleInternalMessage(const Message& msg) { |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 241 | DCHECK_EQ(msg.type(), static_cast<unsigned>(Channel::HELLO_MESSAGE_TYPE)); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 242 | // The hello message contains one parameter containing the PID. |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 243 | base::PickleIterator it(msg); |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 244 | int32_t claimed_pid; |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 245 | bool failed = !it.ReadInt(&claimed_pid); |
| 246 | |
| 247 | if (!failed && validate_client_) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 248 | int32_t secret; |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 249 | failed = it.ReadInt(&secret) ? (secret != client_secret_) : true; |
| 250 | } |
| 251 | |
| 252 | if (failed) { |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 253 | NOTREACHED(); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 254 | Close(); |
| 255 | listener()->OnChannelError(); |
| 256 | return; |
| 257 | } |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 258 | |
[email protected] | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 259 | peer_pid_ = claimed_pid; |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 260 | // Validation completed. |
[email protected] | 0590878 | 2012-04-03 08:49:43 | [diff] [blame] | 261 | validate_client_ = false; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 262 | |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 263 | listener()->OnChannelConnected(claimed_pid); |
erikchen | f81b10a | 2015-10-06 21:46:37 | [diff] [blame] | 264 | |
| 265 | FlushPrelimQueue(); |
erikchen | 3e0f4d3 | 2016-05-16 18:47:54 | [diff] [blame] | 266 | |
| 267 | if (IsAttachmentBrokerEndpoint() && |
| 268 | AttachmentBroker::GetGlobal()->IsPrivilegedBroker()) { |
| 269 | AttachmentBroker::GetGlobal()->ReceivedPeerPid(claimed_pid); |
| 270 | } |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 271 | } |
| 272 | |
erikchen | 3c175a3 | 2015-07-28 23:16:48 | [diff] [blame] | 273 | base::ProcessId ChannelWin::GetSenderPID() { |
| 274 | return GetPeerPID(); |
| 275 | } |
| 276 | |
erikchen | 8c73f83 | 2015-07-30 22:26:08 | [diff] [blame] | 277 | bool ChannelWin::IsAttachmentBrokerEndpoint() { |
| 278 | return is_attachment_broker_endpoint(); |
| 279 | } |
| 280 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 281 | bool ChannelWin::DidEmptyInputBuffers() { |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 282 | // We don't need to do anything here. |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 283 | return true; |
| 284 | } |
| 285 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 286 | // static |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 287 | const base::string16 ChannelWin::PipeName(const std::string& channel_id, |
| 288 | int32_t* secret) { |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 289 | std::string name("\\\\.\\pipe\\chrome."); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 290 | |
| 291 | // Prevent the shared secret from ending up in the pipe name. |
| 292 | size_t index = channel_id.find_first_of('\\'); |
| 293 | if (index != std::string::npos) { |
| 294 | if (secret) // Retrieve the secret if asked for. |
| 295 | base::StringToInt(channel_id.substr(index + 1), secret); |
thestig | e5c64d9 | 2014-11-07 01:19:24 | [diff] [blame] | 296 | return base::ASCIIToUTF16(name.append(channel_id.substr(0, index - 1))); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // This case is here to support predictable named pipes in tests. |
| 300 | if (secret) |
| 301 | *secret = 0; |
thestig | e5c64d9 | 2014-11-07 01:19:24 | [diff] [blame] | 302 | return base::ASCIIToUTF16(name.append(channel_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | } |
| 304 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 305 | bool ChannelWin::CreatePipe(const IPC::ChannelHandle &channel_handle, |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 306 | Mode mode) { |
| 307 | DCHECK(!pipe_.IsValid()); |
[email protected] | 905cda81 | 2013-12-20 09:04:55 | [diff] [blame] | 308 | base::string16 pipe_name; |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 309 | // If we already have a valid pipe for channel just copy it. |
| 310 | if (channel_handle.pipe.handle) { |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 311 | // TODO(rvargas) crbug.com/415294: ChannelHandle should either go away in |
| 312 | // favor of two independent entities (name/file), or it should be a move- |
| 313 | // only type with a base::File member. In any case, this code should not |
| 314 | // call DuplicateHandle. |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 315 | DCHECK(channel_handle.name.empty()); |
| 316 | pipe_name = L"Not Available"; // Just used for LOG |
| 317 | // Check that the given pipe confirms to the specified mode. We can |
| 318 | // only check for PIPE_TYPE_MESSAGE & PIPE_SERVER_END flags since the |
| 319 | // other flags (PIPE_TYPE_BYTE, and PIPE_CLIENT_END) are defined as 0. |
| 320 | DWORD flags = 0; |
| 321 | GetNamedPipeInfo(channel_handle.pipe.handle, &flags, NULL, NULL, NULL); |
| 322 | DCHECK(!(flags & PIPE_TYPE_MESSAGE)); |
| 323 | if (((mode & MODE_SERVER_FLAG) && !(flags & PIPE_SERVER_END)) || |
| 324 | ((mode & MODE_CLIENT_FLAG) && (flags & PIPE_SERVER_END))) { |
| 325 | LOG(WARNING) << "Inconsistent open mode. Mode :" << mode; |
| 326 | return false; |
| 327 | } |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 328 | HANDLE local_handle; |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 329 | if (!DuplicateHandle(GetCurrentProcess(), |
| 330 | channel_handle.pipe.handle, |
| 331 | GetCurrentProcess(), |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 332 | &local_handle, |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 333 | 0, |
| 334 | FALSE, |
| 335 | DUPLICATE_SAME_ACCESS)) { |
| 336 | LOG(WARNING) << "DuplicateHandle failed. Error :" << GetLastError(); |
| 337 | return false; |
| 338 | } |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 339 | pipe_.Set(local_handle); |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 340 | } else if (mode & MODE_SERVER_FLAG) { |
| 341 | DCHECK(!channel_handle.pipe.handle); |
| 342 | const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | |
| 343 | FILE_FLAG_FIRST_PIPE_INSTANCE; |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 344 | pipe_name = PipeName(channel_handle.name, &client_secret_); |
| 345 | validate_client_ = !!client_secret_; |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 346 | pipe_.Set(CreateNamedPipeW(pipe_name.c_str(), |
| 347 | open_mode, |
| 348 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
| 349 | 1, |
| 350 | Channel::kReadBufferSize, |
| 351 | Channel::kReadBufferSize, |
| 352 | 5000, |
| 353 | NULL)); |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 354 | } else if (mode & MODE_CLIENT_FLAG) { |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 355 | DCHECK(!channel_handle.pipe.handle); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 356 | pipe_name = PipeName(channel_handle.name, &client_secret_); |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 357 | pipe_.Set(CreateFileW(pipe_name.c_str(), |
| 358 | GENERIC_READ | GENERIC_WRITE, |
| 359 | 0, |
| 360 | NULL, |
| 361 | OPEN_EXISTING, |
forshaw | 4a94dff | 2014-12-16 21:32:53 | [diff] [blame] | 362 | SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 363 | FILE_FLAG_OVERLAPPED, |
| 364 | NULL)); |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 365 | } else { |
| 366 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | } |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 368 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 369 | if (!pipe_.IsValid()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 370 | // If this process is being closed, the pipe may be gone already. |
[email protected] | 8398bf1 | 2014-06-26 21:11:34 | [diff] [blame] | 371 | PLOG(WARNING) << "Unable to create pipe \"" << pipe_name << "\" in " |
| 372 | << (mode & MODE_SERVER_FLAG ? "server" : "client") << " mode"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | return false; |
| 374 | } |
| 375 | |
| 376 | // Create the Hello message to be sent when Connect is called |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 377 | std::unique_ptr<Message> m(new Message(MSG_ROUTING_NONE, HELLO_MESSAGE_TYPE, |
| 378 | IPC::Message::PRIORITY_NORMAL)); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 379 | |
| 380 | // Don't send the secret to the untrusted process, and don't send a secret |
| 381 | // if the value is zero (for IPC backwards compatability). |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 382 | int32_t secret = validate_client_ ? 0 : client_secret_; |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 383 | if (!m->WriteInt(GetCurrentProcessId()) || |
| 384 | (secret && !m->WriteUInt32(secret))) { |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 385 | pipe_.Close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | |
erikchen | 5142dc7 | 2015-09-10 21:00:18 | [diff] [blame] | 389 | OutputElement* element = new OutputElement(m.release()); |
| 390 | output_queue_.push(element); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 391 | return true; |
| 392 | } |
| 393 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 394 | bool ChannelWin::Connect() { |
erikchen | 9097190 | 2016-04-25 23:45:31 | [diff] [blame] | 395 | WillConnect(); |
| 396 | |
[email protected] | 1934072 | 2009-08-17 19:53:25 | [diff] [blame] | 397 | DLOG_IF(WARNING, thread_check_.get()) << "Connect called more than once"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 398 | |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 399 | if (!thread_check_.get()) |
[email protected] | 327e52b | 2012-06-25 21:11:36 | [diff] [blame] | 400 | thread_check_.reset(new base::ThreadChecker()); |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 401 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 402 | if (!pipe_.IsValid()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 403 | return false; |
| 404 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 405 | base::MessageLoopForIO::current()->RegisterIOHandler(pipe_.Get(), this); |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 406 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | // Check to see if there is a client connected to our pipe... |
| 408 | if (waiting_connect_) |
| 409 | ProcessConnection(); |
| 410 | |
| 411 | if (!input_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 412 | // Complete setup asynchronously. By not setting input_state_.is_pending |
| 413 | // to true, we indicate to OnIOCompleted that this is the special |
| 414 | // initialization signal. |
fdoray | 471891b | 2016-06-06 22:20:09 | [diff] [blame] | 415 | base::MessageLoopForIO::current()->PostTask( |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 416 | FROM_HERE, |
fdoray | 471891b | 2016-06-06 22:20:09 | [diff] [blame] | 417 | base::Bind(&ChannelWin::OnIOCompleted, |
| 418 | weak_factory_.GetWeakPtr(), |
| 419 | &input_state_.context, |
| 420 | 0, |
| 421 | 0)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | if (!waiting_connect_) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 425 | ProcessOutgoingMessages(NULL, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 426 | return true; |
| 427 | } |
| 428 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 429 | bool ChannelWin::ProcessConnection() { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 430 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 431 | if (input_state_.is_pending) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 432 | input_state_.is_pending = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 433 | |
| 434 | // Do we have a client connected to our pipe? |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 435 | if (!pipe_.IsValid()) |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 436 | return false; |
| 437 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 438 | BOOL ok = ConnectNamedPipe(pipe_.Get(), &input_state_.context.overlapped); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 439 | DWORD err = GetLastError(); |
| 440 | if (ok) { |
| 441 | // Uhm, the API documentation says that this function should never |
| 442 | // return success when used in overlapped mode. |
| 443 | NOTREACHED(); |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | switch (err) { |
| 448 | case ERROR_IO_PENDING: |
| 449 | input_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | break; |
| 451 | case ERROR_PIPE_CONNECTED: |
| 452 | waiting_connect_ = false; |
| 453 | break; |
[email protected] | 20aa32c | 2009-07-14 22:25:49 | [diff] [blame] | 454 | case ERROR_NO_DATA: |
| 455 | // The pipe is being closed. |
| 456 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 457 | default: |
| 458 | NOTREACHED(); |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | return true; |
| 463 | } |
| 464 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 465 | bool ChannelWin::ProcessOutgoingMessages( |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 466 | base::MessageLoopForIO::IOContext* context, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 467 | DWORD bytes_written) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 468 | DCHECK(!waiting_connect_); // Why are we trying to send messages if there's |
| 469 | // no connection? |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 470 | DCHECK(thread_check_->CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 471 | |
| 472 | if (output_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 473 | DCHECK(context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 474 | output_state_.is_pending = false; |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 475 | if (!context || bytes_written == 0) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 476 | DWORD err = GetLastError(); |
| 477 | LOG(ERROR) << "pipe error: " << err; |
| 478 | return false; |
| 479 | } |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 480 | // Message was sent. |
[email protected] | 88ecb5f5 | 2014-01-21 14:29:36 | [diff] [blame] | 481 | CHECK(!output_queue_.empty()); |
erikchen | 5142dc7 | 2015-09-10 21:00:18 | [diff] [blame] | 482 | OutputElement* element = output_queue_.front(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 483 | output_queue_.pop(); |
erikchen | 5142dc7 | 2015-09-10 21:00:18 | [diff] [blame] | 484 | delete element; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 485 | } |
| 486 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 487 | if (output_queue_.empty()) |
| 488 | return true; |
| 489 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 490 | if (!pipe_.IsValid()) |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 491 | return false; |
| 492 | |
| 493 | // Write to pipe... |
erikchen | 5142dc7 | 2015-09-10 21:00:18 | [diff] [blame] | 494 | OutputElement* element = output_queue_.front(); |
| 495 | DCHECK(element->size() <= INT_MAX); |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame] | 496 | BOOL ok = WriteFile(pipe_.Get(), |
erikchen | 5142dc7 | 2015-09-10 21:00:18 | [diff] [blame] | 497 | element->data(), |
| 498 | static_cast<uint32_t>(element->size()), |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame] | 499 | NULL, |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 500 | &output_state_.context.overlapped); |
| 501 | if (!ok) { |
rvargas | cd742bcb | 2014-11-14 19:27:31 | [diff] [blame] | 502 | DWORD write_error = GetLastError(); |
| 503 | if (write_error == ERROR_IO_PENDING) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 504 | output_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 505 | |
erikchen | 5142dc7 | 2015-09-10 21:00:18 | [diff] [blame] | 506 | const Message* m = element->get_message(); |
| 507 | if (m) { |
| 508 | DVLOG(2) << "sent pending message @" << m << " on channel @" << this |
| 509 | << " with type " << m->type(); |
| 510 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 511 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 512 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 513 | } |
rvargas | cd742bcb | 2014-11-14 19:27:31 | [diff] [blame] | 514 | LOG(ERROR) << "pipe error: " << write_error; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 515 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 516 | } |
| 517 | |
erikchen | 5142dc7 | 2015-09-10 21:00:18 | [diff] [blame] | 518 | const Message* m = element->get_message(); |
| 519 | if (m) { |
| 520 | DVLOG(2) << "sent message @" << m << " on channel @" << this |
| 521 | << " with type " << m->type(); |
| 522 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 523 | |
| 524 | output_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 525 | return true; |
| 526 | } |
| 527 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 528 | void ChannelWin::OnIOCompleted( |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 529 | base::MessageLoopForIO::IOContext* context, |
| 530 | DWORD bytes_transfered, |
| 531 | DWORD error) { |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 532 | bool ok = true; |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 533 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 534 | if (context == &input_state_.context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 535 | if (waiting_connect_) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 536 | if (!ProcessConnection()) |
| 537 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 538 | // We may have some messages queued up to send... |
| 539 | if (!output_queue_.empty() && !output_state_.is_pending) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 540 | ProcessOutgoingMessages(NULL, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 541 | if (input_state_.is_pending) |
| 542 | return; |
| 543 | // else, fall-through and look for incoming messages... |
| 544 | } |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 545 | |
| 546 | // We don't support recursion through OnMessageReceived yet! |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 547 | DCHECK(!processing_incoming_); |
[email protected] | 997ec9f | 2012-11-21 04:44:14 | [diff] [blame] | 548 | base::AutoReset<bool> auto_reset_processing_incoming( |
| 549 | &processing_incoming_, true); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 550 | |
| 551 | // Process the new data. |
| 552 | if (input_state_.is_pending) { |
| 553 | // This is the normal case for everything except the initialization step. |
| 554 | input_state_.is_pending = false; |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 555 | if (!bytes_transfered) { |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 556 | ok = false; |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 557 | } else if (pipe_.IsValid()) { |
| 558 | ok = (AsyncReadComplete(bytes_transfered) != DISPATCH_ERROR); |
| 559 | } |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 560 | } else { |
| 561 | DCHECK(!bytes_transfered); |
| 562 | } |
| 563 | |
| 564 | // Request more data. |
| 565 | if (ok) |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 566 | ok = (ProcessIncomingMessages() != DISPATCH_ERROR); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 567 | } else { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 568 | DCHECK(context == &output_state_.context); |
[email protected] | 65727eb | 2014-07-31 12:24:29 | [diff] [blame] | 569 | CHECK(output_state_.is_pending); |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 570 | ok = ProcessOutgoingMessages(context, bytes_transfered); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 571 | } |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 572 | if (!ok && pipe_.IsValid()) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 573 | // We don't want to re-enter Close(). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 574 | Close(); |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 575 | listener()->OnChannelError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 579 | //------------------------------------------------------------------------------ |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 580 | // Channel's methods |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 581 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 582 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 583 | std::unique_ptr<Channel> Channel::Create( |
| 584 | const IPC::ChannelHandle& channel_handle, |
| 585 | Mode mode, |
| 586 | Listener* listener) { |
| 587 | return base::WrapUnique(new ChannelWin(channel_handle, mode, listener)); |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 588 | } |
| 589 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 590 | // static |
| 591 | bool Channel::IsNamedServerInitialized(const std::string& channel_id) { |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 592 | return ChannelWin::IsNamedServerInitialized(channel_id); |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 593 | } |
| 594 | |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 595 | // static |
| 596 | std::string Channel::GenerateVerifiedChannelID(const std::string& prefix) { |
| 597 | // Windows pipes can be enumerated by low-privileged processes. So, we |
| 598 | // append a strong random value after the \ character. This value is not |
| 599 | // included in the pipe name, but sent as part of the client hello, to |
| 600 | // hijacking the pipe name to spoof the client. |
| 601 | |
| 602 | std::string id = prefix; |
| 603 | if (!id.empty()) |
| 604 | id.append("."); |
| 605 | |
| 606 | int secret; |
| 607 | do { // Guarantee we get a non-zero value. |
| 608 | secret = base::RandInt(0, std::numeric_limits<int>::max()); |
| 609 | } while (secret == 0); |
| 610 | |
| 611 | id.append(GenerateUniqueRandomChannelID()); |
| 612 | return id.append(base::StringPrintf("\\%d", secret)); |
| 613 | } |
| 614 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 615 | } // namespace IPC |