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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_TEST_CHROMEDRIVER_NET_STUB_SYNC_WEBSOCKET_H_
#define CHROME_TEST_CHROMEDRIVER_NET_STUB_SYNC_WEBSOCKET_H_
#include <limits>
#include <queue>
#include <string>
#include <unordered_map>
#include "base/functional/callback_forward.h"
#include "base/values.h"
#include "chrome/test/chromedriver/net/sync_websocket.h"
// Proxy for using a WebSocket running on a background thread synchronously.
class StubSyncWebSocket : public SyncWebSocket {
public:
using CommandHandler = base::RepeatingCallback<
bool(int, const base::Value::Dict&, base::Value::Dict&)>;
StubSyncWebSocket();
~StubSyncWebSocket() override;
// Return true if connected, otherwise return false.
bool IsConnected() override;
// Connects to the WebSocket server. Returns true on success.
bool Connect(const GURL& url) override;
// Sends message. Returns true on success.
bool Send(const std::string& message) override;
// Receives next message and modifies the message on success. Returns
// StatusCode::kTimedout if no message is received within |timeout|.
// Returns StatusCode::kDisconnected if the socket is closed.
SyncWebSocket::StatusCode ReceiveNextMessage(std::string* message,
const Timeout& timeout) override;
// Returns whether there are any messages that have been received and not yet
// handled by ReceiveNextMessage.
bool HasNextMessage() override;
void AddCommandHandler(const std::string& method, CommandHandler handler);
void EnqueueResponse(const std::string& message);
void Disconnect();
base::RepeatingClosure DisconnectClosure();
void NotifyOnEmptyQueue(base::RepeatingClosure callback);
void SetResponseLimit(int count);
protected:
void GenerateDefaultResponse(int cmd_id, base::Value::Dict& response);
void EnqueueHandshakeResponse(int cmd_id, const std::string& method);
bool PopMessage(std::string* dest);
bool connected_ = false;
bool handshake_add_script_handled_ = false;
bool handshake_runtime_eval_handled_ = false;
bool handshake_page_enable_handled_ = false;
bool handshake_target_set_autoattach_handled_ = false;
bool connect_complete_ = false;
int response_limit_ = std::numeric_limits<int>::max();
std::queue<std::string> queued_response_;
std::unordered_map<std::string, CommandHandler> command_handlers_;
base::RepeatingClosure on_empty_queue_;
// must be the last member
base::WeakPtrFactory<StubSyncWebSocket> weak_ptr_factory_{this};
};
#endif // CHROME_TEST_CHROMEDRIVER_NET_STUB_SYNC_WEBSOCKET_H_
|