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

Side by Side Diff: content/browser/gamepad/gamepad_service.cc

Issue 2081583002: Migrating majority of gamepad from content/browser/ to device/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Next attempt Created 4 years, 5 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/gamepad/gamepad_service.h" 5 #include "content/browser/gamepad/gamepad_service.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h"
11 #include "base/memory/singleton.h" 12 #include "base/memory/singleton.h"
12 #include "content/browser/gamepad/gamepad_consumer.h" 13 #include "content/browser/gamepad/gamepad_shared_buffer_impl.h"
13 #include "content/browser/gamepad/gamepad_data_fetcher.h"
14 #include "content/browser/gamepad/gamepad_provider.h"
15 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/render_process_host.h" 15 #include "content/public/browser/render_process_host.h"
16 #include "device/gamepad/gamepad_consumer.h"
17 #include "device/gamepad/gamepad_data_fetcher.h"
18 #include "device/gamepad/gamepad_provider.h"
17 19
18 namespace content { 20 namespace content {
19 21
20 namespace { 22 namespace {
21 GamepadService* g_gamepad_service = 0; 23 GamepadService* g_gamepad_service = 0;
22 } 24 }
23 25
24 GamepadService::GamepadService() 26 GamepadService::GamepadService()
25 : num_active_consumers_(0), 27 : num_active_consumers_(0),
26 gesture_callback_pending_(false) { 28 gesture_callback_pending_(false) {
27 SetInstance(this); 29 SetInstance(this);
28 } 30 }
29 31
30 GamepadService::GamepadService(std::unique_ptr<GamepadDataFetcher> fetcher) 32 GamepadService::GamepadService(
31 : provider_(new GamepadProvider(std::move(fetcher))), 33 std::unique_ptr<device::GamepadDataFetcher> fetcher)
34 : provider_(new device::GamepadProvider(
35 base::MakeUnique<GamepadSharedBufferImpl>(), this, std::move(fetcher))),
32 num_active_consumers_(0), 36 num_active_consumers_(0),
33 gesture_callback_pending_(false) { 37 gesture_callback_pending_(false) {
34 SetInstance(this); 38 SetInstance(this);
35 thread_checker_.DetachFromThread(); 39 thread_checker_.DetachFromThread();
36 } 40 }
37 41
38 GamepadService::~GamepadService() { 42 GamepadService::~GamepadService() {
39 SetInstance(NULL); 43 SetInstance(NULL);
40 } 44 }
41 45
42 void GamepadService::SetInstance(GamepadService* instance) { 46 void GamepadService::SetInstance(GamepadService* instance) {
43 // Unit tests can create multiple instances but only one should exist at any 47 // Unit tests can create multiple instances but only one should exist at any
44 // given time so g_gamepad_service should only go from NULL to non-NULL and 48 // given time so g_gamepad_service should only go from NULL to non-NULL and
45 // vica versa. 49 // vica versa.
46 CHECK(!!instance != !!g_gamepad_service); 50 CHECK(!!instance != !!g_gamepad_service);
47 g_gamepad_service = instance; 51 g_gamepad_service = instance;
48 } 52 }
49 53
50 GamepadService* GamepadService::GetInstance() { 54 GamepadService* GamepadService::GetInstance() {
51 if (!g_gamepad_service) 55 if (!g_gamepad_service)
52 g_gamepad_service = new GamepadService; 56 g_gamepad_service = new GamepadService;
53 return g_gamepad_service; 57 return g_gamepad_service;
54 } 58 }
55 59
56 void GamepadService::ConsumerBecameActive(GamepadConsumer* consumer) { 60 void GamepadService::ConsumerBecameActive(device::GamepadConsumer* consumer) {
57 DCHECK(thread_checker_.CalledOnValidThread()); 61 DCHECK(thread_checker_.CalledOnValidThread());
58 62
59 if (!provider_) 63 if (!provider_)
60 provider_.reset(new GamepadProvider); 64 provider_.reset(new device::GamepadProvider(
65 base::MakeUnique<GamepadSharedBufferImpl>(), this));
61 66
62 std::pair<ConsumerSet::iterator, bool> insert_result = 67 std::pair<ConsumerSet::iterator, bool> insert_result =
63 consumers_.insert(consumer); 68 consumers_.insert(consumer);
64 insert_result.first->is_active = true; 69 insert_result.first->is_active = true;
65 if (!insert_result.first->did_observe_user_gesture && 70 if (!insert_result.first->did_observe_user_gesture &&
66 !gesture_callback_pending_) { 71 !gesture_callback_pending_) {
67 gesture_callback_pending_ = true; 72 gesture_callback_pending_ = true;
68 provider_->RegisterForUserGesture( 73 provider_->RegisterForUserGesture(
69 base::Bind(&GamepadService::OnUserGesture, 74 base::Bind(&GamepadService::OnUserGesture,
70 base::Unretained(this))); 75 base::Unretained(this)));
71 } 76 }
72 77
73 if (num_active_consumers_++ == 0) 78 if (num_active_consumers_++ == 0)
74 provider_->Resume(); 79 provider_->Resume();
75 } 80 }
76 81
77 void GamepadService::ConsumerBecameInactive(GamepadConsumer* consumer) { 82 void GamepadService::ConsumerBecameInactive(device::GamepadConsumer* consumer) {
78 DCHECK(provider_); 83 DCHECK(provider_);
79 DCHECK(num_active_consumers_ > 0); 84 DCHECK(num_active_consumers_ > 0);
80 DCHECK(consumers_.count(consumer) > 0); 85 DCHECK(consumers_.count(consumer) > 0);
81 DCHECK(consumers_.find(consumer)->is_active); 86 DCHECK(consumers_.find(consumer)->is_active);
82 87
83 consumers_.find(consumer)->is_active = false; 88 consumers_.find(consumer)->is_active = false;
84 if (--num_active_consumers_ == 0) 89 if (--num_active_consumers_ == 0)
85 provider_->Pause(); 90 provider_->Pause();
86 } 91 }
87 92
88 void GamepadService::RemoveConsumer(GamepadConsumer* consumer) { 93 void GamepadService::RemoveConsumer(device::GamepadConsumer* consumer) {
89 DCHECK(thread_checker_.CalledOnValidThread()); 94 DCHECK(thread_checker_.CalledOnValidThread());
90 95
91 ConsumerSet::iterator it = consumers_.find(consumer); 96 ConsumerSet::iterator it = consumers_.find(consumer);
92 if (it->is_active && --num_active_consumers_ == 0) 97 if (it->is_active && --num_active_consumers_ == 0)
93 provider_->Pause(); 98 provider_->Pause();
94 consumers_.erase(it); 99 consumers_.erase(it);
95 } 100 }
96 101
97 void GamepadService::RegisterForUserGesture(const base::Closure& closure) { 102 void GamepadService::RegisterForUserGesture(const base::Closure& closure) {
98 DCHECK(consumers_.size() > 0); 103 DCHECK(consumers_.size() > 0);
99 DCHECK(thread_checker_.CalledOnValidThread()); 104 DCHECK(thread_checker_.CalledOnValidThread());
100 provider_->RegisterForUserGesture(closure); 105 provider_->RegisterForUserGesture(closure);
101 } 106 }
102 107
103 void GamepadService::Terminate() { 108 void GamepadService::Terminate() {
104 provider_.reset(); 109 provider_.reset();
105 } 110 }
106 111
112 void GamepadService::OnGamepadConnectionChange(bool connected,
113 int index,
114 const blink::WebGamepad& pad) {
115 if (connected) {
116 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
117 base::Bind(&GamepadService::OnGamepadConnected,
118 base::Unretained(this), index, pad));
119 } else {
120 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
121 base::Bind(&GamepadService::OnGamepadDisconnected,
122 base::Unretained(this), index, pad));
123 }
124 }
125
107 void GamepadService::OnGamepadConnected( 126 void GamepadService::OnGamepadConnected(
108 int index, 127 int index,
109 const blink::WebGamepad& pad) { 128 const blink::WebGamepad& pad) {
110 DCHECK(thread_checker_.CalledOnValidThread()); 129 DCHECK(thread_checker_.CalledOnValidThread());
111 130
112 for (ConsumerSet::iterator it = consumers_.begin(); 131 for (ConsumerSet::iterator it = consumers_.begin();
113 it != consumers_.end(); ++it) { 132 it != consumers_.end(); ++it) {
114 if (it->did_observe_user_gesture && it->is_active) 133 if (it->did_observe_user_gesture && it->is_active)
115 it->consumer->OnGamepadConnected(index, pad); 134 it->consumer->OnGamepadConnected(index, pad);
116 } 135 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { 172 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) {
154 const blink::WebGamepad& pad = gamepads.items[i]; 173 const blink::WebGamepad& pad = gamepads.items[i];
155 if (pad.connected) 174 if (pad.connected)
156 info.consumer->OnGamepadConnected(i, pad); 175 info.consumer->OnGamepadConnected(i, pad);
157 } 176 }
158 } 177 }
159 } 178 }
160 } 179 }
161 180
162 } // namespace content 181 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698