| 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 "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "device/vr/vr_device.h" | 11 #include "device/vr/vr_device.h" |
| 12 #include "device/vr/vr_device_manager.h" | 12 #include "device/vr/vr_device_manager.h" |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" | 13 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 14 | 14 |
| 15 namespace device { | 15 namespace device { |
| 16 | 16 |
| 17 VRServiceImpl::VRServiceImpl() | 17 VRServiceImpl::VRServiceImpl() |
| 18 : listening_for_activate_(false), weak_ptr_factory_(this) {} | 18 : listening_for_activate_(false), |
| 19 connected_devices_(0), |
| 20 weak_ptr_factory_(this) {} |
| 19 | 21 |
| 20 VRServiceImpl::~VRServiceImpl() { | 22 VRServiceImpl::~VRServiceImpl() { |
| 21 // Destroy VRDisplay before calling RemoveService below. RemoveService might | 23 // Destroy VRDisplay before calling RemoveService below. RemoveService might |
| 22 // implicitly trigger destory VRDevice which VRDisplay needs to access in its | 24 // implicitly trigger destory VRDevice which VRDisplay needs to access in its |
| 23 // dtor. | 25 // dtor. |
| 24 displays_.clear(); | 26 displays_.clear(); |
| 25 VRDeviceManager::GetInstance()->RemoveService(this); | 27 VRDeviceManager::GetInstance()->RemoveService(this); |
| 26 } | 28 } |
| 27 | 29 |
| 28 void VRServiceImpl::Create(const service_manager::BindSourceInfo& source_info, | 30 void VRServiceImpl::Create(const service_manager::BindSourceInfo& source_info, |
| 29 mojom::VRServiceRequest request) { | 31 mojom::VRServiceRequest request) { |
| 30 mojo::MakeStrongBinding(base::MakeUnique<VRServiceImpl>(), | 32 mojo::MakeStrongBinding(base::MakeUnique<VRServiceImpl>(), |
| 31 std::move(request)); | 33 std::move(request)); |
| 32 } | 34 } |
| 33 | 35 |
| 34 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, | 36 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, |
| 35 SetClientCallback callback) { | 37 SetClientCallback callback) { |
| 36 DCHECK(!client_.get()); | 38 DCHECK(!client_.get()); |
| 37 client_ = std::move(service_client); | 39 client_ = std::move(service_client); |
| 38 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); | |
| 39 // Once a client has been connected AddService will force any VRDisplays to | 40 // Once a client has been connected AddService will force any VRDisplays to |
| 40 // send ConnectDevice to it so that it's populated with the currently active | 41 // send ConnectDevice to it so that it's populated with the currently active |
| 41 // displays. Thereafter it will stay up to date by virtue of listening for new | 42 // displays. Thereafter it will stay up to date by virtue of listening for new |
| 42 // connected events. | 43 // connected events. |
| 43 device_manager->AddService(this); | 44 VRDeviceManager::GetInstance()->AddService(this); |
| 44 std::move(callback).Run(device_manager->GetNumberOfConnectedDevices()); | 45 std::move(callback).Run(connected_devices_); |
| 45 } | 46 } |
| 46 | 47 |
| 47 void VRServiceImpl::ConnectDevice(VRDevice* device) { | 48 void VRServiceImpl::ConnectDevice(VRDevice* device) { |
| 48 DCHECK(displays_.count(device) == 0); | 49 DCHECK(displays_.count(device) == 0); |
| 49 base::Callback<void(mojom::VRDisplayInfoPtr)> on_created = | 50 base::Callback<void(mojom::VRDisplayInfoPtr)> on_created = |
| 50 base::Bind(&VRServiceImpl::OnVRDisplayInfoCreated, | 51 base::Bind(&VRServiceImpl::OnVRDisplayInfoCreated, |
| 51 weak_ptr_factory_.GetWeakPtr(), device); | 52 weak_ptr_factory_.GetWeakPtr(), device); |
| 52 device->CreateVRDisplayInfo(on_created); | 53 device->CreateVRDisplayInfo(on_created); |
| 53 } | 54 } |
| 54 | 55 |
| 55 void VRServiceImpl::SetListeningForActivate(bool listening) { | 56 void VRServiceImpl::SetListeningForActivate(bool listening) { |
| 56 listening_for_activate_ = listening; | 57 listening_for_activate_ = listening; |
| 57 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); | 58 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 58 device_manager->ListeningForActivateChanged(listening, this); | 59 device_manager->ListeningForActivateChanged(listening, this); |
| 59 } | 60 } |
| 60 | 61 |
| 61 // Creates a VRDisplayPtr unique to this service so that the associated page can | 62 // Creates a VRDisplayPtr unique to this service so that the associated page can |
| 62 // communicate with the VRDevice. | 63 // communicate with the VRDevice. |
| 63 void VRServiceImpl::OnVRDisplayInfoCreated( | 64 void VRServiceImpl::OnVRDisplayInfoCreated( |
| 64 VRDevice* device, | 65 VRDevice* device, |
| 65 mojom::VRDisplayInfoPtr display_info) { | 66 mojom::VRDisplayInfoPtr display_info) { |
| 66 // TODO(crbug/701027): make sure that client_ is never null by initializing it | 67 // TODO(crbug/701027): make sure that client_ is never null by initializing it |
| 67 // in the constructor. | 68 // in the constructor. |
| 68 if (!client_) { | 69 if (!client_) { |
| 69 DLOG(ERROR) << "Cannot create VR display because connection to render " | 70 DLOG(ERROR) << "Cannot create VR display because connection to render " |
| 70 "process is not established"; | 71 "process is not established"; |
| 71 return; | 72 return; |
| 72 } | 73 } |
| 74 |
| 73 if (!display_info) { | 75 if (!display_info) { |
| 74 // If we get passed a null display info it means the device does not exist. | 76 // If we get passed a null display info it means the device does not exist. |
| 75 // This can happen for example if VR services are not installed. We will not | 77 // This can happen for example if VR services are not installed. We will not |
| 76 // instantiate a display in this case. | 78 // instantiate a display in this case and don't count it as connected. |
| 77 return; | 79 return; |
| 78 } | 80 } |
| 79 displays_[device] = base::MakeUnique<VRDisplayImpl>( | 81 displays_[device] = base::MakeUnique<VRDisplayImpl>( |
| 80 device, this, client_.get(), std::move(display_info)); | 82 device, this, client_.get(), std::move(display_info)); |
| 83 connected_devices_++; |
| 81 } | 84 } |
| 82 | 85 |
| 83 VRDisplayImpl* VRServiceImpl::GetVRDisplayImplForTesting(VRDevice* device) { | 86 VRDisplayImpl* VRServiceImpl::GetVRDisplayImplForTesting(VRDevice* device) { |
| 84 auto it = displays_.find(device); | 87 auto it = displays_.find(device); |
| 85 return (it == displays_.end()) ? nullptr : it->second.get(); | 88 return (it == displays_.end()) ? nullptr : it->second.get(); |
| 86 } | 89 } |
| 87 | 90 |
| 88 } // namespace device | 91 } // namespace device |
| OLD | NEW |