node_stream_loader.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2019 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_NET_NODE_STREAM_LOADER_H_
  5. #define ELECTRON_SHELL_BROWSER_NET_NODE_STREAM_LOADER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "mojo/public/cpp/bindings/receiver.h"
  12. #include "mojo/public/cpp/bindings/remote.h"
  13. #include "services/network/public/mojom/url_loader.mojom.h"
  14. #include "services/network/public/mojom/url_response_head.mojom.h"
  15. #include "v8/include/v8-forward.h"
  16. #include "v8/include/v8-object.h"
  17. #include "v8/include/v8-persistent-handle.h"
  18. namespace mojo {
  19. class DataPipeProducer;
  20. template <typename T>
  21. class PendingReceiver;
  22. template <typename T>
  23. class PendingRemote;
  24. } // namespace mojo
  25. namespace electron {
  26. // Read data from node Stream and feed it to NetworkService.
  27. //
  28. // This class manages its own lifetime and should delete itself when the
  29. // connection is lost or finished.
  30. //
  31. // We use |paused mode| to read data from |Readable| stream, so we don't need to
  32. // copy data from buffer and hold it in memory, and we only need to make sure
  33. // the passed |Buffer| is alive while writing data to pipe.
  34. class NodeStreamLoader : public network::mojom::URLLoader {
  35. public:
  36. NodeStreamLoader(network::mojom::URLResponseHeadPtr head,
  37. mojo::PendingReceiver<network::mojom::URLLoader> loader,
  38. mojo::PendingRemote<network::mojom::URLLoaderClient> client,
  39. v8::Isolate* isolate,
  40. v8::Local<v8::Object> emitter);
  41. // disable copy
  42. NodeStreamLoader(const NodeStreamLoader&) = delete;
  43. NodeStreamLoader& operator=(const NodeStreamLoader&) = delete;
  44. private:
  45. ~NodeStreamLoader() override;
  46. using EventCallback = base::RepeatingCallback<void()>;
  47. void Start(network::mojom::URLResponseHeadPtr head);
  48. void NotifyEnd();
  49. void NotifyError();
  50. void NotifyReadable();
  51. void NotifyComplete(int result);
  52. void ReadMore();
  53. void DidWrite(MojoResult result);
  54. // Subscribe to events of |emitter|.
  55. void On(const char* event, EventCallback callback);
  56. // URLLoader:
  57. void FollowRedirect(
  58. const std::vector<std::string>& removed_headers,
  59. const net::HttpRequestHeaders& modified_headers,
  60. const net::HttpRequestHeaders& modified_cors_exempt_headers,
  61. const std::optional<GURL>& new_url) override {}
  62. void SetPriority(net::RequestPriority priority,
  63. int32_t intra_priority_value) override {}
  64. mojo::Receiver<network::mojom::URLLoader> url_loader_;
  65. mojo::Remote<network::mojom::URLLoaderClient> client_;
  66. raw_ptr<v8::Isolate> isolate_;
  67. v8::Global<v8::Object> emitter_;
  68. v8::Global<v8::Value> buffer_;
  69. // Mojo data pipe where the data that is being read is written to.
  70. std::unique_ptr<mojo::DataPipeProducer> producer_;
  71. // Whether we are in the middle of write.
  72. bool is_writing_ = false;
  73. // Whether we are in the middle of a stream.read().
  74. bool is_reading_ = false;
  75. size_t bytes_written_ = 0;
  76. // When NotifyComplete is called while writing, we will save the result and
  77. // quit with it after the write is done.
  78. bool pending_result_ = false;
  79. int result_ = net::OK;
  80. // Set to `true` when we get either `end` or `error` event on the stream.
  81. // If `false` - we call `stream.destroy()` to finalize the stream.
  82. bool destroyed_ = false;
  83. // When the stream emits the readable event, we only want to start reading
  84. // data if the stream was not readable before, so we store the state in a
  85. // flag.
  86. bool readable_ = false;
  87. // It's possible for reads to be queued using nextTick() during read()
  88. // which will cause 'readable' to emit during ReadMore, so we track if
  89. // that occurred in a flag.
  90. bool has_read_waiting_ = false;
  91. // Store the V8 callbacks to unsubscribe them later.
  92. std::map<std::string, v8::Global<v8::Value>> handlers_;
  93. base::WeakPtrFactory<NodeStreamLoader> weak_factory_{this};
  94. };
  95. } // namespace electron
  96. #endif // ELECTRON_SHELL_BROWSER_NET_NODE_STREAM_LOADER_H_