Index: device/gamepad/gamepad_provider.cc |
diff --git a/content/browser/gamepad/gamepad_provider.cc b/device/gamepad/gamepad_provider.cc |
similarity index 76% |
rename from content/browser/gamepad/gamepad_provider.cc |
rename to device/gamepad/gamepad_provider.cc |
index 5f196b11ecafe656fd6ff0c154cafe72f3b85421..dded2673f8ce9de6d318d732cf14cee696b2ddde 100644 |
--- a/content/browser/gamepad/gamepad_provider.cc |
+++ b/device/gamepad/gamepad_provider.cc |
@@ -2,7 +2,7 @@ |
// 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_provider.h" |
+#include "device/gamepad/gamepad_provider.h" |
#include <stddef.h> |
#include <string.h> |
@@ -20,44 +20,47 @@ |
#include "base/threading/thread_restrictions.h" |
#include "base/threading/thread_task_runner_handle.h" |
#include "build/build_config.h" |
-#include "content/browser/gamepad/gamepad_data_fetcher.h" |
-#include "content/browser/gamepad/gamepad_platform_data_fetcher.h" |
-#include "content/browser/gamepad/gamepad_service.h" |
-#include "content/common/gamepad_hardware_buffer.h" |
-#include "content/common/gamepad_messages.h" |
-#include "content/common/gamepad_user_gesture.h" |
-#include "content/public/browser/browser_thread.h" |
+#include "device/gamepad/gamepad_data_fetcher.h" |
+#include "device/gamepad/gamepad_platform_data_fetcher.h" |
+#include "device/gamepad/gamepad_user_gesture.h" |
using blink::WebGamepad; |
using blink::WebGamepads; |
-namespace content { |
+namespace device { |
GamepadProvider::ClosureAndThread::ClosureAndThread( |
const base::Closure& c, |
const scoped_refptr<base::SingleThreadTaskRunner>& m) |
- : closure(c), task_runner(m) { |
-} |
+ : closure(c), task_runner(m) {} |
GamepadProvider::ClosureAndThread::ClosureAndThread( |
const ClosureAndThread& other) = default; |
-GamepadProvider::ClosureAndThread::~ClosureAndThread() { |
-} |
+GamepadProvider::ClosureAndThread::~ClosureAndThread() {} |
-GamepadProvider::GamepadProvider() |
+GamepadProvider::GamepadProvider( |
+ std::unique_ptr<GamepadSharedBuffer> buffer, |
+ GamepadConnectionChangeClient* connection_change_client) |
: is_paused_(true), |
have_scheduled_do_poll_(false), |
devices_changed_(true), |
- ever_had_user_gesture_(false) { |
+ ever_had_user_gesture_(false), |
+ gamepad_shared_buffer_(std::move(buffer)), |
+ connection_change_client_(connection_change_client) { |
Initialize(std::unique_ptr<GamepadDataFetcher>()); |
} |
-GamepadProvider::GamepadProvider(std::unique_ptr<GamepadDataFetcher> fetcher) |
+GamepadProvider::GamepadProvider( |
+ std::unique_ptr<GamepadSharedBuffer> buffer, |
+ GamepadConnectionChangeClient* connection_change_client, |
+ std::unique_ptr<GamepadDataFetcher> fetcher) |
: is_paused_(true), |
have_scheduled_do_poll_(false), |
devices_changed_(true), |
- ever_had_user_gesture_(false) { |
+ ever_had_user_gesture_(false), |
+ gamepad_shared_buffer_(std::move(buffer)), |
+ connection_change_client_(connection_change_client) { |
Initialize(std::move(fetcher)); |
} |
@@ -75,14 +78,15 @@ GamepadProvider::~GamepadProvider() { |
base::SharedMemoryHandle GamepadProvider::GetSharedMemoryHandleForProcess( |
base::ProcessHandle process) { |
base::SharedMemoryHandle renderer_handle; |
- gamepad_shared_memory_.ShareToProcess(process, &renderer_handle); |
+ gamepad_shared_buffer_->shared_memory()->ShareToProcess(process, |
+ &renderer_handle); |
return renderer_handle; |
} |
void GamepadProvider::GetCurrentGamepadData(WebGamepads* data) { |
- const WebGamepads& pads = SharedMemoryAsHardwareBuffer()->buffer; |
+ const WebGamepads* pads = gamepad_shared_buffer_->buffer(); |
base::AutoLock lock(shared_memory_lock_); |
- *data = pads; |
+ *data = *pads; |
} |
void GamepadProvider::Pause() { |
@@ -100,7 +104,7 @@ void GamepadProvider::Resume() { |
{ |
base::AutoLock lock(is_paused_lock_); |
if (!is_paused_) |
- return; |
+ return; |
is_paused_ = false; |
} |
@@ -125,14 +129,10 @@ void GamepadProvider::OnDevicesChanged(base::SystemMonitor::DeviceType type) { |
} |
void GamepadProvider::Initialize(std::unique_ptr<GamepadDataFetcher> fetcher) { |
- size_t data_size = sizeof(GamepadHardwareBuffer); |
base::SystemMonitor* monitor = base::SystemMonitor::Get(); |
if (monitor) |
monitor->AddDevicesChangedObserver(this); |
- bool res = gamepad_shared_memory_.CreateAndMapAnonymous(data_size); |
- CHECK(res); |
- GamepadHardwareBuffer* hwbuf = SharedMemoryAsHardwareBuffer(); |
- memset(hwbuf, 0, sizeof(GamepadHardwareBuffer)); |
+ |
pad_states_.reset(new PadState[WebGamepads::itemsLengthCap]); |
polling_thread_.reset(new base::Thread("Gamepad polling thread")); |
@@ -174,8 +174,7 @@ void GamepadProvider::SendPauseHint(bool paused) { |
} |
bool GamepadProvider::PadState::Match(const WebGamepad& pad) const { |
- return connected_ == pad.connected && |
- axes_length_ == pad.axesLength && |
+ return connected_ == pad.connected && axes_length_ == pad.axesLength && |
buttons_length_ == pad.buttonsLength && |
memcmp(id_, pad.id, sizeof(id_)) == 0 && |
memcmp(mapping_, pad.mapping, sizeof(mapping_)) == 0; |
@@ -213,12 +212,9 @@ void GamepadProvider::DoPoll() { |
have_scheduled_do_poll_ = false; |
bool changed; |
- GamepadHardwareBuffer* hwbuf = SharedMemoryAsHardwareBuffer(); |
- ANNOTATE_BENIGN_RACE_SIZED( |
- &hwbuf->buffer, |
- sizeof(WebGamepads), |
- "Racey reads are discarded"); |
+ ANNOTATE_BENIGN_RACE_SIZED(gamepad_shared_buffer_->buffer(), |
+ sizeof(WebGamepads), "Racey reads are discarded"); |
{ |
base::AutoLock lock(devices_changed_lock_); |
@@ -231,14 +227,14 @@ void GamepadProvider::DoPoll() { |
// Acquire the SeqLock. There is only ever one writer to this data. |
// See gamepad_hardware_buffer.h. |
- hwbuf->sequence.WriteBegin(); |
- data_fetcher_->GetGamepadData(&hwbuf->buffer, changed); |
- hwbuf->sequence.WriteEnd(); |
+ gamepad_shared_buffer_->WriteBegin(); |
+ data_fetcher_->GetGamepadData(gamepad_shared_buffer_->buffer(), changed); |
+ gamepad_shared_buffer_->WriteEnd(); |
} |
if (ever_had_user_gesture_) { |
for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
- WebGamepad& pad = hwbuf->buffer.items[i]; |
+ WebGamepad& pad = gamepad_shared_buffer_->buffer()->items[i]; |
PadState& state = pad_states_.get()[i]; |
if (pad.connected && !state.connected()) { |
OnGamepadConnectionChange(true, i, pad); |
@@ -276,36 +272,17 @@ void GamepadProvider::ScheduleDoPoll() { |
have_scheduled_do_poll_ = true; |
} |
-void GamepadProvider::OnGamepadConnectionChange( |
- bool connected, int index, const WebGamepad& pad) { |
+void GamepadProvider::OnGamepadConnectionChange(bool connected, |
+ int index, |
+ const WebGamepad& pad) { |
PadState& state = pad_states_.get()[index]; |
if (connected) |
state.SetPad(pad); |
else |
state.SetDisconnected(); |
- BrowserThread::PostTask( |
- BrowserThread::IO, |
- FROM_HERE, |
- base::Bind(&GamepadProvider::DispatchGamepadConnectionChange, |
- base::Unretained(this), |
- connected, |
- index, |
- pad)); |
-} |
- |
-void GamepadProvider::DispatchGamepadConnectionChange( |
- bool connected, int index, const WebGamepad& pad) { |
- if (connected) |
- GamepadService::GetInstance()->OnGamepadConnected(index, pad); |
- else |
- GamepadService::GetInstance()->OnGamepadDisconnected(index, pad); |
-} |
- |
-GamepadHardwareBuffer* GamepadProvider::SharedMemoryAsHardwareBuffer() { |
- void* mem = gamepad_shared_memory_.memory(); |
- CHECK(mem); |
- return static_cast<GamepadHardwareBuffer*>(mem); |
+ if (connection_change_client_) |
+ connection_change_client_->OnGamepadConnectionChange(connected, index, pad); |
} |
void GamepadProvider::CheckForUserGesture() { |
@@ -314,8 +291,8 @@ void GamepadProvider::CheckForUserGesture() { |
return; |
bool had_gesture_before = ever_had_user_gesture_; |
- const WebGamepads& pads = SharedMemoryAsHardwareBuffer()->buffer; |
- if (GamepadsHaveUserGesture(pads)) { |
+ const WebGamepads* pads = gamepad_shared_buffer_->buffer(); |
+ if (GamepadsHaveUserGesture(*pads)) { |
ever_had_user_gesture_ = true; |
for (size_t i = 0; i < user_gesture_observers_.size(); i++) { |
user_gesture_observers_[i].task_runner->PostTask( |
@@ -326,9 +303,9 @@ void GamepadProvider::CheckForUserGesture() { |
if (!had_gesture_before && ever_had_user_gesture_) { |
// Initialize pad_states_ for the first time. |
for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
- pad_states_.get()[i].SetPad(pads.items[i]); |
+ pad_states_.get()[i].SetPad(pads->items[i]); |
} |
} |
} |
-} // namespace content |
+} // namespace device |