Chromium Code Reviews| Index: chrome/browser/extensions/api/serial/serial_connection.cc |
| diff --git a/chrome/browser/extensions/api/serial/serial_connection.cc b/chrome/browser/extensions/api/serial/serial_connection.cc |
| index 2d46b7931651b7fec04518e4a5e9b67f3add26cc..66e2a89d3437dbdd386255be20bacb69fb3fd1d8 100644 |
| --- a/chrome/browser/extensions/api/serial/serial_connection.cc |
| +++ b/chrome/browser/extensions/api/serial/serial_connection.cc |
| @@ -6,8 +6,61 @@ |
| #include <string> |
| +#include "base/file_path.h" |
| +#include "base/string_util.h" |
| + |
| namespace extensions { |
| const char kSerialConnectionNotFoundError[] = "Serial connection not found"; |
| +SerialConnection::SerialConnection(const std::string& port, |
| + APIResourceEventNotifier* event_notifier) |
| + : APIResource(APIResource::SerialConnectionResource, event_notifier), |
| + port_(port), |
| + file_(base::kInvalidPlatformFileValue) { |
| +} |
| + |
| +SerialConnection::~SerialConnection() { |
| + Close(); |
| +} |
| + |
| +bool SerialConnection::Open() { |
| + bool created = false; |
| + |
| + // It's the responsibility of the API wrapper around SerialConnection to |
| + // validate the supplied path against the set of valid port names, and |
| + // it is a reasonable assumption that serial port names are ASCII. |
| + CHECK(IsStringASCII(port_)); |
|
asargent_no_longer_on_chrome
2012/05/24 22:43:03
Does this really warrant a CHECK (eg all of chrome
miket_OOO
2012/05/24 23:44:43
In this case, I'd say yes. It's because if this we
|
| + FilePath file_path(FilePath::FromUTF8Unsafe(port_)); |
| + |
| + file_ = base::CreatePlatformFile(file_path, |
| + base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | |
| + base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_READ | |
| + base::PLATFORM_FILE_EXCLUSIVE_WRITE | |
| + base::PLATFORM_FILE_TERMINAL_DEVICE, &created, NULL); |
| + if (file_ == base::kInvalidPlatformFileValue) { |
| + return false; |
| + } |
| + return PostOpen(); |
| +} |
| + |
| +void SerialConnection::Close() { |
| + if (file_ != base::kInvalidPlatformFileValue) { |
| + base::ClosePlatformFile(file_); |
| + file_ = base::kInvalidPlatformFileValue; |
| + } |
| +} |
| + |
| +int SerialConnection::Read(uint8* byte) { |
| + DCHECK(byte); |
| + return base::ReadPlatformFile(file_, 0, reinterpret_cast<char*>(byte), 1); |
| +} |
| + |
| +int SerialConnection::Write(scoped_refptr<net::IOBuffer> io_buffer, |
| + int byte_count) { |
| + DCHECK(io_buffer->data()); |
| + DCHECK(byte_count >= 0); |
| + return base::WritePlatformFile(file_, 0, io_buffer->data(), byte_count); |
| +} |
| + |
| } // namespace extensions |