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