| 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 // Promptly clear connection to bridge + loader proxy. | 107 // Promptly clear connection to bridge + loader proxy. |
| 108 EAGERLY_FINALIZE(); | 108 EAGERLY_FINALIZE(); |
| 109 | 109 |
| 110 // WebSocketChannelClient functions. | 110 // WebSocketChannelClient functions. |
| 111 void didConnect(const String& subprotocol, const String& extensions) ove
rride; | 111 void didConnect(const String& subprotocol, const String& extensions) ove
rride; |
| 112 void didReceiveTextMessage(const String& payload) override; | 112 void didReceiveTextMessage(const String& payload) override; |
| 113 void didReceiveBinaryMessage(PassOwnPtr<Vector<char>>) override; | 113 void didReceiveBinaryMessage(std::unique_ptr<Vector<char>>) override; |
| 114 void didConsumeBufferedAmount(uint64_t) override; | 114 void didConsumeBufferedAmount(uint64_t) override; |
| 115 void didStartClosingHandshake() override; | 115 void didStartClosingHandshake() override; |
| 116 void didClose(ClosingHandshakeCompletionStatus, unsigned short code, con
st String& reason) override; | 116 void didClose(ClosingHandshakeCompletionStatus, unsigned short code, con
st String& reason) override; |
| 117 void didError() override; | 117 void didError() override; |
| 118 | 118 |
| 119 // WorkerThreadLifecycleObserver function. | 119 // WorkerThreadLifecycleObserver function. |
| 120 void contextDestroyed() override; | 120 void contextDestroyed() override; |
| 121 | 121 |
| 122 private: | 122 private: |
| 123 CrossThreadWeakPersistent<Bridge> m_bridge; | 123 CrossThreadWeakPersistent<Bridge> m_bridge; |
| 124 RefPtr<WorkerLoaderProxy> m_loaderProxy; | 124 RefPtr<WorkerLoaderProxy> m_loaderProxy; |
| 125 Member<WebSocketChannel> m_mainWebSocketChannel; | 125 Member<WebSocketChannel> m_mainWebSocketChannel; |
| 126 Member<WebSocketChannelSyncHelper> m_syncHelper; | 126 Member<WebSocketChannelSyncHelper> m_syncHelper; |
| 127 }; | 127 }; |
| 128 | 128 |
| 129 // Bridge for Peer. Running on the worker thread. | 129 // Bridge for Peer. Running on the worker thread. |
| 130 class Bridge final : public GarbageCollectedFinalized<Bridge> { | 130 class Bridge final : public GarbageCollectedFinalized<Bridge> { |
| 131 WTF_MAKE_NONCOPYABLE(Bridge); | 131 WTF_MAKE_NONCOPYABLE(Bridge); |
| 132 public: | 132 public: |
| 133 Bridge(WebSocketChannelClient*, WorkerGlobalScope&); | 133 Bridge(WebSocketChannelClient*, WorkerGlobalScope&); |
| 134 ~Bridge(); | 134 ~Bridge(); |
| 135 // SourceLocation parameter may be shown when the connection fails. | 135 // SourceLocation parameter may be shown when the connection fails. |
| 136 void initialize(PassOwnPtr<SourceLocation>); | 136 void initialize(std::unique_ptr<SourceLocation>); |
| 137 bool connect(const KURL&, const String& protocol); | 137 bool connect(const KURL&, const String& protocol); |
| 138 void send(const CString& message); | 138 void send(const CString& message); |
| 139 void send(const DOMArrayBuffer&, unsigned byteOffset, unsigned byteLengt
h); | 139 void send(const DOMArrayBuffer&, unsigned byteOffset, unsigned byteLengt
h); |
| 140 void send(PassRefPtr<BlobDataHandle>); | 140 void send(PassRefPtr<BlobDataHandle>); |
| 141 void close(int code, const String& reason); | 141 void close(int code, const String& reason); |
| 142 void fail(const String& reason, MessageLevel, PassOwnPtr<SourceLocation>
); | 142 void fail(const String& reason, MessageLevel, std::unique_ptr<SourceLoca
tion>); |
| 143 void disconnect(); | 143 void disconnect(); |
| 144 | 144 |
| 145 void createPeerOnMainThread(PassOwnPtr<SourceLocation>, WorkerThreadLife
cycleContext*, ExecutionContext*); | 145 void createPeerOnMainThread(std::unique_ptr<SourceLocation>, WorkerThrea
dLifecycleContext*, ExecutionContext*); |
| 146 | 146 |
| 147 // Returns null when |disconnect| has already been called. | 147 // Returns null when |disconnect| has already been called. |
| 148 WebSocketChannelClient* client() { return m_client; } | 148 WebSocketChannelClient* client() { return m_client; } |
| 149 | 149 |
| 150 DECLARE_TRACE(); | 150 DECLARE_TRACE(); |
| 151 // Promptly clear connection to peer + loader proxy. | 151 // Promptly clear connection to peer + loader proxy. |
| 152 EAGERLY_FINALIZE(); | 152 EAGERLY_FINALIZE(); |
| 153 | 153 |
| 154 private: | 154 private: |
| 155 // Returns false if shutdown event is received before method completion. | 155 // Returns false if shutdown event is received before method completion. |
| 156 bool waitForMethodCompletion(std::unique_ptr<ExecutionContextTask>); | 156 bool waitForMethodCompletion(std::unique_ptr<ExecutionContextTask>); |
| 157 | 157 |
| 158 Member<WebSocketChannelClient> m_client; | 158 Member<WebSocketChannelClient> m_client; |
| 159 Member<WorkerGlobalScope> m_workerGlobalScope; | 159 Member<WorkerGlobalScope> m_workerGlobalScope; |
| 160 RefPtr<WorkerLoaderProxy> m_loaderProxy; | 160 RefPtr<WorkerLoaderProxy> m_loaderProxy; |
| 161 Member<WebSocketChannelSyncHelper> m_syncHelper; | 161 Member<WebSocketChannelSyncHelper> m_syncHelper; |
| 162 CrossThreadPersistent<Peer> m_peer; | 162 CrossThreadPersistent<Peer> m_peer; |
| 163 }; | 163 }; |
| 164 | 164 |
| 165 private: | 165 private: |
| 166 WorkerWebSocketChannel(WorkerGlobalScope&, WebSocketChannelClient*, PassOwnP
tr<SourceLocation>); | 166 WorkerWebSocketChannel(WorkerGlobalScope&, WebSocketChannelClient*, std::uni
que_ptr<SourceLocation>); |
| 167 | 167 |
| 168 Member<Bridge> m_bridge; | 168 Member<Bridge> m_bridge; |
| 169 OwnPtr<SourceLocation> m_locationAtConnection; | 169 std::unique_ptr<SourceLocation> m_locationAtConnection; |
| 170 }; | 170 }; |
| 171 | 171 |
| 172 } // namespace blink | 172 } // namespace blink |
| 173 | 173 |
| 174 #endif // WorkerWebSocketChannel_h | 174 #endif // WorkerWebSocketChannel_h |
| OLD | NEW |