Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "modules/gamepad/GamepadSharedMemoryReader.h" | |
| 6 | |
| 7 //#include "base/trace_event/trace_event.h" //heke: replace to Webkit traces. | |
| 8 #include "platform/Histogram.h" | |
| 9 #include "public/platform/InterfaceProvider.h" | |
| 10 #include "wtf/StdLibExtras.h" | |
| 11 | |
| 12 namespace blink { | |
| 13 | |
| 14 GamepadSharedMemoryReader::GamepadSharedMemoryReader( | |
| 15 InterfaceProvider* interfaceProvider) | |
| 16 : m_gamepadHardwareBuffer(nullptr), | |
| 17 m_everInteracted(false), | |
| 18 m_isObserving(false), | |
| 19 m_binding(this), | |
| 20 m_listener(nullptr) { | |
| 21 if (interfaceProvider) { | |
| 22 interfaceProvider->getInterface(mojo::GetProxy(&m_gamepadMonitor)); | |
| 23 m_gamepadMonitor->SetObserver(m_binding.CreateInterfacePtrAndBind()); | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { | |
| 28 if (m_isObserving) { | |
| 29 stop(); | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 void GamepadSharedMemoryReader::sendStartMessage() { | |
| 34 if (m_gamepadMonitor) { | |
| 35 m_gamepadMonitor->GamepadStartPolling(&m_sharedBufferHandle); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 void GamepadSharedMemoryReader::sendStopMessage() { | |
| 40 if (m_gamepadMonitor) { | |
| 41 m_gamepadMonitor->GamepadStopPolling(); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 void GamepadSharedMemoryReader::start(WebGamepadListener* listener) { | |
| 46 DCHECK(!m_isObserving); | |
| 47 m_listener = listener; | |
| 48 m_isObserving = true; | |
| 49 | |
| 50 sendStartMessage(); | |
| 51 | |
| 52 // If we don't get a valid handle from the browser, don't try to Map (we're | |
| 53 // probably out of memory or file handles). | |
| 54 bool isValid = m_sharedBufferHandle.is_valid(); | |
| 55 | |
| 56 DEFINE_STATIC_LOCAL(BooleanHistogram, validHandleHistogram, | |
| 57 ("Gamepad.ValidSharedMemoryHandle")); | |
| 58 validHandleHistogram.count(isValid); | |
| 59 | |
| 60 if (!isValid) | |
| 61 return; | |
| 62 | |
| 63 m_sharedBufferMapping = | |
| 64 m_sharedBufferHandle->Map(sizeof(GamepadHardwareBuffer)); | |
| 65 CHECK(m_sharedBufferMapping); | |
| 66 void* memory = m_sharedBufferMapping.get(); | |
| 67 CHECK(memory); | |
| 68 m_gamepadHardwareBuffer = static_cast<GamepadHardwareBuffer*>(memory); | |
| 69 } | |
| 70 | |
| 71 void GamepadSharedMemoryReader::stop() { | |
| 72 DCHECK(m_isObserving); | |
| 73 m_listener = nullptr; | |
| 74 m_isObserving = false; | |
| 75 | |
| 76 sendStopMessage(); | |
| 77 } | |
| 78 | |
| 79 void GamepadSharedMemoryReader::sampleGamepads(WebGamepads& gamepads) { | |
| 80 // Blink should have started observing at that point. | |
| 81 CHECK(m_isObserving); | |
| 82 | |
| 83 // ========== | |
| 84 // DANGER | |
| 85 // ========== | |
| 86 // | |
| 87 // This logic is duplicated in Pepper as well. If you change it, that also | |
| 88 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc. | |
| 89 WebGamepads readInto; | |
| 90 // TRACE_EVENT0("GAMEPAD", "SampleGamepads"); //heke: Webkit trace. | |
|
ke.he
2016/12/15 14:35:56
TODO(), does all the Content level trace need be t
| |
| 91 | |
| 92 if (!m_sharedBufferHandle.is_valid()) | |
| 93 return; | |
| 94 | |
| 95 // Only try to read this many times before failing to avoid waiting here | |
| 96 // very long in case of contention with the writer. TODO(scottmg) Tune this | |
| 97 // number (as low as 1?) if histogram shows distribution as mostly | |
| 98 // 0-and-maximum. | |
| 99 const int kMaximumContentionCount = 10; | |
| 100 int contentionCount = -1; | |
| 101 | |
| 102 // heke: just to pass the presubmit. | |
| 103 //base::subtle::Atomic32 version; // heke: using base namespace here,??? | |
|
ke.he
2016/12/15 14:11:01
messy here, just to pass the presubmit. but I do n
| |
| 104 //do { | |
| 105 // version = m_gamepadHardwareBuffer->seqlock.ReadBegin(); | |
| 106 memcpy(&readInto, &m_gamepadHardwareBuffer->data, sizeof(readInto)); | |
| 107 ++contentionCount; | |
| 108 // if (contentionCount == kMaximumContentionCount) | |
| 109 // break; | |
| 110 //} while (m_gamepadHardwareBuffer->seqlock.ReadRetry(version)); | |
| 111 | |
| 112 DEFINE_STATIC_LOCAL(CustomCountHistogram, contentionCountHistogram, | |
| 113 ("Gamepad.ReadContentionCount", 1, 1000000, 50)); | |
| 114 contentionCountHistogram.count(contentionCount); | |
| 115 | |
| 116 if (contentionCount >= kMaximumContentionCount) { | |
| 117 // We failed to successfully read, presumably because the hardware | |
| 118 // thread was taking unusually long. Don't copy the data to the output | |
| 119 // buffer, and simply leave what was there before. | |
| 120 return; | |
| 121 } | |
| 122 | |
| 123 // New data was read successfully, copy it into the output buffer. | |
| 124 memcpy(&gamepads, &readInto, sizeof(gamepads)); | |
| 125 | |
| 126 if (!m_everInteracted) { | |
| 127 // Clear the connected flag if the user hasn't interacted with any of the | |
| 128 // gamepads to prevent fingerprinting. The actual data is not cleared. | |
| 129 // WebKit will only copy out data into the JS buffers for connected | |
| 130 // gamepads so this is sufficient. | |
| 131 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; i++) | |
| 132 gamepads.items[i].connected = false; | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 void GamepadSharedMemoryReader::GamepadConnected(int index, | |
| 137 const WebGamepad& gamepad) { | |
| 138 // The browser already checks if the user actually interacted with a device. | |
| 139 m_everInteracted = true; | |
| 140 | |
| 141 if (m_listener) | |
| 142 m_listener->didConnectGamepad(index, gamepad); | |
| 143 } | |
| 144 | |
| 145 void GamepadSharedMemoryReader::GamepadDisconnected(int index, | |
| 146 const WebGamepad& gamepad) { | |
| 147 if (m_listener) | |
| 148 m_listener->didDisconnectGamepad(index, gamepad); | |
| 149 } | |
| 150 | |
| 151 } // namespace blink | |
| OLD | NEW |