protobuf_http_stream_request.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2020 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_PROTOBUF_HTTP_STREAM_REQUEST_H_
  5. #define REMOTING_BASE_PROTOBUF_HTTP_STREAM_REQUEST_H_
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/timer/timer.h"
  10. #include "remoting/base/protobuf_http_request_base.h"
  11. #include "services/network/public/cpp/simple_url_loader_stream_consumer.h"
  12. namespace google {
  13. namespace protobuf {
  14. class MessageLite;
  15. } // namespace protobuf
  16. } // namespace google
  17. namespace remoting {
  18. class ProtobufHttpClient;
  19. class ProtobufHttpStatus;
  20. class ProtobufHttpStreamParser;
  21. // A server streaming request.
  22. class ProtobufHttpStreamRequest final
  23. : public ProtobufHttpRequestBase,
  24. public network::SimpleURLLoaderStreamConsumer {
  25. public:
  26. template <typename MessageType>
  27. using MessageCallback =
  28. base::RepeatingCallback<void(std::unique_ptr<MessageType> message)>;
  29. using StreamClosedCallback =
  30. base::OnceCallback<void(const ProtobufHttpStatus& status)>;
  31. static constexpr base::TimeDelta kStreamReadyTimeoutDuration =
  32. base::Seconds(30);
  33. explicit ProtobufHttpStreamRequest(
  34. std::unique_ptr<ProtobufHttpRequestConfig> config);
  35. ~ProtobufHttpStreamRequest() override;
  36. // Sets a callback that gets called when the stream is ready to receive data.
  37. void SetStreamReadyCallback(base::OnceClosure callback);
  38. // Sets a callback that gets called when the stream is closed.
  39. void SetStreamClosedCallback(StreamClosedCallback callback);
  40. // Sets the callback to be called every time a new message is received.
  41. // |MessageType| needs to be a protobuf message type.
  42. template <typename MessageType>
  43. void SetMessageCallback(const MessageCallback<MessageType>& callback) {
  44. default_message_ = &MessageType::default_instance();
  45. message_callback_ = base::BindRepeating(
  46. [](MessageCallback<MessageType> callback,
  47. std::unique_ptr<google::protobuf::MessageLite> generic_message) {
  48. std::move(callback).Run(std::unique_ptr<MessageType>(
  49. static_cast<MessageType*>(generic_message.release())));
  50. },
  51. callback);
  52. }
  53. private:
  54. friend class ProtobufHttpClient;
  55. // ProtobufHttpStreamParser callbacks.
  56. void OnMessage(const std::string& message);
  57. void OnStreamClosed(const ProtobufHttpStatus& status);
  58. // ProtobufHttpRequestBase implementations.
  59. void OnAuthFailed(const ProtobufHttpStatus& status) override;
  60. void StartRequestInternal(
  61. network::mojom::URLLoaderFactory* loader_factory) override;
  62. base::TimeDelta GetRequestTimeoutDuration() const override;
  63. // network::SimpleURLLoaderStreamConsumer implementations.
  64. void OnDataReceived(base::StringPiece string_piece,
  65. base::OnceClosure resume) override;
  66. void OnComplete(bool success) override;
  67. void OnRetry(base::OnceClosure start_retry) override;
  68. void OnStreamReadyTimeout();
  69. // Used to create new response message instances.
  70. raw_ptr<const google::protobuf::MessageLite> default_message_;
  71. std::unique_ptr<ProtobufHttpStreamParser> stream_parser_;
  72. base::OneShotTimer stream_ready_timeout_timer_;
  73. base::OnceClosure stream_ready_callback_;
  74. StreamClosedCallback stream_closed_callback_;
  75. base::RepeatingCallback<void(std::unique_ptr<google::protobuf::MessageLite>)>
  76. message_callback_;
  77. };
  78. } // namespace remoting
  79. #endif // REMOTING_BASE_PROTOBUF_HTTP_STREAM_REQUEST_H_