| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/serial/serial_api.h" | 5 #include "chrome/browser/extensions/api/serial/serial_api.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/values.h" | 7 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/api/api_resource_controller.h" | 8 #include "chrome/browser/extensions/api/api_resource_controller.h" |
| 11 #include "chrome/browser/extensions/api/serial/serial_connection.h" | 9 #include "chrome/browser/extensions/api/serial/serial_connection.h" |
| 12 #include "chrome/browser/extensions/api/serial/serial_port_enumerator.h" | 10 #include "chrome/browser/extensions/api/serial/serial_port_enumerator.h" |
| 13 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 14 | 12 |
| 15 using content::BrowserThread; | 13 using content::BrowserThread; |
| 16 | 14 |
| 17 namespace extensions { | 15 namespace extensions { |
| 18 | 16 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 return true; | 60 return true; |
| 63 } | 61 } |
| 64 | 62 |
| 65 void SerialOpenFunction::AsyncWorkStart() { | 63 void SerialOpenFunction::AsyncWorkStart() { |
| 66 Work(); | 64 Work(); |
| 67 } | 65 } |
| 68 | 66 |
| 69 void SerialOpenFunction::Work() { | 67 void SerialOpenFunction::Work() { |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 71 const SerialPortEnumerator::StringSet name_set( | 69 const SerialPortEnumerator::StringSet name_set( |
| 72 SerialPortEnumerator::GenerateValidSerialPortNames()); | 70 SerialPortEnumerator::GenerateValidSerialPortNames()); |
| 73 if (SerialPortEnumerator::DoesPortExist(name_set, port_)) { | 71 if (SerialPortEnumerator::DoesPortExist(name_set, port_)) { |
| 74 bool rv = BrowserThread::PostTask( | 72 SerialConnection* serial_connection = new SerialConnection( |
| 75 BrowserThread::IO, FROM_HERE, | 73 port_, |
| 76 base::Bind(&SerialOpenFunction::OpenPortOnIOThread, this)); | 74 event_notifier_); |
| 77 DCHECK(rv); | 75 CHECK(serial_connection); |
| 76 int id = controller()->AddAPIResource(serial_connection); |
| 77 CHECK(id); |
| 78 |
| 79 bool open_result = serial_connection->Open(); |
| 80 if (!open_result) { |
| 81 serial_connection->Close(); |
| 82 controller()->RemoveSerialConnection(id); |
| 83 id = -1; |
| 84 } |
| 85 |
| 86 DictionaryValue* result = new DictionaryValue(); |
| 87 result->SetInteger(kConnectionIdKey, id); |
| 88 result_.reset(result); |
| 89 AsyncWorkCompleted(); |
| 78 } else { | 90 } else { |
| 79 DictionaryValue* result = new DictionaryValue(); | 91 DictionaryValue* result = new DictionaryValue(); |
| 80 result->SetInteger(kConnectionIdKey, -1); | 92 result->SetInteger(kConnectionIdKey, -1); |
| 81 result_.reset(result); | 93 result_.reset(result); |
| 82 AsyncWorkCompleted(); | 94 AsyncWorkCompleted(); |
| 83 } | 95 } |
| 84 } | 96 } |
| 85 | 97 |
| 86 void SerialOpenFunction::OpenPortOnIOThread() { | |
| 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 88 SerialConnection* serial_connection = new SerialConnection( | |
| 89 port_, | |
| 90 event_notifier_); | |
| 91 CHECK(serial_connection); | |
| 92 int id = controller()->AddAPIResource(serial_connection); | |
| 93 CHECK(id); | |
| 94 | |
| 95 bool open_result = serial_connection->Open(); | |
| 96 if (!open_result) { | |
| 97 serial_connection->Close(); | |
| 98 controller()->RemoveAPIResource(id); | |
| 99 id = -1; | |
| 100 } | |
| 101 | |
| 102 DictionaryValue* result = new DictionaryValue(); | |
| 103 result->SetInteger(kConnectionIdKey, id); | |
| 104 result_.reset(result); | |
| 105 AsyncWorkCompleted(); | |
| 106 } | |
| 107 | |
| 108 bool SerialOpenFunction::Respond() { | 98 bool SerialOpenFunction::Respond() { |
| 109 return true; | 99 return true; |
| 110 } | 100 } |
| 111 | 101 |
| 112 bool SerialCloseFunction::Prepare() { | 102 bool SerialCloseFunction::Prepare() { |
| 103 set_work_thread_id(BrowserThread::FILE); |
| 104 |
| 113 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); | 105 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); |
| 114 return true; | 106 return true; |
| 115 } | 107 } |
| 116 | 108 |
| 117 void SerialCloseFunction::Work() { | 109 void SerialCloseFunction::Work() { |
| 118 bool close_result = false; | 110 bool close_result = false; |
| 119 SerialConnection* serial_connection = | 111 SerialConnection* serial_connection = |
| 120 controller()->GetSerialConnection(connection_id_); | 112 controller()->GetSerialConnection(connection_id_); |
| 121 if (serial_connection) { | 113 if (serial_connection) { |
| 122 serial_connection->Close(); | 114 serial_connection->Close(); |
| 123 controller()->RemoveAPIResource(connection_id_); | 115 controller()->RemoveSerialConnection(connection_id_); |
| 124 close_result = true; | 116 close_result = true; |
| 125 } | 117 } |
| 126 | 118 |
| 127 result_.reset(Value::CreateBooleanValue(close_result)); | 119 result_.reset(Value::CreateBooleanValue(close_result)); |
| 128 } | 120 } |
| 129 | 121 |
| 130 bool SerialCloseFunction::Respond() { | 122 bool SerialCloseFunction::Respond() { |
| 131 return true; | 123 return true; |
| 132 } | 124 } |
| 133 | 125 |
| 134 bool SerialReadFunction::Prepare() { | 126 bool SerialReadFunction::Prepare() { |
| 127 set_work_thread_id(BrowserThread::FILE); |
| 128 |
| 135 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); | 129 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); |
| 136 return true; | 130 return true; |
| 137 } | 131 } |
| 138 | 132 |
| 139 void SerialReadFunction::Work() { | 133 void SerialReadFunction::Work() { |
| 134 uint8 byte = '\0'; |
| 140 int bytes_read = -1; | 135 int bytes_read = -1; |
| 141 std::string data; | |
| 142 SerialConnection* serial_connection = | 136 SerialConnection* serial_connection = |
| 143 controller()->GetSerialConnection(connection_id_); | 137 controller()->GetSerialConnection(connection_id_); |
| 144 if (serial_connection) { | 138 if (serial_connection) |
| 145 unsigned char byte = '\0'; | |
| 146 bytes_read = serial_connection->Read(&byte); | 139 bytes_read = serial_connection->Read(&byte); |
| 147 if (bytes_read == 1) | |
| 148 data = byte; | |
| 149 } | |
| 150 | 140 |
| 151 DictionaryValue* result = new DictionaryValue(); | 141 DictionaryValue* result = new DictionaryValue(); |
| 142 |
| 143 // The API is defined to require a 'data' value, so we will always |
| 144 // create a BinaryValue, even if it's zero-length. |
| 145 if (bytes_read < 0) |
| 146 bytes_read = 0; |
| 152 result->SetInteger(kBytesReadKey, bytes_read); | 147 result->SetInteger(kBytesReadKey, bytes_read); |
| 153 result->SetString(kDataKey, data); | 148 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer( |
| 149 reinterpret_cast<char*>(&byte), bytes_read)); |
| 154 result_.reset(result); | 150 result_.reset(result); |
| 155 } | 151 } |
| 156 | 152 |
| 157 bool SerialReadFunction::Respond() { | 153 bool SerialReadFunction::Respond() { |
| 158 return true; | 154 return true; |
| 159 } | 155 } |
| 160 | 156 |
| 161 SerialWriteFunction::SerialWriteFunction() | 157 SerialWriteFunction::SerialWriteFunction() |
| 162 : connection_id_(-1), io_buffer_(NULL) { | 158 : connection_id_(-1), io_buffer_(NULL) { |
| 163 } | 159 } |
| 164 | 160 |
| 165 SerialWriteFunction::~SerialWriteFunction() {} | 161 SerialWriteFunction::~SerialWriteFunction() {} |
| 166 | 162 |
| 167 bool SerialWriteFunction::Prepare() { | 163 bool SerialWriteFunction::Prepare() { |
| 164 set_work_thread_id(BrowserThread::FILE); |
| 165 |
| 168 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); | 166 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); |
| 169 base::ListValue* data_list_value = NULL; | 167 base::BinaryValue *data = NULL; |
| 170 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &data_list_value)); | 168 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
| 171 size_t size = data_list_value->GetSize(); | 169 |
| 172 if (size != 0) { | 170 io_buffer_size_ = data->GetSize(); |
| 173 io_buffer_ = new net::IOBufferWithSize(size); | 171 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
| 174 uint8* data_buffer = | 172 |
| 175 reinterpret_cast<uint8*>(io_buffer_->data()); | |
| 176 for (size_t i = 0; i < size; ++i) { | |
| 177 int int_value = -1; | |
| 178 data_list_value->GetInteger(i, &int_value); | |
| 179 DCHECK(int_value < 256); | |
| 180 DCHECK(int_value >= 0); | |
| 181 uint8 truncated_int = static_cast<uint8>(int_value); | |
| 182 *data_buffer++ = truncated_int; | |
| 183 } | |
| 184 } | |
| 185 return true; | 173 return true; |
| 186 } | 174 } |
| 187 | 175 |
| 188 void SerialWriteFunction::Work() { | 176 void SerialWriteFunction::Work() { |
| 189 int bytes_written = -1; | 177 int bytes_written = -1; |
| 190 SerialConnection* serial_connection = | 178 SerialConnection* serial_connection = |
| 191 controller()->GetSerialConnection(connection_id_); | 179 controller()->GetSerialConnection(connection_id_); |
| 192 if (serial_connection) | 180 if (serial_connection) |
| 193 bytes_written = serial_connection->Write(io_buffer_, io_buffer_->size()); | 181 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); |
| 194 else | 182 else |
| 195 error_ = kSerialConnectionNotFoundError; | 183 error_ = kSerialConnectionNotFoundError; |
| 196 | 184 |
| 197 DictionaryValue* result = new DictionaryValue(); | 185 DictionaryValue* result = new DictionaryValue(); |
| 198 result->SetInteger(kBytesWrittenKey, bytes_written); | 186 result->SetInteger(kBytesWrittenKey, bytes_written); |
| 199 result_.reset(result); | 187 result_.reset(result); |
| 200 } | 188 } |
| 201 | 189 |
| 202 bool SerialWriteFunction::Respond() { | 190 bool SerialWriteFunction::Respond() { |
| 203 return true; | 191 return true; |
| 204 } | 192 } |
| 205 | 193 |
| 206 } // namespace extensions | 194 } // namespace extensions |
| OLD | NEW |