| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 #include "chrome/common/profiling/memlog_sender_pipe_win.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/common/profiling/memlog_stream.h" |
| 9 |
| 10 namespace profiling { |
| 11 |
| 12 MemlogSenderPipe::MemlogSenderPipe(const base::string16& pipe_id) |
| 13 : pipe_id_(pipe_id), handle_(INVALID_HANDLE_VALUE) {} |
| 14 |
| 15 MemlogSenderPipe::~MemlogSenderPipe() { |
| 16 if (handle_ != INVALID_HANDLE_VALUE) |
| 17 ::CloseHandle(handle_); |
| 18 } |
| 19 |
| 20 bool MemlogSenderPipe::Connect() { |
| 21 DCHECK(handle_ == INVALID_HANDLE_VALUE); |
| 22 |
| 23 base::string16 pipe_name = kWindowsPipePrefix; |
| 24 pipe_name.append(pipe_id_); |
| 25 |
| 26 const int kMaxWaitMS = 30000; |
| 27 ULONGLONG timeout_ticks = ::GetTickCount64() + kMaxWaitMS; |
| 28 do { |
| 29 if (!::WaitNamedPipe(pipe_name.c_str(), kMaxWaitMS)) { |
| 30 // Since it will wait "forever", the only time WaitNamedPipe should fail |
| 31 // is if the pipe doesn't exist. |
| 32 return false; |
| 33 } |
| 34 // This is a single-direction pipe so we don't specify GENERIC_READ, but |
| 35 // MSDN says we need FILE_READ_ATTRIBUTES. |
| 36 handle_ = ::CreateFile(pipe_name.c_str(), |
| 37 FILE_READ_ATTRIBUTES | GENERIC_WRITE, 0, |
| 38 NULL, OPEN_EXISTING, 0, NULL); |
| 39 // Need to loop since there is a race condition waiting for a connection to |
| 40 // be available and actually connecting to it. |
| 41 } while (handle_ == INVALID_HANDLE_VALUE && |
| 42 ::GetTickCount64() < timeout_ticks); |
| 43 |
| 44 return handle_ != INVALID_HANDLE_VALUE; |
| 45 } |
| 46 |
| 47 bool MemlogSenderPipe::Send(const void* data, size_t sz) { |
| 48 // The pipe uses a blocking wait mode (it doesn't specify PIPE_NOWAIT) so |
| 49 // WriteFile should do only complete writes. |
| 50 // |
| 51 // Note: don't use logging here (CHECK, DCHECK) because they will allocate, |
| 52 // and this function is called from within a malloc hook. |
| 53 DWORD bytes_written = 0; |
| 54 if (!::WriteFile(handle_, data, static_cast<DWORD>(sz), &bytes_written, NULL)) |
| 55 return false; |
| 56 return true; |
| 57 } |
| 58 |
| 59 } // namespace profiling |
| OLD | NEW |