| 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..697384f546c56532cf61abcd97b5e32ca46a410c 100644
|
| --- a/chrome/browser/extensions/api/serial/serial_connection.cc
|
| +++ b/chrome/browser/extensions/api/serial/serial_connection.cc
|
| @@ -6,8 +6,63 @@
|
|
|
| #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_));
|
| + 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::ReadPlatformFileFromCurrentPos(file_,
|
| + 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::WritePlatformFileFromCurrentPos(file_,
|
| + io_buffer->data(), byte_count);
|
| +}
|
| +
|
| } // namespace extensions
|
|
|