blob: 70133f3f7248287786258e20b3316d6981265808 [file] [log] [blame]
sergeyu9b3d516c2016-02-11 20:58:091// 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
dcheng0765c492016-04-06 22:41:538#include <memory>
sergeyu9b3d516c2016-02-11 20:58:099#include <string>
10
11#include "base/callback_forward.h"
rhalavati6826b7b2017-05-09 12:12:1112#include "net/traffic_annotation/network_traffic_annotation.h"
sergeyu9b3d516c2016-02-11 20:58:0913
14namespace remoting {
15
16// Abstract interface for URL requests.
17class UrlRequest {
18 public:
sergeyua79277c92016-02-18 20:15:2719 enum class Type {
20 GET,
21 POST,
22 };
23
sergeyu9b3d516c2016-02-11 20:58:0924 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 Cai74286442020-02-28 18:24:4643 typedef base::OnceCallback<void(const Result& result)> OnResultCallback;
sergeyu9b3d516c2016-02-11 20:58:0944
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
sergeyua79277c92016-02-18 20:15:2750 // 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
sergeyu9b3d516c2016-02-11 20:58:0954 // Sends a request to the server. |on_response_callback| will be called to
55 // return result of the request.
Jun Cai74286442020-02-28 18:24:4656 virtual void Start(OnResultCallback on_result_callback) = 0;
sergeyu9b3d516c2016-02-11 20:58:0957};
58
59// Factory for UrlRequest instances.
60class UrlRequestFactory {
61 public:
62 virtual ~UrlRequestFactory() {}
dcheng0765c492016-04-06 22:41:5363 virtual std::unique_ptr<UrlRequest> CreateUrlRequest(
64 UrlRequest::Type type,
rhalavati6826b7b2017-05-09 12:12:1165 const std::string& url,
66 const net::NetworkTrafficAnnotationTag& traffic_annotation) = 0;
sergeyu9b3d516c2016-02-11 20:58:0967};
68
69} // namespace remoting
70
71#endif // REMOTING_BASE_URL_REQUEST_H_