1 /* 2 * Copyright (C) 2009 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 #ifndef WebSocketChannel_h 32 #define WebSocketChannel_h 33 34 #include "core/frame/ConsoleTypes.h" 35 #include "platform/heap/Handle.h" 36 #include "wtf/Forward.h" 37 #include "wtf/Noncopyable.h" 38 #include "wtf/PassRefPtr.h" 39 #include "wtf/RefCounted.h" 40 #include "wtf/text/WTFString.h" 41 42 namespace WebCore { 43 44 class BlobDataHandle; 45 class KURL; 46 class ExecutionContext; 47 class WebSocketChannelClient; 48 49 // FIXME: WebSocketChannel needs to be RefCountedGarbageCollected to support manual ref/deref 50 // in MainThreadWebSocketChannelImpl. We should change it to GarbageCollectedFinalized once 51 // we remove MainThreadWebSocketChannelImpl. 52 class WebSocketChannel : public RefCountedWillBeRefCountedGarbageCollected<WebSocketChannel> { 53 WTF_MAKE_NONCOPYABLE(WebSocketChannel); 54 public: 55 WebSocketChannel() { } 56 static PassRefPtrWillBeRawPtr<WebSocketChannel> create(ExecutionContext*, WebSocketChannelClient*); 57 58 enum SendResult { 59 SendSuccess, 60 SendFail, 61 InvalidMessage 62 }; 63 64 enum CloseEventCode { 65 CloseEventCodeNotSpecified = -1, 66 CloseEventCodeNormalClosure = 1000, 67 CloseEventCodeGoingAway = 1001, 68 CloseEventCodeProtocolError = 1002, 69 CloseEventCodeUnsupportedData = 1003, 70 CloseEventCodeFrameTooLarge = 1004, 71 CloseEventCodeNoStatusRcvd = 1005, 72 CloseEventCodeAbnormalClosure = 1006, 73 CloseEventCodeInvalidFramePayloadData = 1007, 74 CloseEventCodePolicyViolation = 1008, 75 CloseEventCodeMessageTooBig = 1009, 76 CloseEventCodeMandatoryExt = 1010, 77 CloseEventCodeInternalError = 1011, 78 CloseEventCodeTLSHandshake = 1015, 79 CloseEventCodeMinimumUserDefined = 3000, 80 CloseEventCodeMaximumUserDefined = 4999 81 }; 82 83 virtual bool connect(const KURL&, const String& protocol) = 0; 84 virtual SendResult send(const String& message) = 0; 85 virtual SendResult send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength) = 0; 86 virtual SendResult send(PassRefPtr<BlobDataHandle>) = 0; 87 88 // For WorkerThreadableWebSocketChannel. 89 virtual SendResult send(PassOwnPtr<Vector<char> >) = 0; 90 91 virtual void close(int code, const String& reason) = 0; 92 93 // Log the reason text and close the connection. Will call didClose(). 94 // The MessageLevel parameter will be used for the level of the message 95 // shown at the devtool console. 96 // sourceURL and lineNumber parameters may be shown with the reason text 97 // at the devtool console. 98 // Even if sourceURL and lineNumber are specified, they may be ignored 99 // and the "current" url and the line number in the sense of 100 // JavaScript execution may be shown if this method is called in 101 // a JS execution context. 102 // You can specify String() and 0 for sourceURL and lineNumber 103 // respectively, if you can't / needn't provide the information. 104 virtual void fail(const String& reason, MessageLevel, const String& sourceURL, unsigned lineNumber) = 0; 105 106 virtual void disconnect() = 0; // Will suppress didClose(). 107 108 virtual void suspend() = 0; 109 virtual void resume() = 0; 110 111 virtual ~WebSocketChannel() { } 112 113 virtual void trace(Visitor*) { } 114 }; 115 116 } // namespace WebCore 117 118 #endif // WebSocketChannel_h 119