node_stream_loader.h 3.8 KB

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