Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2011, 2012 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 WebSocket_h
     32 #define WebSocket_h
     33 
     34 #include "../platform/WebCommon.h"
     35 #include "../platform/WebPrivatePtr.h"
     36 #include "../platform/WebString.h"
     37 
     38 namespace blink {
     39 
     40 class WebArrayBuffer;
     41 class WebDocument;
     42 class WebURL;
     43 class WebSocketClient;
     44 
     45 class WebSocket {
     46 public:
     47     enum CloseEventCode {
     48         CloseEventCodeNotSpecified = -1,
     49         CloseEventCodeNormalClosure = 1000,
     50         CloseEventCodeGoingAway = 1001,
     51         CloseEventCodeProtocolError = 1002,
     52         CloseEventCodeUnsupportedData = 1003,
     53         CloseEventCodeFrameTooLarge = 1004,
     54         CloseEventCodeNoStatusRcvd = 1005,
     55         CloseEventCodeAbnormalClosure = 1006,
     56         CloseEventCodeInvalidFramePayloadData = 1007,
     57         CloseEventCodePolicyViolation = 1008,
     58         CloseEventCodeMessageTooBig = 1009,
     59         CloseEventCodeMandatoryExt = 1010,
     60         CloseEventCodeInternalError = 1011,
     61         CloseEventCodeTLSHandshake = 1015,
     62         CloseEventCodeMinimumUserDefined = 3000,
     63         CloseEventCodeMaximumUserDefined = 4999
     64     };
     65 
     66     enum BinaryType {
     67         BinaryTypeBlob = 0,
     68         BinaryTypeArrayBuffer = 1
     69     };
     70 
     71     BLINK_EXPORT static WebSocket* create(const WebDocument&, WebSocketClient*);
     72     virtual ~WebSocket() { }
     73 
     74     // These functions come from binaryType attribute of the WebSocket API
     75     // specification. It specifies binary object type for receiving binary
     76     // frames representation. Receiving text frames are always mapped to
     77     // WebString type regardless of this attribute.
     78     // Default type is BinaryTypeBlob. But currently it is not supported.
     79     // Set BinaryTypeArrayBuffer here ahead of using binary communication.
     80     // See also, The WebSocket API - http://www.w3.org/TR/websockets/ .
     81     virtual BinaryType binaryType() const = 0;
     82     virtual bool setBinaryType(BinaryType) = 0;
     83 
     84     virtual void connect(const WebURL&, const WebString& protocol) = 0;
     85     virtual WebString subprotocol() { return WebString(); }
     86     virtual WebString extensions() { return WebString(); }
     87     virtual bool sendText(const WebString&) = 0;
     88     virtual bool sendArrayBuffer(const WebArrayBuffer&) = 0;
     89     virtual unsigned long bufferedAmount() const { return 0; }
     90     virtual void close(int code, const WebString& reason) = 0;
     91     virtual void fail(const WebString& reason) = 0;
     92     virtual void disconnect() = 0;
     93 };
     94 
     95 } // namespace blink
     96 
     97 #endif // WebSocket_h
     98