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 "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/extensions/extension_system.h" | 8 #include "chrome/browser/extensions/extension_system.h" |
9 #include "chrome/browser/extensions/api/serial/serial_connection.h" | 9 #include "chrome/browser/extensions/api/serial/serial_connection.h" |
10 #include "chrome/browser/extensions/api/serial/serial_port_enumerator.h" | 10 #include "chrome/browser/extensions/api/serial/serial_port_enumerator.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 SerialGetPortsFunction::SerialGetPortsFunction() {} | 53 SerialGetPortsFunction::SerialGetPortsFunction() {} |
54 | 54 |
55 bool SerialGetPortsFunction::Prepare() { | 55 bool SerialGetPortsFunction::Prepare() { |
56 set_work_thread_id(BrowserThread::FILE); | 56 set_work_thread_id(BrowserThread::FILE); |
57 return true; | 57 return true; |
58 } | 58 } |
59 | 59 |
60 void SerialGetPortsFunction::Work() { | 60 void SerialGetPortsFunction::Work() { |
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
62 | 62 |
63 ListValue* ports = new ListValue(); | 63 base::ListValue* ports = new base::ListValue(); |
64 SerialPortEnumerator::StringSet port_names = | 64 SerialPortEnumerator::StringSet port_names = |
65 SerialPortEnumerator::GenerateValidSerialPortNames(); | 65 SerialPortEnumerator::GenerateValidSerialPortNames(); |
66 SerialPortEnumerator::StringSet::const_iterator i = port_names.begin(); | 66 SerialPortEnumerator::StringSet::const_iterator i = port_names.begin(); |
67 while (i != port_names.end()) { | 67 while (i != port_names.end()) { |
68 ports->Append(Value::CreateStringValue(*i++)); | 68 ports->Append(Value::CreateStringValue(*i++)); |
69 } | 69 } |
70 | 70 |
71 SetResult(ports); | 71 SetResult(ports); |
72 } | 72 } |
73 | 73 |
(...skipping 15 matching lines...) Expand all Loading... |
89 SerialOpenFunction::~SerialOpenFunction() { | 89 SerialOpenFunction::~SerialOpenFunction() { |
90 } | 90 } |
91 | 91 |
92 bool SerialOpenFunction::Prepare() { | 92 bool SerialOpenFunction::Prepare() { |
93 set_work_thread_id(BrowserThread::FILE); | 93 set_work_thread_id(BrowserThread::FILE); |
94 | 94 |
95 params_ = api::serial::Open::Params::Create(*args_); | 95 params_ = api::serial::Open::Params::Create(*args_); |
96 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 96 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
97 | 97 |
98 if (params_->options.get()) { | 98 if (params_->options.get()) { |
99 scoped_ptr<DictionaryValue> options = params_->options->ToValue(); | 99 scoped_ptr<base::DictionaryValue> options = params_->options->ToValue(); |
100 if (options->HasKey(kBitrateKey)) | 100 if (options->HasKey(kBitrateKey)) |
101 EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kBitrateKey, &bitrate_)); | 101 EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kBitrateKey, &bitrate_)); |
102 } | 102 } |
103 | 103 |
104 return true; | 104 return true; |
105 } | 105 } |
106 | 106 |
107 void SerialOpenFunction::AsyncWorkStart() { | 107 void SerialOpenFunction::AsyncWorkStart() { |
108 Work(); | 108 Work(); |
109 } | 109 } |
(...skipping 11 matching lines...) Expand all Loading... |
121 int id = manager_->Add(serial_connection); | 121 int id = manager_->Add(serial_connection); |
122 CHECK(id); | 122 CHECK(id); |
123 | 123 |
124 bool open_result = serial_connection->Open(); | 124 bool open_result = serial_connection->Open(); |
125 if (!open_result) { | 125 if (!open_result) { |
126 serial_connection->Close(); | 126 serial_connection->Close(); |
127 RemoveSerialConnection(id); | 127 RemoveSerialConnection(id); |
128 id = -1; | 128 id = -1; |
129 } | 129 } |
130 | 130 |
131 DictionaryValue* result = new DictionaryValue(); | 131 base::DictionaryValue* result = new base::DictionaryValue(); |
132 result->SetInteger(kConnectionIdKey, id); | 132 result->SetInteger(kConnectionIdKey, id); |
133 SetResult(result); | 133 SetResult(result); |
134 AsyncWorkCompleted(); | 134 AsyncWorkCompleted(); |
135 } else { | 135 } else { |
136 DictionaryValue* result = new DictionaryValue(); | 136 base::DictionaryValue* result = new base::DictionaryValue(); |
137 result->SetInteger(kConnectionIdKey, -1); | 137 result->SetInteger(kConnectionIdKey, -1); |
138 SetResult(result); | 138 SetResult(result); |
139 AsyncWorkCompleted(); | 139 AsyncWorkCompleted(); |
140 } | 140 } |
141 } | 141 } |
142 | 142 |
143 SerialConnection* SerialOpenFunction::CreateSerialConnection( | 143 SerialConnection* SerialOpenFunction::CreateSerialConnection( |
144 const std::string& port, | 144 const std::string& port, |
145 int bitrate, | 145 int bitrate, |
146 const std::string& owner_extension_id) { | 146 const std::string& owner_extension_id) { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 void SerialReadFunction::Work() { | 211 void SerialReadFunction::Work() { |
212 int bytes_read = -1; | 212 int bytes_read = -1; |
213 scoped_refptr<net::IOBufferWithSize> io_buffer( | 213 scoped_refptr<net::IOBufferWithSize> io_buffer( |
214 new net::IOBufferWithSize(params_->bytes_to_read)); | 214 new net::IOBufferWithSize(params_->bytes_to_read)); |
215 SerialConnection* serial_connection(GetSerialConnection( | 215 SerialConnection* serial_connection(GetSerialConnection( |
216 params_->connection_id)); | 216 params_->connection_id)); |
217 | 217 |
218 if (serial_connection) | 218 if (serial_connection) |
219 bytes_read = serial_connection->Read(io_buffer); | 219 bytes_read = serial_connection->Read(io_buffer); |
220 | 220 |
221 DictionaryValue* result = new DictionaryValue(); | 221 base::DictionaryValue* result = new base::DictionaryValue(); |
222 | 222 |
223 // The API is defined to require a 'data' value, so we will always | 223 // The API is defined to require a 'data' value, so we will always |
224 // create a BinaryValue, even if it's zero-length. | 224 // create a BinaryValue, even if it's zero-length. |
225 if (bytes_read < 0) | 225 if (bytes_read < 0) |
226 bytes_read = 0; | 226 bytes_read = 0; |
227 result->SetInteger(kBytesReadKey, bytes_read); | 227 result->SetInteger(kBytesReadKey, bytes_read); |
228 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer( | 228 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer( |
229 io_buffer->data(), bytes_read)); | 229 io_buffer->data(), bytes_read)); |
230 SetResult(result); | 230 SetResult(result); |
231 } | 231 } |
(...skipping 23 matching lines...) Expand all Loading... |
255 | 255 |
256 void SerialWriteFunction::Work() { | 256 void SerialWriteFunction::Work() { |
257 int bytes_written = -1; | 257 int bytes_written = -1; |
258 SerialConnection* serial_connection = GetSerialConnection( | 258 SerialConnection* serial_connection = GetSerialConnection( |
259 params_->connection_id); | 259 params_->connection_id); |
260 if (serial_connection) | 260 if (serial_connection) |
261 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); | 261 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); |
262 else | 262 else |
263 error_ = kSerialConnectionNotFoundError; | 263 error_ = kSerialConnectionNotFoundError; |
264 | 264 |
265 DictionaryValue* result = new DictionaryValue(); | 265 base::DictionaryValue* result = new base::DictionaryValue(); |
266 result->SetInteger(kBytesWrittenKey, bytes_written); | 266 result->SetInteger(kBytesWrittenKey, bytes_written); |
267 SetResult(result); | 267 SetResult(result); |
268 } | 268 } |
269 | 269 |
270 bool SerialWriteFunction::Respond() { | 270 bool SerialWriteFunction::Respond() { |
271 return true; | 271 return true; |
272 } | 272 } |
273 | 273 |
274 SerialFlushFunction::SerialFlushFunction() { | 274 SerialFlushFunction::SerialFlushFunction() { |
275 } | 275 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 bool SerialGetControlSignalsFunction::Prepare() { | 311 bool SerialGetControlSignalsFunction::Prepare() { |
312 set_work_thread_id(BrowserThread::FILE); | 312 set_work_thread_id(BrowserThread::FILE); |
313 | 313 |
314 params_ = api::serial::GetControlSignals::Params::Create(*args_); | 314 params_ = api::serial::GetControlSignals::Params::Create(*args_); |
315 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 315 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
316 | 316 |
317 return true; | 317 return true; |
318 } | 318 } |
319 | 319 |
320 void SerialGetControlSignalsFunction::Work() { | 320 void SerialGetControlSignalsFunction::Work() { |
321 DictionaryValue *result = new DictionaryValue(); | 321 base::DictionaryValue *result = new base::DictionaryValue(); |
322 SerialConnection* serial_connection = GetSerialConnection( | 322 SerialConnection* serial_connection = GetSerialConnection( |
323 params_->connection_id); | 323 params_->connection_id); |
324 if (serial_connection) { | 324 if (serial_connection) { |
325 SerialConnection::ControlSignals control_signals = { 0 }; | 325 SerialConnection::ControlSignals control_signals = { 0 }; |
326 if (serial_connection->GetControlSignals(control_signals)) { | 326 if (serial_connection->GetControlSignals(control_signals)) { |
327 api_response_ = true; | 327 api_response_ = true; |
328 result->SetBoolean(kDcdKey, control_signals.dcd); | 328 result->SetBoolean(kDcdKey, control_signals.dcd); |
329 result->SetBoolean(kCtsKey, control_signals.cts); | 329 result->SetBoolean(kCtsKey, control_signals.cts); |
330 } else { | 330 } else { |
331 error_ = kErrorGetControlSignalsFailed; | 331 error_ = kErrorGetControlSignalsFailed; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 error_ = kSerialConnectionNotFoundError; | 378 error_ = kSerialConnectionNotFoundError; |
379 SetResult(Value::CreateBooleanValue(false)); | 379 SetResult(Value::CreateBooleanValue(false)); |
380 } | 380 } |
381 } | 381 } |
382 | 382 |
383 bool SerialSetControlSignalsFunction::Respond() { | 383 bool SerialSetControlSignalsFunction::Respond() { |
384 return true; | 384 return true; |
385 } | 385 } |
386 | 386 |
387 } // namespace extensions | 387 } // namespace extensions |
OLD | NEW |