summaryrefslogtreecommitdiffstats
path: root/content/browser/gamepad/gamepad_service.cc
blob: a6e90d3acca3c6c9044c25c69d5f97088c5cd791 (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
73
74
75
76
// 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 "base/bind.h"
#include "base/memory/singleton.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() : num_readers_(0) {
}

GamepadService::~GamepadService() {
}

GamepadService* GamepadService::GetInstance() {
  return Singleton<GamepadService>::get();
}

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,
                 base::Unretained(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) {
  return provider_->GetRendererSharedMemoryHandle(handle);
}

void GamepadService::Stop() {
  --num_readers_;
  DCHECK(num_readers_ >= 0);

  if (num_readers_ == 0)
    provider_->Pause();
}

void GamepadService::Observe(int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED);
  content::BrowserThread::PostTask(
      content::BrowserThread::IO,
      FROM_HERE,
      base::Bind(&GamepadService::Stop, base::Unretained(this)));
}

} // namespace content