| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/vr/vr_service_impl.h" | 5 #include "device/vr/vr_service_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "device/vr/vr_device.h" | 10 #include "device/vr/vr_device.h" |
| 11 #include "device/vr/vr_device_manager.h" | 11 #include "device/vr/vr_device_manager.h" |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 12 | 13 |
| 13 namespace device { | 14 namespace device { |
| 14 | 15 |
| 15 VRServiceImpl::VRServiceImpl() : listening_for_activate_(false) {} | 16 VRServiceImpl::VRServiceImpl() : listening_for_activate_(false) {} |
| 16 | 17 |
| 17 VRServiceImpl::~VRServiceImpl() { | 18 VRServiceImpl::~VRServiceImpl() { |
| 18 RemoveFromDeviceManager(); | 19 VRDeviceManager::GetInstance()->RemoveService(this); |
| 19 } | 20 } |
| 20 | 21 |
| 21 void VRServiceImpl::BindRequest( | 22 void VRServiceImpl::Create(mojo::InterfaceRequest<mojom::VRService> request) { |
| 22 mojo::InterfaceRequest<mojom::VRService> request) { | 23 mojo::MakeStrongBinding(base::MakeUnique<VRServiceImpl>(), |
| 23 VRServiceImpl* service = new VRServiceImpl(); | 24 std::move(request)); |
| 24 service->Bind(std::move(request)); | |
| 25 } | 25 } |
| 26 | 26 |
| 27 // Gets a VRDisplayPtr unique to this service so that the associated page can | 27 // Gets a VRDisplayPtr unique to this service so that the associated page can |
| 28 // communicate with the VRDevice. | 28 // communicate with the VRDevice. |
| 29 VRDisplayImpl* VRServiceImpl::GetVRDisplayImpl(VRDevice* device) { | 29 VRDisplayImpl* VRServiceImpl::GetVRDisplayImpl(VRDevice* device) { |
| 30 DisplayImplMap::iterator it = displays_.find(device); | 30 auto it = displays_.find(device); |
| 31 if (it != displays_.end()) | 31 if (it != displays_.end()) |
| 32 return it->second.get(); | 32 return it->second.get(); |
| 33 | 33 |
| 34 VRDisplayImpl* display_impl = new VRDisplayImpl(device, this); | 34 VRDisplayImpl* display_impl = new VRDisplayImpl(device, this); |
| 35 displays_[device] = base::WrapUnique(display_impl); | 35 displays_[device] = base::WrapUnique(display_impl); |
| 36 return display_impl; | 36 return display_impl; |
| 37 } | 37 } |
| 38 | 38 |
| 39 void VRServiceImpl::Bind(mojo::InterfaceRequest<mojom::VRService> request) { | |
| 40 if (!binding_) | |
| 41 binding_.reset( | |
| 42 new mojo::Binding<mojom::VRService>(this, std::move(request))); | |
| 43 else | |
| 44 binding_->Bind(std::move(request)); | |
| 45 binding_->set_connection_error_handler(base::Bind( | |
| 46 &VRServiceImpl::RemoveFromDeviceManager, base::Unretained(this))); | |
| 47 } | |
| 48 | |
| 49 void VRServiceImpl::RemoveFromDeviceManager() { | |
| 50 // Remove VRDisplayImpl in service/device pair. | |
| 51 // displays_ is a map which first element is a VRDevice pointer, | |
| 52 // the second element is a VRDisplayImpl pointer. | |
| 53 for (DisplayImplMap::iterator it = displays_.begin(); it != displays_.end(); | |
| 54 ++it) { | |
| 55 it->first->RemoveDisplay(it->second.get()); | |
| 56 } | |
| 57 | |
| 58 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); | |
| 59 device_manager->RemoveService(this); | |
| 60 displays_.clear(); | |
| 61 } | |
| 62 | |
| 63 void VRServiceImpl::RemoveDevice(VRDevice* device) { | 39 void VRServiceImpl::RemoveDevice(VRDevice* device) { |
| 64 displays_.erase(device); | 40 displays_.erase(device); |
| 65 } | 41 } |
| 66 | 42 |
| 67 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, | 43 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, |
| 68 const SetClientCallback& callback) { | 44 const SetClientCallback& callback) { |
| 69 DCHECK(!client_.get()); | 45 DCHECK(!client_.get()); |
| 70 client_ = std::move(service_client); | 46 client_ = std::move(service_client); |
| 71 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); | 47 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 72 // Once a client has been connected AddService will force any VRDisplays to | 48 // Once a client has been connected AddService will force any VRDisplays to |
| 73 // send OnConnected to it so that it's populated with the currently active | 49 // send OnConnected to it so that it's populated with the currently active |
| 74 // displays. Thereafer it will stay up to date by virtue of listening for new | 50 // displays. Thereafer it will stay up to date by virtue of listening for new |
| 75 // connected events. | 51 // connected events. |
| 76 device_manager->AddService(this); | 52 device_manager->AddService(this); |
| 77 callback.Run(device_manager->GetNumberOfConnectedDevices()); | 53 callback.Run(device_manager->GetNumberOfConnectedDevices()); |
| 78 } | 54 } |
| 79 | 55 |
| 80 void VRServiceImpl::SetListeningForActivate(bool listening) { | 56 void VRServiceImpl::SetListeningForActivate(bool listening) { |
| 81 listening_for_activate_ = listening; | 57 listening_for_activate_ = listening; |
| 82 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); | 58 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 83 device_manager->ListeningForActivateChanged(listening); | 59 device_manager->ListeningForActivateChanged(listening); |
| 84 } | 60 } |
| 85 | 61 |
| 86 } // namespace device | 62 } // namespace device |
| OLD | NEW |