xref: /aosp_15_r20/external/cronet/net/test/spawned_test_server/remote_test_server_spawner_request.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_TEST_SPAWNED_TEST_SERVER_REMOTE_TEST_SERVER_SPAWNER_REQUEST_H_
6 #define NET_TEST_SPAWNED_TEST_SERVER_REMOTE_TEST_SERVER_SPAWNER_REQUEST_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/memory/scoped_refptr.h"
12 #include "base/threading/thread_checker.h"
13 
14 class GURL;
15 
16 namespace base {
17 class SingleThreadTaskRunner;
18 }  // namespace base
19 
20 namespace net {
21 
22 class ScopedPortException;
23 
24 // RemoteTestServerSpawnerRequest is used by RemoteTestServer to send a request
25 // to the test server spawner.
26 class RemoteTestServerSpawnerRequest {
27  public:
28   // Queries the specified URL. If |post_data| is empty then a GET request is
29   // sent. Otherwise |post_data| must be a json blob which is sent as a POST
30   // request body.
31   RemoteTestServerSpawnerRequest(
32       scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
33       const GURL& url,
34       const std::string& post_data);
35 
36   RemoteTestServerSpawnerRequest(const RemoteTestServerSpawnerRequest&) =
37       delete;
38   RemoteTestServerSpawnerRequest& operator=(
39       const RemoteTestServerSpawnerRequest&) = delete;
40 
41   ~RemoteTestServerSpawnerRequest();
42 
43   // Blocks until request is finished. If |response| isn't nullptr then server
44   // response is copied to *response. Returns true if the request was completed
45   // successfully.
46   [[nodiscard]] bool WaitForCompletion(std::string* response);
47 
48  private:
49   class Core;
50 
51   scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
52 
53   // Core runs on |io_task_runner_|. It's responsible for sending the request
54   // and reading the response.
55   std::unique_ptr<Core> core_;
56 
57   // Helper to add spawner port to the list of the globally explicitly allowed
58   // ports. It needs to be here instead of in Core because ScopedPortException
59   // is not thread-safe.
60   std::unique_ptr<ScopedPortException> allowed_port_;
61 
62   THREAD_CHECKER(thread_checker_);
63 };
64 
65 }  // namespace net
66 
67 #endif  // NET_TEST_SPAWNED_TEST_SERVER_REMOTE_TEST_SERVER_SPAWNER_REQUEST_H_
68