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

Unified Diff: content/browser/gamepad/gamepad_service.cc

Issue 2563483006: Move gamepad_service out of content/ and into device/ (Closed)
Patch Set: addressed Blundell's review comments Created 4 years 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
« no previous file with comments | « content/browser/gamepad/gamepad_service.h ('k') | content/browser/gamepad/gamepad_service_test_helpers.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/gamepad/gamepad_service.cc
diff --git a/content/browser/gamepad/gamepad_service.cc b/content/browser/gamepad/gamepad_service.cc
deleted file mode 100644
index 8450fe713b57da55032e3aa561c41e11731655a0..0000000000000000000000000000000000000000
--- a/content/browser/gamepad/gamepad_service.cc
+++ /dev/null
@@ -1,197 +0,0 @@
-// Copyright (c) 2012 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/gamepad/gamepad_service.h"
-
-#include <utility>
-
-#include "base/bind.h"
-#include "base/logging.h"
-#include "base/memory/ptr_util.h"
-#include "base/memory/singleton.h"
-#include "content/browser/gamepad/gamepad_shared_buffer_impl.h"
-#include "content/common/gamepad_hardware_buffer.h"
-#include "content/public/browser/browser_thread.h"
-#include "content/public/browser/render_process_host.h"
-#include "device/gamepad/gamepad_consumer.h"
-#include "device/gamepad/gamepad_data_fetcher.h"
-#include "device/gamepad/gamepad_provider.h"
-#include "mojo/public/cpp/system/platform_handle.h"
-
-namespace content {
-
-namespace {
-GamepadService* g_gamepad_service = 0;
-}
-
-GamepadService::GamepadService()
- : num_active_consumers_(0),
- gesture_callback_pending_(false) {
- SetInstance(this);
-}
-
-GamepadService::GamepadService(
- std::unique_ptr<device::GamepadDataFetcher> fetcher)
- : provider_(new device::GamepadProvider(
- base::MakeUnique<GamepadSharedBufferImpl>(), this, std::move(fetcher))),
- num_active_consumers_(0),
- gesture_callback_pending_(false) {
- SetInstance(this);
- thread_checker_.DetachFromThread();
-}
-
-GamepadService::~GamepadService() {
- SetInstance(NULL);
-}
-
-void GamepadService::SetInstance(GamepadService* instance) {
- // Unit tests can create multiple instances but only one should exist at any
- // given time so g_gamepad_service should only go from NULL to non-NULL and
- // vica versa.
- CHECK(!!instance != !!g_gamepad_service);
- g_gamepad_service = instance;
-}
-
-GamepadService* GamepadService::GetInstance() {
- if (!g_gamepad_service)
- g_gamepad_service = new GamepadService;
- return g_gamepad_service;
-}
-
-void GamepadService::ConsumerBecameActive(device::GamepadConsumer* consumer) {
- DCHECK(thread_checker_.CalledOnValidThread());
-
- if (!provider_)
- provider_.reset(new device::GamepadProvider(
- base::MakeUnique<GamepadSharedBufferImpl>(), this));
-
- std::pair<ConsumerSet::iterator, bool> insert_result =
- consumers_.insert(consumer);
- insert_result.first->is_active = true;
- if (!insert_result.first->did_observe_user_gesture &&
- !gesture_callback_pending_) {
- gesture_callback_pending_ = true;
- provider_->RegisterForUserGesture(
- base::Bind(&GamepadService::OnUserGesture,
- base::Unretained(this)));
- }
-
- if (num_active_consumers_++ == 0)
- provider_->Resume();
-}
-
-void GamepadService::ConsumerBecameInactive(device::GamepadConsumer* consumer) {
- DCHECK(provider_);
- DCHECK(num_active_consumers_ > 0);
- DCHECK(consumers_.count(consumer) > 0);
- DCHECK(consumers_.find(consumer)->is_active);
-
- consumers_.find(consumer)->is_active = false;
- if (--num_active_consumers_ == 0)
- provider_->Pause();
-}
-
-void GamepadService::RemoveConsumer(device::GamepadConsumer* consumer) {
- DCHECK(thread_checker_.CalledOnValidThread());
-
- ConsumerSet::iterator it = consumers_.find(consumer);
- if (it->is_active && --num_active_consumers_ == 0)
- provider_->Pause();
- consumers_.erase(it);
-}
-
-void GamepadService::RegisterForUserGesture(const base::Closure& closure) {
- DCHECK(consumers_.size() > 0);
- DCHECK(thread_checker_.CalledOnValidThread());
- provider_->RegisterForUserGesture(closure);
-}
-
-void GamepadService::Terminate() {
- provider_.reset();
-}
-
-void GamepadService::OnGamepadConnectionChange(bool connected,
- int index,
- const blink::WebGamepad& pad) {
- if (connected) {
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
- base::Bind(&GamepadService::OnGamepadConnected,
- base::Unretained(this), index, pad));
- } else {
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
- base::Bind(&GamepadService::OnGamepadDisconnected,
- base::Unretained(this), index, pad));
- }
-}
-
-void GamepadService::OnGamepadConnected(
- int index,
- const blink::WebGamepad& pad) {
- DCHECK(thread_checker_.CalledOnValidThread());
-
- for (ConsumerSet::iterator it = consumers_.begin();
- it != consumers_.end(); ++it) {
- if (it->did_observe_user_gesture && it->is_active)
- it->consumer->OnGamepadConnected(index, pad);
- }
-}
-
-void GamepadService::OnGamepadDisconnected(
- int index,
- const blink::WebGamepad& pad) {
- DCHECK(thread_checker_.CalledOnValidThread());
-
- for (ConsumerSet::iterator it = consumers_.begin();
- it != consumers_.end(); ++it) {
- if (it->did_observe_user_gesture && it->is_active)
- it->consumer->OnGamepadDisconnected(index, pad);
- }
-}
-
-base::SharedMemoryHandle GamepadService::GetSharedMemoryHandleForProcess(
- base::ProcessHandle handle) {
- DCHECK(thread_checker_.CalledOnValidThread());
- return provider_->GetSharedMemoryHandleForProcess(handle);
-}
-
-mojo::ScopedSharedBufferHandle GamepadService::GetSharedBufferHandle() {
- DCHECK(thread_checker_.CalledOnValidThread());
-
- // TODO(heke): Use mojo::SharedBuffer rather than base::SharedMemory in
- // GamepadSharedBuffer. See crbug.com/670655 for details.
- return mojo::WrapSharedMemoryHandle(provider_->GetSharedMemoryHandle(),
- sizeof(GamepadHardwareBuffer),
- true /* read_only */);
-}
-
-void GamepadService::OnUserGesture() {
- DCHECK(thread_checker_.CalledOnValidThread());
-
- gesture_callback_pending_ = false;
-
- if (!provider_ ||
- num_active_consumers_ == 0)
- return;
-
- for (ConsumerSet::iterator it = consumers_.begin();
- it != consumers_.end(); ++it) {
- if (!it->did_observe_user_gesture && it->is_active) {
- const ConsumerInfo& info = *it;
- info.did_observe_user_gesture = true;
- blink::WebGamepads gamepads;
- provider_->GetCurrentGamepadData(&gamepads);
- for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) {
- const blink::WebGamepad& pad = gamepads.items[i];
- if (pad.connected)
- info.consumer->OnGamepadConnected(i, pad);
- }
- }
- }
-}
-
-void GamepadService::SetSanitizationEnabled(bool sanitize) {
- provider_->SetSanitizationEnabled(sanitize);
-}
-
-} // namespace content
« no previous file with comments | « content/browser/gamepad/gamepad_service.h ('k') | content/browser/gamepad/gamepad_service_test_helpers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698