| 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 |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "ActiveDOMObject.h" |
| 34 | #include "EventTarget.h" |
| 35 | #include "ExceptionOr.h" |
| 36 | #include "Timer.h" |
| 37 | #include <wtf/URL.h> |
| 38 | #include "WebSocketChannelClient.h" |
| 39 | #include <wtf/Deque.h> |
| 40 | #include <wtf/HashSet.h> |
| 41 | #include <wtf/Lock.h> |
| 42 | |
| 43 | namespace JSC { |
| 44 | class ArrayBuffer; |
| 45 | class ArrayBufferView; |
| 46 | } |
| 47 | |
| 48 | namespace WebCore { |
| 49 | |
| 50 | class Blob; |
| 51 | class ThreadableWebSocketChannel; |
| 52 | |
| 53 | class WebSocket final : public RefCounted<WebSocket>, public EventTargetWithInlineData, public ActiveDOMObject, private WebSocketChannelClient { |
| 54 | WTF_MAKE_ISO_ALLOCATED(WebSocket); |
| 55 | public: |
| 56 | static const char* subprotocolSeparator(); |
| 57 | |
| 58 | static ExceptionOr<Ref<WebSocket>> create(ScriptExecutionContext&, const String& url); |
| 59 | static ExceptionOr<Ref<WebSocket>> create(ScriptExecutionContext&, const String& url, const String& protocol); |
| 60 | static ExceptionOr<Ref<WebSocket>> create(ScriptExecutionContext&, const String& url, const Vector<String>& protocols); |
| 61 | virtual ~WebSocket(); |
| 62 | |
| 63 | static HashSet<WebSocket*>& allActiveWebSockets(const LockHolder&); |
| 64 | static Lock& allActiveWebSocketsMutex(); |
| 65 | |
| 66 | enum State { |
| 67 | CONNECTING = 0, |
| 68 | OPEN = 1, |
| 69 | CLOSING = 2, |
| 70 | CLOSED = 3 |
| 71 | }; |
| 72 | |
| 73 | ExceptionOr<void> connect(const String& url); |
| 74 | ExceptionOr<void> connect(const String& url, const String& protocol); |
| 75 | ExceptionOr<void> connect(const String& url, const Vector<String>& protocols); |
| 76 | |
| 77 | ExceptionOr<void> send(const String& message); |
| 78 | ExceptionOr<void> send(JSC::ArrayBuffer&); |
| 79 | ExceptionOr<void> send(JSC::ArrayBufferView&); |
| 80 | ExceptionOr<void> send(Blob&); |
| 81 | |
| 82 | ExceptionOr<void> close(Optional<unsigned short> code, const String& reason); |
| 83 | |
| 84 | RefPtr<ThreadableWebSocketChannel> channel() const; |
| 85 | |
| 86 | const URL& url() const; |
| 87 | State readyState() const; |
| 88 | unsigned bufferedAmount() const; |
| 89 | |
| 90 | String protocol() const; |
| 91 | String extensions() const; |
| 92 | |
| 93 | String binaryType() const; |
| 94 | ExceptionOr<void> setBinaryType(const String&); |
| 95 | |
| 96 | ScriptExecutionContext* scriptExecutionContext() const final; |
| 97 | |
| 98 | using RefCounted::ref; |
| 99 | using RefCounted::deref; |
| 100 | |
| 101 | private: |
| 102 | explicit WebSocket(ScriptExecutionContext&); |
| 103 | |
| 104 | void resumeTimerFired(); |
| 105 | void dispatchOrQueueErrorEvent(); |
| 106 | void dispatchOrQueueEvent(Ref<Event>&&); |
| 107 | |
| 108 | void contextDestroyed() final; |
| 109 | bool canSuspendForDocumentSuspension() const final; |
| 110 | void suspend(ReasonForSuspension) final; |
| 111 | void resume() final; |
| 112 | void stop() final; |
| 113 | const char* activeDOMObjectName() const final; |
| 114 | |
| 115 | EventTargetInterface eventTargetInterface() const final; |
| 116 | |
| 117 | void refEventTarget() final { ref(); } |
| 118 | void derefEventTarget() final { deref(); } |
| 119 | |
| 120 | void didConnect() final; |
| 121 | void didReceiveMessage(const String& message) final; |
| 122 | void didReceiveBinaryData(Vector<uint8_t>&&) final; |
| 123 | void didReceiveMessageError() final; |
| 124 | void didUpdateBufferedAmount(unsigned bufferedAmount) final; |
| 125 | void didStartClosingHandshake() final; |
| 126 | void didClose(unsigned unhandledBufferedAmount, ClosingHandshakeCompletionStatus, unsigned short code, const String& reason) final; |
| 127 | void didUpgradeURL() final; |
| 128 | |
| 129 | size_t getFramingOverhead(size_t payloadSize); |
| 130 | |
| 131 | enum class BinaryType { Blob, ArrayBuffer }; |
| 132 | |
| 133 | RefPtr<ThreadableWebSocketChannel> m_channel; |
| 134 | |
| 135 | State m_state { CONNECTING }; |
| 136 | URL m_url; |
| 137 | unsigned m_bufferedAmount { 0 }; |
| 138 | unsigned m_bufferedAmountAfterClose { 0 }; |
| 139 | BinaryType m_binaryType { BinaryType::Blob }; |
| 140 | String m_subprotocol; |
| 141 | String m_extensions; |
| 142 | |
| 143 | Timer m_resumeTimer; |
| 144 | bool m_shouldDelayEventFiring { false }; |
| 145 | Deque<Ref<Event>> m_pendingEvents; |
| 146 | bool m_dispatchedErrorEvent { false }; |
| 147 | RefPtr<PendingActivity<WebSocket>> m_pendingActivity; |
| 148 | }; |
| 149 | |
| 150 | } // namespace WebCore |
| 151 | |