WorkerThreadableWebSocketChannel.h 8.23 KB
Newer Older
1
/*
2
 * Copyright (C) 2011 Google Inc.  All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef WorkerThreadableWebSocketChannel_h
#define WorkerThreadableWebSocketChannel_h

#if ENABLE(WEB_SOCKETS) && ENABLE(WORKERS)

#include "PlatformString.h"
#include "ThreadableWebSocketChannel.h"
#include "WebSocketChannelClient.h"

#include <wtf/PassRefPtr.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
#include <wtf/Threading.h>

namespace WebCore {

class KURL;
class ScriptExecutionContext;
class ThreadableWebSocketChannelClientWrapper;
class WorkerContext;
class WorkerLoaderProxy;
class WorkerRunLoop;

class WorkerThreadableWebSocketChannel : public RefCounted<WorkerThreadableWebSocketChannel>, public ThreadableWebSocketChannel {
55
    WTF_MAKE_FAST_ALLOCATED;
56
public:
57
    static PassRefPtr<ThreadableWebSocketChannel> create(WorkerContext* workerContext, WebSocketChannelClient* client, const String& taskMode)
58
    {
59
        return adoptRef(new WorkerThreadableWebSocketChannel(workerContext, client, taskMode));
60 61 62
    }
    virtual ~WorkerThreadableWebSocketChannel();

63 64 65 66
    // ThreadableWebSocketChannel functions.
    virtual bool useHixie76Protocol() OVERRIDE;
    virtual void connect(const KURL&, const String& protocol) OVERRIDE;
    virtual String subprotocol() OVERRIDE;
67
    virtual String extensions() OVERRIDE;
68 69 70
    virtual bool send(const String& message) OVERRIDE;
    virtual bool send(const ArrayBuffer&) OVERRIDE;
    virtual bool send(const Blob&) OVERRIDE;
71 72 73 74 75 76
    virtual unsigned long bufferedAmount() const OVERRIDE;
    virtual void close(int code, const String& reason) OVERRIDE;
    virtual void fail(const String& reason) OVERRIDE;
    virtual void disconnect() OVERRIDE; // Will suppress didClose().
    virtual void suspend() OVERRIDE;
    virtual void resume() OVERRIDE;
77 78 79 80 81 82 83 84 85 86 87

    using RefCounted<WorkerThreadableWebSocketChannel>::ref;
    using RefCounted<WorkerThreadableWebSocketChannel>::deref;

protected:
    virtual void refThreadableWebSocketChannel() { ref(); }
    virtual void derefThreadableWebSocketChannel() { deref(); }

private:
    // Generated by the bridge.  The Peer and its bridge should have identical
    // lifetimes.
88 89
    class Peer : public WebSocketChannelClient {
        WTF_MAKE_NONCOPYABLE(Peer); WTF_MAKE_FAST_ALLOCATED;
90
    public:
91
        static Peer* create(PassRefPtr<ThreadableWebSocketChannelClientWrapper> clientWrapper, WorkerLoaderProxy& loaderProxy, ScriptExecutionContext* context, const String& taskMode)
92
        {
93
            return new Peer(clientWrapper, loaderProxy, context, taskMode);
94 95 96
        }
        ~Peer();

97
        bool useHixie76Protocol();
98
        void connect(const KURL&, const String& protocol);
99
        void send(const String& message);
100
        void send(const ArrayBuffer&);
101
        void send(const Blob&);
102
        void bufferedAmount();
103
        void close(int code, const String& reason);
104
        void fail(const String& reason);
105
        void disconnect();
106 107
        void suspend();
        void resume();
108

109 110 111 112 113 114 115
        // WebSocketChannelClient functions.
        virtual void didConnect() OVERRIDE;
        virtual void didReceiveMessage(const String& message) OVERRIDE;
        virtual void didReceiveBinaryData(PassOwnPtr<Vector<char> >) OVERRIDE;
        virtual void didUpdateBufferedAmount(unsigned long bufferedAmount) OVERRIDE;
        virtual void didStartClosingHandshake() OVERRIDE;
        virtual void didClose(unsigned long unhandledBufferedAmount, ClosingHandshakeCompletionStatus, unsigned short code, const String& reason) OVERRIDE;
116 117

    private:
118
        Peer(PassRefPtr<ThreadableWebSocketChannelClientWrapper>, WorkerLoaderProxy&, ScriptExecutionContext*, const String& taskMode);
119

120
        RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
121 122 123 124 125 126 127 128
        WorkerLoaderProxy& m_loaderProxy;
        RefPtr<ThreadableWebSocketChannel> m_mainWebSocketChannel;
        String m_taskMode;
    };

    // Bridge for Peer.  Running on the worker thread.
    class Bridge : public RefCounted<Bridge> {
    public:
129
        static PassRefPtr<Bridge> create(PassRefPtr<ThreadableWebSocketChannelClientWrapper> workerClientWrapper, PassRefPtr<WorkerContext> workerContext, const String& taskMode)
130
        {
131
            return adoptRef(new Bridge(workerClientWrapper, workerContext, taskMode));
132
        }
133
        ~Bridge();
134
        void connect(const KURL&, const String& protocol);
135 136 137
        bool send(const String& message);
        bool send(const ArrayBuffer&);
        bool send(const Blob&);
138
        unsigned long bufferedAmount();
139
        void close(int code, const String& reason);
140
        void fail(const String& reason);
141
        void disconnect();
142 143
        void suspend();
        void resume();
144 145 146 147 148

        using RefCounted<Bridge>::ref;
        using RefCounted<Bridge>::deref;

    private:
149
        Bridge(PassRefPtr<ThreadableWebSocketChannelClientWrapper>, PassRefPtr<WorkerContext>, const String& taskMode);
150

151
        static void setWebSocketChannel(ScriptExecutionContext*, Bridge* thisPtr, Peer*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, bool useHixie76Protocol);
152 153

        // Executed on the main thread to create a Peer for this bridge.
154
        static void mainThreadCreateWebSocketChannel(ScriptExecutionContext*, Bridge* thisPtr, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, const String& taskMode);
155 156 157 158 159 160 161

        // Executed on the worker context's thread.
        void clearClientWrapper();

        void setMethodNotCompleted();
        void waitForMethodCompletion();

162
        RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
163 164 165 166 167 168
        RefPtr<WorkerContext> m_workerContext;
        WorkerLoaderProxy& m_loaderProxy;
        String m_taskMode;
        Peer* m_peer;
    };

169
    WorkerThreadableWebSocketChannel(WorkerContext*, WebSocketChannelClient*, const String& taskMode);
170

171
    static void mainThreadConnect(ScriptExecutionContext*, Peer*, const KURL&, const String& protocol);
172
    static void mainThreadSend(ScriptExecutionContext*, Peer*, const String& message);
173
    static void mainThreadSendArrayBuffer(ScriptExecutionContext*, Peer*, PassOwnPtr<Vector<char> >);
174
    static void mainThreadSendBlob(ScriptExecutionContext*, Peer*, const KURL&, const String& type, long long size);
175
    static void mainThreadBufferedAmount(ScriptExecutionContext*, Peer*);
176
    static void mainThreadClose(ScriptExecutionContext*, Peer*, int code, const String& reason);
177
    static void mainThreadFail(ScriptExecutionContext*, Peer*, const String& reason);
178
    static void mainThreadDestroy(ScriptExecutionContext*, Peer*);
179 180
    static void mainThreadSuspend(ScriptExecutionContext*, Peer*);
    static void mainThreadResume(ScriptExecutionContext*, Peer*);
181 182 183 184 185 186 187 188 189 190 191

    RefPtr<WorkerContext> m_workerContext;
    RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
    RefPtr<Bridge> m_bridge;
};

} // namespace WebCore

#endif // ENABLE(WEB_SOCKETS)

#endif // WorkerThreadableWebSocketChannel_h