print_preview_message_handler.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2018 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_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
  5. #define SHELL_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
  6. #include <map>
  7. #include "base/memory/ref_counted_memory.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "components/printing/common/print.mojom.h"
  10. #include "components/services/print_compositor/public/mojom/print_compositor.mojom.h"
  11. #include "content/public/browser/web_contents_observer.h"
  12. #include "content/public/browser/web_contents_user_data.h"
  13. #include "mojo/public/cpp/bindings/associated_remote.h"
  14. #include "shell/common/gin_helper/promise.h"
  15. #include "v8/include/v8.h"
  16. struct PrintHostMsg_DidPreviewDocument_Params;
  17. struct PrintHostMsg_PreviewIds;
  18. namespace content {
  19. class RenderFrameHost;
  20. }
  21. namespace electron {
  22. // Manages the print preview handling for a WebContents.
  23. class PrintPreviewMessageHandler
  24. : public content::WebContentsObserver,
  25. public content::WebContentsUserData<PrintPreviewMessageHandler> {
  26. public:
  27. ~PrintPreviewMessageHandler() override;
  28. void PrintToPDF(base::DictionaryValue options,
  29. gin_helper::Promise<v8::Local<v8::Value>> promise);
  30. protected:
  31. // content::WebContentsObserver implementation.
  32. bool OnMessageReceived(const IPC::Message& message,
  33. content::RenderFrameHost* render_frame_host) override;
  34. private:
  35. friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
  36. explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
  37. void OnMetafileReadyForPrinting(
  38. content::RenderFrameHost* render_frame_host,
  39. const PrintHostMsg_DidPreviewDocument_Params& params,
  40. const PrintHostMsg_PreviewIds& ids);
  41. void OnCompositePdfDocumentDone(
  42. const PrintHostMsg_PreviewIds& ids,
  43. printing::mojom::PrintCompositor::Status status,
  44. base::ReadOnlySharedMemoryRegion region);
  45. void OnPrintPreviewFailed(int document_cookie,
  46. const PrintHostMsg_PreviewIds& ids);
  47. void OnPrintPreviewCancelled(int document_cookie,
  48. const PrintHostMsg_PreviewIds& ids);
  49. gin_helper::Promise<v8::Local<v8::Value>> GetPromise(int request_id);
  50. void ResolvePromise(int request_id,
  51. scoped_refptr<base::RefCountedMemory> data_bytes);
  52. void RejectPromise(int request_id);
  53. using PromiseMap = std::map<int, gin_helper::Promise<v8::Local<v8::Value>>>;
  54. PromiseMap promise_map_;
  55. mojo::AssociatedRemote<printing::mojom::PrintRenderFrame> print_render_frame_;
  56. base::WeakPtrFactory<PrintPreviewMessageHandler> weak_ptr_factory_;
  57. WEB_CONTENTS_USER_DATA_KEY_DECL();
  58. DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);
  59. };
  60. } // namespace electron
  61. #endif // SHELL_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_