url_pipe_loader.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_URL_PIPE_LOADER_H_
  5. #define SHELL_BROWSER_NET_URL_PIPE_LOADER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "mojo/public/cpp/bindings/receiver.h"
  10. #include "mojo/public/cpp/bindings/remote.h"
  11. #include "mojo/public/cpp/bindings/strong_binding.h"
  12. #include "mojo/public/cpp/system/data_pipe_producer.h"
  13. #include "services/network/public/cpp/simple_url_loader.h"
  14. #include "services/network/public/cpp/simple_url_loader_stream_consumer.h"
  15. #include "services/network/public/mojom/url_loader.mojom.h"
  16. namespace network {
  17. class SharedURLLoaderFactory;
  18. }
  19. namespace electron {
  20. // Read data from URL and pipe it to NetworkService.
  21. //
  22. // Different from creating a new loader for the URL directly, protocol handlers
  23. // using this loader can work around CORS restrictions.
  24. //
  25. // This class manages its own lifetime and should delete itself when the
  26. // connection is lost or finished.
  27. class URLPipeLoader : public network::mojom::URLLoader,
  28. public network::SimpleURLLoaderStreamConsumer {
  29. public:
  30. URLPipeLoader(scoped_refptr<network::SharedURLLoaderFactory> factory,
  31. std::unique_ptr<network::ResourceRequest> request,
  32. network::mojom::URLLoaderRequest loader,
  33. mojo::PendingRemote<network::mojom::URLLoaderClient> client,
  34. const net::NetworkTrafficAnnotationTag& annotation,
  35. base::DictionaryValue upload_data);
  36. private:
  37. ~URLPipeLoader() override;
  38. void Start(scoped_refptr<network::SharedURLLoaderFactory> factory,
  39. std::unique_ptr<network::ResourceRequest> request,
  40. const net::NetworkTrafficAnnotationTag& annotation,
  41. base::DictionaryValue upload_data);
  42. void NotifyComplete(int result);
  43. void OnResponseStarted(const GURL& final_url,
  44. const network::mojom::URLResponseHead& response_head);
  45. void OnWrite(base::OnceClosure resume, MojoResult result);
  46. // SimpleURLLoaderStreamConsumer:
  47. void OnDataReceived(base::StringPiece string_piece,
  48. base::OnceClosure resume) override;
  49. void OnComplete(bool success) override;
  50. void OnRetry(base::OnceClosure start_retry) override;
  51. // URLLoader:
  52. void FollowRedirect(const std::vector<std::string>& removed_headers,
  53. const net::HttpRequestHeaders& modified_headers,
  54. const base::Optional<GURL>& new_url) override {}
  55. void SetPriority(net::RequestPriority priority,
  56. int32_t intra_priority_value) override {}
  57. void PauseReadingBodyFromNet() override {}
  58. void ResumeReadingBodyFromNet() override {}
  59. mojo::Binding<network::mojom::URLLoader> binding_;
  60. mojo::Remote<network::mojom::URLLoaderClient> client_;
  61. std::unique_ptr<mojo::DataPipeProducer> producer_;
  62. std::unique_ptr<network::SimpleURLLoader> loader_;
  63. base::WeakPtrFactory<URLPipeLoader> weak_factory_;
  64. DISALLOW_COPY_AND_ASSIGN(URLPipeLoader);
  65. };
  66. } // namespace electron
  67. #endif // SHELL_BROWSER_NET_URL_PIPE_LOADER_H_