2018-11-09 03:42:34 +00:00
|
|
|
// Copyright (c) 2018 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
|
|
|
|
#define ELECTRON_SHELL_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
|
2018-11-09 03:42:34 +00:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "base/memory/ref_counted_memory.h"
|
|
|
|
#include "base/memory/weak_ptr.h"
|
2020-01-17 18:41:52 +00:00
|
|
|
#include "components/printing/common/print.mojom.h"
|
|
|
|
#include "components/services/print_compositor/public/mojom/print_compositor.mojom.h"
|
2018-11-09 03:42:34 +00:00
|
|
|
#include "content/public/browser/web_contents_user_data.h"
|
2020-05-26 20:06:26 +00:00
|
|
|
#include "mojo/public/cpp/bindings/associated_receiver.h"
|
2020-01-17 18:41:52 +00:00
|
|
|
#include "mojo/public/cpp/bindings/associated_remote.h"
|
2021-06-22 19:17:16 +00:00
|
|
|
#include "printing/mojom/print.mojom.h"
|
2019-11-01 06:10:32 +00:00
|
|
|
#include "shell/common/gin_helper/promise.h"
|
2018-11-09 03:42:34 +00:00
|
|
|
#include "v8/include/v8.h"
|
|
|
|
|
|
|
|
namespace content {
|
|
|
|
class RenderFrameHost;
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2018-11-09 03:42:34 +00:00
|
|
|
|
|
|
|
// Manages the print preview handling for a WebContents.
|
|
|
|
class PrintPreviewMessageHandler
|
2021-02-09 20:16:21 +00:00
|
|
|
: public printing::mojom::PrintPreviewUI,
|
2018-11-09 03:42:34 +00:00
|
|
|
public content::WebContentsUserData<PrintPreviewMessageHandler> {
|
|
|
|
public:
|
|
|
|
~PrintPreviewMessageHandler() override;
|
|
|
|
|
2021-11-03 11:41:45 +00:00
|
|
|
// disable copy
|
|
|
|
PrintPreviewMessageHandler(const PrintPreviewMessageHandler&) = delete;
|
|
|
|
PrintPreviewMessageHandler& operator=(const PrintPreviewMessageHandler&) =
|
|
|
|
delete;
|
|
|
|
|
2019-10-30 05:30:59 +00:00
|
|
|
void PrintToPDF(base::DictionaryValue options,
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise);
|
2018-11-09 03:42:34 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
|
|
|
|
|
|
|
|
explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
|
|
|
|
|
2020-07-14 01:13:34 +00:00
|
|
|
void OnCompositeDocumentToPdfDone(
|
2021-02-09 20:16:21 +00:00
|
|
|
int32_t request_id,
|
2020-01-17 18:41:52 +00:00
|
|
|
printing::mojom::PrintCompositor::Status status,
|
|
|
|
base::ReadOnlySharedMemoryRegion region);
|
2020-07-14 01:13:34 +00:00
|
|
|
void OnPrepareForDocumentToPdfDone(
|
2021-02-09 20:16:21 +00:00
|
|
|
int32_t request_id,
|
2020-07-14 01:13:34 +00:00
|
|
|
printing::mojom::PrintCompositor::Status status);
|
|
|
|
void OnCompositePdfPageDone(int page_number,
|
|
|
|
int document_cookie,
|
2021-02-09 20:16:21 +00:00
|
|
|
int32_t request_id,
|
2020-07-14 01:13:34 +00:00
|
|
|
printing::mojom::PrintCompositor::Status status,
|
|
|
|
base::ReadOnlySharedMemoryRegion region);
|
2020-05-26 20:06:26 +00:00
|
|
|
|
|
|
|
// printing::mojo::PrintPreviewUI:
|
|
|
|
void SetOptionsFromDocument(
|
|
|
|
const printing::mojom::OptionsFromDocumentParamsPtr params,
|
|
|
|
int32_t request_id) override {}
|
|
|
|
void PrintPreviewFailed(int32_t document_cookie, int32_t request_id) override;
|
|
|
|
void PrintPreviewCancelled(int32_t document_cookie,
|
|
|
|
int32_t request_id) override;
|
|
|
|
void PrinterSettingsInvalid(int32_t document_cookie,
|
|
|
|
int32_t request_id) override {}
|
2021-02-09 20:16:21 +00:00
|
|
|
void DidPrepareDocumentForPreview(int32_t document_cookie,
|
|
|
|
int32_t request_id) override;
|
|
|
|
void DidPreviewPage(printing::mojom::DidPreviewPageParamsPtr params,
|
|
|
|
int32_t request_id) override;
|
|
|
|
void MetafileReadyForPrinting(
|
|
|
|
printing::mojom::DidPreviewDocumentParamsPtr params,
|
|
|
|
int32_t request_id) override;
|
|
|
|
void DidGetDefaultPageLayout(
|
|
|
|
printing::mojom::PageSizeMarginsPtr page_layout_in_points,
|
|
|
|
const gfx::Rect& printable_area_in_points,
|
|
|
|
bool has_custom_page_size_style,
|
|
|
|
int32_t request_id) override {}
|
|
|
|
void DidStartPreview(printing::mojom::DidStartPreviewParamsPtr params,
|
|
|
|
int32_t request_id) override {}
|
2018-11-09 03:42:34 +00:00
|
|
|
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<v8::Local<v8::Value>> GetPromise(int request_id);
|
2019-02-11 19:20:04 +00:00
|
|
|
|
|
|
|
void ResolvePromise(int request_id,
|
|
|
|
scoped_refptr<base::RefCountedMemory> data_bytes);
|
|
|
|
void RejectPromise(int request_id);
|
|
|
|
|
2019-11-01 06:10:32 +00:00
|
|
|
using PromiseMap = std::map<int, gin_helper::Promise<v8::Local<v8::Value>>>;
|
2019-02-11 19:20:04 +00:00
|
|
|
PromiseMap promise_map_;
|
2018-11-09 03:42:34 +00:00
|
|
|
|
2022-01-10 22:31:39 +00:00
|
|
|
// TODO(clavin): refactor to use the WebContents provided by the
|
|
|
|
// WebContentsUserData base class instead of storing a duplicate ref
|
2021-02-09 20:16:21 +00:00
|
|
|
content::WebContents* web_contents_ = nullptr;
|
|
|
|
|
2020-01-17 18:41:52 +00:00
|
|
|
mojo::AssociatedRemote<printing::mojom::PrintRenderFrame> print_render_frame_;
|
|
|
|
|
2020-05-26 20:06:26 +00:00
|
|
|
mojo::AssociatedReceiver<printing::mojom::PrintPreviewUI> receiver_{this};
|
|
|
|
|
2021-01-26 18:16:21 +00:00
|
|
|
base::WeakPtrFactory<PrintPreviewMessageHandler> weak_ptr_factory_{this};
|
2018-11-09 03:42:34 +00:00
|
|
|
|
2019-01-21 16:56:54 +00:00
|
|
|
WEB_CONTENTS_USER_DATA_KEY_DECL();
|
2018-11-09 03:42:34 +00:00
|
|
|
};
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2018-11-09 03:42:34 +00:00
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
|