Chromium Code Reviews| 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" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 | 12 |
| 13 using content::BrowserThread; | 13 using content::BrowserThread; |
| 14 | 14 |
| 15 namespace serial = extensions::api::serial; | |
| 16 | |
| 15 namespace extensions { | 17 namespace extensions { |
| 16 | 18 |
| 17 const char kConnectionIdKey[] = "connectionId"; | 19 const char kConnectionIdKey[] = "connectionId"; |
| 18 const char kDataKey[] = "data"; | 20 const char kDataKey[] = "data"; |
| 19 const char kBytesReadKey[] = "bytesRead"; | 21 const char kBytesReadKey[] = "bytesRead"; |
| 20 const char kBytesWrittenKey[] = "bytesWritten"; | 22 const char kBytesWrittenKey[] = "bytesWritten"; |
| 21 const char kBitrateKey[] = "bitrate"; | 23 const char kBitrateKey[] = "bitrate"; |
| 24 const char kDataBitKey[] = "dataBit"; | |
| 25 const char kParityKey[] = "parityBit"; | |
| 26 const char kStopBitKey[] = "stopBit"; | |
| 22 const char kSuccessKey[] = "success"; | 27 const char kSuccessKey[] = "success"; |
| 23 const char kDcdKey[] = "dcd"; | 28 const char kDcdKey[] = "dcd"; |
| 24 const char kCtsKey[] = "cts"; | 29 const char kCtsKey[] = "cts"; |
| 25 | 30 |
| 26 const char kErrorGetControlSignalsFailed[] = "Failed to get control signals."; | 31 const char kErrorGetControlSignalsFailed[] = "Failed to get control signals."; |
| 27 const char kErrorSetControlSignalsFailed[] = "Failed to set control signals."; | 32 const char kErrorSetControlSignalsFailed[] = "Failed to set control signals."; |
| 28 const char kSerialReadInvalidBytesToRead[] = "Number of bytes to read must " | 33 const char kSerialReadInvalidBytesToRead[] = "Number of bytes to read must " |
| 29 "be a positive number less than 1,048,576."; | 34 "be a positive number less than 1,048,576."; |
| 30 | 35 |
| 31 SerialAsyncApiFunction::SerialAsyncApiFunction() | 36 SerialAsyncApiFunction::SerialAsyncApiFunction() |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 } | 81 } |
| 77 | 82 |
| 78 // It's a fool's errand to come up with a default bitrate, because we don't get | 83 // It's a fool's errand to come up with a default bitrate, because we don't get |
| 79 // to control both sides of the communication. Unless the other side has | 84 // to control both sides of the communication. Unless the other side has |
| 80 // implemented auto-bitrate detection (rare), if we pick the wrong rate, then | 85 // implemented auto-bitrate detection (rare), if we pick the wrong rate, then |
| 81 // you're gonna have a bad time. Close doesn't count. | 86 // you're gonna have a bad time. Close doesn't count. |
| 82 // | 87 // |
| 83 // But we'd like to pick something that has a chance of working, and 9600 is a | 88 // But we'd like to pick something that has a chance of working, and 9600 is a |
| 84 // good balance between popularity and speed. So 9600 it is. | 89 // good balance between popularity and speed. So 9600 it is. |
| 85 SerialOpenFunction::SerialOpenFunction() | 90 SerialOpenFunction::SerialOpenFunction() |
| 86 : bitrate_(9600) { | 91 : bitrate_(9600), databit_(serial::DATA_BIT_EIGHTBIT), |
| 92 parity_(serial::PARITY_BIT_NOPARITY), | |
| 93 stopbit_(serial::STOP_BIT_ONESTOPBIT) { | |
| 87 } | 94 } |
| 88 | 95 |
| 89 SerialOpenFunction::~SerialOpenFunction() { | 96 SerialOpenFunction::~SerialOpenFunction() { |
| 90 } | 97 } |
| 91 | 98 |
| 92 bool SerialOpenFunction::Prepare() { | 99 bool SerialOpenFunction::Prepare() { |
| 93 set_work_thread_id(BrowserThread::FILE); | 100 set_work_thread_id(BrowserThread::FILE); |
| 94 | 101 |
| 95 params_ = api::serial::Open::Params::Create(*args_); | 102 params_ = api::serial::Open::Params::Create(*args_); |
| 96 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 103 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 97 | 104 |
| 98 if (params_->options.get()) { | 105 if (params_->options.get()) { |
| 99 scoped_ptr<base::DictionaryValue> options = params_->options->ToValue(); | 106 scoped_ptr<base::DictionaryValue> options = params_->options->ToValue(); |
| 100 if (options->HasKey(kBitrateKey)) | 107 if (options->HasKey(kBitrateKey)) |
| 101 EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kBitrateKey, &bitrate_)); | 108 EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kBitrateKey, &bitrate_)); |
| 109 if (options->HasKey(kDataBitKey)) { | |
| 110 std::string data; | |
| 111 EXTENSION_FUNCTION_VALIDATE(options->GetString(kDataBitKey, &data)); | |
|
miket_OOO
2013/08/21 19:43:44
What exactly does E_F_V do here? You've checked th
limasdf
2013/08/24 15:46:24
It seems unnecessary. Removed.
| |
| 112 if (!data.empty()) { | |
| 113 databit_ = serial::ParseDataBit(data); | |
|
miket_OOO
2013/08/21 19:43:44
no braces needed around these one-line blocks.
| |
| 114 } | |
| 115 } | |
| 116 if (options->HasKey(kParityKey)) { | |
| 117 std::string parity; | |
| 118 EXTENSION_FUNCTION_VALIDATE(options->GetString(kParityKey, &parity)); | |
| 119 if (!parity.empty()) { | |
| 120 parity_ = serial::ParseParityBit(parity); | |
| 121 } | |
| 122 } | |
| 123 if (options->HasKey(kStopBitKey)) { | |
| 124 std::string stopbit; | |
| 125 EXTENSION_FUNCTION_VALIDATE(options->GetString(kStopBitKey, &stopbit)); | |
| 126 if (!stopbit.empty()) { | |
| 127 stopbit_ = serial::ParseStopBit(stopbit); | |
| 128 } | |
| 129 } | |
| 102 } | 130 } |
| 103 | 131 |
| 104 return true; | 132 return true; |
| 105 } | 133 } |
| 106 | 134 |
| 107 void SerialOpenFunction::AsyncWorkStart() { | 135 void SerialOpenFunction::AsyncWorkStart() { |
| 108 Work(); | 136 Work(); |
| 109 } | 137 } |
| 110 | 138 |
| 111 void SerialOpenFunction::Work() { | 139 void SerialOpenFunction::Work() { |
| 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 113 const SerialPortEnumerator::StringSet name_set( | 141 const SerialPortEnumerator::StringSet name_set( |
| 114 SerialPortEnumerator::GenerateValidSerialPortNames()); | 142 SerialPortEnumerator::GenerateValidSerialPortNames()); |
| 115 if (DoesPortExist(params_->port)) { | 143 if (DoesPortExist(params_->port)) { |
| 116 SerialConnection* serial_connection = CreateSerialConnection( | 144 SerialConnection* serial_connection = CreateSerialConnection( |
| 117 params_->port, | 145 params_->port, |
| 118 bitrate_, | 146 bitrate_, |
| 147 databit_, | |
| 148 parity_, | |
| 149 stopbit_, | |
| 119 extension_->id()); | 150 extension_->id()); |
| 120 CHECK(serial_connection); | 151 CHECK(serial_connection); |
| 121 int id = manager_->Add(serial_connection); | 152 int id = manager_->Add(serial_connection); |
| 122 CHECK(id); | 153 CHECK(id); |
| 123 | 154 |
| 124 bool open_result = serial_connection->Open(); | 155 bool open_result = serial_connection->Open(); |
| 125 if (!open_result) { | 156 if (!open_result) { |
| 126 serial_connection->Close(); | 157 serial_connection->Close(); |
| 127 RemoveSerialConnection(id); | 158 RemoveSerialConnection(id); |
| 128 id = -1; | 159 id = -1; |
| 129 } | 160 } |
| 130 | 161 |
| 131 base::DictionaryValue* result = new base::DictionaryValue(); | 162 base::DictionaryValue* result = new base::DictionaryValue(); |
| 132 result->SetInteger(kConnectionIdKey, id); | 163 result->SetInteger(kConnectionIdKey, id); |
| 133 SetResult(result); | 164 SetResult(result); |
| 134 AsyncWorkCompleted(); | 165 AsyncWorkCompleted(); |
| 135 } else { | 166 } else { |
| 136 base::DictionaryValue* result = new base::DictionaryValue(); | 167 base::DictionaryValue* result = new base::DictionaryValue(); |
| 137 result->SetInteger(kConnectionIdKey, -1); | 168 result->SetInteger(kConnectionIdKey, -1); |
| 138 SetResult(result); | 169 SetResult(result); |
| 139 AsyncWorkCompleted(); | 170 AsyncWorkCompleted(); |
| 140 } | 171 } |
| 141 } | 172 } |
| 142 | 173 |
| 143 SerialConnection* SerialOpenFunction::CreateSerialConnection( | 174 SerialConnection* SerialOpenFunction::CreateSerialConnection( |
| 144 const std::string& port, | 175 const std::string& port, |
| 145 int bitrate, | 176 int bitrate, |
| 177 serial::DataBit databit, | |
| 178 serial::ParityBit parity, | |
| 179 serial::StopBit stopbit, | |
| 146 const std::string& owner_extension_id) { | 180 const std::string& owner_extension_id) { |
| 147 return new SerialConnection(port, bitrate, owner_extension_id); | 181 return new SerialConnection(port, bitrate, databit, parity, stopbit, |
| 182 owner_extension_id); | |
| 148 } | 183 } |
| 149 | 184 |
| 150 bool SerialOpenFunction::DoesPortExist(const std::string& port) { | 185 bool SerialOpenFunction::DoesPortExist(const std::string& port) { |
| 151 const SerialPortEnumerator::StringSet name_set( | 186 const SerialPortEnumerator::StringSet name_set( |
| 152 SerialPortEnumerator::GenerateValidSerialPortNames()); | 187 SerialPortEnumerator::GenerateValidSerialPortNames()); |
| 153 return SerialPortEnumerator::DoesPortExist(name_set, params_->port); | 188 return SerialPortEnumerator::DoesPortExist(name_set, params_->port); |
| 154 } | 189 } |
| 155 | 190 |
| 156 bool SerialOpenFunction::Respond() { | 191 bool SerialOpenFunction::Respond() { |
| 157 return true; | 192 return true; |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 error_ = kSerialConnectionNotFoundError; | 413 error_ = kSerialConnectionNotFoundError; |
| 379 SetResult(new base::FundamentalValue(false)); | 414 SetResult(new base::FundamentalValue(false)); |
| 380 } | 415 } |
| 381 } | 416 } |
| 382 | 417 |
| 383 bool SerialSetControlSignalsFunction::Respond() { | 418 bool SerialSetControlSignalsFunction::Respond() { |
| 384 return true; | 419 return true; |
| 385 } | 420 } |
| 386 | 421 |
| 387 } // namespace extensions | 422 } // namespace extensions |
| OLD | NEW |