1 2 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Common functionality shared between DOMWebSocket and WebSocketStream.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBSOCKETS_WEBSOCKET_COMMON_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBSOCKETS_WEBSOCKET_COMMON_H_
#include <stdint.h>
#include <optional>
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
namespace blink {
class ExecutionContext;
class ExceptionState;
class WebSocketChannel;
enum class MixedContentAutoupgradeStatus;
// Implements connection- and closing- related functionality that otherwise
// would be duplicated between DOMWebSocket and WebSocketStream. Is embedded
// into those classes and delegated to when needed.
class MODULES_EXPORT WebSocketCommon {
DISALLOW_NEW();
public:
WebSocketCommon() = default;
WebSocketCommon(const WebSocketCommon&) = delete;
WebSocketCommon& operator=(const WebSocketCommon&) = delete;
~WebSocketCommon() = default;
enum State { kConnecting = 0, kOpen = 1, kClosing = 2, kClosed = 3 };
enum class ConnectResult { kSuccess, kException, kAsyncError };
// Checks |url| and |protocols| are valid, and starts a connection if they
// are.
ConnectResult Connect(ExecutionContext*,
const String& url,
const Vector<String>& protocols,
WebSocketChannel*,
ExceptionState&);
// Closes the connection if |code| and |reason| are valid.
void CloseInternal(std::optional<uint16_t> code,
const String& reason,
WebSocketChannel*,
ExceptionState&);
State GetState() const { return state_; }
void SetState(State state) { state_ = state; }
const KURL& Url() const { return url_; }
// The following methods are public for testing.
// Returns true if |protocol| is a valid WebSocket subprotocol name.
static bool IsValidSubprotocolString(const String& protocol);
// Escapes non-printing or non-ASCII characters in |protocol| as "\uXXXX" for
// inclusion in exception messages.
static String EncodeSubprotocolString(const String& protocol);
// Joins the strings in |strings| into a single string, with |separator|
// between each string.
static String JoinStrings(const Vector<String>& strings,
const char* separator);
// Determines if `code` and `reason` are valid and throws an exception if not.
// Returns `code` if supplied, otherwise std::nullopt if
// `reason` is empty and kCloseEventCodeNormalClosure otherwise. `reason` is
// expected to be a USVString, ie. no unmatched surrogates.
static std::optional<uint16_t> ValidateCloseCodeAndReason(
std::optional<uint16_t> code,
const String& reason,
ExceptionState&);
private:
// Returns true if |character| is allowed in a WebSocket subprotocol name.
static bool IsValidSubprotocolCharacter(UChar character);
KURL url_;
State state_ = kConnecting;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBSOCKETS_WEBSOCKET_COMMON_H_
|