2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								/*
  
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 *  Copyright  ( c )  2021 - 2022 ,  Dex ♪  < dexes . ttp @ gmail . com > 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								 * 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  SPDX - License - Identifier :  BSD - 2 - Clause 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 */ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-11 03:43:03 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  <LibJS/Runtime/ArrayBuffer.h> 
  
						 
					
						
							
								
									
										
										
										
											2021-06-27 21:48:34 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  <LibJS/Runtime/FunctionObject.h> 
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								# include  <LibWeb/DOM/Document.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <LibWeb/DOM/Event.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <LibWeb/DOM/EventDispatcher.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-02-16 20:43:24 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  <LibWeb/DOM/IDLEventListener.h> 
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								# include  <LibWeb/HTML/CloseEvent.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <LibWeb/HTML/EventHandler.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <LibWeb/HTML/EventNames.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <LibWeb/HTML/MessageEvent.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-07-12 20:37:43 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  <LibWeb/HTML/Origin.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-03-07 23:08:26 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  <LibWeb/HTML/Window.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-09-25 17:28:46 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  <LibWeb/WebIDL/DOMException.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-09-25 17:03:42 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  <LibWeb/WebIDL/ExceptionOr.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  <LibWeb/WebSockets/WebSocket.h> 
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								namespace  Web : : WebSockets  {  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								static  RefPtr < WebSocketClientManager >  s_websocket_client_manager ;  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								void  WebSocketClientManager : : initialize ( RefPtr < WebSocketClientManager >  websocket_client_manager )  
						 
					
						
							
								
									
										
										
										
											2022-01-14 13:12:49 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    s_websocket_client_manager  =  websocket_client_manager ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-14 13:12:49 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								WebSocketClientManager &  WebSocketClientManager : : the ( )  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if  ( ! s_websocket_client_manager )  [[unlikely]]  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        dbgln ( " Web::WebSockets::WebSocketClientManager was not initialized! " ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        VERIFY_NOT_REACHED ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return  * s_websocket_client_manager ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								WebSocketClientSocket : : WebSocketClientSocket ( )  =  default ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								WebSocketClientSocket : : ~ WebSocketClientSocket ( )  =  default ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								WebSocketClientManager : : WebSocketClientManager ( )  =  default ;  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 18:14:22 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// https://websockets.spec.whatwg.org/#dom-websocket-websocket
  
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								WebIDL : : ExceptionOr < JS : : NonnullGCPtr < WebSocket > >  WebSocket : : construct_impl ( JS : : Realm &  realm ,  DeprecatedString  const &  url )  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    auto &  window  =  verify_cast < HTML : : Window > ( realm . global_object ( ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-13 00:33:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    AK : : URL  url_record ( url ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    if  ( ! url_record . is_valid ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        return  WebIDL : : SyntaxError : : create ( realm ,  " Invalid URL " ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-29 01:30:58 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if  ( ! url_record . scheme ( ) . is_one_of ( " ws " ,  " wss " ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        return  WebIDL : : SyntaxError : : create ( realm ,  " Invalid protocol " ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    if  ( ! url_record . fragment ( ) . is_empty ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        return  WebIDL : : SyntaxError : : create ( realm ,  " Presence of URL fragment is invalid " ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // 5. If `protocols` is a string, set `protocols` to a sequence consisting of just that string
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 6. If any of the values in `protocols` occur more than once or otherwise fail to match the requirements, throw SyntaxError
 
							 
						 
					
						
							
								
									
										
										
										
											2023-01-28 13:39:44 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    return  MUST_OR_THROW_OOM ( realm . heap ( ) . allocate < WebSocket > ( realm ,  window ,  url_record ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-03-07 23:08:26 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								WebSocket : : WebSocket ( HTML : : Window &  window ,  AK : : URL &  url )  
						 
					
						
							
								
									
										
										
										
											2022-08-28 13:42:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    :  EventTarget ( window . realm ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    ,  m_window ( window ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // FIXME: Integrate properly with FETCH as per https://fetch.spec.whatwg.org/#websocket-opening-handshake
 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-17 11:19:19 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    auto  origin_string  =  m_window - > associated_document ( ) . origin ( ) . serialize ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    m_websocket  =  WebSocketClientManager : : the ( ) . connect ( url ,  origin_string ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-28 13:42:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    m_websocket - > on_open  =  [ weak_this  =  make_weak_ptr < WebSocket > ( ) ]  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        if  ( ! weak_this ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        auto &  websocket  =  const_cast < WebSocket & > ( * weak_this ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        websocket . on_open ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-28 13:42:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    m_websocket - > on_message  =  [ weak_this  =  make_weak_ptr < WebSocket > ( ) ] ( auto  message )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        if  ( ! weak_this ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        auto &  websocket  =  const_cast < WebSocket & > ( * weak_this ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        websocket . on_message ( move ( message . data ) ,  message . is_text ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-28 13:42:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    m_websocket - > on_close  =  [ weak_this  =  make_weak_ptr < WebSocket > ( ) ] ( auto  code ,  auto  reason ,  bool  was_clean )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        if  ( ! weak_this ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        auto &  websocket  =  const_cast < WebSocket & > ( * weak_this ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        websocket . on_close ( code ,  reason ,  was_clean ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-28 13:42:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    m_websocket - > on_error  =  [ weak_this  =  make_weak_ptr < WebSocket > ( ) ] ( auto )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        if  ( ! weak_this ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        auto &  websocket  =  const_cast < WebSocket & > ( * weak_this ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        websocket . on_error ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-03-14 13:21:51 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								WebSocket : : ~ WebSocket ( )  =  default ;  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-01-28 12:33:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								JS : : ThrowCompletionOr < void >  WebSocket : : initialize ( JS : : Realm &  realm )  
						 
					
						
							
								
									
										
										
										
											2023-01-10 06:28:20 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2023-01-28 12:33:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    MUST_OR_THROW_OOM ( Base : : initialize ( realm ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-01-10 06:28:20 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    set_prototype ( & Bindings : : ensure_web_prototype < Bindings : : WebSocketPrototype > ( realm ,  " WebSocket " ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-01-28 12:33:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return  { } ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-01-10 06:28:20 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-28 13:42:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								void  WebSocket : : visit_edges ( Cell : : Visitor &  visitor )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Base : : visit_edges ( visitor ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    visitor . visit ( m_window . ptr ( ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 18:14:22 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// https://websockets.spec.whatwg.org/#dom-websocket-readystate
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								WebSocket : : ReadyState  WebSocket : : ready_state ( )  const  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( ! m_websocket ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return  WebSocket : : ReadyState : : Closed ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    return  const_cast < WebSocketClientSocket & > ( * m_websocket ) . ready_state ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 18:14:22 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// https://websockets.spec.whatwg.org/#dom-websocket-extensions
  
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								DeprecatedString  WebSocket : : extensions ( )  const  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( ! m_websocket ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        return  DeprecatedString : : empty ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // https://websockets.spec.whatwg.org/#feedback-from-the-protocol
 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // FIXME: Change the extensions attribute's value to the extensions in use, if it is not the null value.
 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    return  DeprecatedString : : empty ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 18:14:22 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// https://websockets.spec.whatwg.org/#dom-websocket-protocol
  
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								DeprecatedString  WebSocket : : protocol ( )  const  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( ! m_websocket ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        return  DeprecatedString : : empty ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // https://websockets.spec.whatwg.org/#feedback-from-the-protocol
 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // FIXME: Change the protocol attribute's value to the subprotocol in use, if it is not the null value.
 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    return  DeprecatedString : : empty ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 18:14:22 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// https://websockets.spec.whatwg.org/#dom-websocket-close
  
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								WebIDL : : ExceptionOr < void >  WebSocket : : close ( Optional < u16 >  code ,  Optional < DeprecatedString >  reason )  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2022-03-29 22:35:54 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // 1. If code is present, but is neither an integer equal to 1000 nor an integer in the range 3000 to 4999, inclusive, throw an "InvalidAccessError" DOMException.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( code . has_value ( )  & &  * code  ! =  1000  & &  ( * code  <  3000  | |  * code  >  4099 ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        return  WebIDL : : InvalidAccessError : : create ( realm ( ) ,  " The close error code is invalid " ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 22:35:54 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // 2. If reason is present, then run these substeps:
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( reason . has_value ( ) )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // 1. Let reasonBytes be the result of encoding reason.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // 2. If reasonBytes is longer than 123 bytes, then throw a "SyntaxError" DOMException.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if  ( reason - > bytes ( ) . size ( )  >  123 ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            return  WebIDL : : SyntaxError : : create ( realm ( ) ,  " The close reason is longer than 123 bytes " ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 22:35:54 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 3. Run the first matching steps from the following list:
 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    auto  state  =  ready_state ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 22:35:54 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // -> If this's ready state is CLOSING (2) or CLOSED (3)
 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    if  ( state  = =  WebSocket : : ReadyState : : Closing  | |  state  = =  WebSocket : : ReadyState : : Closed ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return  { } ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 22:35:54 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // -> If the WebSocket connection is not yet established [WSP]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // -> If the WebSocket closing handshake has not yet been started [WSP]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // -> Otherwise
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // NOTE: All of these are handled by the WebSocket Protocol when calling close()
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // FIXME: LibProtocol does not yet support sending empty Close messages, so we use default values for now
 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    m_websocket - > close ( code . value_or ( 1000 ) ,  reason . value_or ( DeprecatedString : : empty ( ) ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    return  { } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 18:14:22 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// https://websockets.spec.whatwg.org/#dom-websocket-send
  
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								WebIDL : : ExceptionOr < void >  WebSocket : : send ( DeprecatedString  const &  data )  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    auto  state  =  ready_state ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( state  = =  WebSocket : : ReadyState : : Connecting ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        return  WebIDL : : InvalidStateError : : create ( realm ( ) ,  " Websocket is still CONNECTING " ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    if  ( state  = =  WebSocket : : ReadyState : : Open )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        m_websocket - > send ( data ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // TODO : If the data cannot be sent, e.g. because it would need to be buffered but the buffer is full, the user agent must flag the WebSocket as full and then close the WebSocket connection.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // TODO : Any invocation of this method with a string argument that does not throw an exception must increase the bufferedAmount attribute by the number of bytes needed to express the argument as UTF-8.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return  { } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								void  WebSocket : : on_open ( )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 1. Change the readyState attribute's value to OPEN (1).
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 2. Change the extensions attribute's value to the extensions in use, if it is not the null value. [WSP]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 3. Change the protocol attribute's value to the subprotocol in use, if it is not the null value. [WSP]
 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    dispatch_event ( * DOM : : Event : : create ( realm ( ) ,  HTML : : EventNames : : open ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								void  WebSocket : : on_error ( )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    dispatch_event ( * DOM : : Event : : create ( realm ( ) ,  HTML : : EventNames : : error ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
  
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								void  WebSocket : : on_close ( u16  code ,  DeprecatedString  reason ,  bool  was_clean )  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 1. Change the readyState attribute's value to CLOSED. This is handled by the Protocol's WebSocket
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 2. If [needed], fire an event named error at the WebSocket object. This is handled by the Protocol's WebSocket
 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    HTML : : CloseEventInit  event_init  { } ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-01 18:16:12 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    event_init . was_clean  =  was_clean ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    event_init . code  =  code ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    event_init . reason  =  move ( reason ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    dispatch_event ( * HTML : : CloseEvent : : create ( realm ( ) ,  HTML : : EventNames : : close ,  event_init ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								void  WebSocket : : on_message ( ByteBuffer  message ,  bool  is_text )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if  ( m_websocket - > ready_state ( )  ! =  WebSocket : : ReadyState : : Open ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( is_text )  { 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        auto  text_message  =  DeprecatedString ( ReadonlyBytes ( message ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        HTML : : MessageEventInit  event_init ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-06 22:17:27 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        event_init . data  =  JS : : PrimitiveString : : create ( vm ( ) ,  text_message ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-01 18:39:03 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        event_init . origin  =  url ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        dispatch_event ( * HTML : : MessageEvent : : create ( realm ( ) ,  HTML : : EventNames : : message ,  event_init ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-11 03:43:03 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( m_binary_type  = =  " blob " )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // type indicates that the data is Binary and binaryType is "blob"
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        TODO ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }  else  if  ( m_binary_type  = =  " arraybuffer " )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // type indicates that the data is Binary and binaryType is "arraybuffer"
 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        HTML : : MessageEventInit  event_init ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-28 13:42:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        event_init . data  =  JS : : ArrayBuffer : : create ( realm ( ) ,  message ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-11 03:43:03 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        event_init . origin  =  url ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        dispatch_event ( * HTML : : MessageEvent : : create ( realm ( ) ,  HTML : : EventNames : : message ,  event_init ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-11 03:43:03 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    dbgln ( " Unsupported WebSocket message type {} " ,  m_binary_type ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    TODO ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# undef __ENUMERATE 
  
						 
					
						
							
								
									
										
										
										
											2022-09-24 16:02:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# define __ENUMERATE(attribute_name, event_name)                       \ 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    void  WebSocket : : set_ # # attribute_name ( WebIDL : : CallbackType *  value )  \
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    {                                                                  \
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        set_event_handler_attribute ( event_name ,  value ) ;                \
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }                                                                  \
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    WebIDL : : CallbackType *  WebSocket : : attribute_name ( )                  \
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    {                                                                  \
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return  event_handler_attribute ( event_name ) ;                    \
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								ENUMERATE_WEBSOCKET_EVENT_HANDLERS ( __ENUMERATE )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# undef __ENUMERATE 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}