| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 #define WorkerWebSocketChannel_h | 32 #define WorkerWebSocketChannel_h |
| 33 | 33 |
| 34 #include "bindings/core/v8/SourceLocation.h" | 34 #include "bindings/core/v8/SourceLocation.h" |
| 35 #include "core/workers/WorkerThreadLifecycleObserver.h" | 35 #include "core/workers/WorkerThreadLifecycleObserver.h" |
| 36 #include "modules/websockets/WebSocketChannel.h" | 36 #include "modules/websockets/WebSocketChannel.h" |
| 37 #include "modules/websockets/WebSocketChannelClient.h" | 37 #include "modules/websockets/WebSocketChannelClient.h" |
| 38 #include "platform/heap/Handle.h" | 38 #include "platform/heap/Handle.h" |
| 39 #include "platform/v8_inspector/public/ConsoleTypes.h" | 39 #include "platform/v8_inspector/public/ConsoleTypes.h" |
| 40 #include "wtf/Assertions.h" | 40 #include "wtf/Assertions.h" |
| 41 #include "wtf/Forward.h" | 41 #include "wtf/Forward.h" |
| 42 #include "wtf/OwnPtr.h" | |
| 43 #include "wtf/RefPtr.h" | 42 #include "wtf/RefPtr.h" |
| 44 #include "wtf/Vector.h" | 43 #include "wtf/Vector.h" |
| 45 #include "wtf/text/WTFString.h" | 44 #include "wtf/text/WTFString.h" |
| 45 #include <memory> |
| 46 #include <stdint.h> | 46 #include <stdint.h> |
| 47 | 47 |
| 48 namespace blink { | 48 namespace blink { |
| 49 | 49 |
| 50 class BlobDataHandle; | 50 class BlobDataHandle; |
| 51 class KURL; | 51 class KURL; |
| 52 class ExecutionContext; | 52 class ExecutionContext; |
| 53 class ExecutionContextTask; | 53 class ExecutionContextTask; |
| 54 class WebSocketChannelSyncHelper; | 54 class WebSocketChannelSyncHelper; |
| 55 class WorkerGlobalScope; | 55 class WorkerGlobalScope; |
| 56 class WorkerLoaderProxy; | 56 class WorkerLoaderProxy; |
| 57 | 57 |
| 58 class WorkerWebSocketChannel final : public WebSocketChannel { | 58 class WorkerWebSocketChannel final : public WebSocketChannel { |
| 59 WTF_MAKE_NONCOPYABLE(WorkerWebSocketChannel); | 59 WTF_MAKE_NONCOPYABLE(WorkerWebSocketChannel); |
| 60 public: | 60 public: |
| 61 static WebSocketChannel* create(WorkerGlobalScope& workerGlobalScope, WebSoc
ketChannelClient* client, PassOwnPtr<SourceLocation> location) | 61 static WebSocketChannel* create(WorkerGlobalScope& workerGlobalScope, WebSoc
ketChannelClient* client, std::unique_ptr<SourceLocation> location) |
| 62 { | 62 { |
| 63 return new WorkerWebSocketChannel(workerGlobalScope, client, std::move(l
ocation)); | 63 return new WorkerWebSocketChannel(workerGlobalScope, client, std::move(l
ocation)); |
| 64 } | 64 } |
| 65 ~WorkerWebSocketChannel() override; | 65 ~WorkerWebSocketChannel() override; |
| 66 | 66 |
| 67 // WebSocketChannel functions. | 67 // WebSocketChannel functions. |
| 68 bool connect(const KURL&, const String& protocol) override; | 68 bool connect(const KURL&, const String& protocol) override; |
| 69 void send(const CString&) override; | 69 void send(const CString&) override; |
| 70 void send(const DOMArrayBuffer&, unsigned byteOffset, unsigned byteLength) o
verride; | 70 void send(const DOMArrayBuffer&, unsigned byteOffset, unsigned byteLength) o
verride; |
| 71 void send(PassRefPtr<BlobDataHandle>) override; | 71 void send(PassRefPtr<BlobDataHandle>) override; |
| 72 void sendTextAsCharVector(PassOwnPtr<Vector<char>>) override | 72 void sendTextAsCharVector(std::unique_ptr<Vector<char>>) override |
| 73 { | 73 { |
| 74 ASSERT_NOT_REACHED(); | 74 ASSERT_NOT_REACHED(); |
| 75 } | 75 } |
| 76 void sendBinaryAsCharVector(PassOwnPtr<Vector<char>>) override | 76 void sendBinaryAsCharVector(std::unique_ptr<Vector<char>>) override |
| 77 { | 77 { |
| 78 ASSERT_NOT_REACHED(); | 78 ASSERT_NOT_REACHED(); |
| 79 } | 79 } |
| 80 void close(int code, const String& reason) override; | 80 void close(int code, const String& reason) override; |
| 81 void fail(const String& reason, MessageLevel, PassOwnPtr<SourceLocation>) ov
erride; | 81 void fail(const String& reason, MessageLevel, std::unique_ptr<SourceLocation
>) override; |
| 82 void disconnect() override; // Will suppress didClose(). | 82 void disconnect() override; // Will suppress didClose(). |
| 83 | 83 |
| 84 DECLARE_VIRTUAL_TRACE(); | 84 DECLARE_VIRTUAL_TRACE(); |
| 85 | 85 |
| 86 class Bridge; | 86 class Bridge; |
| 87 // Allocated and used in the main thread. | 87 // Allocated and used in the main thread. |
| 88 class Peer final : public GarbageCollectedFinalized<Peer>, public WebSocketC
hannelClient, public WorkerThreadLifecycleObserver { | 88 class Peer final : public GarbageCollectedFinalized<Peer>, public WebSocketC
hannelClient, public WorkerThreadLifecycleObserver { |
| 89 USING_GARBAGE_COLLECTED_MIXIN(Peer); | 89 USING_GARBAGE_COLLECTED_MIXIN(Peer); |
| 90 WTF_MAKE_NONCOPYABLE(Peer); | 90 WTF_MAKE_NONCOPYABLE(Peer); |
| 91 public: | 91 public: |
| 92 Peer(Bridge*, PassRefPtr<WorkerLoaderProxy>, WebSocketChannelSyncHelper*
, WorkerThreadLifecycleContext*); | 92 Peer(Bridge*, PassRefPtr<WorkerLoaderProxy>, WebSocketChannelSyncHelper*
, WorkerThreadLifecycleContext*); |
| 93 ~Peer() override; | 93 ~Peer() override; |
| 94 | 94 |
| 95 // SourceLocation parameter may be shown when the connection fails. | 95 // SourceLocation parameter may be shown when the connection fails. |
| 96 bool initialize(PassOwnPtr<SourceLocation>, ExecutionContext*); | 96 bool initialize(std::unique_ptr<SourceLocation>, ExecutionContext*); |
| 97 | 97 |
| 98 void connect(const KURL&, const String& protocol); | 98 void connect(const KURL&, const String& protocol); |
| 99 void sendTextAsCharVector(PassOwnPtr<Vector<char>>); | 99 void sendTextAsCharVector(std::unique_ptr<Vector<char>>); |
| 100 void sendBinaryAsCharVector(PassOwnPtr<Vector<char>>); | 100 void sendBinaryAsCharVector(std::unique_ptr<Vector<char>>); |
| 101 void sendBlob(PassRefPtr<BlobDataHandle>); | 101 void sendBlob(PassRefPtr<BlobDataHandle>); |
| 102 void close(int code, const String& reason); | 102 void close(int code, const String& reason); |
| 103 void fail(const String& reason, MessageLevel, PassOwnPtr<SourceLocation>
); | 103 void fail(const String& reason, MessageLevel, std::unique_ptr<SourceLoca
tion>); |
| 104 void disconnect(); | 104 void disconnect(); |
| 105 | 105 |
| 106 DECLARE_VIRTUAL_TRACE(); | 106 DECLARE_VIRTUAL_TRACE(); |
| 107 | 107 |
| 108 // WebSocketChannelClient functions. | 108 // WebSocketChannelClient functions. |
| 109 void didConnect(const String& subprotocol, const String& extensions) ove
rride; | 109 void didConnect(const String& subprotocol, const String& extensions) ove
rride; |
| 110 void didReceiveTextMessage(const String& payload) override; | 110 void didReceiveTextMessage(const String& payload) override; |
| 111 void didReceiveBinaryMessage(PassOwnPtr<Vector<char>>) override; | 111 void didReceiveBinaryMessage(std::unique_ptr<Vector<char>>) override; |
| 112 void didConsumeBufferedAmount(uint64_t) override; | 112 void didConsumeBufferedAmount(uint64_t) override; |
| 113 void didStartClosingHandshake() override; | 113 void didStartClosingHandshake() override; |
| 114 void didClose(ClosingHandshakeCompletionStatus, unsigned short code, con
st String& reason) override; | 114 void didClose(ClosingHandshakeCompletionStatus, unsigned short code, con
st String& reason) override; |
| 115 void didError() override; | 115 void didError() override; |
| 116 | 116 |
| 117 // WorkerThreadLifecycleObserver function. | 117 // WorkerThreadLifecycleObserver function. |
| 118 void contextDestroyed() override; | 118 void contextDestroyed() override; |
| 119 | 119 |
| 120 private: | 120 private: |
| 121 CrossThreadWeakPersistent<Bridge> m_bridge; | 121 CrossThreadWeakPersistent<Bridge> m_bridge; |
| 122 RefPtr<WorkerLoaderProxy> m_loaderProxy; | 122 RefPtr<WorkerLoaderProxy> m_loaderProxy; |
| 123 Member<WebSocketChannel> m_mainWebSocketChannel; | 123 Member<WebSocketChannel> m_mainWebSocketChannel; |
| 124 Member<WebSocketChannelSyncHelper> m_syncHelper; | 124 Member<WebSocketChannelSyncHelper> m_syncHelper; |
| 125 }; | 125 }; |
| 126 | 126 |
| 127 // Bridge for Peer. Running on the worker thread. | 127 // Bridge for Peer. Running on the worker thread. |
| 128 class Bridge final : public GarbageCollectedFinalized<Bridge> { | 128 class Bridge final : public GarbageCollectedFinalized<Bridge> { |
| 129 WTF_MAKE_NONCOPYABLE(Bridge); | 129 WTF_MAKE_NONCOPYABLE(Bridge); |
| 130 public: | 130 public: |
| 131 Bridge(WebSocketChannelClient*, WorkerGlobalScope&); | 131 Bridge(WebSocketChannelClient*, WorkerGlobalScope&); |
| 132 ~Bridge(); | 132 ~Bridge(); |
| 133 // SourceLocation parameter may be shown when the connection fails. | 133 // SourceLocation parameter may be shown when the connection fails. |
| 134 void initialize(PassOwnPtr<SourceLocation>); | 134 void initialize(std::unique_ptr<SourceLocation>); |
| 135 bool connect(const KURL&, const String& protocol); | 135 bool connect(const KURL&, const String& protocol); |
| 136 void send(const CString& message); | 136 void send(const CString& message); |
| 137 void send(const DOMArrayBuffer&, unsigned byteOffset, unsigned byteLengt
h); | 137 void send(const DOMArrayBuffer&, unsigned byteOffset, unsigned byteLengt
h); |
| 138 void send(PassRefPtr<BlobDataHandle>); | 138 void send(PassRefPtr<BlobDataHandle>); |
| 139 void close(int code, const String& reason); | 139 void close(int code, const String& reason); |
| 140 void fail(const String& reason, MessageLevel, PassOwnPtr<SourceLocation>
); | 140 void fail(const String& reason, MessageLevel, std::unique_ptr<SourceLoca
tion>); |
| 141 void disconnect(); | 141 void disconnect(); |
| 142 | 142 |
| 143 void createPeerOnMainThread(PassOwnPtr<SourceLocation>, WorkerThreadLife
cycleContext*, ExecutionContext*); | 143 void createPeerOnMainThread(std::unique_ptr<SourceLocation>, WorkerThrea
dLifecycleContext*, ExecutionContext*); |
| 144 | 144 |
| 145 // Returns null when |disconnect| has already been called. | 145 // Returns null when |disconnect| has already been called. |
| 146 WebSocketChannelClient* client() { return m_client; } | 146 WebSocketChannelClient* client() { return m_client; } |
| 147 | 147 |
| 148 DECLARE_TRACE(); | 148 DECLARE_TRACE(); |
| 149 | 149 |
| 150 private: | 150 private: |
| 151 // Returns false if shutdown event is received before method completion. | 151 // Returns false if shutdown event is received before method completion. |
| 152 bool waitForMethodCompletion(std::unique_ptr<ExecutionContextTask>); | 152 bool waitForMethodCompletion(std::unique_ptr<ExecutionContextTask>); |
| 153 | 153 |
| 154 Member<WebSocketChannelClient> m_client; | 154 Member<WebSocketChannelClient> m_client; |
| 155 Member<WorkerGlobalScope> m_workerGlobalScope; | 155 Member<WorkerGlobalScope> m_workerGlobalScope; |
| 156 RefPtr<WorkerLoaderProxy> m_loaderProxy; | 156 RefPtr<WorkerLoaderProxy> m_loaderProxy; |
| 157 Member<WebSocketChannelSyncHelper> m_syncHelper; | 157 Member<WebSocketChannelSyncHelper> m_syncHelper; |
| 158 CrossThreadPersistent<Peer> m_peer; | 158 CrossThreadPersistent<Peer> m_peer; |
| 159 }; | 159 }; |
| 160 | 160 |
| 161 private: | 161 private: |
| 162 WorkerWebSocketChannel(WorkerGlobalScope&, WebSocketChannelClient*, PassOwnP
tr<SourceLocation>); | 162 WorkerWebSocketChannel(WorkerGlobalScope&, WebSocketChannelClient*, std::uni
que_ptr<SourceLocation>); |
| 163 | 163 |
| 164 Member<Bridge> m_bridge; | 164 Member<Bridge> m_bridge; |
| 165 OwnPtr<SourceLocation> m_locationAtConnection; | 165 std::unique_ptr<SourceLocation> m_locationAtConnection; |
| 166 }; | 166 }; |
| 167 | 167 |
| 168 } // namespace blink | 168 } // namespace blink |
| 169 | 169 |
| 170 #endif // WorkerWebSocketChannel_h | 170 #endif // WorkerWebSocketChannel_h |
| OLD | NEW |