print_preview_message_handler.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2012 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. #include "chrome/browser/printing/print_preview_message_handler.h"
  5. #include "base/bind.h"
  6. #include "base/memory/shared_memory.h"
  7. #include "chrome/browser/browser_process.h"
  8. #include "chrome/browser/printing/print_job_manager.h"
  9. #include "chrome/browser/printing/printer_query.h"
  10. #include "chrome/common/print_messages.h"
  11. #include "content/public/browser/browser_thread.h"
  12. #include "content/public/browser/render_frame_host.h"
  13. #include "content/public/browser/render_view_host.h"
  14. #include "content/public/browser/web_contents.h"
  15. #include "printing/page_size_margins.h"
  16. #include "printing/pdf_metafile_skia.h"
  17. #include "printing/print_job_constants.h"
  18. #include "atom/common/node_includes.h"
  19. using content::BrowserThread;
  20. using content::WebContents;
  21. DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::PrintPreviewMessageHandler);
  22. namespace {
  23. void StopWorker(int document_cookie) {
  24. if (document_cookie <= 0)
  25. return;
  26. scoped_refptr<printing::PrintQueriesQueue> queue =
  27. g_browser_process->print_job_manager()->queue();
  28. scoped_refptr<printing::PrinterQuery> printer_query =
  29. queue->PopPrinterQuery(document_cookie);
  30. if (printer_query.get()) {
  31. BrowserThread::PostTask(
  32. BrowserThread::IO, FROM_HERE,
  33. base::Bind(&printing::PrinterQuery::StopWorker, printer_query));
  34. }
  35. }
  36. char* CopyPDFDataOnIOThread(
  37. const PrintHostMsg_DidPreviewDocument_Params& params) {
  38. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  39. std::unique_ptr<base::SharedMemory> shared_buf(
  40. new base::SharedMemory(params.metafile_data_handle, true));
  41. if (!shared_buf->Map(params.data_size))
  42. return nullptr;
  43. char* pdf_data = new char[params.data_size];
  44. memcpy(pdf_data, shared_buf->memory(), params.data_size);
  45. return pdf_data;
  46. }
  47. void FreeNodeBufferData(char* data, void* hint) {
  48. delete[] data;
  49. }
  50. } // namespace
  51. namespace printing {
  52. PrintPreviewMessageHandler::PrintPreviewMessageHandler(
  53. WebContents* web_contents)
  54. : content::WebContentsObserver(web_contents) {
  55. DCHECK(web_contents);
  56. }
  57. PrintPreviewMessageHandler::~PrintPreviewMessageHandler() {}
  58. void PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
  59. const PrintHostMsg_DidPreviewDocument_Params& params) {
  60. // Always try to stop the worker.
  61. StopWorker(params.document_cookie);
  62. if (params.expected_pages_count <= 0) {
  63. NOTREACHED();
  64. return;
  65. }
  66. BrowserThread::PostTaskAndReplyWithResult(
  67. BrowserThread::IO, FROM_HERE, base::Bind(&CopyPDFDataOnIOThread, params),
  68. base::Bind(&PrintPreviewMessageHandler::RunPrintToPDFCallback,
  69. base::Unretained(this), params.preview_request_id,
  70. params.data_size));
  71. }
  72. void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie,
  73. int request_id) {
  74. StopWorker(document_cookie);
  75. RunPrintToPDFCallback(request_id, 0, nullptr);
  76. }
  77. bool PrintPreviewMessageHandler::OnMessageReceived(
  78. const IPC::Message& message,
  79. content::RenderFrameHost* render_frame_host) {
  80. bool handled = true;
  81. IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message)
  82. IPC_MESSAGE_HANDLER(PrintHostMsg_MetafileReadyForPrinting,
  83. OnMetafileReadyForPrinting)
  84. IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed, OnPrintPreviewFailed)
  85. IPC_MESSAGE_UNHANDLED(handled = false)
  86. IPC_END_MESSAGE_MAP()
  87. return handled;
  88. }
  89. void PrintPreviewMessageHandler::PrintToPDF(
  90. const base::DictionaryValue& options,
  91. const atom::api::WebContents::PrintToPDFCallback& callback) {
  92. int request_id;
  93. options.GetInteger(printing::kPreviewRequestID, &request_id);
  94. print_to_pdf_callback_map_[request_id] = callback;
  95. content::RenderFrameHost* rfh = web_contents()->GetMainFrame();
  96. rfh->Send(new PrintMsg_PrintPreview(rfh->GetRoutingID(), options));
  97. }
  98. void PrintPreviewMessageHandler::RunPrintToPDFCallback(int request_id,
  99. uint32_t data_size,
  100. char* data) {
  101. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  102. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  103. v8::Locker locker(isolate);
  104. v8::HandleScope handle_scope(isolate);
  105. if (data) {
  106. v8::Local<v8::Value> buffer =
  107. node::Buffer::New(isolate, data, static_cast<size_t>(data_size),
  108. &FreeNodeBufferData, nullptr)
  109. .ToLocalChecked();
  110. print_to_pdf_callback_map_[request_id].Run(v8::Null(isolate), buffer);
  111. } else {
  112. v8::Local<v8::String> error_message =
  113. v8::String::NewFromUtf8(isolate, "Failed to generate PDF");
  114. print_to_pdf_callback_map_[request_id].Run(
  115. v8::Exception::Error(error_message), v8::Null(isolate));
  116. }
  117. print_to_pdf_callback_map_.erase(request_id);
  118. }
  119. } // namespace printing