1 /* 2 * Copyright (C) 2011, Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 23 * DAMAGE. 24 */ 25 26 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_GAMEPAD_NAVIGATOR_GAMEPAD_H_ 27 #define THIRD_PARTY_BLINK_RENDERER_MODULES_GAMEPAD_NAVIGATOR_GAMEPAD_H_ 28 29 #include "third_party/blink/renderer/core/dom/dom_high_res_time_stamp.h" 30 #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h" 31 #include "third_party/blink/renderer/core/frame/local_dom_window.h" 32 #include "third_party/blink/renderer/core/frame/navigator.h" 33 #include "third_party/blink/renderer/core/frame/platform_event_controller.h" 34 #include "third_party/blink/renderer/modules/gamepad/gamepad.h" 35 #include "third_party/blink/renderer/modules/modules_export.h" 36 #include "third_party/blink/renderer/platform/heap/member.h" 37 #include "third_party/blink/renderer/platform/supplementable.h" 38 #include "third_party/blink/renderer/platform/wtf/forward.h" 39 40 namespace device { 41 class Gamepad; 42 } 43 44 namespace blink { 45 46 class Document; 47 class GamepadDispatcher; 48 class GamepadHapticActuator; 49 class GamepadList; 50 class Navigator; 51 52 class MODULES_EXPORT NavigatorGamepad final 53 : public GarbageCollected<NavigatorGamepad>, 54 public Supplement<Navigator>, 55 public DOMWindowClient, 56 public PlatformEventController, 57 public LocalDOMWindow::EventListenerObserver, 58 public Gamepad::Client { 59 USING_GARBAGE_COLLECTED_MIXIN(NavigatorGamepad); 60 61 public: 62 static const char kSupplementName[]; 63 64 static NavigatorGamepad* From(Document&); 65 static NavigatorGamepad& From(Navigator&); 66 67 explicit NavigatorGamepad(Navigator&); 68 ~NavigatorGamepad() override; 69 70 static GamepadList* getGamepads(Navigator&); 71 GamepadList* Gamepads(); 72 73 void Trace(Visitor*) override; 74 75 private: 76 void SampleGamepads(); 77 78 void DidRemoveGamepadEventListeners(); 79 bool StartUpdatingIfAttached(); 80 void SampleAndCompareGamepadState(); 81 void DispatchGamepadEvent(const AtomicString&, Gamepad*); 82 83 // PageVisibilityObserver 84 void PageVisibilityChanged() override; 85 86 // PlatformEventController 87 void RegisterWithDispatcher() override; 88 void UnregisterWithDispatcher() override; 89 bool HasLastData() override; 90 void DidUpdateData() override; 91 92 // LocalDOMWindow::EventListenerObserver 93 void DidAddEventListener(LocalDOMWindow*, const AtomicString&) override; 94 void DidRemoveEventListener(LocalDOMWindow*, const AtomicString&) override; 95 void DidRemoveAllEventListeners(LocalDOMWindow*) override; 96 97 // Gamepad::Client 98 GamepadHapticActuator* GetVibrationActuatorForGamepad( 99 const Gamepad&) override; 100 101 // A reference to the buffer containing the last-received gamepad state. May 102 // be nullptr if no data has been received yet. Do not overwrite this buffer 103 // as it may have already been returned to the page. Instead, write to 104 // |gamepads_back_| and swap buffers. 105 Member<GamepadList> gamepads_; 106 107 // True if the buffer referenced by |gamepads_| has been exposed to the page. 108 // When the buffer is not exposed, prefer to reuse it. 109 bool is_gamepads_exposed_ = false; 110 111 // A reference to the buffer for receiving new gamepad state. May be 112 // overwritten. 113 Member<GamepadList> gamepads_back_; 114 115 HeapVector<Member<GamepadHapticActuator>> vibration_actuators_; 116 117 // The timestamp for the navigationStart attribute. Gamepad timestamps are 118 // reported relative to this value. 119 base::TimeTicks navigation_start_; 120 121 // The timestamp when gamepads were made available to the page. If no data has 122 // been received from the hardware, the gamepad timestamp should be equal to 123 // this value. 124 base::TimeTicks gamepads_start_; 125 126 // True if there is at least one listener for gamepad connection or 127 // disconnection events. 128 bool has_connection_event_listener_ = false; 129 130 // True while processing gamepad events. 131 bool processing_events_ = false; 132 133 Member<GamepadDispatcher> gamepad_dispatcher_; 134 }; 135 136 } // namespace blink 137 138 #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_GAMEPAD_NAVIGATOR_GAMEPAD_H_ 139