url_request.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #ifndef REMOTING_BASE_URL_REQUEST_H_
  5. #define REMOTING_BASE_URL_REQUEST_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback_forward.h"
  9. #include "net/traffic_annotation/network_traffic_annotation.h"
  10. namespace remoting {
  11. // Abstract interface for URL requests.
  12. class UrlRequest {
  13. public:
  14. enum class Type {
  15. GET,
  16. POST,
  17. };
  18. struct Result {
  19. Result() = default;
  20. Result(int status, std::string response_body)
  21. : success(true), status(status), response_body(response_body) {}
  22. static Result Failed() { return Result(); }
  23. // Set to true when the URL has been fetched successfully.
  24. bool success = false;
  25. // HTTP status code received from the server. Valid only when |success| is
  26. // set to true.
  27. int status = 0;
  28. // Body of the response received from the server. Valid only when |success|
  29. // is set to true.
  30. std::string response_body;
  31. };
  32. typedef base::OnceCallback<void(const Result& result)> OnResultCallback;
  33. virtual ~UrlRequest() {}
  34. // Adds an HTTP header to the request. Has no effect if called after Start().
  35. virtual void AddHeader(const std::string& value) = 0;
  36. // Sets data to be sent for POST requests.
  37. virtual void SetPostData(const std::string& content_type,
  38. const std::string& post_data) = 0;
  39. // Sends a request to the server. |on_response_callback| will be called to
  40. // return result of the request.
  41. virtual void Start(OnResultCallback on_result_callback) = 0;
  42. };
  43. // Factory for UrlRequest instances.
  44. class UrlRequestFactory {
  45. public:
  46. virtual ~UrlRequestFactory() {}
  47. virtual std::unique_ptr<UrlRequest> CreateUrlRequest(
  48. UrlRequest::Type type,
  49. const std::string& url,
  50. const net::NetworkTrafficAnnotationTag& traffic_annotation) = 0;
  51. };
  52. } // namespace remoting
  53. #endif // REMOTING_BASE_URL_REQUEST_H_