plugin_response_writer.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_PDF_BROWSER_PLUGIN_RESPONSE_WRITER_H_
  5. #define COMPONENTS_PDF_BROWSER_PLUGIN_RESPONSE_WRITER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback_forward.h"
  9. #include "components/pdf/browser/pdf_stream_delegate.h"
  10. #include "mojo/public/c/system/types.h"
  11. #include "mojo/public/cpp/bindings/pending_remote.h"
  12. #include "mojo/public/cpp/bindings/remote.h"
  13. #include "services/network/public/mojom/url_loader.mojom.h"
  14. namespace mojo {
  15. class DataPipeProducer;
  16. } // namespace mojo
  17. namespace pdf {
  18. // Writes out a response containing an <embed> with type
  19. // "application/x-google-chrome-pdf". `PdfURLLoaderRequestInterceptor` replaces
  20. // PDF loads with this response in order to trigger loading `source_url` with
  21. // `chrome_pdf::PdfViewWebPlugin` instead.
  22. class PluginResponseWriter final {
  23. public:
  24. PluginResponseWriter(
  25. const PdfStreamDelegate::StreamInfo& stream_info,
  26. mojo::PendingRemote<network::mojom::URLLoaderClient> client);
  27. PluginResponseWriter(const PluginResponseWriter&) = delete;
  28. PluginResponseWriter& operator=(const PluginResponseWriter&) = delete;
  29. ~PluginResponseWriter();
  30. // Starts sending the response, calling `done_callback` once the entire
  31. // response is sent to (but not necessarily received by) the
  32. // `URLLoaderClient`.
  33. //
  34. // Caller is responsible for keeping this response writer alive until
  35. // `done_callback` is called.
  36. void Start(base::OnceClosure done_callback);
  37. private:
  38. void OnWrite(base::OnceClosure done_callback, MojoResult result);
  39. std::string body_;
  40. mojo::Remote<network::mojom::URLLoaderClient> client_;
  41. std::unique_ptr<mojo::DataPipeProducer> producer_;
  42. };
  43. } // namespace pdf
  44. #endif // COMPONENTS_PDF_BROWSER_PLUGIN_RESPONSE_WRITER_H_