| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "extensions/browser/api/webcam_private/visca_webcam.h" | 5 #include "extensions/browser/api/webcam_private/visca_webcam.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 | 10 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 void ViscaWebcam::Open(const OpenCompleteCallback& open_callback) { | 104 void ViscaWebcam::Open(const OpenCompleteCallback& open_callback) { |
| 105 BrowserThread::PostTask( | 105 BrowserThread::PostTask( |
| 106 BrowserThread::IO, FROM_HERE, | 106 BrowserThread::IO, FROM_HERE, |
| 107 base::Bind(&ViscaWebcam::OpenOnIOThread, weak_ptr_factory_.GetWeakPtr(), | 107 base::Bind(&ViscaWebcam::OpenOnIOThread, weak_ptr_factory_.GetWeakPtr(), |
| 108 open_callback)); | 108 open_callback)); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void ViscaWebcam::OpenOnIOThread(const OpenCompleteCallback& open_callback) { | 111 void ViscaWebcam::OpenOnIOThread(const OpenCompleteCallback& open_callback) { |
| 112 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 112 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 113 | 113 |
| 114 core_api::serial::ConnectionOptions options; | 114 api::serial::ConnectionOptions options; |
| 115 | 115 |
| 116 // Set the receive buffer size to receive the response data 1 by 1. | 116 // Set the receive buffer size to receive the response data 1 by 1. |
| 117 options.buffer_size.reset(new int(1)); | 117 options.buffer_size.reset(new int(1)); |
| 118 options.persistent.reset(new bool(false)); | 118 options.persistent.reset(new bool(false)); |
| 119 options.bitrate.reset(new int(9600)); | 119 options.bitrate.reset(new int(9600)); |
| 120 options.cts_flow_control.reset(new bool(false)); | 120 options.cts_flow_control.reset(new bool(false)); |
| 121 options.receive_timeout.reset(new int(0)); | 121 options.receive_timeout.reset(new int(0)); |
| 122 options.send_timeout.reset(new int(0)); | 122 options.send_timeout.reset(new int(0)); |
| 123 options.data_bits = core_api::serial::DATA_BITS_EIGHT; | 123 options.data_bits = api::serial::DATA_BITS_EIGHT; |
| 124 options.parity_bit = core_api::serial::PARITY_BIT_NO; | 124 options.parity_bit = api::serial::PARITY_BIT_NO; |
| 125 options.stop_bits = core_api::serial::STOP_BITS_ONE; | 125 options.stop_bits = api::serial::STOP_BITS_ONE; |
| 126 | 126 |
| 127 serial_connection_.reset(new SerialConnection(path_, extension_id_)); | 127 serial_connection_.reset(new SerialConnection(path_, extension_id_)); |
| 128 serial_connection_->Open( | 128 serial_connection_->Open( |
| 129 options, base::Bind(&ViscaWebcam::OnConnected, | 129 options, base::Bind(&ViscaWebcam::OnConnected, |
| 130 weak_ptr_factory_.GetWeakPtr(), open_callback)); | 130 weak_ptr_factory_.GetWeakPtr(), open_callback)); |
| 131 } | 131 } |
| 132 | 132 |
| 133 void ViscaWebcam::OnConnected(const OpenCompleteCallback& open_callback, | 133 void ViscaWebcam::OnConnected(const OpenCompleteCallback& open_callback, |
| 134 bool success) { | 134 bool success) { |
| 135 if (!success) { | 135 if (!success) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 if (!commands_.empty() || | 178 if (!commands_.empty() || |
| 179 !serial_connection_->Send( | 179 !serial_connection_->Send( |
| 180 command, base::Bind(&ViscaWebcam::OnSendCompleted, | 180 command, base::Bind(&ViscaWebcam::OnSendCompleted, |
| 181 weak_ptr_factory_.GetWeakPtr(), callback))) { | 181 weak_ptr_factory_.GetWeakPtr(), callback))) { |
| 182 commands_.push_back(std::make_pair(command, callback)); | 182 commands_.push_back(std::make_pair(command, callback)); |
| 183 } | 183 } |
| 184 } | 184 } |
| 185 | 185 |
| 186 void ViscaWebcam::OnSendCompleted(const CommandCompleteCallback& callback, | 186 void ViscaWebcam::OnSendCompleted(const CommandCompleteCallback& callback, |
| 187 int bytes_sent, | 187 int bytes_sent, |
| 188 core_api::serial::SendError error) { | 188 api::serial::SendError error) { |
| 189 if (error == core_api::serial::SEND_ERROR_NONE) { | 189 if (error == api::serial::SEND_ERROR_NONE) { |
| 190 ReceiveLoop(callback); | 190 ReceiveLoop(callback); |
| 191 } else { | 191 } else { |
| 192 const std::vector<char> response; | 192 const std::vector<char> response; |
| 193 callback.Run(false, response); | 193 callback.Run(false, response); |
| 194 } | 194 } |
| 195 } | 195 } |
| 196 | 196 |
| 197 void ViscaWebcam::ReceiveLoop(const CommandCompleteCallback& callback) { | 197 void ViscaWebcam::ReceiveLoop(const CommandCompleteCallback& callback) { |
| 198 serial_connection_->Receive(base::Bind(&ViscaWebcam::OnReceiveCompleted, | 198 serial_connection_->Receive(base::Bind(&ViscaWebcam::OnReceiveCompleted, |
| 199 weak_ptr_factory_.GetWeakPtr(), | 199 weak_ptr_factory_.GetWeakPtr(), |
| 200 callback)); | 200 callback)); |
| 201 } | 201 } |
| 202 | 202 |
| 203 void ViscaWebcam::OnReceiveCompleted(const CommandCompleteCallback& callback, | 203 void ViscaWebcam::OnReceiveCompleted(const CommandCompleteCallback& callback, |
| 204 const std::vector<char>& data, | 204 const std::vector<char>& data, |
| 205 core_api::serial::ReceiveError error) { | 205 api::serial::ReceiveError error) { |
| 206 data_buffer_.insert(data_buffer_.end(), data.begin(), data.end()); | 206 data_buffer_.insert(data_buffer_.end(), data.begin(), data.end()); |
| 207 | 207 |
| 208 if (error == core_api::serial::RECEIVE_ERROR_NONE) { | 208 if (error == api::serial::RECEIVE_ERROR_NONE) { |
| 209 // Loop until encounter the terminator. | 209 // Loop until encounter the terminator. |
| 210 if (int(data_buffer_.back()) != VISCA_TERMINATOR) { | 210 if (int(data_buffer_.back()) != VISCA_TERMINATOR) { |
| 211 base::MessageLoop::current()->PostTask( | 211 base::MessageLoop::current()->PostTask( |
| 212 FROM_HERE, base::Bind(&ViscaWebcam::ReceiveLoop, | 212 FROM_HERE, base::Bind(&ViscaWebcam::ReceiveLoop, |
| 213 weak_ptr_factory_.GetWeakPtr(), callback)); | 213 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 214 } else { | 214 } else { |
| 215 // Clear |data_buffer_|. | 215 // Clear |data_buffer_|. |
| 216 std::vector<char> response; | 216 std::vector<char> response; |
| 217 response.swap(data_buffer_); | 217 response.swap(data_buffer_); |
| 218 | 218 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 case TILT_DOWN: | 390 case TILT_DOWN: |
| 391 command = kPTDownCommand; | 391 command = kPTDownCommand; |
| 392 break; | 392 break; |
| 393 } | 393 } |
| 394 Send(command, base::Bind(&ViscaWebcam::OnCommandCompleted, | 394 Send(command, base::Bind(&ViscaWebcam::OnCommandCompleted, |
| 395 weak_ptr_factory_.GetWeakPtr(), COMMAND, &tilt_)); | 395 weak_ptr_factory_.GetWeakPtr(), COMMAND, &tilt_)); |
| 396 return true; | 396 return true; |
| 397 } | 397 } |
| 398 | 398 |
| 399 } // namespace extensions | 399 } // namespace extensions |
| OLD | NEW |