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 "device/usb/mojo/device_impl.h" | 5 #include "device/usb/mojo/device_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <numeric> | 10 #include <numeric> |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 scoped_refptr<net::IOBuffer> buffer, | 172 scoped_refptr<net::IOBuffer> buffer, |
173 const std::vector<UsbDeviceHandle::IsochronousPacket>& packets) { | 173 const std::vector<UsbDeviceHandle::IsochronousPacket>& packets) { |
174 callback->Run(mojo::Array<IsochronousPacketPtr>::From(packets)); | 174 callback->Run(mojo::Array<IsochronousPacketPtr>::From(packets)); |
175 } | 175 } |
176 | 176 |
177 } // namespace | 177 } // namespace |
178 | 178 |
179 DeviceImpl::DeviceImpl(scoped_refptr<UsbDevice> device, | 179 DeviceImpl::DeviceImpl(scoped_refptr<UsbDevice> device, |
180 PermissionProviderPtr permission_provider, | 180 PermissionProviderPtr permission_provider, |
181 mojo::InterfaceRequest<Device> request) | 181 mojo::InterfaceRequest<Device> request) |
182 : binding_(this, std::move(request)), | 182 : device_(device), |
183 device_(device), | 183 observer_(this), |
184 permission_provider_(std::move(permission_provider)), | 184 permission_provider_(std::move(permission_provider)), |
| 185 binding_(this, std::move(request)), |
185 weak_factory_(this) { | 186 weak_factory_(this) { |
186 // This object owns itself and will be destroyed if either the message pipe | 187 DCHECK(device_); |
187 // it is bound to is closed or the PermissionProvider it depends on is | 188 // This object owns itself and will be destroyed if, |
188 // unavailable. | 189 // * the device is disconnected, |
| 190 // * the message pipe it is bound to is closed, or |
| 191 // * the PermissionProvider it depends on is unavailable. |
| 192 observer_.Add(device_.get()); |
189 binding_.set_connection_error_handler([this]() { delete this; }); | 193 binding_.set_connection_error_handler([this]() { delete this; }); |
190 permission_provider_.set_connection_error_handler([this]() { delete this; }); | 194 permission_provider_.set_connection_error_handler([this]() { delete this; }); |
191 } | 195 } |
192 | 196 |
193 DeviceImpl::~DeviceImpl() { | 197 DeviceImpl::~DeviceImpl() { |
194 CloseHandle(); | 198 CloseHandle(); |
195 } | 199 } |
196 | 200 |
197 void DeviceImpl::CloseHandle() { | 201 void DeviceImpl::CloseHandle() { |
198 if (device_handle_) | 202 if (device_handle_) |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size()); | 460 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size()); |
457 { | 461 { |
458 const std::vector<uint8_t>& storage = data.storage(); | 462 const std::vector<uint8_t>& storage = data.storage(); |
459 std::copy(storage.begin(), storage.end(), buffer->data()); | 463 std::copy(storage.begin(), storage.end(), buffer->data()); |
460 } | 464 } |
461 device_handle_->IsochronousTransferOut( | 465 device_handle_->IsochronousTransferOut( |
462 endpoint_address, buffer, packet_lengths.storage(), timeout, | 466 endpoint_address, buffer, packet_lengths.storage(), timeout, |
463 base::Bind(&OnIsochronousTransferOut, base::Passed(&callback_ptr))); | 467 base::Bind(&OnIsochronousTransferOut, base::Passed(&callback_ptr))); |
464 } | 468 } |
465 | 469 |
| 470 void DeviceImpl::OnDeviceRemoved(scoped_refptr<UsbDevice> device) { |
| 471 DCHECK_EQ(device_, device); |
| 472 delete this; |
| 473 } |
| 474 |
466 } // namespace usb | 475 } // namespace usb |
467 } // namespace device | 476 } // namespace device |
OLD | NEW |