Home | History | Annotate | Download | only in serial
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/extensions/api/serial/serial_io_handler.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/message_loop/message_loop.h"
      9 
     10 namespace extensions {
     11 
     12 SerialIoHandler::SerialIoHandler()
     13     : file_(base::kInvalidPlatformFileValue),
     14       pending_read_buffer_len_(0),
     15       pending_write_buffer_len_(0) {
     16 }
     17 
     18 SerialIoHandler::~SerialIoHandler() {
     19   DCHECK(CalledOnValidThread());
     20 }
     21 
     22 void SerialIoHandler::Initialize(base::PlatformFile file,
     23                                  const ReadCompleteCallback& read_callback,
     24                                  const WriteCompleteCallback& write_callback) {
     25   DCHECK(CalledOnValidThread());
     26   DCHECK_EQ(file_, base::kInvalidPlatformFileValue);
     27 
     28   file_ = file;
     29   read_complete_ = read_callback;
     30   write_complete_ = write_callback;
     31 
     32   InitializeImpl();
     33 }
     34 
     35 void SerialIoHandler::Read(int max_bytes) {
     36   DCHECK(CalledOnValidThread());
     37   DCHECK(!IsReadPending());
     38   pending_read_buffer_ = new net::IOBuffer(max_bytes);
     39   pending_read_buffer_len_ = max_bytes;
     40   read_canceled_ = false;
     41   AddRef();
     42   ReadImpl();
     43 }
     44 
     45 void SerialIoHandler::Write(const std::string& data) {
     46   DCHECK(CalledOnValidThread());
     47   DCHECK(!IsWritePending());
     48   pending_write_buffer_ = new net::IOBuffer(data.length());
     49   pending_write_buffer_len_ = data.length();
     50   memcpy(pending_write_buffer_->data(), data.data(), pending_write_buffer_len_);
     51   write_canceled_ = false;
     52   AddRef();
     53   WriteImpl();
     54 }
     55 
     56 void SerialIoHandler::ReadCompleted(int bytes_read,
     57                                     api::serial::ReceiveError error) {
     58   DCHECK(CalledOnValidThread());
     59   DCHECK(IsReadPending());
     60   read_complete_.Run(std::string(pending_read_buffer_->data(), bytes_read),
     61                      error);
     62   pending_read_buffer_ = NULL;
     63   pending_read_buffer_len_ = 0;
     64   Release();
     65 }
     66 
     67 void SerialIoHandler::WriteCompleted(int bytes_written,
     68                                      api::serial::SendError error) {
     69   DCHECK(CalledOnValidThread());
     70   DCHECK(IsWritePending());
     71   write_complete_.Run(bytes_written, error);
     72   pending_write_buffer_ = NULL;
     73   pending_write_buffer_len_ = 0;
     74   Release();
     75 }
     76 
     77 bool SerialIoHandler::IsReadPending() const {
     78   DCHECK(CalledOnValidThread());
     79   return pending_read_buffer_ != NULL;
     80 }
     81 
     82 bool SerialIoHandler::IsWritePending() const {
     83   DCHECK(CalledOnValidThread());
     84   return pending_write_buffer_ != NULL;
     85 }
     86 
     87 void SerialIoHandler::CancelRead(api::serial::ReceiveError reason) {
     88   DCHECK(CalledOnValidThread());
     89   if (IsReadPending()) {
     90     read_canceled_ = true;
     91     read_cancel_reason_ = reason;
     92     CancelReadImpl();
     93   }
     94 }
     95 
     96 void SerialIoHandler::CancelWrite(api::serial::SendError reason) {
     97   DCHECK(CalledOnValidThread());
     98   if (IsWritePending()) {
     99     write_canceled_ = true;
    100     write_cancel_reason_ = reason;
    101     CancelWriteImpl();
    102   }
    103 }
    104 
    105 void SerialIoHandler::QueueReadCompleted(int bytes_read,
    106                                          api::serial::ReceiveError error) {
    107   base::MessageLoop::current()->PostTask(
    108       FROM_HERE, base::Bind(&SerialIoHandler::ReadCompleted, this,
    109                             bytes_read, error));
    110 }
    111 
    112 void SerialIoHandler::QueueWriteCompleted(int bytes_written,
    113                                           api::serial::SendError error) {
    114   base::MessageLoop::current()->PostTask(
    115       FROM_HERE, base::Bind(&SerialIoHandler::WriteCompleted, this,
    116                             bytes_written, error));
    117 }
    118 
    119 }  // namespace extensions
    120 
    121