sergeyu | 9b3d516c | 2016-02-11 20:58:09 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 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 REMOTING_BASE_URL_REQUEST_H_ |
| 6 | #define REMOTING_BASE_URL_REQUEST_H_ |
| 7 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 8 | #include <memory> |
sergeyu | 9b3d516c | 2016-02-11 20:58:09 | [diff] [blame] | 9 | #include <string> |
| 10 | |
| 11 | #include "base/callback_forward.h" |
rhalavati | 6826b7b | 2017-05-09 12:12:11 | [diff] [blame] | 12 | #include "net/traffic_annotation/network_traffic_annotation.h" |
sergeyu | 9b3d516c | 2016-02-11 20:58:09 | [diff] [blame] | 13 | |
| 14 | namespace remoting { |
| 15 | |
| 16 | // Abstract interface for URL requests. |
| 17 | class UrlRequest { |
| 18 | public: |
sergeyu | a79277c9 | 2016-02-18 20:15:27 | [diff] [blame] | 19 | enum class Type { |
| 20 | GET, |
| 21 | POST, |
| 22 | }; |
| 23 | |
sergeyu | 9b3d516c | 2016-02-11 20:58:09 | [diff] [blame] | 24 | struct Result { |
| 25 | Result() = default; |
| 26 | Result(int status, std::string response_body) |
| 27 | : success(true), status(status), response_body(response_body) {} |
| 28 | |
| 29 | static Result Failed() { return Result(); } |
| 30 | |
| 31 | // Set to true when the URL has been fetched successfully. |
| 32 | bool success = false; |
| 33 | |
| 34 | // HTTP status code received from the server. Valid only when |success| is |
| 35 | // set to true. |
| 36 | int status = 0; |
| 37 | |
| 38 | // Body of the response received from the server. Valid only when |success| |
| 39 | // is set to true. |
| 40 | std::string response_body; |
| 41 | }; |
| 42 | |
Jun Cai | 7428644 | 2020-02-28 18:24:46 | [diff] [blame] | 43 | typedef base::OnceCallback<void(const Result& result)> OnResultCallback; |
sergeyu | 9b3d516c | 2016-02-11 20:58:09 | [diff] [blame] | 44 | |
| 45 | virtual ~UrlRequest() {} |
| 46 | |
| 47 | // Adds an HTTP header to the request. Has no effect if called after Start(). |
| 48 | virtual void AddHeader(const std::string& value) = 0; |
| 49 | |
sergeyu | a79277c9 | 2016-02-18 20:15:27 | [diff] [blame] | 50 | // Sets data to be sent for POST requests. |
| 51 | virtual void SetPostData(const std::string& content_type, |
| 52 | const std::string& post_data) = 0; |
| 53 | |
sergeyu | 9b3d516c | 2016-02-11 20:58:09 | [diff] [blame] | 54 | // Sends a request to the server. |on_response_callback| will be called to |
| 55 | // return result of the request. |
Jun Cai | 7428644 | 2020-02-28 18:24:46 | [diff] [blame] | 56 | virtual void Start(OnResultCallback on_result_callback) = 0; |
sergeyu | 9b3d516c | 2016-02-11 20:58:09 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | // Factory for UrlRequest instances. |
| 60 | class UrlRequestFactory { |
| 61 | public: |
| 62 | virtual ~UrlRequestFactory() {} |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 63 | virtual std::unique_ptr<UrlRequest> CreateUrlRequest( |
| 64 | UrlRequest::Type type, |
rhalavati | 6826b7b | 2017-05-09 12:12:11 | [diff] [blame] | 65 | const std::string& url, |
| 66 | const net::NetworkTrafficAnnotationTag& traffic_annotation) = 0; |
sergeyu | 9b3d516c | 2016-02-11 20:58:09 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } // namespace remoting |
| 70 | |
| 71 | #endif // REMOTING_BASE_URL_REQUEST_H_ |