Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <cmath> | |
| 6 #include <set> | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/message_loop.h" | |
| 12 #include "base/task.h" | |
| 13 #include "base/threading/thread.h" | |
| 14 #include "base/threading/thread_restrictions.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/browser/gamepad/gamepad_provider.h" | |
| 17 #include "content/browser/gamepad/data_fetcher.h" | |
| 18 #include "content/common/gamepad_messages.h" | |
| 19 | |
| 20 namespace gamepad { | |
| 21 | |
| 22 using namespace content; | |
| 23 | |
| 24 Provider::Provider(DataFetcher* fetcher) | |
| 25 : creator_loop_(MessageLoop::current()), | |
| 26 provided_fetcher_(fetcher), | |
| 27 devices_changed_(true), | |
| 28 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
| 29 const size_t DataSize = sizeof(GamepadHardwareBuffer); | |
|
jam
2011/11/16 00:24:58
nit: data_size per style guide. also, from what I'
scottmg
2011/11/16 18:04:35
Done.
| |
| 30 base::SystemMonitor* monitor = base::SystemMonitor::Get(); | |
| 31 if (monitor) | |
| 32 monitor->AddDevicesChangedObserver(this); | |
| 33 gamepad_shared_memory_.CreateAndMapAnonymous(DataSize); | |
| 34 GamepadHardwareBuffer* hwbuf = SharedMemoryAsHardwareBuffer(); | |
| 35 memset(hwbuf, 0, sizeof(GamepadHardwareBuffer)); | |
| 36 } | |
| 37 | |
| 38 Provider::~Provider() { | |
| 39 base::SystemMonitor* monitor = base::SystemMonitor::Get(); | |
| 40 if (monitor) | |
| 41 monitor->RemoveDevicesChangedObserver(this); | |
| 42 Stop(); | |
| 43 } | |
| 44 | |
| 45 base::SharedMemoryHandle Provider::GetRendererSharedMemoryHandle( | |
| 46 base::ProcessHandle process) { | |
| 47 base::SharedMemoryHandle renderer_handle; | |
| 48 gamepad_shared_memory_.ShareToProcess(process, &renderer_handle); | |
| 49 return renderer_handle; | |
| 50 } | |
| 51 | |
| 52 void Provider::OnDevicesChanged() { | |
| 53 devices_changed_ = true; | |
| 54 } | |
| 55 | |
| 56 void Provider::Start() { | |
| 57 DCHECK(MessageLoop::current() == creator_loop_); | |
| 58 | |
| 59 if (polling_thread_.get()) | |
| 60 return; | |
| 61 | |
| 62 polling_thread_.reset(new base::Thread("Gamepad polling thread")); | |
| 63 if (!polling_thread_->Start()) { | |
| 64 LOG(ERROR) << "Failed to start gamepad polling thread"; | |
| 65 polling_thread_.reset(); | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 MessageLoop* polling_loop = polling_thread_->message_loop(); | |
| 70 polling_loop->PostTask(FROM_HERE, | |
|
jam
2011/11/16 00:24:58
nit: this style of spacing isn't in the style guid
scottmg
2011/11/16 18:04:35
Done.
| |
| 71 base::Bind(&Provider::DoInitializePollingThread, this)); | |
| 72 } | |
| 73 | |
| 74 void Provider::Stop() { | |
| 75 DCHECK(MessageLoop::current() == creator_loop_); | |
| 76 | |
| 77 polling_thread_.reset(); | |
| 78 data_fetcher_.reset(); | |
| 79 } | |
| 80 | |
| 81 void Provider::DoInitializePollingThread() { | |
| 82 DCHECK(MessageLoop::current() == polling_thread_->message_loop()); | |
| 83 | |
| 84 if (!provided_fetcher_.get()) | |
| 85 provided_fetcher_.reset(new PlatformDataFetcher); | |
| 86 | |
| 87 // Pass ownership of fetcher to provider_. | |
| 88 data_fetcher_.swap(provided_fetcher_); | |
| 89 | |
| 90 // Start polling. | |
| 91 ScheduleDoPoll(); | |
| 92 } | |
| 93 | |
| 94 void Provider::DoPoll() { | |
| 95 DCHECK(MessageLoop::current() == polling_thread_->message_loop()); | |
| 96 | |
| 97 GamepadHardwareBuffer* hwbuf = SharedMemoryAsHardwareBuffer(); | |
| 98 | |
|
jam
2011/11/16 00:24:58
nit: i'm not sure why each line in this function i
scottmg
2011/11/16 18:04:35
Done.
| |
| 99 base::subtle::Barrier_AtomicIncrement(&hwbuf->start_marker, 1); | |
| 100 | |
| 101 data_fetcher_->GetGamepadData(hwbuf->buffer, devices_changed_); | |
| 102 | |
| 103 base::subtle::Barrier_AtomicIncrement(&hwbuf->end_marker, 1); | |
| 104 | |
| 105 devices_changed_ = false; | |
| 106 | |
| 107 // Schedule our next interval of polling. | |
| 108 ScheduleDoPoll(); | |
| 109 } | |
| 110 | |
| 111 void Provider::ScheduleDoPoll() { | |
| 112 DCHECK(MessageLoop::current() == polling_thread_->message_loop()); | |
| 113 | |
| 114 MessageLoop::current()->PostDelayedTask(FROM_HERE, | |
|
jam
2011/11/16 00:24:58
nit: ditto for tabbing
scottmg
2011/11/16 18:04:35
Done.
| |
| 115 base::Bind(&Provider::DoPoll, weak_factory_.GetWeakPtr()), | |
| 116 kDesiredSamplingIntervalMs); | |
| 117 } | |
| 118 | |
| 119 GamepadHardwareBuffer* Provider::SharedMemoryAsHardwareBuffer() { | |
| 120 void* mem = gamepad_shared_memory_.memory(); | |
| 121 DCHECK(mem); | |
| 122 return static_cast<GamepadHardwareBuffer*>(mem); | |
| 123 } | |
| 124 | |
| 125 Provider* Provider::instance_ = NULL; | |
|
jam
2011/11/16 00:24:58
nit: statics get listed at the top of the file bef
scottmg
2011/11/16 18:04:35
Done.
| |
| 126 | |
| 127 } // namespace gamepad | |
| OLD | NEW |