Home | History | Annotate | Download | only in websockets
      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/page/ConsoleTypes.h"
     35 #include "wtf/Forward.h"
     36 #include "wtf/Noncopyable.h"
     37 #include "wtf/PassRefPtr.h"
     38 #include "wtf/text/WTFString.h"
     39 
     40 namespace WebCore {
     41 
     42 class Blob;
     43 class KURL;
     44 class ScriptExecutionContext;
     45 class WebSocketChannelClient;
     46 
     47 class WebSocketChannel {
     48     WTF_MAKE_NONCOPYABLE(WebSocketChannel);
     49 public:
     50     WebSocketChannel() { }
     51     static PassRefPtr<WebSocketChannel> create(ScriptExecutionContext*, WebSocketChannelClient*);
     52 
     53     enum SendResult {
     54         SendSuccess,
     55         SendFail,
     56         InvalidMessage
     57     };
     58 
     59     enum CloseEventCode {
     60         CloseEventCodeNotSpecified = -1,
     61         CloseEventCodeNormalClosure = 1000,
     62         CloseEventCodeGoingAway = 1001,
     63         CloseEventCodeProtocolError = 1002,
     64         CloseEventCodeUnsupportedData = 1003,
     65         CloseEventCodeFrameTooLarge = 1004,
     66         CloseEventCodeNoStatusRcvd = 1005,
     67         CloseEventCodeAbnormalClosure = 1006,
     68         CloseEventCodeInvalidFramePayloadData = 1007,
     69         CloseEventCodePolicyViolation = 1008,
     70         CloseEventCodeMessageTooBig = 1009,
     71         CloseEventCodeMandatoryExt = 1010,
     72         CloseEventCodeInternalError = 1011,
     73         CloseEventCodeTLSHandshake = 1015,
     74         CloseEventCodeMinimumUserDefined = 3000,
     75         CloseEventCodeMaximumUserDefined = 4999
     76     };
     77 
     78     virtual void connect(const KURL&, const String& protocol) = 0;
     79     virtual String subprotocol() = 0; // Will be available after didConnect() callback is invoked.
     80     virtual String extensions() = 0; // Will be available after didConnect() callback is invoked.
     81     virtual SendResult send(const String& message) = 0;
     82     virtual SendResult send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength) = 0;
     83     virtual SendResult send(const Blob&) = 0;
     84     virtual unsigned long bufferedAmount() const = 0;
     85     virtual void close(int code, const String& reason) = 0;
     86 
     87     // Log the reason text and close the connection. Will call didClose().
     88     // The MessageLevel parameter will be used for the level of the message
     89     // shown at the devtool console.
     90     // sourceURL and lineNumber parameters may be shown with the reason text
     91     // at the devtool console.
     92     // Even if sourceURL and lineNumber are specified, they may be ignored
     93     // and the "current" url and the line number in the sense of
     94     // JavaScript execution may be shown if this method is called in
     95     // a JS execution context.
     96     // You can specify String() and 0 for sourceURL and lineNumber
     97     // respectively, if you can't / needn't provide the information.
     98     virtual void fail(const String& reason, MessageLevel, const String& sourceURL, unsigned lineNumber) = 0;
     99     void fail(const String& reason, MessageLevel level) { fail(reason, level, String(), 0); }
    100 
    101     virtual void disconnect() = 0; // Will suppress didClose().
    102 
    103     virtual void suspend() = 0;
    104     virtual void resume() = 0;
    105 
    106     void ref() { refWebSocketChannel(); }
    107     void deref() { derefWebSocketChannel(); }
    108 
    109 protected:
    110     virtual ~WebSocketChannel() { }
    111     virtual void refWebSocketChannel() = 0;
    112     virtual void derefWebSocketChannel() = 0;
    113 };
    114 
    115 } // namespace WebCore
    116 
    117 #endif // WebSocketChannel_h
    118