node_stream_loader.h 3.2 KB

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