summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/vr/vr_dispatcher.cc
blob: acd893747f3d9e1965d2e2affc68a38b6eef907b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/vr/vr_dispatcher.h"

#include <stddef.h>

#include "content/public/common/service_registry.h"
#include "content/renderer/vr/vr_type_converters.h"

namespace content {

VRDispatcher::VRDispatcher(ServiceRegistry* service_registry)
    : service_registry_(service_registry) {
}

VRDispatcher::~VRDispatcher() {
}

mojom::VRServicePtr& VRDispatcher::GetVRServicePtr() {
  if (!vr_service_) {
    service_registry_->ConnectToRemoteService(mojo::GetProxy(&vr_service_));
  }
  return vr_service_;
}

void VRDispatcher::getDevices(blink::WebVRGetDevicesCallback* callback) {
  int request_id = pending_requests_.Add(callback);
  GetVRServicePtr()->GetDevices(base::Bind(&VRDispatcher::OnGetDevices,
                                           base::Unretained(this), request_id));
}

void VRDispatcher::getSensorState(unsigned int index,
                                  blink::WebHMDSensorState& state) {
  GetVRServicePtr()->GetSensorState(
      index,
      base::Bind(&VRDispatcher::OnGetSensorState, base::Unretained(&state)));

  // This call needs to return results synchronously in order to be useful and
  // provide the lowest latency results possible.
  GetVRServicePtr().WaitForIncomingResponse();
}

void VRDispatcher::resetSensor(unsigned int index) {
  GetVRServicePtr()->ResetSensor(index);
}

void VRDispatcher::OnGetDevices(
    int request_id,
    const mojo::Array<mojom::VRDeviceInfoPtr>& devices) {
  blink::WebVector<blink::WebVRDevice> web_devices(devices.size());

  blink::WebVRGetDevicesCallback* callback =
      pending_requests_.Lookup(request_id);
  if (!callback)
    return;

  for (size_t i = 0; i < devices.size(); ++i) {
    web_devices[i] = devices[i].To<blink::WebVRDevice>();
  }

  callback->onSuccess(web_devices);
  pending_requests_.Remove(request_id);
}

void VRDispatcher::OnGetSensorState(blink::WebHMDSensorState* state,
                                    const mojom::VRSensorStatePtr& mojo_state) {
  *state = mojo_state.To<blink::WebHMDSensorState>();
}

}  // namespace content