Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(397)

Side by Side Diff: device/vr/vr_service_impl.cc

Issue 2746233002: Fixes 2D-to-WebVR site transitions when browsing in VR. (Closed)
Patch Set: Rebased on ToT, nits, removed DCHECK Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/vr/vr_service_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 base::Callback<void(mojom::VRDisplayInfoPtr)> on_created =
49 base::Bind(&VRServiceImpl::OnVRDisplayInfoCreated,
50 weak_ptr_factory_.GetWeakPtr(), device);
51 device->CreateVRDisplayInfo(on_created);
52 }
53
61 void VRServiceImpl::SetListeningForActivate(bool listening) { 54 void VRServiceImpl::SetListeningForActivate(bool listening) {
62 listening_for_activate_ = listening; 55 listening_for_activate_ = listening;
63 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); 56 VRDeviceManager* device_manager = VRDeviceManager::GetInstance();
64 device_manager->ListeningForActivateChanged(listening); 57 device_manager->ListeningForActivateChanged(listening);
65 } 58 }
66 59
60 // Creates a VRDisplayPtr unique to this service so that the associated page can
61 // communicate with the VRDevice.
62 void VRServiceImpl::OnVRDisplayInfoCreated(
63 VRDevice* device,
64 mojom::VRDisplayInfoPtr display_info) {
65 // TODO(crbug/701027): make sure that client_ is never null by initializing it
66 // in the constructor.
67 if (!client_) {
68 LOG(ERROR) << "Cannot create VR display because connection to render "
dcheng 2017/03/16 21:39:12 Nit: DLOG() please.
69 "process is not established";
70 return;
71 }
72 displays_[device] = base::MakeUnique<VRDisplayImpl>(
73 device, this, client_.get(), std::move(display_info));
74 }
75
76 VRDisplayImpl* VRServiceImpl::GetVRDisplayImplForTesting(VRDevice* device) {
77 auto it = displays_.find(device);
78 return (it == displays_.end()) ? nullptr : it->second.get();
79 }
80
67 } // namespace device 81 } // namespace device
OLDNEW
« no previous file with comments | « device/vr/vr_service_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698