WebSocketFrame.h (2795B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_net_WebSocketFrame_h 8 #define mozilla_net_WebSocketFrame_h 9 10 #include <cstdint> 11 #include "nsISupports.h" 12 #include "nsIWebSocketEventService.h" 13 #include "nsString.h" 14 15 class PickleIterator; 16 17 // Avoid including nsDOMNavigationTiming.h here, where the canonical definition 18 // of DOMHighResTimeStamp resides. 19 using DOMHighResTimeStamp = double; 20 21 namespace IPC { 22 class Message; 23 class MessageReader; 24 class MessageWriter; 25 template <class P> 26 struct ParamTraits; 27 } // namespace IPC 28 29 namespace mozilla { 30 namespace net { 31 32 class WebSocketFrameData final { 33 public: 34 WebSocketFrameData(); 35 36 explicit WebSocketFrameData(const WebSocketFrameData&) = default; 37 WebSocketFrameData(WebSocketFrameData&&) = default; 38 WebSocketFrameData& operator=(WebSocketFrameData&&) = default; 39 WebSocketFrameData& operator=(const WebSocketFrameData&) = default; 40 41 WebSocketFrameData(DOMHighResTimeStamp aTimeStamp, bool aFinBit, 42 bool aRsvBit1, bool aRsvBit2, bool aRsvBit3, 43 uint8_t aOpCode, bool aMaskBit, uint32_t aMask, 44 const nsCString& aPayload); 45 46 ~WebSocketFrameData() = default; 47 48 // For IPC serialization 49 void WriteIPCParams(IPC::MessageWriter* aWriter) const; 50 bool ReadIPCParams(IPC::MessageReader* aReader); 51 52 DOMHighResTimeStamp mTimeStamp{0}; 53 54 bool mFinBit : 1; 55 bool mRsvBit1 : 1; 56 bool mRsvBit2 : 1; 57 bool mRsvBit3 : 1; 58 bool mMaskBit : 1; 59 uint8_t mOpCode{0}; 60 61 uint32_t mMask{0}; 62 63 nsCString mPayload; 64 }; 65 66 class WebSocketFrame final : public nsIWebSocketFrame { 67 public: 68 NS_DECL_THREADSAFE_ISUPPORTS 69 NS_DECL_NSIWEBSOCKETFRAME 70 71 explicit WebSocketFrame(const WebSocketFrameData& aData); 72 73 WebSocketFrame(bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3, 74 uint8_t aOpCode, bool aMaskBit, uint32_t aMask, 75 const nsCString& aPayload); 76 77 const WebSocketFrameData& Data() const { return mData; } 78 79 private: 80 ~WebSocketFrame() = default; 81 82 WebSocketFrameData mData; 83 }; 84 85 } // namespace net 86 } // namespace mozilla 87 88 namespace IPC { 89 template <> 90 struct ParamTraits<mozilla::net::WebSocketFrameData> { 91 using paramType = mozilla::net::WebSocketFrameData; 92 93 static void Write(MessageWriter* aWriter, const paramType& aParam) { 94 aParam.WriteIPCParams(aWriter); 95 } 96 97 static bool Read(MessageReader* aReader, paramType* aResult) { 98 return aResult->ReadIPCParams(aReader); 99 } 100 }; 101 102 } // namespace IPC 103 104 #endif // mozilla_net_WebSocketFrame_h