1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef DEVICE_VR_VR_DEVICE_BASE_H_
6 #define DEVICE_VR_VR_DEVICE_BASE_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "device/vr/public/mojom/vr_service.mojom.h"
14 #include "device/vr/vr_device.h"
15 #include "device/vr/vr_export.h"
16 #include "mojo/public/cpp/bindings/associated_remote.h"
17 #include "mojo/public/cpp/bindings/pending_associated_remote.h"
18 #include "mojo/public/cpp/bindings/pending_remote.h"
19 #include "mojo/public/cpp/bindings/receiver.h"
20 #include "ui/display/display.h"
21 
22 namespace device {
23 
24 // Represents one of the platform's VR devices. Owned by the respective
25 // VRDeviceProvider.
26 // TODO(mthiesse, crbug.com/769373): Remove DEVICE_VR_EXPORT.
27 class DEVICE_VR_EXPORT VRDeviceBase : public mojom::XRRuntime {
28  public:
29   explicit VRDeviceBase(mojom::XRDeviceId id);
30   ~VRDeviceBase() override;
31 
32   // XRRuntime implementation
33   void ListenToDeviceChanges(
34       mojo::PendingAssociatedRemote<mojom::XRRuntimeEventListener> listener,
35       mojom::XRRuntime::ListenToDeviceChangesCallback callback) final;
36   void SetInlinePosesEnabled(bool enable) override;
37   void ShutdownSession(mojom::XRRuntime::ShutdownSessionCallback) override;
38 
39   device::mojom::XRDeviceId GetId() const;
40 
41   bool HasExclusiveSession();
42 
43   // Devices may be paused/resumed when focus changes by GVR delegate.
44   virtual void PauseTracking();
45   virtual void ResumeTracking();
46 
47   mojom::VRDisplayInfoPtr GetVRDisplayInfo();
48 
49   // Used by providers to bind devices.
50   mojo::PendingRemote<mojom::XRRuntime> BindXRRuntime();
51 
52   // TODO(mthiesse): The browser should handle browser-side exiting of
53   // presentation before device/ is even aware presentation is being exited.
54   // Then the browser should call StopSession() on Device, which does device/
55   // exiting of presentation before notifying displays. This is currently messy
56   // because browser-side notions of presentation are mostly Android-specific.
57   virtual void OnExitPresent();
58 
59  protected:
60   // Devices tell VRDeviceBase when they start presenting.  It will be paired
61   // with an OnExitPresent when the device stops presenting.
62   void OnStartPresenting();
IsPresenting()63   bool IsPresenting() { return presenting_; }  // Exposed for test.
64   void SetVRDisplayInfo(mojom::VRDisplayInfoPtr display_info);
65   void OnVisibilityStateChanged(mojom::XRVisibilityState visibility_state);
66 
67   mojom::VRDisplayInfoPtr display_info_;
68 
69   bool inline_poses_enabled_ = true;
70 
71  private:
72   mojo::AssociatedRemote<mojom::XRRuntimeEventListener> listener_;
73 
74   bool presenting_ = false;
75 
76   device::mojom::XRDeviceId id_;
77 
78   mojo::Receiver<mojom::XRRuntime> runtime_receiver_{this};
79 
80   DISALLOW_COPY_AND_ASSIGN(VRDeviceBase);
81 };
82 
83 }  // namespace device
84 
85 #endif  // DEVICE_VR_VR_DEVICE_BASE_H_
86