Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(644)

Unified Diff: chrome/browser/extensions/api/serial/serial_api.cc

Issue 10392181: Implement serial API for Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: OSX/Win fixes and review feedback. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a6863670a0c4d3cac464532b0c0d5ea852a196a4 100644
--- a/chrome/browser/extensions/api/serial/serial_api.cc
+++ b/chrome/browser/extensions/api/serial/serial_api.cc
@@ -4,8 +4,7 @@
#include "chrome/browser/extensions/api/serial/serial_api.h"
-#include <string>
-
+#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/api_resource_controller.h"
#include "chrome/browser/extensions/api/serial/serial_connection.h"
@@ -56,6 +55,8 @@ bool SerialOpenFunction::Prepare() {
size_t argument_position = 0;
EXTENSION_FUNCTION_VALIDATE(args_->GetString(argument_position++, &port_));
+ EXTENSION_FUNCTION_VALIDATE(IsStringASCII(port_));
+
src_id_ = ExtractSrcId(argument_position);
event_notifier_ = CreateEventNotifier(src_id_);
@@ -68,39 +69,30 @@ void SerialOpenFunction::AsyncWorkStart() {
void SerialOpenFunction::Work() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+
+ DictionaryValue* result = new DictionaryValue();
const SerialPortEnumerator::StringSet name_set(
SerialPortEnumerator::GenerateValidSerialPortNames());
- if (SerialPortEnumerator::DoesPortExist(name_set, port_)) {
- bool rv = BrowserThread::PostTask(
- BrowserThread::IO, FROM_HERE,
- base::Bind(&SerialOpenFunction::OpenPortOnIOThread, this));
- DCHECK(rv);
- } else {
- DictionaryValue* result = new DictionaryValue();
- result->SetInteger(kConnectionIdKey, -1);
- result_.reset(result);
- AsyncWorkCompleted();
- }
-}
-void SerialOpenFunction::OpenPortOnIOThread() {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
- SerialConnection* serial_connection = new SerialConnection(
+ if (SerialPortEnumerator::DoesPortExist(name_set, port_)) {
+ 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;
+ 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;
+ }
+ result->SetInteger(kConnectionIdKey, id);
+ } else {
+ result->SetInteger(kConnectionIdKey, -1);
}
- DictionaryValue* result = new DictionaryValue();
- result->SetInteger(kConnectionIdKey, id);
result_.reset(result);
AsyncWorkCompleted();
}
@@ -110,6 +102,8 @@ bool SerialOpenFunction::Respond() {
}
bool SerialCloseFunction::Prepare() {
+ set_work_thread_id(BrowserThread::FILE);
+
EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_));
return true;
}
@@ -120,7 +114,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 +126,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 +163,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 +180,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;

Powered by Google App Engine
This is Rietveld 408576698