| 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_connection.h" | 5 #include "chrome/browser/extensions/api/serial/serial_connection.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "chrome/browser/extensions/api/api_resource_manager.h" | 13 #include "chrome/browser/extensions/api/api_resource_manager.h" |
| 14 #include "chrome/browser/extensions/api/serial/serial_port_enumerator.h" | |
| 15 #include "chrome/common/extensions/api/serial.h" | 14 #include "chrome/common/extensions/api/serial.h" |
| 16 | 15 |
| 17 namespace extensions { | 16 namespace extensions { |
| 18 | 17 |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 const int kDefaultBufferSize = 4096; | 20 const int kDefaultBufferSize = 4096; |
| 22 | 21 |
| 23 } | 22 } |
| 24 | 23 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 info->buffer_size = buffer_size_; | 158 info->buffer_size = buffer_size_; |
| 160 info->receive_timeout = receive_timeout_; | 159 info->receive_timeout = receive_timeout_; |
| 161 info->send_timeout = send_timeout_; | 160 info->send_timeout = send_timeout_; |
| 162 return GetPortInfo(info); | 161 return GetPortInfo(info); |
| 163 } | 162 } |
| 164 | 163 |
| 165 void SerialConnection::StartOpen() { | 164 void SerialConnection::StartOpen() { |
| 166 DCHECK(!open_complete_.is_null()); | 165 DCHECK(!open_complete_.is_null()); |
| 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 168 DCHECK_EQ(file_, base::kInvalidPlatformFileValue); | 167 DCHECK_EQ(file_, base::kInvalidPlatformFileValue); |
| 169 const SerialPortEnumerator::StringSet name_set( | |
| 170 SerialPortEnumerator::GenerateValidSerialPortNames()); | |
| 171 base::PlatformFile file = base::kInvalidPlatformFileValue; | 168 base::PlatformFile file = base::kInvalidPlatformFileValue; |
| 172 if (SerialPortEnumerator::DoesPortExist(name_set, port_)) { | 169 // It's the responsibility of the API wrapper around SerialConnection to |
| 173 // It's the responsibility of the API wrapper around SerialConnection to | 170 // validate the supplied path against the set of valid port names, and |
| 174 // validate the supplied path against the set of valid port names, and | 171 // it is a reasonable assumption that serial port names are ASCII. |
| 175 // it is a reasonable assumption that serial port names are ASCII. | 172 DCHECK(IsStringASCII(port_)); |
| 176 DCHECK(IsStringASCII(port_)); | 173 base::FilePath path( |
| 177 base::FilePath path( | 174 base::FilePath::FromUTF8Unsafe(MaybeFixUpPortName(port_))); |
| 178 base::FilePath::FromUTF8Unsafe(MaybeFixUpPortName(port_))); | 175 int flags = base::PLATFORM_FILE_OPEN | |
| 179 int flags = base::PLATFORM_FILE_OPEN | | 176 base::PLATFORM_FILE_READ | |
| 180 base::PLATFORM_FILE_READ | | 177 base::PLATFORM_FILE_EXCLUSIVE_READ | |
| 181 base::PLATFORM_FILE_EXCLUSIVE_READ | | 178 base::PLATFORM_FILE_WRITE | |
| 182 base::PLATFORM_FILE_WRITE | | 179 base::PLATFORM_FILE_EXCLUSIVE_WRITE | |
| 183 base::PLATFORM_FILE_EXCLUSIVE_WRITE | | 180 base::PLATFORM_FILE_ASYNC | |
| 184 base::PLATFORM_FILE_ASYNC | | 181 base::PLATFORM_FILE_TERMINAL_DEVICE; |
| 185 base::PLATFORM_FILE_TERMINAL_DEVICE; | 182 file = base::CreatePlatformFile(path, flags, NULL, NULL); |
| 186 file = base::CreatePlatformFile(path, flags, NULL, NULL); | |
| 187 } | |
| 188 BrowserThread::PostTask( | 183 BrowserThread::PostTask( |
| 189 BrowserThread::IO, FROM_HERE, | 184 BrowserThread::IO, FROM_HERE, |
| 190 base::Bind(&SerialConnection::FinishOpen, base::Unretained(this), file)); | 185 base::Bind(&SerialConnection::FinishOpen, base::Unretained(this), file)); |
| 191 } | 186 } |
| 192 | 187 |
| 193 void SerialConnection::FinishOpen(base::PlatformFile file) { | 188 void SerialConnection::FinishOpen(base::PlatformFile file) { |
| 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 195 DCHECK(!open_complete_.is_null()); | 190 DCHECK(!open_complete_.is_null()); |
| 196 DCHECK_EQ(file_, base::kInvalidPlatformFileValue); | 191 DCHECK_EQ(file_, base::kInvalidPlatformFileValue); |
| 197 OpenCompleteCallback callback = open_complete_; | 192 OpenCompleteCallback callback = open_complete_; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 delay_); | 260 delay_); |
| 266 } | 261 } |
| 267 | 262 |
| 268 SerialConnection::TimeoutTask::~TimeoutTask() {} | 263 SerialConnection::TimeoutTask::~TimeoutTask() {} |
| 269 | 264 |
| 270 void SerialConnection::TimeoutTask::Run() const { | 265 void SerialConnection::TimeoutTask::Run() const { |
| 271 closure_.Run(); | 266 closure_.Run(); |
| 272 } | 267 } |
| 273 | 268 |
| 274 } // namespace extensions | 269 } // namespace extensions |
| OLD | NEW |