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

Unified Diff: content/browser/vr/vr_service.cc

Issue 829803003: Adding Chrome-side WebVR interface (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved cardboard-java gyp and gn config into third_party Created 5 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/vr/vr_service.cc
diff --git a/content/browser/vr/vr_service.cc b/content/browser/vr/vr_service.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a9725d2d2576c51d347a1c2e4fbcbe1ee31370be
--- /dev/null
+++ b/content/browser/vr/vr_service.cc
@@ -0,0 +1,147 @@
+// 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/browser/vr/vr_service.h"
+
+#include "base/memory/singleton.h"
+#include "third_party/WebKit/public/platform/WebVR.h"
+
+#if defined(OS_ANDROID)
+#include "content/browser/vr/cardboard/cardboard_vr_device_provider.h"
+#endif
+
+namespace content {
+
+namespace {
+VRService* g_vr_service = NULL;
+}
+
+VRService::VRService() :
+ vr_initialized_(false),
+ keep_alive_(false) {
+#if defined(OS_ANDROID)
+ scoped_ptr<VRDeviceProvider> cardboard_provider(
+ new CardboardVRDeviceProvider());
+ RegisterProvider(cardboard_provider.Pass());
+#endif
+}
+
+VRService::VRService(scoped_ptr<VRDeviceProvider> provider) :
+ vr_initialized_(false),
+ keep_alive_(true) {
+ thread_checker_.DetachFromThread();
+ RegisterProvider(provider.Pass());
+ SetInstance(this);
+}
+
+VRService::~VRService() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ g_vr_service = NULL;
+}
+
+VRService* VRService::GetInstance() {
+ if (!g_vr_service)
+ g_vr_service = new VRService();
+ return g_vr_service;
+}
+
+void VRService::SetInstance(VRService* instance) {
+ // Unit tests can create multiple instances but only one should exist at any
+ // given time so g_vr_service should only go from NULL to non-NULL and
+ // vica versa.
+ CHECK_NE(!!instance, !!g_vr_service);
+ g_vr_service = instance;
+}
+
+bool VRService::HasInstance() {
+ // For testing. Checks to see if a VRService instance is active.
+ return !!g_vr_service;
+}
+
+void VRService::GetVRDevices(
+ std::vector<blink::WebVRDevice>* webvr_devices) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ if (!webvr_devices)
+ return;
+
+ InitializeProviders();
+
+ webvr_devices->clear();
+
+ ProviderList::iterator iter;
+
+ std::vector<VRDevice*> devices;
+
+ for (iter = providers_.begin(); iter != providers_.end(); ++iter) {
+ (*iter)->GetDevices(devices);
+ }
+
+ std::vector<VRDevice*>::iterator diter;
+ for (diter = devices.begin(); diter != devices.end(); ++diter) {
+ VRDevice* device = *diter;
+ if (devices_.find(device->id()) == devices_.end()) {
+ devices_[device->id()] = device;
+ }
+
+ blink::WebVRDevice webvr_device;
+ device->GetVRDevice(&webvr_device);
+ webvr_device.index = device->id();
+ webvr_devices->push_back(webvr_device);
+ }
+}
+
+VRDevice* VRService::GetDevice(unsigned int index) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ DeviceMap::iterator iter = devices_.find(index);
+ if (iter == devices_.end()) {
+ return NULL;
Ted C 2015/04/24 01:06:07 nullptr (and other places instead of NULL)
+ }
+ return iter->second;
+}
+
+void VRService::AddConsumer(VRServiceConsumer* consumer) {
+ VRService* service = GetInstance();
+ DCHECK(service->thread_checker_.CalledOnValidThread());
+
+ // Explicitly does not initialize the providers. That's done lazily the first
+ // time GetVRDevices is called.
+ service->consumers_.insert(consumer);
+}
+
+void VRService::RemoveConsumer(VRServiceConsumer* consumer) {
+ CHECK(g_vr_service);
+ DCHECK(g_vr_service->thread_checker_.CalledOnValidThread());
+
+ ConsumerSet::iterator it = g_vr_service->consumers_.find(consumer);
+ DCHECK(it != g_vr_service->consumers_.end());
+
+ if (it != g_vr_service->consumers_.end()) {
+ g_vr_service->consumers_.erase(it);
+ if (g_vr_service->consumers_.size() == 0 && !g_vr_service->keep_alive_) {
+ // Delete the service when it has no consumers left.
+ delete g_vr_service;
+ }
+ }
+}
+
+void VRService::InitializeProviders() {
+ if (vr_initialized_ == true) {
Ted C 2015/04/24 01:06:07 no need for == true
+ return;
+ }
+
+ ProviderList::iterator iter;
+ for (iter = providers_.begin(); iter != providers_.end(); ++iter) {
+ (*iter)->Initialize();
+ }
+
+ vr_initialized_ = true;
+}
+
+void VRService::RegisterProvider(scoped_ptr<VRDeviceProvider> provider) {
+ providers_.push_back(make_linked_ptr(provider.release()));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698