| 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/devices_app/devices_app.h" | 5 #include "device/devices_app/devices_app.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
| 11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "device/core/device_client.h" | 14 #include "device/core/device_client.h" |
| 15 #include "device/devices_app/usb/device_manager_impl.h" | 15 #include "device/devices_app/usb/device_manager_impl.h" |
| 16 #include "device/devices_app/usb/public/cpp/device_manager_delegate.h" | 16 #include "device/devices_app/usb/public/cpp/device_manager_delegate.h" |
| 17 #include "device/usb/usb_service.h" | 17 #include "device/usb/usb_service.h" |
| 18 #include "mojo/application/public/cpp/application_connection.h" | 18 #include "mojo/application/public/cpp/application_connection.h" |
| 19 #include "mojo/application/public/cpp/application_impl.h" | 19 #include "mojo/application/public/cpp/application_impl.h" |
| 20 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" | |
| 21 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" | 20 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" |
| 22 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 23 | 22 |
| 24 namespace device { | 23 namespace device { |
| 25 | 24 |
| 26 namespace { | 25 namespace { |
| 27 | 26 |
| 28 // The number of seconds to wait without any bound DeviceManagers before | 27 // The number of seconds to wait without any bound DeviceManagers before |
| 29 // exiting the app. | 28 // exiting the app. |
| 30 const int64 kIdleTimeoutInSeconds = 10; | 29 const int64 kIdleTimeoutInSeconds = 10; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 } | 128 } |
| 130 | 129 |
| 131 void DevicesApp::Create(mojo::ApplicationConnection* connection, | 130 void DevicesApp::Create(mojo::ApplicationConnection* connection, |
| 132 mojo::InterfaceRequest<usb::DeviceManager> request) { | 131 mojo::InterfaceRequest<usb::DeviceManager> request) { |
| 133 scoped_ptr<usb::DeviceManagerDelegate> delegate(new USBDeviceManagerDelegate( | 132 scoped_ptr<usb::DeviceManagerDelegate> delegate(new USBDeviceManagerDelegate( |
| 134 GURL(connection->GetRemoteApplicationURL()))); | 133 GURL(connection->GetRemoteApplicationURL()))); |
| 135 | 134 |
| 136 // Owned by its message pipe. | 135 // Owned by its message pipe. |
| 137 usb::DeviceManagerImpl* device_manager = new usb::DeviceManagerImpl( | 136 usb::DeviceManagerImpl* device_manager = new usb::DeviceManagerImpl( |
| 138 request.Pass(), delegate.Pass(), service_task_runner_); | 137 request.Pass(), delegate.Pass(), service_task_runner_); |
| 139 device_manager->set_error_handler(this); | 138 device_manager->set_connection_error_handler( |
| 139 base::Bind(&DevicesApp::OnConnectionError, base::Unretained(this))); |
| 140 | 140 |
| 141 active_device_manager_count_++; | 141 active_device_manager_count_++; |
| 142 idle_timeout_callback_.Cancel(); | 142 idle_timeout_callback_.Cancel(); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void DevicesApp::OnConnectionError() { | 145 void DevicesApp::OnConnectionError() { |
| 146 DCHECK_GE(active_device_manager_count_, 0u); | 146 DCHECK_GE(active_device_manager_count_, 0u); |
| 147 active_device_manager_count_--; | 147 active_device_manager_count_--; |
| 148 if (active_device_manager_count_ == 0) { | 148 if (active_device_manager_count_ == 0) { |
| 149 // If the last DeviceManager connection has been dropped, kick off an idle | 149 // If the last DeviceManager connection has been dropped, kick off an idle |
| 150 // timeout to shut ourselves down. | 150 // timeout to shut ourselves down. |
| 151 StartIdleTimer(); | 151 StartIdleTimer(); |
| 152 } | 152 } |
| 153 } | 153 } |
| 154 | 154 |
| 155 void DevicesApp::StartIdleTimer() { | 155 void DevicesApp::StartIdleTimer() { |
| 156 // Passing unretained |app_impl_| is safe here because |app_impl_| is | 156 // Passing unretained |app_impl_| is safe here because |app_impl_| is |
| 157 // guaranteed to outlive |this|, and the callback is canceled if |this| is | 157 // guaranteed to outlive |this|, and the callback is canceled if |this| is |
| 158 // destroyed. | 158 // destroyed. |
| 159 idle_timeout_callback_.Reset(base::Bind(&mojo::ApplicationImpl::Terminate, | 159 idle_timeout_callback_.Reset(base::Bind(&mojo::ApplicationImpl::Terminate, |
| 160 base::Unretained(app_impl_))); | 160 base::Unretained(app_impl_))); |
| 161 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 161 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 162 FROM_HERE, idle_timeout_callback_.callback(), | 162 FROM_HERE, idle_timeout_callback_.callback(), |
| 163 base::TimeDelta::FromSeconds(kIdleTimeoutInSeconds)); | 163 base::TimeDelta::FromSeconds(kIdleTimeoutInSeconds)); |
| 164 } | 164 } |
| 165 | 165 |
| 166 } // namespace device | 166 } // namespace device |
| OLD | NEW |