1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <windows.h>
#include <sstream>
#include "chrome/common/chrome_counters.h"
#include "chrome/common/ipc_channel.h"
#include "base/logging.h"
#include "base/win_util.h"
#include "chrome/common/ipc_logging.h"
#include "chrome/common/ipc_message_utils.h"
using namespace std;
namespace IPC {
//------------------------------------------------------------------------------
Channel::State::State()
: is_pending(false) {
memset(&overlapped, 0, sizeof(overlapped));
overlapped.hEvent = CreateEvent(NULL, // default security attributes
TRUE, // manual-reset event
TRUE, // initial state = signaled
NULL); // unnamed event object
}
Channel::State::~State() {
if (overlapped.hEvent)
CloseHandle(overlapped.hEvent);
}
//------------------------------------------------------------------------------
Channel::Channel(const wstring& channel_id, Mode mode, Listener* listener)
: pipe_(INVALID_HANDLE_VALUE),
listener_(listener),
waiting_connect_(mode == MODE_SERVER),
processing_incoming_(false) {
if (!CreatePipe(channel_id, mode)) {
// The pipe may have been closed already.
LOG(WARNING) << "Unable to create pipe named \"" << channel_id <<
"\" in " << (mode == 0 ? "server" : "client") << " mode.";
}
}
void Channel::Close() {
// make sure we are no longer watching the pipe events
MessageLoop::current()->WatchObject(input_state_.overlapped.hEvent, NULL);
MessageLoop::current()->WatchObject(output_state_.overlapped.hEvent, NULL);
if (pipe_ != INVALID_HANDLE_VALUE) {
CloseHandle(pipe_);
pipe_ = INVALID_HANDLE_VALUE;
}
while (!output_queue_.empty()) {
Message* m = output_queue_.front();
output_queue_.pop();
delete m;
}
}
bool Channel::Send(Message* message) {
chrome::Counters::ipc_send_counter().Increment();
#ifdef IPC_MESSAGE_DEBUG_EXTRA
DLOG(INFO) << "sending message @" << message << " on channel @" << this
<< " with type " << message->type()
<< " (" << output_queue_.size() << " in queue)";
#endif
#ifdef IPC_MESSAGE_LOG_ENABLED
Logging::current()->OnSendMessage(message, L"");
#endif
output_queue_.push(message);
// ensure waiting to write
if (!waiting_connect_) {
if (!output_state_.is_pending) {
if (!ProcessOutgoingMessages())
return false;
} else if (WaitForSingleObject(output_state_.overlapped.hEvent, 0) ==
WAIT_OBJECT_0) {
OnObjectSignaled(output_state_.overlapped.hEvent);
}
}
return true;
}
const wstring Channel::PipeName(const wstring& channel_id) const {
wostringstream ss;
// XXX(darin): get application name from somewhere else
ss << L"\\\\.\\pipe\\chrome." << channel_id;
return ss.str();
}
bool Channel::CreatePipe(const wstring& channel_id, Mode mode) {
DCHECK(pipe_ == INVALID_HANDLE_VALUE);
const wstring pipe_name = PipeName(channel_id);
if (mode == MODE_SERVER) {
SECURITY_ATTRIBUTES security_attributes = {0};
security_attributes.bInheritHandle = FALSE;
security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
if (!win_util::GetLogonSessionOnlyDACL(
reinterpret_cast<SECURITY_DESCRIPTOR**>(
&security_attributes.lpSecurityDescriptor))) {
NOTREACHED();
}
pipe_ = CreateNamedPipeW(pipe_name.c_str(),
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED |
FILE_FLAG_FIRST_PIPE_INSTANCE,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
1, // number of pipe instances
BUF_SIZE, // output buffer size (XXX tune)
BUF_SIZE, // input buffer size (XXX tune)
5000, // timeout in milliseconds (XXX tune)
&security_attributes);
LocalFree(security_attributes.lpSecurityDescriptor);
} else {
pipe_ = CreateFileW(pipe_name.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION |
FILE_FLAG_OVERLAPPED,
NULL);
}
if (pipe_ == INVALID_HANDLE_VALUE) {
// If this process is being closed, the pipe may be gone already.
LOG(WARNING) << "failed to create pipe: " << GetLastError();
return false;
}
// Create the Hello message to be sent when Connect is called
scoped_ptr<Message> m(new Message(MSG_ROUTING_NONE,
HELLO_MESSAGE_TYPE,
IPC::Message::PRIORITY_NORMAL));
if (!m->WriteInt(GetCurrentProcessId())) {
CloseHandle(pipe_);
pipe_ = INVALID_HANDLE_VALUE;
return false;
}
output_queue_.push(m.release());
return true;
}
bool Channel::Connect() {
DLOG(WARNING) << "Connect called twice";
if (pipe_ == INVALID_HANDLE_VALUE)
return false;
// Check to see if there is a client connected to our pipe...
if (waiting_connect_)
ProcessConnection();
if (!input_state_.is_pending) {
// complete setup asynchronously. to do that, we force the input state
// to be signaled, which will cause us to be called back from the event
// queue. by not setting input_state_.is_pending to true, we indicate
// to OnObjectSignaled that this is the special initialization signal.
SetEvent(input_state_.overlapped.hEvent);
MessageLoop::current()->WatchObject(input_state_.overlapped.hEvent, this);
}
if (!waiting_connect_)
ProcessOutgoingMessages();
return true;
}
bool Channel::ProcessConnection() {
input_state_.is_pending = false;
MessageLoop::current()->WatchObject(input_state_.overlapped.hEvent, NULL);
// Do we have a client connected to our pipe?
DCHECK(pipe_ != INVALID_HANDLE_VALUE);
BOOL ok = ConnectNamedPipe(pipe_, &input_state_.overlapped);
DWORD err = GetLastError();
if (ok) {
// Uhm, the API documentation says that this function should never
// return success when used in overlapped mode.
NOTREACHED();
return false;
}
switch (err) {
case ERROR_IO_PENDING:
input_state_.is_pending = true;
MessageLoop::current()->WatchObject(input_state_.overlapped.hEvent, this);
break;
case ERROR_PIPE_CONNECTED:
waiting_connect_ = false;
break;
default:
NOTREACHED();
return false;
}
return true;
}
bool Channel::ProcessIncomingMessages() {
DWORD bytes_read = 0;
MessageLoop::current()->WatchObject(input_state_.overlapped.hEvent, NULL);
if (input_state_.is_pending) {
input_state_.is_pending = false;
BOOL ok = GetOverlappedResult(pipe_,
&input_state_.overlapped,
&bytes_read,
FALSE);
if (!ok || bytes_read == 0) {
DWORD err = GetLastError();
// TODO(pkasting): http://b/119851 We can get ERR_BROKEN_PIPE here if the
// renderer crashes. We should handle this cleanly.
LOG(ERROR) << "pipe error: " << err;
return false;
}
} else {
// this happens at channel initialization
ResetEvent(input_state_.overlapped.hEvent);
}
for (;;) {
if (bytes_read == 0) {
// read from pipe...
BOOL ok = ReadFile(pipe_,
input_buf_,
BUF_SIZE,
&bytes_read,
&input_state_.overlapped);
if (!ok) {
DWORD err = GetLastError();
if (err == ERROR_IO_PENDING) {
MessageLoop::current()->WatchObject(input_state_.overlapped.hEvent,
this);
input_state_.is_pending = true;
return true;
}
LOG(ERROR) << "pipe error: " << err;
return false;
}
}
DCHECK(bytes_read);
// process messages from input buffer
const char* p, *end;
if (input_overflow_buf_.empty()) {
p = input_buf_;
end = p + bytes_read;
} else {
if (input_overflow_buf_.size() > (kMaximumMessageSize - bytes_read)) {
input_overflow_buf_.clear();
LOG(ERROR) << "IPC message is too big";
return false;
}
input_overflow_buf_.append(input_buf_, bytes_read);
p = input_overflow_buf_.data();
end = p + input_overflow_buf_.size();
}
while (p < end) {
const char* message_tail = Message::FindNext(p, end);
if (message_tail) {
int len = static_cast<int>(message_tail - p);
const Message m(p, len);
#ifdef IPC_MESSAGE_DEBUG_EXTRA
DLOG(INFO) << "received message on channel @" << this << " with type " <<
m.type();
#endif
if (m.routing_id() == MSG_ROUTING_NONE &&
m.type() == HELLO_MESSAGE_TYPE) {
// The Hello message contains only the process id.
listener_->OnChannelConnected(MessageIterator(m).NextInt());
} else {
listener_->OnMessageReceived(m);
}
p = message_tail;
} else {
// last message is partial
break;
}
}
input_overflow_buf_.assign(p, end - p);
bytes_read = 0; // get more data
}
return true;
}
bool Channel::ProcessOutgoingMessages() {
DCHECK(!waiting_connect_); // Why are we trying to send messages if there's
// no connection?
DWORD bytes_written;
if (output_state_.is_pending) {
MessageLoop::current()->WatchObject(output_state_.overlapped.hEvent, NULL);
output_state_.is_pending = false;
BOOL ok = GetOverlappedResult(pipe_,
&output_state_.overlapped,
&bytes_written,
FALSE);
if (!ok || bytes_written == 0) {
DWORD err = GetLastError();
LOG(ERROR) << "pipe error: " << err;
return false;
}
// message was sent
DCHECK(!output_queue_.empty());
Message* m = output_queue_.front();
output_queue_.pop();
delete m;
}
while (!output_queue_.empty()) {
// write to pipe...
Message* m = output_queue_.front();
BOOL ok = WriteFile(pipe_,
m->data(),
m->size(),
&bytes_written,
&output_state_.overlapped);
if (!ok) {
DWORD err = GetLastError();
if (err == ERROR_IO_PENDING) {
MessageLoop::current()->WatchObject(output_state_.overlapped.hEvent,
this);
output_state_.is_pending = true;
#ifdef IPC_MESSAGE_DEBUG_EXTRA
DLOG(INFO) << "sent pending message @" << m << " on channel @" << this <<
" with type " << m->type();
#endif
return true;
}
LOG(ERROR) << "pipe error: " << err;
return false;
}
DCHECK(bytes_written == m->size());
output_queue_.pop();
#ifdef IPC_MESSAGE_DEBUG_EXTRA
DLOG(INFO) << "sent message @" << m << " on channel @" << this <<
" with type " << m->type();
#endif
delete m;
}
return true;
}
bool Channel::ProcessPendingMessages(DWORD max_wait_msec) {
return false;
// TODO(darin): this code is broken and leads to busy waiting
#if 0
DCHECK(max_wait_msec <= 0x7FFFFFFF || max_wait_msec == INFINITE);
HANDLE events[] = {
input_state_.overlapped.hEvent,
output_state_.overlapped.hEvent
};
// Only deal with output messages if we have a connection on which to send
const int wait_count = waiting_connect_ ? 1 : 2;
DCHECK(wait_count <= _countof(events));
if (max_wait_msec) {
DWORD result = WaitForMultipleObjects(wait_count, events, FALSE,
max_wait_msec);
if (result == WAIT_TIMEOUT)
return true;
}
bool rv = true;
for (int i = 0; i < wait_count; ++i) {
if (WaitForSingleObject(events[i], 0) == WAIT_OBJECT_0) {
if (i == 0 && processing_incoming_) {
rv = false;
DLOG(WARNING) << "Would recurse into ProcessIncomingMessages";
} else {
OnObjectSignaled(events[i]);
}
}
}
return rv;
#endif
}
void Channel::OnObjectSignaled(HANDLE object) {
bool ok;
if (object == input_state_.overlapped.hEvent) {
if (waiting_connect_) {
ProcessConnection();
// We may have some messages queued up to send...
if (!output_queue_.empty() && !output_state_.is_pending)
ProcessOutgoingMessages();
if (input_state_.is_pending)
return;
// else, fall-through and look for incoming messages...
}
// we don't support recursion through OnMessageReceived yet!
DCHECK(!processing_incoming_);
processing_incoming_ = true;
ok = ProcessIncomingMessages();
processing_incoming_ = false;
} else {
DCHECK(object == output_state_.overlapped.hEvent);
ok = ProcessOutgoingMessages();
}
if (!ok) {
Close();
listener_->OnChannelError();
}
}
//------------------------------------------------------------------------------
}
|