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" | |
17 #include "device/usb/usb_service.h" | 16 #include "device/usb/usb_service.h" |
18 #include "mojo/application/public/cpp/application_connection.h" | 17 #include "mojo/application/public/cpp/application_connection.h" |
19 #include "mojo/application/public/cpp/application_impl.h" | 18 #include "mojo/application/public/cpp/application_impl.h" |
20 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" | 19 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" |
21 #include "url/gurl.h" | 20 #include "url/gurl.h" |
22 | 21 |
23 namespace device { | 22 namespace device { |
24 | 23 |
25 namespace { | 24 namespace { |
26 | 25 |
27 // The number of seconds to wait without any bound DeviceManagers before | 26 // The number of seconds to wait without any bound DeviceManagers before |
28 // exiting the app. | 27 // exiting the app. |
29 const int64 kIdleTimeoutInSeconds = 10; | 28 const int64 kIdleTimeoutInSeconds = 10; |
30 | 29 |
31 // A usb::DeviceManagerDelegate implementation which provides origin-based | |
32 // device access control. | |
33 // | |
34 // TODO(rockot/reillyg): We should just get rid of DeviceManagerDelegate, or | |
35 // at least repurpose it as test support to provide device mocks. | |
36 class USBDeviceManagerDelegate : public usb::DeviceManagerDelegate { | |
37 public: | |
38 explicit USBDeviceManagerDelegate(const GURL& remote_url) | |
39 : remote_url_(remote_url) {} | |
40 ~USBDeviceManagerDelegate() override {} | |
41 | |
42 private: | |
43 // usb::DeviceManagerDelegate: | |
44 bool IsDeviceAllowed(const usb::DeviceInfo& device) override { | |
45 // Also let browser apps and mojo apptests talk to all devices. | |
46 if (remote_url_.SchemeIs("system") || | |
47 remote_url_ == GURL("mojo://devices_apptests/")) | |
48 return true; | |
49 return false; | |
50 } | |
51 | |
52 GURL remote_url_; | |
53 | |
54 DISALLOW_COPY_AND_ASSIGN(USBDeviceManagerDelegate); | |
55 }; | |
56 | |
57 // A DeviceClient implementation to be constructed iff the app is not running | 30 // A DeviceClient implementation to be constructed iff the app is not running |
58 // in an embedder that provides a DeviceClient (i.e. running as a standalone | 31 // in an embedder that provides a DeviceClient (i.e. running as a standalone |
59 // Mojo app, not in Chrome). | 32 // Mojo app, not in Chrome). |
60 class AppDeviceClient : public DeviceClient { | 33 class AppDeviceClient : public DeviceClient { |
61 public: | 34 public: |
62 explicit AppDeviceClient( | 35 explicit AppDeviceClient( |
63 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | 36 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) |
64 : blocking_task_runner_(blocking_task_runner) {} | 37 : blocking_task_runner_(blocking_task_runner) {} |
65 ~AppDeviceClient() override {} | 38 ~AppDeviceClient() override {} |
66 | 39 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 return true; | 97 return true; |
125 } | 98 } |
126 | 99 |
127 void DevicesApp::Quit() { | 100 void DevicesApp::Quit() { |
128 service_initializer_.reset(); | 101 service_initializer_.reset(); |
129 app_impl_ = nullptr; | 102 app_impl_ = nullptr; |
130 } | 103 } |
131 | 104 |
132 void DevicesApp::Create(mojo::ApplicationConnection* connection, | 105 void DevicesApp::Create(mojo::ApplicationConnection* connection, |
133 mojo::InterfaceRequest<usb::DeviceManager> request) { | 106 mojo::InterfaceRequest<usb::DeviceManager> request) { |
134 scoped_ptr<usb::DeviceManagerDelegate> delegate(new USBDeviceManagerDelegate( | 107 // Bind the new device manager to the connecting application's permission |
135 GURL(connection->GetRemoteApplicationURL()))); | 108 // provider. |
| 109 usb::PermissionProviderPtr permission_provider; |
| 110 connection->ConnectToService(&permission_provider); |
136 | 111 |
137 // Owned by its message pipe. | 112 // Owned by its message pipe. |
138 usb::DeviceManagerImpl* device_manager = new usb::DeviceManagerImpl( | 113 usb::DeviceManagerImpl* device_manager = new usb::DeviceManagerImpl( |
139 request.Pass(), delegate.Pass(), service_task_runner_); | 114 request.Pass(), permission_provider.Pass(), service_task_runner_); |
140 device_manager->set_connection_error_handler( | 115 device_manager->set_connection_error_handler( |
141 base::Bind(&DevicesApp::OnConnectionError, base::Unretained(this))); | 116 base::Bind(&DevicesApp::OnConnectionError, base::Unretained(this))); |
142 | 117 |
143 active_device_manager_count_++; | 118 active_device_manager_count_++; |
144 idle_timeout_callback_.Cancel(); | 119 idle_timeout_callback_.Cancel(); |
145 } | 120 } |
146 | 121 |
147 void DevicesApp::OnConnectionError() { | 122 void DevicesApp::OnConnectionError() { |
148 DCHECK_GE(active_device_manager_count_, 0u); | 123 DCHECK_GE(active_device_manager_count_, 0u); |
149 active_device_manager_count_--; | 124 active_device_manager_count_--; |
150 if (active_device_manager_count_ == 0) { | 125 if (active_device_manager_count_ == 0) { |
151 // 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 |
152 // timeout to shut ourselves down. | 127 // timeout to shut ourselves down. |
153 StartIdleTimer(); | 128 StartIdleTimer(); |
154 } | 129 } |
155 } | 130 } |
156 | 131 |
157 void DevicesApp::StartIdleTimer() { | 132 void DevicesApp::StartIdleTimer() { |
158 // Passing unretained |app_impl_| is safe here because |app_impl_| is | 133 // Passing unretained |app_impl_| is safe here because |app_impl_| is |
159 // 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 |
160 // destroyed. | 135 // destroyed. |
161 idle_timeout_callback_.Reset(base::Bind(&mojo::ApplicationImpl::Quit, | 136 idle_timeout_callback_.Reset(base::Bind(&mojo::ApplicationImpl::Quit, |
162 base::Unretained(app_impl_))); | 137 base::Unretained(app_impl_))); |
163 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 138 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
164 FROM_HERE, idle_timeout_callback_.callback(), | 139 FROM_HERE, idle_timeout_callback_.callback(), |
165 base::TimeDelta::FromSeconds(kIdleTimeoutInSeconds)); | 140 base::TimeDelta::FromSeconds(kIdleTimeoutInSeconds)); |
166 } | 141 } |
167 | 142 |
168 } // namespace device | 143 } // namespace device |
OLD | NEW |