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() : listening_for_activate_(false) {} | 17 VRServiceImpl::VRServiceImpl() |
18 : listening_for_activate_(false), weak_ptr_factory_(this) {} | |
18 | 19 |
19 VRServiceImpl::~VRServiceImpl() { | 20 VRServiceImpl::~VRServiceImpl() { |
20 // Destroy VRDisplay before calling RemoveService below. RemoveService might | 21 // Destroy VRDisplay before calling RemoveService below. RemoveService might |
21 // implicitly trigger destory VRDevice which VRDisplay needs to access in its | 22 // implicitly trigger destory VRDevice which VRDisplay needs to access in its |
22 // dtor. | 23 // dtor. |
23 displays_.clear(); | 24 displays_.clear(); |
24 VRDeviceManager::GetInstance()->RemoveService(this); | 25 VRDeviceManager::GetInstance()->RemoveService(this); |
25 } | 26 } |
26 | 27 |
27 void VRServiceImpl::Create(mojo::InterfaceRequest<mojom::VRService> request) { | 28 void VRServiceImpl::Create(mojo::InterfaceRequest<mojom::VRService> request) { |
28 mojo::MakeStrongBinding(base::MakeUnique<VRServiceImpl>(), | 29 mojo::MakeStrongBinding(base::MakeUnique<VRServiceImpl>(), |
29 std::move(request)); | 30 std::move(request)); |
30 } | 31 } |
31 | 32 |
32 // Gets a VRDisplayPtr unique to this service so that the associated page can | |
33 // communicate with the VRDevice. | |
34 VRDisplayImpl* VRServiceImpl::GetVRDisplayImpl(VRDevice* device) { | |
35 auto it = displays_.find(device); | |
36 if (it != displays_.end()) | |
37 return it->second.get(); | |
38 | |
39 VRDisplayImpl* display_impl = new VRDisplayImpl(device, this); | |
40 displays_[device] = base::WrapUnique(display_impl); | |
41 return display_impl; | |
42 } | |
43 | |
44 void VRServiceImpl::RemoveDevice(VRDevice* device) { | |
45 displays_.erase(device); | |
46 } | |
47 | |
48 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, | 33 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, |
49 const SetClientCallback& callback) { | 34 const SetClientCallback& callback) { |
50 DCHECK(!client_.get()); | 35 DCHECK(!client_.get()); |
51 client_ = std::move(service_client); | 36 client_ = std::move(service_client); |
52 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); | 37 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
53 // Once a client has been connected AddService will force any VRDisplays to | 38 // Once a client has been connected AddService will force any VRDisplays to |
54 // send OnConnected to it so that it's populated with the currently active | 39 // send OnConnected to it so that it's populated with the currently active |
55 // displays. Thereafer it will stay up to date by virtue of listening for new | 40 // displays. Thereafer it will stay up to date by virtue of listening for new |
56 // connected events. | 41 // connected events. |
57 device_manager->AddService(this); | 42 device_manager->AddService(this); |
58 callback.Run(device_manager->GetNumberOfConnectedDevices()); | 43 callback.Run(device_manager->GetNumberOfConnectedDevices()); |
59 } | 44 } |
60 | 45 |
46 void VRServiceImpl::ConnectDevice(VRDevice* device) { | |
47 DCHECK(displays_.count(device) == 0); | |
48 // TODO(crbug/701027): make sure that client_ is never null by initializing it | |
49 // in the constructor. | |
50 DCHECK(client_); | |
dcheng
2017/03/16 05:38:22
This can't be a DCHECK: if the renderer process in
tiborg
2017/03/16 15:02:40
Ok, changed it so that it does not crash for now.
| |
51 base::Callback<void(mojom::VRDisplayInfoPtr)> onCreated = | |
52 base::Bind(&VRServiceImpl::OnVRDisplayInfoCreated, | |
53 weak_ptr_factory_.GetWeakPtr(), device); | |
54 device->CreateVRDisplayInfo(onCreated); | |
55 } | |
56 | |
61 void VRServiceImpl::SetListeningForActivate(bool listening) { | 57 void VRServiceImpl::SetListeningForActivate(bool listening) { |
62 listening_for_activate_ = listening; | 58 listening_for_activate_ = listening; |
63 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); | 59 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
64 device_manager->ListeningForActivateChanged(listening); | 60 device_manager->ListeningForActivateChanged(listening); |
65 } | 61 } |
66 | 62 |
63 // Creates a VRDisplayPtr unique to this service so that the associated page can | |
64 // communicate with the VRDevice. | |
65 void VRServiceImpl::OnVRDisplayInfoCreated( | |
66 VRDevice* device, | |
67 mojom::VRDisplayInfoPtr display_info) { | |
68 displays_[device] = base::MakeUnique<VRDisplayImpl>( | |
69 device, this, client_.get(), std::move(display_info)); | |
70 } | |
71 | |
72 VRDisplayImpl* VRServiceImpl::GetVRDisplayImpl(VRDevice* device) { | |
73 auto it = displays_.find(device); | |
74 return (it == displays_.end()) ? nullptr : it->second.get(); | |
75 } | |
76 | |
67 } // namespace device | 77 } // namespace device |
OLD | NEW |