| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <windows.h> |
| 6 |
| 5 #include "chrome/browser/extensions/api/serial/serial_io_handler_win.h" | 7 #include "chrome/browser/extensions/api/serial/serial_io_handler_win.h" |
| 6 | 8 |
| 7 #include "base/win/windows_version.h" | 9 #include "base/win/windows_version.h" |
| 8 | 10 |
| 9 #include <windows.h> | 11 namespace { |
| 12 const base::PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE; |
| 13 } // namespace |
| 10 | 14 |
| 11 namespace extensions { | 15 namespace extensions { |
| 12 | 16 |
| 13 // static | 17 // static |
| 14 scoped_refptr<SerialIoHandler> SerialIoHandler::Create() { | 18 scoped_refptr<SerialIoHandler> SerialIoHandler::Create() { |
| 15 return new SerialIoHandlerWin(); | 19 return new SerialIoHandlerWin(); |
| 16 } | 20 } |
| 17 | 21 |
| 18 void SerialIoHandlerWin::InitializeImpl() { | 22 void SerialIoHandlerWin::InitializeImpl() { |
| 19 DCHECK(!comm_context_); | 23 DCHECK(!comm_context_); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 31 memset(&read_context_->overlapped, 0, sizeof(read_context_->overlapped)); | 35 memset(&read_context_->overlapped, 0, sizeof(read_context_->overlapped)); |
| 32 | 36 |
| 33 write_context_.reset(new base::MessageLoopForIO::IOContext()); | 37 write_context_.reset(new base::MessageLoopForIO::IOContext()); |
| 34 write_context_->handler = this; | 38 write_context_->handler = this; |
| 35 memset(&write_context_->overlapped, 0, sizeof(write_context_->overlapped)); | 39 memset(&write_context_->overlapped, 0, sizeof(write_context_->overlapped)); |
| 36 } | 40 } |
| 37 | 41 |
| 38 void SerialIoHandlerWin::ReadImpl() { | 42 void SerialIoHandlerWin::ReadImpl() { |
| 39 DCHECK(CalledOnValidThread()); | 43 DCHECK(CalledOnValidThread()); |
| 40 DCHECK(pending_read_buffer()); | 44 DCHECK(pending_read_buffer()); |
| 41 DCHECK_NE(file(), base::kInvalidPlatformFileValue); | 45 DCHECK_NE(file(), kInvalidPlatformFileValue); |
| 42 | 46 |
| 43 DWORD errors; | 47 DWORD errors; |
| 44 COMSTAT status; | 48 COMSTAT status; |
| 45 if (!ClearCommError(file(), &errors, &status) || errors != 0) { | 49 if (!ClearCommError(file(), &errors, &status) || errors != 0) { |
| 46 QueueReadCompleted(0, api::serial::RECEIVE_ERROR_SYSTEM_ERROR); | 50 QueueReadCompleted(0, api::serial::RECEIVE_ERROR_SYSTEM_ERROR); |
| 47 return; | 51 return; |
| 48 } | 52 } |
| 49 | 53 |
| 50 SetCommMask(file(), EV_RXCHAR); | 54 SetCommMask(file(), EV_RXCHAR); |
| 51 | 55 |
| 52 event_mask_ = 0; | 56 event_mask_ = 0; |
| 53 BOOL ok = ::WaitCommEvent(file(), &event_mask_, &comm_context_->overlapped); | 57 BOOL ok = ::WaitCommEvent(file(), &event_mask_, &comm_context_->overlapped); |
| 54 if (!ok && GetLastError() != ERROR_IO_PENDING) { | 58 if (!ok && GetLastError() != ERROR_IO_PENDING) { |
| 55 QueueReadCompleted(0, api::serial::RECEIVE_ERROR_SYSTEM_ERROR); | 59 QueueReadCompleted(0, api::serial::RECEIVE_ERROR_SYSTEM_ERROR); |
| 56 } | 60 } |
| 57 is_comm_pending_ = true; | 61 is_comm_pending_ = true; |
| 58 } | 62 } |
| 59 | 63 |
| 60 void SerialIoHandlerWin::WriteImpl() { | 64 void SerialIoHandlerWin::WriteImpl() { |
| 61 DCHECK(CalledOnValidThread()); | 65 DCHECK(CalledOnValidThread()); |
| 62 DCHECK(pending_write_buffer()); | 66 DCHECK(pending_write_buffer()); |
| 63 DCHECK_NE(file(), base::kInvalidPlatformFileValue); | 67 DCHECK_NE(file(), kInvalidPlatformFileValue); |
| 64 | 68 |
| 65 BOOL ok = ::WriteFile(file(), | 69 BOOL ok = ::WriteFile(file(), |
| 66 pending_write_buffer()->data(), | 70 pending_write_buffer()->data(), |
| 67 pending_write_buffer_len(), NULL, | 71 pending_write_buffer_len(), NULL, |
| 68 &write_context_->overlapped); | 72 &write_context_->overlapped); |
| 69 if (!ok && GetLastError() != ERROR_IO_PENDING) { | 73 if (!ok && GetLastError() != ERROR_IO_PENDING) { |
| 70 QueueWriteCompleted(0, api::serial::SEND_ERROR_SYSTEM_ERROR); | 74 QueueWriteCompleted(0, api::serial::SEND_ERROR_SYSTEM_ERROR); |
| 71 } | 75 } |
| 72 } | 76 } |
| 73 | 77 |
| 74 void SerialIoHandlerWin::CancelReadImpl() { | 78 void SerialIoHandlerWin::CancelReadImpl() { |
| 75 DCHECK(CalledOnValidThread()); | 79 DCHECK(CalledOnValidThread()); |
| 76 DCHECK_NE(file(), base::kInvalidPlatformFileValue); | 80 DCHECK_NE(file(), kInvalidPlatformFileValue); |
| 77 ::CancelIo(file()); | 81 ::CancelIo(file()); |
| 78 } | 82 } |
| 79 | 83 |
| 80 void SerialIoHandlerWin::CancelWriteImpl() { | 84 void SerialIoHandlerWin::CancelWriteImpl() { |
| 81 DCHECK(CalledOnValidThread()); | 85 DCHECK(CalledOnValidThread()); |
| 82 DCHECK_NE(file(), base::kInvalidPlatformFileValue); | 86 DCHECK_NE(file(), kInvalidPlatformFileValue); |
| 83 ::CancelIo(file()); | 87 ::CancelIo(file()); |
| 84 } | 88 } |
| 85 | 89 |
| 86 SerialIoHandlerWin::SerialIoHandlerWin() | 90 SerialIoHandlerWin::SerialIoHandlerWin() |
| 87 : event_mask_(0), | 91 : event_mask_(0), |
| 88 is_comm_pending_(false) { | 92 is_comm_pending_(false) { |
| 89 } | 93 } |
| 90 | 94 |
| 91 SerialIoHandlerWin::~SerialIoHandlerWin() { | 95 SerialIoHandlerWin::~SerialIoHandlerWin() { |
| 92 } | 96 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 bytes_transferred, | 137 bytes_transferred, |
| 134 error == ERROR_SUCCESS ? api::serial::SEND_ERROR_NONE | 138 error == ERROR_SUCCESS ? api::serial::SEND_ERROR_NONE |
| 135 : api::serial::SEND_ERROR_SYSTEM_ERROR); | 139 : api::serial::SEND_ERROR_SYSTEM_ERROR); |
| 136 } | 140 } |
| 137 } else { | 141 } else { |
| 138 NOTREACHED() << "Invalid IOContext"; | 142 NOTREACHED() << "Invalid IOContext"; |
| 139 } | 143 } |
| 140 } | 144 } |
| 141 | 145 |
| 142 } // namespace extensions | 146 } // namespace extensions |
| OLD | NEW |