Chromium Code Reviews| Index: content/browser/gamepad/gamepad_service.cc |
| diff --git a/content/browser/gamepad/gamepad_service.cc b/content/browser/gamepad/gamepad_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..424518cbbe51800e4503602bb1a1dccc67a446ef |
| --- /dev/null |
| +++ b/content/browser/gamepad/gamepad_service.cc |
| @@ -0,0 +1,89 @@ |
| +// Copyright (c) 2011 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 "content/browser/gamepad/data_fetcher.h" |
| +#include "content/browser/gamepad/gamepad_provider.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_source.h" |
| +#include "content/public/browser/notification_types.h" |
| +#include "content/public/browser/render_process_host.h" |
| + |
| +namespace content { |
| + |
| +GamepadService* GamepadService::instance_; |
| + |
| +GamepadService::GamepadService() : num_readers_(0) { |
| +} |
| + |
| +GamepadService::~GamepadService() { |
| +} |
| + |
| +GamepadService* GamepadService::GetInstance() { |
| + if (!instance_) { |
| + instance_ = new GamepadService(); |
|
jam
2011/12/01 20:07:31
just use Singleton for singletons :)
scottmg
2011/12/01 22:05:32
Done. (was because of the refcount cruft which is
|
| + instance_->AddRef(); |
| + } |
| + return instance_; |
| +} |
| + |
| +void GamepadService::Start( |
| + GamepadDataFetcher* data_fetcher, |
| + content::RenderProcessHost* associated_rph) { |
| + num_readers_++; |
| + if (!provider_) |
| + provider_ = new GamepadProvider(data_fetcher); |
| + DCHECK(num_readers_ > 0); |
| + provider_->Resume(); |
| + |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&GamepadService::RegisterForCloseNotification, |
| + this, |
| + associated_rph)); |
| +} |
| + |
| +void GamepadService::RegisterForCloseNotification( |
| + content::RenderProcessHost* rph) { |
| + registrar_.Add(this, |
| + content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| + content::Source<content::RenderProcessHost>(rph)); |
| +} |
| + |
| +base::SharedMemoryHandle GamepadService::GetSharedMemoryHandle( |
| + base::ProcessHandle handle) { |
| + DCHECK(provider_); |
|
jam
2011/12/01 20:07:31
nit: by convention we avoid DCHECKing variables be
scottmg
2011/12/01 22:05:32
Done.
|
| + return provider_->GetRendererSharedMemoryHandle(handle); |
| +} |
| + |
| +void GamepadService::Stop() { |
| + --num_readers_; |
| + DCHECK(provider_); |
|
jam
2011/12/01 20:07:31
ditto
scottmg
2011/12/01 22:05:32
Done.
|
| + DCHECK(num_readers_ >= 0); |
| + |
| + if (num_readers_ == 0) |
| + provider_->Pause(); |
| +} |
| + |
| +void GamepadService::Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + switch (type) { |
|
jam
2011/12/01 20:07:31
nit: just do line 77-80 only since you're register
scottmg
2011/12/01 22:05:32
Done.
|
| + case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(&GamepadService::Stop, this)); |
| + break; |
| + } |
| + default: { |
| + break; |
| + } |
| + } |
| +} |
| + |
| +} // namespace content |