Part 2 of removing PlatformString.h, remove PlatformString.h
[WebKit-https.git] / Source / WebCore / Modules / websockets / ThreadableWebSocketChannelClientWrapper.h
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 #ifndef ThreadableWebSocketChannelClientWrapper_h
32 #define ThreadableWebSocketChannelClientWrapper_h
33
34 #if ENABLE(WEB_SOCKETS) && ENABLE(WORKERS)
35
36 #include "ScriptExecutionContext.h"
37 #include "ThreadableWebSocketChannel.h"
38 #include "WebSocketChannelClient.h"
39 #include "WorkerThreadableWebSocketChannel.h"
40 #include <wtf/Forward.h>
41 #include <wtf/OwnPtr.h>
42 #include <wtf/PassOwnPtr.h>
43 #include <wtf/Threading.h>
44 #include <wtf/Vector.h>
45 #include <wtf/text/WTFString.h>
46
47 namespace WebCore {
48
49 class ScriptExecutionContext;
50 class WebSocketChannelClient;
51
52 class ThreadableWebSocketChannelClientWrapper : public ThreadSafeRefCounted<ThreadableWebSocketChannelClientWrapper> {
53 public:
54     static PassRefPtr<ThreadableWebSocketChannelClientWrapper> create(ScriptExecutionContext*, WebSocketChannelClient*);
55
56     void clearSyncMethodDone();
57     void setSyncMethodDone();
58     bool syncMethodDone() const;
59
60     WorkerThreadableWebSocketChannel::Peer* peer() const;
61     void didCreateWebSocketChannel(WorkerThreadableWebSocketChannel::Peer*);
62     void clearPeer();
63
64     bool failedWebSocketChannelCreation() const;
65     void setFailedWebSocketChannelCreation();
66
67     // Subprotocol and extensions will be available when didConnect() callback is invoked.
68     String subprotocol() const;
69     void setSubprotocol(const String&);
70     String extensions() const;
71     void setExtensions(const String&);
72
73     ThreadableWebSocketChannel::SendResult sendRequestResult() const;
74     void setSendRequestResult(ThreadableWebSocketChannel::SendResult);
75
76     unsigned long bufferedAmount() const;
77     void setBufferedAmount(unsigned long);
78
79     void clearClient();
80
81     void didConnect();
82     void didReceiveMessage(const String& message);
83     void didReceiveBinaryData(PassOwnPtr<Vector<char> >);
84     void didUpdateBufferedAmount(unsigned long bufferedAmount);
85     void didStartClosingHandshake();
86     void didClose(unsigned long unhandledBufferedAmount, WebSocketChannelClient::ClosingHandshakeCompletionStatus, unsigned short code, const String& reason);
87     void didReceiveMessageError();
88
89     void suspend();
90     void resume();
91
92 private:
93     ThreadableWebSocketChannelClientWrapper(ScriptExecutionContext*, WebSocketChannelClient*);
94
95     void processPendingTasks();
96
97     static void didConnectCallback(ScriptExecutionContext*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>);
98     static void didReceiveMessageCallback(ScriptExecutionContext*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, const String& message);
99     static void didReceiveBinaryDataCallback(ScriptExecutionContext*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, PassOwnPtr<Vector<char> >);
100     static void didUpdateBufferedAmountCallback(ScriptExecutionContext*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, unsigned long bufferedAmount);
101     static void didStartClosingHandshakeCallback(ScriptExecutionContext*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>);
102     static void didCloseCallback(ScriptExecutionContext*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, unsigned long unhandledBufferedAmount, WebSocketChannelClient::ClosingHandshakeCompletionStatus, unsigned short code, const String& reason);
103     static void processPendingTasksCallback(ScriptExecutionContext*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>);
104     static void didReceiveMessageErrorCallback(ScriptExecutionContext*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>);
105
106     ScriptExecutionContext* m_context;
107     WebSocketChannelClient* m_client;
108     WorkerThreadableWebSocketChannel::Peer* m_peer;
109     bool m_failedWebSocketChannelCreation;
110     bool m_syncMethodDone;
111     // ThreadSafeRefCounted must not have String member variables.
112     Vector<UChar> m_subprotocol;
113     Vector<UChar> m_extensions;
114     ThreadableWebSocketChannel::SendResult m_sendRequestResult;
115     unsigned long m_bufferedAmount;
116     bool m_suspended;
117     Vector<OwnPtr<ScriptExecutionContext::Task> > m_pendingTasks;
118 };
119
120 } // namespace WebCore
121
122 #endif // ENABLE(WEB_SOCKETS)
123
124 #endif // ThreadableWebSocketChannelClientWrapper_h