Chromium Code Reviews| 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), | 18 : listening_for_activate_(false), |
| 19 connected_devices_(0), | 19 connected_devices_(0), |
| 20 handled_devices_(0), | |
| 20 weak_ptr_factory_(this) {} | 21 weak_ptr_factory_(this) {} |
| 21 | 22 |
| 22 VRServiceImpl::~VRServiceImpl() { | 23 VRServiceImpl::~VRServiceImpl() { |
| 23 // Destroy VRDisplay before calling RemoveService below. RemoveService might | 24 // Destroy VRDisplay before calling RemoveService below. RemoveService might |
| 24 // implicitly trigger destory VRDevice which VRDisplay needs to access in its | 25 // implicitly trigger destory VRDevice which VRDisplay needs to access in its |
| 25 // dtor. | 26 // dtor. |
| 26 displays_.clear(); | 27 displays_.clear(); |
| 27 VRDeviceManager::GetInstance()->RemoveService(this); | 28 VRDeviceManager::GetInstance()->RemoveService(this); |
| 28 } | 29 } |
| 29 | 30 |
| 30 void VRServiceImpl::Create(const service_manager::BindSourceInfo& source_info, | 31 void VRServiceImpl::Create(const service_manager::BindSourceInfo& source_info, |
| 31 mojom::VRServiceRequest request) { | 32 mojom::VRServiceRequest request) { |
| 32 mojo::MakeStrongBinding(base::MakeUnique<VRServiceImpl>(), | 33 mojo::MakeStrongBinding(base::MakeUnique<VRServiceImpl>(), |
| 33 std::move(request)); | 34 std::move(request)); |
| 34 } | 35 } |
| 35 | 36 |
| 36 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, | 37 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, |
| 37 SetClientCallback callback) { | 38 SetClientCallback callback) { |
| 38 DCHECK(!client_.get()); | 39 DCHECK(!client_.get()); |
| 39 client_ = std::move(service_client); | 40 client_ = std::move(service_client); |
| 40 // Once a client has been connected AddService will force any VRDisplays to | 41 // Once a client has been connected AddService will force any VRDisplays to |
| 41 // send ConnectDevice to it so that it's populated with the currently active | 42 // send ConnectDevice to it so that it's populated with the currently active |
| 42 // displays. Thereafter it will stay up to date by virtue of listening for new | 43 // displays. Thereafter it will stay up to date by virtue of listening for new |
| 43 // connected events. | 44 // connected events. |
| 44 VRDeviceManager::GetInstance()->AddService(this); | 45 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 45 std::move(callback).Run(connected_devices_); | 46 device_manager->AddService(this); |
| 47 unsigned expected_devices = device_manager->GetNumberOfConnectedDevices(); | |
| 48 // TODO(amp): Remove this count based synchronization. | |
| 49 // If libraries are not loaded, new devices will immediatly be handled but not | |
| 50 // connect, return only those devices which have already connected. | |
| 51 // If libraries were loaded then all devices may not be handled yet so return | |
| 52 // the number we expect to eventually connect. | |
| 53 if (expected_devices == handled_devices_) { | |
| 54 expected_devices = connected_devices_; | |
| 55 } | |
| 56 std::move(callback).Run(expected_devices); | |
|
mthiesse
2017/06/05 16:18:07
I'm confused. Doesn't this just re-introduce the b
amp
2017/06/05 16:31:18
Let me attempt a better explanation (let me know i
| |
| 46 } | 57 } |
| 47 | 58 |
| 48 void VRServiceImpl::ConnectDevice(VRDevice* device) { | 59 void VRServiceImpl::ConnectDevice(VRDevice* device) { |
| 49 DCHECK(displays_.count(device) == 0); | 60 DCHECK(displays_.count(device) == 0); |
| 50 base::Callback<void(mojom::VRDisplayInfoPtr)> on_created = | 61 base::Callback<void(mojom::VRDisplayInfoPtr)> on_created = |
| 51 base::Bind(&VRServiceImpl::OnVRDisplayInfoCreated, | 62 base::Bind(&VRServiceImpl::OnVRDisplayInfoCreated, |
| 52 weak_ptr_factory_.GetWeakPtr(), device); | 63 weak_ptr_factory_.GetWeakPtr(), device); |
| 53 device->CreateVRDisplayInfo(on_created); | 64 device->CreateVRDisplayInfo(on_created); |
| 54 } | 65 } |
| 55 | 66 |
| 56 void VRServiceImpl::SetListeningForActivate(bool listening) { | 67 void VRServiceImpl::SetListeningForActivate(bool listening) { |
| 57 listening_for_activate_ = listening; | 68 listening_for_activate_ = listening; |
| 58 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); | 69 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 59 device_manager->ListeningForActivateChanged(listening, this); | 70 device_manager->ListeningForActivateChanged(listening, this); |
| 60 } | 71 } |
| 61 | 72 |
| 62 // Creates a VRDisplayPtr unique to this service so that the associated page can | 73 // Creates a VRDisplayPtr unique to this service so that the associated page can |
| 63 // communicate with the VRDevice. | 74 // communicate with the VRDevice. |
| 64 void VRServiceImpl::OnVRDisplayInfoCreated( | 75 void VRServiceImpl::OnVRDisplayInfoCreated( |
| 65 VRDevice* device, | 76 VRDevice* device, |
| 66 mojom::VRDisplayInfoPtr display_info) { | 77 mojom::VRDisplayInfoPtr display_info) { |
| 67 // TODO(crbug/701027): make sure that client_ is never null by initializing it | 78 // TODO(crbug/701027): make sure that client_ is never null by initializing it |
| 68 // in the constructor. | 79 // in the constructor. |
| 69 if (!client_) { | 80 if (!client_) { |
| 70 DLOG(ERROR) << "Cannot create VR display because connection to render " | 81 DLOG(ERROR) << "Cannot create VR display because connection to render " |
| 71 "process is not established"; | 82 "process is not established"; |
| 72 return; | 83 return; |
| 73 } | 84 } |
| 74 | 85 |
| 75 if (!display_info) { | 86 // 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. | 87 // 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 | 88 // instantiate a display in this case and don't count it as connected, but we |
| 78 // instantiate a display in this case and don't count it as connected. | 89 // do mark that we have handled it. |
| 79 return; | 90 if (display_info) { |
| 91 displays_[device] = base::MakeUnique<VRDisplayImpl>( | |
| 92 device, this, client_.get(), std::move(display_info)); | |
| 93 connected_devices_++; | |
| 80 } | 94 } |
|
mthiesse
2017/06/05 17:36:27
Can we DCHECK on the else case there we're inside
amp
2017/06/05 18:40:39
Done. Switched the if around (back to the origina
| |
| 81 displays_[device] = base::MakeUnique<VRDisplayImpl>( | 95 handled_devices_++; |
| 82 device, this, client_.get(), std::move(display_info)); | |
| 83 connected_devices_++; | |
| 84 } | 96 } |
| 85 | 97 |
| 86 VRDisplayImpl* VRServiceImpl::GetVRDisplayImplForTesting(VRDevice* device) { | 98 VRDisplayImpl* VRServiceImpl::GetVRDisplayImplForTesting(VRDevice* device) { |
| 87 auto it = displays_.find(device); | 99 auto it = displays_.find(device); |
| 88 return (it == displays_.end()) ? nullptr : it->second.get(); | 100 return (it == displays_.end()) ? nullptr : it->second.get(); |
| 89 } | 101 } |
| 90 | 102 |
| 91 } // namespace device | 103 } // namespace device |
| OLD | NEW |