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" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 ~USBServiceInitializer() {} | 66 ~USBServiceInitializer() {} |
67 | 67 |
68 private: | 68 private: |
69 scoped_ptr<AppDeviceClient> app_device_client_; | 69 scoped_ptr<AppDeviceClient> app_device_client_; |
70 base::Thread blocking_thread_; | 70 base::Thread blocking_thread_; |
71 | 71 |
72 DISALLOW_COPY_AND_ASSIGN(USBServiceInitializer); | 72 DISALLOW_COPY_AND_ASSIGN(USBServiceInitializer); |
73 }; | 73 }; |
74 | 74 |
75 DevicesApp::DevicesApp() | 75 DevicesApp::DevicesApp( |
76 : app_impl_(nullptr), active_device_manager_count_(0) {} | 76 scoped_refptr<base::SequencedTaskRunner> service_task_runner) |
| 77 : app_impl_(nullptr), |
| 78 service_task_runner_(service_task_runner), |
| 79 active_device_manager_count_(0) { |
| 80 } |
77 | 81 |
78 DevicesApp::~DevicesApp() { | 82 DevicesApp::~DevicesApp() { |
79 } | 83 } |
80 | 84 |
81 void DevicesApp::Initialize(mojo::ApplicationImpl* app) { | 85 void DevicesApp::Initialize(mojo::ApplicationImpl* app) { |
82 app_impl_ = app; | 86 app_impl_ = app; |
83 service_initializer_.reset(new USBServiceInitializer); | 87 if (!service_task_runner_) { |
| 88 service_initializer_.reset(new USBServiceInitializer); |
| 89 service_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 90 } |
84 StartIdleTimer(); | 91 StartIdleTimer(); |
85 } | 92 } |
86 | 93 |
87 bool DevicesApp::ConfigureIncomingConnection( | 94 bool DevicesApp::ConfigureIncomingConnection( |
88 mojo::ApplicationConnection* connection) { | 95 mojo::ApplicationConnection* connection) { |
89 connection->AddService<usb::DeviceManager>(this); | 96 connection->AddService<usb::DeviceManager>(this); |
90 return true; | 97 return true; |
91 } | 98 } |
92 | 99 |
93 void DevicesApp::Quit() { | 100 void DevicesApp::Quit() { |
94 service_initializer_.reset(); | 101 service_initializer_.reset(); |
95 app_impl_ = nullptr; | 102 app_impl_ = nullptr; |
96 } | 103 } |
97 | 104 |
98 void DevicesApp::Create(mojo::ApplicationConnection* connection, | 105 void DevicesApp::Create(mojo::ApplicationConnection* connection, |
99 mojo::InterfaceRequest<usb::DeviceManager> request) { | 106 mojo::InterfaceRequest<usb::DeviceManager> request) { |
100 // Bind the new device manager to the connecting application's permission | 107 // Bind the new device manager to the connecting application's permission |
101 // provider. | 108 // provider. |
102 usb::PermissionProviderPtr permission_provider; | 109 usb::PermissionProviderPtr permission_provider; |
103 connection->ConnectToService(&permission_provider); | 110 connection->ConnectToService(&permission_provider); |
104 | 111 |
105 // Owned by its message pipe. | 112 // Owned by its message pipe. |
106 usb::DeviceManagerImpl* device_manager = | 113 usb::DeviceManagerImpl* device_manager = new usb::DeviceManagerImpl( |
107 new usb::DeviceManagerImpl(permission_provider.Pass(), request.Pass()); | 114 request.Pass(), permission_provider.Pass(), service_task_runner_); |
108 device_manager->set_connection_error_handler( | 115 device_manager->set_connection_error_handler( |
109 base::Bind(&DevicesApp::OnConnectionError, base::Unretained(this))); | 116 base::Bind(&DevicesApp::OnConnectionError, base::Unretained(this))); |
110 | 117 |
111 active_device_manager_count_++; | 118 active_device_manager_count_++; |
112 idle_timeout_callback_.Cancel(); | 119 idle_timeout_callback_.Cancel(); |
113 } | 120 } |
114 | 121 |
115 void DevicesApp::OnConnectionError() { | 122 void DevicesApp::OnConnectionError() { |
116 DCHECK_GE(active_device_manager_count_, 0u); | 123 DCHECK_GE(active_device_manager_count_, 0u); |
117 active_device_manager_count_--; | 124 active_device_manager_count_--; |
118 if (active_device_manager_count_ == 0) { | 125 if (active_device_manager_count_ == 0) { |
119 // If the last DeviceManager connection has been dropped, kick off an idle | 126 // If the last DeviceManager connection has been dropped, kick off an idle |
120 // timeout to shut ourselves down. | 127 // timeout to shut ourselves down. |
121 StartIdleTimer(); | 128 StartIdleTimer(); |
122 } | 129 } |
123 } | 130 } |
124 | 131 |
125 void DevicesApp::StartIdleTimer() { | 132 void DevicesApp::StartIdleTimer() { |
126 // Passing unretained |app_impl_| is safe here because |app_impl_| is | 133 // Passing unretained |app_impl_| is safe here because |app_impl_| is |
127 // guaranteed to outlive |this|, and the callback is canceled if |this| is | 134 // guaranteed to outlive |this|, and the callback is canceled if |this| is |
128 // destroyed. | 135 // destroyed. |
129 idle_timeout_callback_.Reset(base::Bind(&mojo::ApplicationImpl::Quit, | 136 idle_timeout_callback_.Reset(base::Bind(&mojo::ApplicationImpl::Quit, |
130 base::Unretained(app_impl_))); | 137 base::Unretained(app_impl_))); |
131 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 138 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
132 FROM_HERE, idle_timeout_callback_.callback(), | 139 FROM_HERE, idle_timeout_callback_.callback(), |
133 base::TimeDelta::FromSeconds(kIdleTimeoutInSeconds)); | 140 base::TimeDelta::FromSeconds(kIdleTimeoutInSeconds)); |
134 } | 141 } |
135 | 142 |
136 } // namespace device | 143 } // namespace device |
OLD | NEW |