OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 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 25 matching lines...) Expand all Loading... |
36 #include "core/fileapi/Blob.h" | 36 #include "core/fileapi/Blob.h" |
37 #include "core/fileapi/FileError.h" | 37 #include "core/fileapi/FileError.h" |
38 #include "modules/ModulesExport.h" | 38 #include "modules/ModulesExport.h" |
39 #include "modules/websockets/WebSocketChannel.h" | 39 #include "modules/websockets/WebSocketChannel.h" |
40 #include "platform/heap/Handle.h" | 40 #include "platform/heap/Handle.h" |
41 #include "platform/v8_inspector/public/ConsoleTypes.h" | 41 #include "platform/v8_inspector/public/ConsoleTypes.h" |
42 #include "platform/weborigin/KURL.h" | 42 #include "platform/weborigin/KURL.h" |
43 #include "public/platform/modules/websockets/WebSocketHandle.h" | 43 #include "public/platform/modules/websockets/WebSocketHandle.h" |
44 #include "public/platform/modules/websockets/WebSocketHandleClient.h" | 44 #include "public/platform/modules/websockets/WebSocketHandleClient.h" |
45 #include "wtf/Deque.h" | 45 #include "wtf/Deque.h" |
46 #include "wtf/OwnPtr.h" | |
47 #include "wtf/PassOwnPtr.h" | |
48 #include "wtf/PassRefPtr.h" | 46 #include "wtf/PassRefPtr.h" |
49 #include "wtf/RefPtr.h" | 47 #include "wtf/RefPtr.h" |
50 #include "wtf/Vector.h" | 48 #include "wtf/Vector.h" |
51 #include "wtf/text/CString.h" | 49 #include "wtf/text/CString.h" |
52 #include "wtf/text/WTFString.h" | 50 #include "wtf/text/WTFString.h" |
| 51 #include <memory> |
53 #include <stdint.h> | 52 #include <stdint.h> |
54 | 53 |
55 namespace blink { | 54 namespace blink { |
56 | 55 |
57 class Document; | 56 class Document; |
58 class WebSocketHandshakeRequest; | 57 class WebSocketHandshakeRequest; |
59 class WebSocketHandshakeRequestInfo; | 58 class WebSocketHandshakeRequestInfo; |
60 class WebSocketHandshakeResponseInfo; | 59 class WebSocketHandshakeResponseInfo; |
61 | 60 |
62 // This class is a WebSocketChannel subclass that works with a Document in a | 61 // This class is a WebSocketChannel subclass that works with a Document in a |
63 // DOMWindow (i.e. works in the main thread). | 62 // DOMWindow (i.e. works in the main thread). |
64 class MODULES_EXPORT DocumentWebSocketChannel final : public WebSocketChannel, p
ublic WebSocketHandleClient, public ContextLifecycleObserver { | 63 class MODULES_EXPORT DocumentWebSocketChannel final : public WebSocketChannel, p
ublic WebSocketHandleClient, public ContextLifecycleObserver { |
65 USING_GARBAGE_COLLECTED_MIXIN(DocumentWebSocketChannel); | 64 USING_GARBAGE_COLLECTED_MIXIN(DocumentWebSocketChannel); |
66 public: | 65 public: |
67 // You can specify the source file and the line number information | 66 // You can specify the source file and the line number information |
68 // explicitly by passing the last parameter. | 67 // explicitly by passing the last parameter. |
69 // In the usual case, they are set automatically and you don't have to | 68 // In the usual case, they are set automatically and you don't have to |
70 // pass it. | 69 // pass it. |
71 // Specify handle explicitly only in tests. | 70 // Specify handle explicitly only in tests. |
72 static DocumentWebSocketChannel* create(Document* document, WebSocketChannel
Client* client, PassOwnPtr<SourceLocation> location, WebSocketHandle *handle = 0
) | 71 static DocumentWebSocketChannel* create(Document* document, WebSocketChannel
Client* client, std::unique_ptr<SourceLocation> location, WebSocketHandle *handl
e = 0) |
73 { | 72 { |
74 return new DocumentWebSocketChannel(document, client, std::move(location
), handle); | 73 return new DocumentWebSocketChannel(document, client, std::move(location
), handle); |
75 } | 74 } |
76 ~DocumentWebSocketChannel() override; | 75 ~DocumentWebSocketChannel() override; |
77 | 76 |
78 // WebSocketChannel functions. | 77 // WebSocketChannel functions. |
79 bool connect(const KURL&, const String& protocol) override; | 78 bool connect(const KURL&, const String& protocol) override; |
80 void send(const CString& message) override; | 79 void send(const CString& message) override; |
81 void send(const DOMArrayBuffer&, unsigned byteOffset, unsigned byteLength) o
verride; | 80 void send(const DOMArrayBuffer&, unsigned byteOffset, unsigned byteLength) o
verride; |
82 void send(PassRefPtr<BlobDataHandle>) override; | 81 void send(PassRefPtr<BlobDataHandle>) override; |
83 void sendTextAsCharVector(PassOwnPtr<Vector<char>> data) override; | 82 void sendTextAsCharVector(std::unique_ptr<Vector<char>> data) override; |
84 void sendBinaryAsCharVector(PassOwnPtr<Vector<char>> data) override; | 83 void sendBinaryAsCharVector(std::unique_ptr<Vector<char>> data) override; |
85 // Start closing handshake. Use the CloseEventCodeNotSpecified for the code | 84 // Start closing handshake. Use the CloseEventCodeNotSpecified for the code |
86 // argument to omit payload. | 85 // argument to omit payload. |
87 void close(int code, const String& reason) override; | 86 void close(int code, const String& reason) override; |
88 void fail(const String& reason, MessageLevel, PassOwnPtr<SourceLocation>) ov
erride; | 87 void fail(const String& reason, MessageLevel, std::unique_ptr<SourceLocation
>) override; |
89 void disconnect() override; | 88 void disconnect() override; |
90 | 89 |
91 DECLARE_VIRTUAL_TRACE(); | 90 DECLARE_VIRTUAL_TRACE(); |
92 | 91 |
93 private: | 92 private: |
94 class BlobLoader; | 93 class BlobLoader; |
95 class Message; | 94 class Message; |
96 | 95 |
97 enum MessageType { | 96 enum MessageType { |
98 MessageTypeText, | 97 MessageTypeText, |
99 MessageTypeBlob, | 98 MessageTypeBlob, |
100 MessageTypeArrayBuffer, | 99 MessageTypeArrayBuffer, |
101 MessageTypeTextAsCharVector, | 100 MessageTypeTextAsCharVector, |
102 MessageTypeBinaryAsCharVector, | 101 MessageTypeBinaryAsCharVector, |
103 MessageTypeClose, | 102 MessageTypeClose, |
104 }; | 103 }; |
105 | 104 |
106 struct ReceivedMessage { | 105 struct ReceivedMessage { |
107 bool isMessageText; | 106 bool isMessageText; |
108 Vector<char> data; | 107 Vector<char> data; |
109 }; | 108 }; |
110 | 109 |
111 DocumentWebSocketChannel(Document*, WebSocketChannelClient*, PassOwnPtr<Sour
ceLocation>, WebSocketHandle*); | 110 DocumentWebSocketChannel(Document*, WebSocketChannelClient*, std::unique_ptr
<SourceLocation>, WebSocketHandle*); |
112 void sendInternal(WebSocketHandle::MessageType, const char* data, size_t tot
alSize, uint64_t* consumedBufferedAmount); | 111 void sendInternal(WebSocketHandle::MessageType, const char* data, size_t tot
alSize, uint64_t* consumedBufferedAmount); |
113 void processSendQueue(); | 112 void processSendQueue(); |
114 void flowControlIfNecessary(); | 113 void flowControlIfNecessary(); |
115 void failAsError(const String& reason) { fail(reason, ErrorMessageLevel, m_l
ocationAtConstruction->clone()); } | 114 void failAsError(const String& reason) { fail(reason, ErrorMessageLevel, m_l
ocationAtConstruction->clone()); } |
116 void abortAsyncOperations(); | 115 void abortAsyncOperations(); |
117 void handleDidClose(bool wasClean, unsigned short code, const String& reason
); | 116 void handleDidClose(bool wasClean, unsigned short code, const String& reason
); |
118 Document* document(); | 117 Document* document(); |
119 | 118 |
120 // WebSocketHandleClient functions. | 119 // WebSocketHandleClient functions. |
121 void didConnect(WebSocketHandle*, const WebString& selectedProtocol, const W
ebString& extensions) override; | 120 void didConnect(WebSocketHandle*, const WebString& selectedProtocol, const W
ebString& extensions) override; |
122 void didStartOpeningHandshake(WebSocketHandle*, const WebSocketHandshakeRequ
estInfo&) override; | 121 void didStartOpeningHandshake(WebSocketHandle*, const WebSocketHandshakeRequ
estInfo&) override; |
123 void didFinishOpeningHandshake(WebSocketHandle*, const WebSocketHandshakeRes
ponseInfo&) override; | 122 void didFinishOpeningHandshake(WebSocketHandle*, const WebSocketHandshakeRes
ponseInfo&) override; |
124 void didFail(WebSocketHandle*, const WebString& message) override; | 123 void didFail(WebSocketHandle*, const WebString& message) override; |
125 void didReceiveData(WebSocketHandle*, bool fin, WebSocketHandle::MessageType
, const char* data, size_t /* size */) override; | 124 void didReceiveData(WebSocketHandle*, bool fin, WebSocketHandle::MessageType
, const char* data, size_t /* size */) override; |
126 void didClose(WebSocketHandle*, bool wasClean, unsigned short code, const We
bString& reason) override; | 125 void didClose(WebSocketHandle*, bool wasClean, unsigned short code, const We
bString& reason) override; |
127 void didReceiveFlowControl(WebSocketHandle*, int64_t quota) override; | 126 void didReceiveFlowControl(WebSocketHandle*, int64_t quota) override; |
128 void didStartClosingHandshake(WebSocketHandle*) override; | 127 void didStartClosingHandshake(WebSocketHandle*) override; |
129 | 128 |
130 // Methods for BlobLoader. | 129 // Methods for BlobLoader. |
131 void didFinishLoadingBlob(DOMArrayBuffer*); | 130 void didFinishLoadingBlob(DOMArrayBuffer*); |
132 void didFailLoadingBlob(FileError::ErrorCode); | 131 void didFailLoadingBlob(FileError::ErrorCode); |
133 | 132 |
134 // m_handle is a handle of the connection. | 133 // m_handle is a handle of the connection. |
135 // m_handle == 0 means this channel is closed. | 134 // m_handle == 0 means this channel is closed. |
136 OwnPtr<WebSocketHandle> m_handle; | 135 std::unique_ptr<WebSocketHandle> m_handle; |
137 | 136 |
138 // m_client can be deleted while this channel is alive, but this class | 137 // m_client can be deleted while this channel is alive, but this class |
139 // expects that disconnect() is called before the deletion. | 138 // expects that disconnect() is called before the deletion. |
140 Member<WebSocketChannelClient> m_client; | 139 Member<WebSocketChannelClient> m_client; |
141 KURL m_url; | 140 KURL m_url; |
142 // m_identifier > 0 means calling scriptContextExecution() returns a Documen
t. | 141 // m_identifier > 0 means calling scriptContextExecution() returns a Documen
t. |
143 unsigned long m_identifier; | 142 unsigned long m_identifier; |
144 Member<BlobLoader> m_blobLoader; | 143 Member<BlobLoader> m_blobLoader; |
145 HeapDeque<Member<Message>> m_messages; | 144 HeapDeque<Member<Message>> m_messages; |
146 Vector<char> m_receivingMessageData; | 145 Vector<char> m_receivingMessageData; |
147 | 146 |
148 bool m_receivingMessageTypeIsText; | 147 bool m_receivingMessageTypeIsText; |
149 uint64_t m_sendingQuota; | 148 uint64_t m_sendingQuota; |
150 uint64_t m_receivedDataSizeForFlowControl; | 149 uint64_t m_receivedDataSizeForFlowControl; |
151 size_t m_sentSizeOfTopMessage; | 150 size_t m_sentSizeOfTopMessage; |
152 | 151 |
153 OwnPtr<SourceLocation> m_locationAtConstruction; | 152 std::unique_ptr<SourceLocation> m_locationAtConstruction; |
154 RefPtr<WebSocketHandshakeRequest> m_handshakeRequest; | 153 RefPtr<WebSocketHandshakeRequest> m_handshakeRequest; |
155 | 154 |
156 static const uint64_t receivedDataSizeForFlowControlHighWaterMark = 1 << 15; | 155 static const uint64_t receivedDataSizeForFlowControlHighWaterMark = 1 << 15; |
157 }; | 156 }; |
158 | 157 |
159 } // namespace blink | 158 } // namespace blink |
160 | 159 |
161 #endif // DocumentWebSocketChannel_h | 160 #endif // DocumentWebSocketChannel_h |
OLD | NEW |