Chromium Code Reviews| Index: chrome/browser/extensions/api/serial/serial_api.cc |
| diff --git a/chrome/browser/extensions/api/serial/serial_api.cc b/chrome/browser/extensions/api/serial/serial_api.cc |
| index ef5aff70b32872f2a873d435d538907c6beb1abc..d5940aeb80496c9f2360125119c0853c4919eaa5 100644 |
| --- a/chrome/browser/extensions/api/serial/serial_api.cc |
| +++ b/chrome/browser/extensions/api/serial/serial_api.cc |
| @@ -4,8 +4,6 @@ |
| #include "chrome/browser/extensions/api/serial/serial_api.h" |
| -#include <string> |
| - |
| #include "base/values.h" |
| #include "chrome/browser/extensions/api/api_resource_controller.h" |
| #include "chrome/browser/extensions/api/serial/serial_connection.h" |
| @@ -69,12 +67,26 @@ void SerialOpenFunction::AsyncWorkStart() { |
| void SerialOpenFunction::Work() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| const SerialPortEnumerator::StringSet name_set( |
| - SerialPortEnumerator::GenerateValidSerialPortNames()); |
| + SerialPortEnumerator::GenerateValidSerialPortNames()); |
|
asargent_no_longer_on_chrome
2012/05/24 22:43:03
shouldn't this remain indented 4 spaces?
miket_OOO
2012/05/24 23:44:43
Yes.
|
| if (SerialPortEnumerator::DoesPortExist(name_set, port_)) { |
| - bool rv = BrowserThread::PostTask( |
| - BrowserThread::IO, FROM_HERE, |
| - base::Bind(&SerialOpenFunction::OpenPortOnIOThread, this)); |
| - DCHECK(rv); |
| + SerialConnection* serial_connection = new SerialConnection( |
| + port_, |
| + event_notifier_); |
| + CHECK(serial_connection); |
| + int id = controller()->AddAPIResource(serial_connection); |
| + CHECK(id); |
| + |
| + bool open_result = serial_connection->Open(); |
| + if (!open_result) { |
| + serial_connection->Close(); |
| + controller()->RemoveSerialConnection(id); |
| + id = -1; |
| + } |
| + |
| + DictionaryValue* result = new DictionaryValue(); |
| + result->SetInteger(kConnectionIdKey, id); |
| + result_.reset(result); |
| + AsyncWorkCompleted(); |
| } else { |
| DictionaryValue* result = new DictionaryValue(); |
| result->SetInteger(kConnectionIdKey, -1); |
| @@ -83,33 +95,13 @@ void SerialOpenFunction::Work() { |
| } |
|
asargent_no_longer_on_chrome
2012/05/24 22:43:03
nit: looks like you call AsyncWorkCompleted in any
miket_OOO
2012/05/24 23:44:43
And result_.reset(result).
|
| } |
| -void SerialOpenFunction::OpenPortOnIOThread() { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - SerialConnection* serial_connection = new SerialConnection( |
| - port_, |
| - event_notifier_); |
| - CHECK(serial_connection); |
| - int id = controller()->AddAPIResource(serial_connection); |
| - CHECK(id); |
| - |
| - bool open_result = serial_connection->Open(); |
| - if (!open_result) { |
| - serial_connection->Close(); |
| - controller()->RemoveAPIResource(id); |
| - id = -1; |
| - } |
| - |
| - DictionaryValue* result = new DictionaryValue(); |
| - result->SetInteger(kConnectionIdKey, id); |
| - result_.reset(result); |
| - AsyncWorkCompleted(); |
| -} |
| - |
| bool SerialOpenFunction::Respond() { |
| return true; |
| } |
| bool SerialCloseFunction::Prepare() { |
| + set_work_thread_id(BrowserThread::FILE); |
| + |
| EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); |
| return true; |
| } |
| @@ -120,7 +112,7 @@ void SerialCloseFunction::Work() { |
| controller()->GetSerialConnection(connection_id_); |
| if (serial_connection) { |
| serial_connection->Close(); |
| - controller()->RemoveAPIResource(connection_id_); |
| + controller()->RemoveSerialConnection(connection_id_); |
| close_result = true; |
| } |
| @@ -132,25 +124,29 @@ bool SerialCloseFunction::Respond() { |
| } |
| bool SerialReadFunction::Prepare() { |
| + set_work_thread_id(BrowserThread::FILE); |
| + |
| EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); |
| return true; |
| } |
| void SerialReadFunction::Work() { |
| + uint8 byte = '\0'; |
| int bytes_read = -1; |
| - std::string data; |
| SerialConnection* serial_connection = |
| controller()->GetSerialConnection(connection_id_); |
| - if (serial_connection) { |
| - unsigned char byte = '\0'; |
| + if (serial_connection) |
| bytes_read = serial_connection->Read(&byte); |
| - if (bytes_read == 1) |
| - data = byte; |
| - } |
| DictionaryValue* result = new DictionaryValue(); |
| + |
| + // The API is defined to require a 'data' value, so we will always |
| + // create a BinaryValue, even if it's zero-length. |
| + if (bytes_read < 0) |
| + bytes_read = 0; |
| result->SetInteger(kBytesReadKey, bytes_read); |
| - result->SetString(kDataKey, data); |
| + result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer( |
| + reinterpret_cast<char*>(&byte), bytes_read)); |
| result_.reset(result); |
| } |
| @@ -165,23 +161,15 @@ SerialWriteFunction::SerialWriteFunction() |
| SerialWriteFunction::~SerialWriteFunction() {} |
| bool SerialWriteFunction::Prepare() { |
| + set_work_thread_id(BrowserThread::FILE); |
| + |
| EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); |
| - base::ListValue* data_list_value = NULL; |
| - EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &data_list_value)); |
| - size_t size = data_list_value->GetSize(); |
| - if (size != 0) { |
| - io_buffer_ = new net::IOBufferWithSize(size); |
| - uint8* data_buffer = |
| - reinterpret_cast<uint8*>(io_buffer_->data()); |
| - for (size_t i = 0; i < size; ++i) { |
| - int int_value = -1; |
| - data_list_value->GetInteger(i, &int_value); |
| - DCHECK(int_value < 256); |
| - DCHECK(int_value >= 0); |
| - uint8 truncated_int = static_cast<uint8>(int_value); |
| - *data_buffer++ = truncated_int; |
| - } |
| - } |
| + base::BinaryValue *data = NULL; |
| + EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
| + |
| + io_buffer_size_ = data->GetSize(); |
| + io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
| + |
| return true; |
| } |
| @@ -190,7 +178,7 @@ void SerialWriteFunction::Work() { |
| SerialConnection* serial_connection = |
| controller()->GetSerialConnection(connection_id_); |
| if (serial_connection) |
| - bytes_written = serial_connection->Write(io_buffer_, io_buffer_->size()); |
| + bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); |
| else |
| error_ = kSerialConnectionNotFoundError; |