node_stream_loader.h 3.5 KB

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