| 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/auto_reset.h" |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 11 #include "device/vr/vr_device.h" | 12 #include "device/vr/vr_device.h" |
| 12 #include "device/vr/vr_device_manager.h" | 13 #include "device/vr/vr_device_manager.h" |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" | 14 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 14 | 15 |
| 15 namespace device { | 16 namespace device { |
| 16 | 17 |
| 17 VRServiceImpl::VRServiceImpl() | 18 VRServiceImpl::VRServiceImpl() |
| 18 : listening_for_activate_(false), | 19 : listening_for_activate_(false), |
| 20 in_set_client_(false), |
| 19 connected_devices_(0), | 21 connected_devices_(0), |
| 22 handled_devices_(0), |
| 20 weak_ptr_factory_(this) {} | 23 weak_ptr_factory_(this) {} |
| 21 | 24 |
| 22 VRServiceImpl::~VRServiceImpl() { | 25 VRServiceImpl::~VRServiceImpl() { |
| 23 // Destroy VRDisplay before calling RemoveService below. RemoveService might | 26 // Destroy VRDisplay before calling RemoveService below. RemoveService might |
| 24 // implicitly trigger destory VRDevice which VRDisplay needs to access in its | 27 // implicitly trigger destory VRDevice which VRDisplay needs to access in its |
| 25 // dtor. | 28 // dtor. |
| 26 displays_.clear(); | 29 displays_.clear(); |
| 27 VRDeviceManager::GetInstance()->RemoveService(this); | 30 VRDeviceManager::GetInstance()->RemoveService(this); |
| 28 } | 31 } |
| 29 | 32 |
| 30 void VRServiceImpl::Create(const service_manager::BindSourceInfo& source_info, | 33 void VRServiceImpl::Create(const service_manager::BindSourceInfo& source_info, |
| 31 mojom::VRServiceRequest request) { | 34 mojom::VRServiceRequest request) { |
| 32 mojo::MakeStrongBinding(base::MakeUnique<VRServiceImpl>(), | 35 mojo::MakeStrongBinding(base::MakeUnique<VRServiceImpl>(), |
| 33 std::move(request)); | 36 std::move(request)); |
| 34 } | 37 } |
| 35 | 38 |
| 36 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, | 39 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, |
| 37 const SetClientCallback& callback) { | 40 const SetClientCallback& callback) { |
| 38 DCHECK(!client_.get()); | 41 DCHECK(!client_.get()); |
| 42 |
| 43 // Set a scoped variable to true so we can verify we are in the same stack. |
| 44 base::AutoReset<bool> set_client(&in_set_client_, true); |
| 45 |
| 39 client_ = std::move(service_client); | 46 client_ = std::move(service_client); |
| 40 // Once a client has been connected AddService will force any VRDisplays to | 47 // 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 | 48 // 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 | 49 // displays. Thereafter it will stay up to date by virtue of listening for new |
| 43 // connected events. | 50 // connected events. |
| 44 VRDeviceManager::GetInstance()->AddService(this); | 51 |
| 45 callback.Run(connected_devices_); | 52 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 53 device_manager->AddService(this); |
| 54 unsigned expected_devices = device_manager->GetNumberOfConnectedDevices(); |
| 55 // TODO(amp): Remove this count based synchronization. |
| 56 // If libraries are not loaded, new devices will immediatly be handled but not |
| 57 // connect, return only those devices which have already connected. |
| 58 // If libraries were loaded then all devices may not be handled yet so return |
| 59 // the number we expect to eventually connect. |
| 60 if (expected_devices == handled_devices_) { |
| 61 expected_devices = connected_devices_; |
| 62 } |
| 63 callback.Run(expected_devices); |
| 46 } | 64 } |
| 47 | 65 |
| 48 void VRServiceImpl::ConnectDevice(VRDevice* device) { | 66 void VRServiceImpl::ConnectDevice(VRDevice* device) { |
| 49 DCHECK(displays_.count(device) == 0); | 67 DCHECK(displays_.count(device) == 0); |
| 50 base::Callback<void(mojom::VRDisplayInfoPtr)> on_created = | 68 base::Callback<void(mojom::VRDisplayInfoPtr)> on_created = |
| 51 base::Bind(&VRServiceImpl::OnVRDisplayInfoCreated, | 69 base::Bind(&VRServiceImpl::OnVRDisplayInfoCreated, |
| 52 weak_ptr_factory_.GetWeakPtr(), device); | 70 weak_ptr_factory_.GetWeakPtr(), device); |
| 53 device->CreateVRDisplayInfo(on_created); | 71 device->CreateVRDisplayInfo(on_created); |
| 54 } | 72 } |
| 55 | 73 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 68 // in the constructor. | 86 // in the constructor. |
| 69 if (!client_) { | 87 if (!client_) { |
| 70 DLOG(ERROR) << "Cannot create VR display because connection to render " | 88 DLOG(ERROR) << "Cannot create VR display because connection to render " |
| 71 "process is not established"; | 89 "process is not established"; |
| 72 return; | 90 return; |
| 73 } | 91 } |
| 74 | 92 |
| 75 if (!display_info) { | 93 if (!display_info) { |
| 76 // If we get passed a null display info it means the device does not exist. | 94 // If we get passed a null display info it means the device does not exist. |
| 77 // This can happen for example if VR services are not installed. We will not | 95 // This can happen for example if VR services are not installed. We will not |
| 78 // instantiate a display in this case and don't count it as connected. | 96 // instantiate a display in this case and don't count it as connected, but |
| 79 return; | 97 // we do mark that we have handled it and verify we haven't changed stacks. |
| 98 DCHECK(in_set_client_); |
| 99 } else { |
| 100 displays_[device] = base::MakeUnique<VRDisplayImpl>( |
| 101 device, this, client_.get(), std::move(display_info)); |
| 102 connected_devices_++; |
| 80 } | 103 } |
| 81 displays_[device] = base::MakeUnique<VRDisplayImpl>( | 104 handled_devices_++; |
| 82 device, this, client_.get(), std::move(display_info)); | |
| 83 connected_devices_++; | |
| 84 } | 105 } |
| 85 | 106 |
| 86 VRDisplayImpl* VRServiceImpl::GetVRDisplayImplForTesting(VRDevice* device) { | 107 VRDisplayImpl* VRServiceImpl::GetVRDisplayImplForTesting(VRDevice* device) { |
| 87 auto it = displays_.find(device); | 108 auto it = displays_.find(device); |
| 88 return (it == displays_.end()) ? nullptr : it->second.get(); | 109 return (it == displays_.end()) ? nullptr : it->second.get(); |
| 89 } | 110 } |
| 90 | 111 |
| 91 } // namespace device | 112 } // namespace device |
| OLD | NEW |