url_pipe_loader.h 3.0 KB

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