| 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/api/api_resource_controller.h" | 8 #include "chrome/browser/extensions/api/api_resource_controller.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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 } | 81 } |
| 82 | 82 |
| 83 void SerialOpenFunction::AsyncWorkStart() { | 83 void SerialOpenFunction::AsyncWorkStart() { |
| 84 Work(); | 84 Work(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void SerialOpenFunction::Work() { | 87 void SerialOpenFunction::Work() { |
| 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 89 const SerialPortEnumerator::StringSet name_set( | 89 const SerialPortEnumerator::StringSet name_set( |
| 90 SerialPortEnumerator::GenerateValidSerialPortNames()); | 90 SerialPortEnumerator::GenerateValidSerialPortNames()); |
| 91 if (SerialPortEnumerator::DoesPortExist(name_set, params_->port)) { | 91 if (DoesPortExist(params_->port)) { |
| 92 SerialConnection* serial_connection = new SerialConnection( | 92 SerialConnection* serial_connection = CreateSerialConnection( |
| 93 params_->port, | 93 params_->port, |
| 94 bitrate_, | 94 bitrate_, |
| 95 event_notifier_); | 95 event_notifier_); |
| 96 CHECK(serial_connection); | 96 CHECK(serial_connection); |
| 97 int id = controller()->AddAPIResource(serial_connection); | 97 int id = controller()->AddAPIResource(serial_connection); |
| 98 CHECK(id); | 98 CHECK(id); |
| 99 | 99 |
| 100 bool open_result = serial_connection->Open(); | 100 bool open_result = serial_connection->Open(); |
| 101 if (!open_result) { | 101 if (!open_result) { |
| 102 serial_connection->Close(); | 102 serial_connection->Close(); |
| 103 controller()->RemoveSerialConnection(id); | 103 controller()->RemoveSerialConnection(id); |
| 104 id = -1; | 104 id = -1; |
| 105 } | 105 } |
| 106 | 106 |
| 107 DictionaryValue* result = new DictionaryValue(); | 107 DictionaryValue* result = new DictionaryValue(); |
| 108 result->SetInteger(kConnectionIdKey, id); | 108 result->SetInteger(kConnectionIdKey, id); |
| 109 result_.reset(result); | 109 result_.reset(result); |
| 110 AsyncWorkCompleted(); | 110 AsyncWorkCompleted(); |
| 111 } else { | 111 } else { |
| 112 DictionaryValue* result = new DictionaryValue(); | 112 DictionaryValue* result = new DictionaryValue(); |
| 113 result->SetInteger(kConnectionIdKey, -1); | 113 result->SetInteger(kConnectionIdKey, -1); |
| 114 result_.reset(result); | 114 result_.reset(result); |
| 115 AsyncWorkCompleted(); | 115 AsyncWorkCompleted(); |
| 116 } | 116 } |
| 117 } | 117 } |
| 118 | 118 |
| 119 SerialConnection* SerialOpenFunction::CreateSerialConnection( |
| 120 const std::string& port, |
| 121 int bitrate, |
| 122 APIResourceEventNotifier* event_notifier) { |
| 123 return new SerialConnection(port, bitrate, event_notifier); |
| 124 } |
| 125 |
| 126 bool SerialOpenFunction::DoesPortExist(const std::string& port) { |
| 127 const SerialPortEnumerator::StringSet name_set( |
| 128 SerialPortEnumerator::GenerateValidSerialPortNames()); |
| 129 return SerialPortEnumerator::DoesPortExist(name_set, params_->port); |
| 130 } |
| 131 |
| 119 bool SerialOpenFunction::Respond() { | 132 bool SerialOpenFunction::Respond() { |
| 120 return true; | 133 return true; |
| 121 } | 134 } |
| 122 | 135 |
| 123 SerialCloseFunction::SerialCloseFunction() { | 136 SerialCloseFunction::SerialCloseFunction() { |
| 124 } | 137 } |
| 125 | 138 |
| 126 SerialCloseFunction::~SerialCloseFunction() { | 139 SerialCloseFunction::~SerialCloseFunction() { |
| 127 } | 140 } |
| 128 | 141 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 } | 263 } |
| 251 | 264 |
| 252 result_.reset(Value::CreateBooleanValue(flush_result)); | 265 result_.reset(Value::CreateBooleanValue(flush_result)); |
| 253 } | 266 } |
| 254 | 267 |
| 255 bool SerialFlushFunction::Respond() { | 268 bool SerialFlushFunction::Respond() { |
| 256 return true; | 269 return true; |
| 257 } | 270 } |
| 258 | 271 |
| 259 } // namespace extensions | 272 } // namespace extensions |
| OLD | NEW |