| 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" | |
| 13 | 12 |
| 14 namespace device { | 13 namespace device { |
| 15 | 14 |
| 16 VRServiceImpl::VRServiceImpl() : listening_for_activate_(false) {} | 15 VRServiceImpl::VRServiceImpl() : listening_for_activate_(false) {} |
| 17 | 16 |
| 18 VRServiceImpl::~VRServiceImpl() { | 17 VRServiceImpl::~VRServiceImpl() { |
| 19 VRDeviceManager::GetInstance()->RemoveService(this); | 18 RemoveFromDeviceManager(); |
| 20 } | 19 } |
| 21 | 20 |
| 22 void VRServiceImpl::Create(mojo::InterfaceRequest<mojom::VRService> request) { | 21 void VRServiceImpl::BindRequest( |
| 23 mojo::MakeStrongBinding(base::MakeUnique<VRServiceImpl>(), | 22 mojo::InterfaceRequest<mojom::VRService> request) { |
| 24 std::move(request)); | 23 VRServiceImpl* service = new VRServiceImpl(); |
| 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 auto it = displays_.find(device); | 30 DisplayImplMap::iterator 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 // TODO(shaobo.yan@intel.com) : Keep one binding_ and use close and rebind. |
| 41 binding_.reset(new mojo::Binding<mojom::VRService>(this, std::move(request))); |
| 42 binding_->set_connection_error_handler(base::Bind( |
| 43 &VRServiceImpl::RemoveFromDeviceManager, base::Unretained(this))); |
| 44 } |
| 45 |
| 46 void VRServiceImpl::RemoveFromDeviceManager() { |
| 47 displays_.clear(); |
| 48 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 49 device_manager->RemoveService(this); |
| 50 } |
| 51 |
| 39 void VRServiceImpl::RemoveDevice(VRDevice* device) { | 52 void VRServiceImpl::RemoveDevice(VRDevice* device) { |
| 40 displays_.erase(device); | 53 displays_.erase(device); |
| 41 } | 54 } |
| 42 | 55 |
| 43 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, | 56 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, |
| 44 const SetClientCallback& callback) { | 57 const SetClientCallback& callback) { |
| 45 DCHECK(!client_.get()); | 58 DCHECK(!client_.get()); |
| 46 | 59 |
| 47 client_ = std::move(service_client); | 60 client_ = std::move(service_client); |
| 48 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); | 61 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 49 // Once a client has been connected AddService will force any VRDisplays to | 62 // Once a client has been connected AddService will force any VRDisplays to |
| 50 // send OnConnected to it so that it's populated with the currently active | 63 // send OnConnected to it so that it's populated with the currently active |
| 51 // displays. Thereafer it will stay up to date by virtue of listening for new | 64 // displays. Thereafer it will stay up to date by virtue of listening for new |
| 52 // connected events. | 65 // connected events. |
| 53 device_manager->AddService(this); | 66 device_manager->AddService(this); |
| 54 callback.Run(device_manager->GetNumberOfConnectedDevices()); | 67 callback.Run(device_manager->GetNumberOfConnectedDevices()); |
| 55 } | 68 } |
| 56 | 69 |
| 57 void VRServiceImpl::SetListeningForActivate(bool listening) { | 70 void VRServiceImpl::SetListeningForActivate(bool listening) { |
| 58 listening_for_activate_ = listening; | 71 listening_for_activate_ = listening; |
| 59 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); | 72 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 60 device_manager->ListeningForActivateChanged(listening); | 73 device_manager->ListeningForActivateChanged(listening); |
| 61 } | 74 } |
| 62 | 75 |
| 63 } // namespace device | 76 } // namespace device |
| OLD | NEW |