| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "device/devices_app/devices_app.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/sequenced_task_runner.h" | |
| 14 #include "base/thread_task_runner_handle.h" | |
| 15 #include "base/threading/thread.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "device/core/device_client.h" | |
| 18 #include "device/devices_app/usb/device_manager_impl.h" | |
| 19 #include "device/usb/usb_service.h" | |
| 20 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 21 #include "mojo/shell/public/cpp/connection.h" | |
| 22 #include "mojo/shell/public/cpp/shell.h" | |
| 23 #include "url/gurl.h" | |
| 24 | |
| 25 namespace device { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 // The number of seconds to wait without any bound DeviceManagers before | |
| 30 // exiting the app. | |
| 31 const int64_t kIdleTimeoutInSeconds = 10; | |
| 32 | |
| 33 // A DeviceClient implementation to be constructed iff the app is not running | |
| 34 // in an embedder that provides a DeviceClient (i.e. running as a standalone | |
| 35 // Mojo app, not in Chrome). | |
| 36 class AppDeviceClient : public DeviceClient { | |
| 37 public: | |
| 38 explicit AppDeviceClient( | |
| 39 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | |
| 40 : blocking_task_runner_(blocking_task_runner) {} | |
| 41 ~AppDeviceClient() override {} | |
| 42 | |
| 43 private: | |
| 44 // DeviceClient: | |
| 45 UsbService* GetUsbService() override { | |
| 46 if (!usb_service_) { | |
| 47 usb_service_ = UsbService::Create(blocking_task_runner_); | |
| 48 } | |
| 49 return usb_service_.get(); | |
| 50 } | |
| 51 | |
| 52 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | |
| 53 scoped_ptr<UsbService> usb_service_; | |
| 54 }; | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 // This class insures that a UsbService has been initialized and is accessible | |
| 59 // via the DeviceClient interface. | |
| 60 class DevicesApp::USBServiceInitializer { | |
| 61 public: | |
| 62 USBServiceInitializer() | |
| 63 : blocking_thread_("USB service blocking I/O thread") { | |
| 64 blocking_thread_.Start(); | |
| 65 app_device_client_.reset( | |
| 66 new AppDeviceClient(blocking_thread_.task_runner())); | |
| 67 } | |
| 68 | |
| 69 ~USBServiceInitializer() {} | |
| 70 | |
| 71 private: | |
| 72 scoped_ptr<AppDeviceClient> app_device_client_; | |
| 73 base::Thread blocking_thread_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(USBServiceInitializer); | |
| 76 }; | |
| 77 | |
| 78 DevicesApp::DevicesApp() : shell_(nullptr), active_device_manager_count_(0) {} | |
| 79 | |
| 80 DevicesApp::~DevicesApp() { | |
| 81 } | |
| 82 | |
| 83 void DevicesApp::Initialize(mojo::Shell* shell, | |
| 84 const std::string& url, | |
| 85 uint32_t id) { | |
| 86 shell_ = shell; | |
| 87 service_initializer_.reset(new USBServiceInitializer); | |
| 88 StartIdleTimer(); | |
| 89 } | |
| 90 | |
| 91 bool DevicesApp::AcceptConnection(mojo::Connection* connection) { | |
| 92 connection->AddInterface<usb::DeviceManager>(this); | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 void DevicesApp::Quit() { | |
| 97 service_initializer_.reset(); | |
| 98 shell_ = nullptr; | |
| 99 } | |
| 100 | |
| 101 void DevicesApp::Create(mojo::Connection* connection, | |
| 102 mojo::InterfaceRequest<usb::DeviceManager> request) { | |
| 103 // Bind the new device manager to the connecting application's permission | |
| 104 // provider. | |
| 105 usb::PermissionProviderPtr permission_provider; | |
| 106 connection->GetInterface(&permission_provider); | |
| 107 | |
| 108 // Owned by its message pipe. | |
| 109 usb::DeviceManagerImpl* device_manager = new usb::DeviceManagerImpl( | |
| 110 std::move(permission_provider), std::move(request)); | |
| 111 device_manager->set_connection_error_handler( | |
| 112 base::Bind(&DevicesApp::OnConnectionError, base::Unretained(this))); | |
| 113 | |
| 114 active_device_manager_count_++; | |
| 115 idle_timeout_callback_.Cancel(); | |
| 116 } | |
| 117 | |
| 118 void DevicesApp::OnConnectionError() { | |
| 119 DCHECK_GE(active_device_manager_count_, 0u); | |
| 120 active_device_manager_count_--; | |
| 121 if (active_device_manager_count_ == 0) { | |
| 122 // If the last DeviceManager connection has been dropped, kick off an idle | |
| 123 // timeout to shut ourselves down. | |
| 124 StartIdleTimer(); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void DevicesApp::StartIdleTimer() { | |
| 129 // Passing unretained |shell_| is safe here because |shell_| is | |
| 130 // guaranteed to outlive |this|, and the callback is canceled if |this| is | |
| 131 // destroyed. | |
| 132 idle_timeout_callback_.Reset( | |
| 133 base::Bind(&mojo::Shell::Quit, base::Unretained(shell_))); | |
| 134 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 135 FROM_HERE, idle_timeout_callback_.callback(), | |
| 136 base::TimeDelta::FromSeconds(kIdleTimeoutInSeconds)); | |
| 137 } | |
| 138 | |
| 139 } // namespace device | |
| OLD | NEW |