зеркало из https://github.com/electron/electron.git
test: add -pdf-ready-to-print event to WebContents for testing (#43436)
This commit is contained in:
Родитель
9ce0ca74c3
Коммит
c1eee18e41
1
BUILD.gn
1
BUILD.gn
|
@ -733,6 +733,7 @@ source_set("electron_lib") {
|
||||||
"//components/pdf/common:util",
|
"//components/pdf/common:util",
|
||||||
"//components/pdf/renderer",
|
"//components/pdf/renderer",
|
||||||
"//pdf",
|
"//pdf",
|
||||||
|
"//pdf:content_restriction",
|
||||||
]
|
]
|
||||||
sources += [
|
sources += [
|
||||||
"shell/browser/electron_pdf_document_helper_client.cc",
|
"shell/browser/electron_pdf_document_helper_client.cc",
|
||||||
|
|
|
@ -3784,6 +3784,10 @@ void WebContents::SetBackgroundColor(std::optional<SkColor> maybe_color) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebContents::PDFReadyToPrint() {
|
||||||
|
Emit("-pdf-ready-to-print");
|
||||||
|
}
|
||||||
|
|
||||||
void WebContents::OnInputEvent(const blink::WebInputEvent& event) {
|
void WebContents::OnInputEvent(const blink::WebInputEvent& event) {
|
||||||
Emit("input-event", event);
|
Emit("input-event", event);
|
||||||
}
|
}
|
||||||
|
|
|
@ -492,6 +492,8 @@ class WebContents : public ExclusiveAccessContext,
|
||||||
|
|
||||||
void SetBackgroundColor(std::optional<SkColor> color);
|
void SetBackgroundColor(std::optional<SkColor> color);
|
||||||
|
|
||||||
|
void PDFReadyToPrint();
|
||||||
|
|
||||||
SkRegion* draggable_region() {
|
SkRegion* draggable_region() {
|
||||||
return force_non_draggable_ ? nullptr : draggable_region_.get();
|
return force_non_draggable_ ? nullptr : draggable_region_.get();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,29 @@
|
||||||
#include "shell/browser/electron_pdf_document_helper_client.h"
|
#include "shell/browser/electron_pdf_document_helper_client.h"
|
||||||
|
|
||||||
#include "content/public/browser/web_contents.h"
|
#include "content/public/browser/web_contents.h"
|
||||||
|
#include "pdf/content_restriction.h"
|
||||||
|
#include "shell/browser/api/electron_api_web_contents.h"
|
||||||
|
|
||||||
ElectronPDFDocumentHelperClient::ElectronPDFDocumentHelperClient() = default;
|
ElectronPDFDocumentHelperClient::ElectronPDFDocumentHelperClient() = default;
|
||||||
ElectronPDFDocumentHelperClient::~ElectronPDFDocumentHelperClient() = default;
|
ElectronPDFDocumentHelperClient::~ElectronPDFDocumentHelperClient() = default;
|
||||||
|
|
||||||
|
void ElectronPDFDocumentHelperClient::UpdateContentRestrictions(
|
||||||
|
content::RenderFrameHost* render_frame_host,
|
||||||
|
int content_restrictions) {
|
||||||
|
// UpdateContentRestrictions potentially gets called twice from
|
||||||
|
// pdf/pdf_view_web_plugin.cc. The first time it is potentially called is
|
||||||
|
// when loading starts and it is called with a restriction on printing. The
|
||||||
|
// second time it is called is when loading is finished and if printing is
|
||||||
|
// allowed there won't be a printing restriction passed, so we can use this
|
||||||
|
// second call to notify that the pdf document is ready to print.
|
||||||
|
if (!(content_restrictions & chrome_pdf::kContentRestrictionPrint)) {
|
||||||
|
content::WebContents* web_contents =
|
||||||
|
content::WebContents::FromRenderFrameHost(render_frame_host);
|
||||||
|
electron::api::WebContents* api_web_contents =
|
||||||
|
electron::api::WebContents::From(
|
||||||
|
web_contents->GetOutermostWebContents());
|
||||||
|
if (api_web_contents) {
|
||||||
|
api_web_contents->PDFReadyToPrint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -17,8 +17,9 @@ class ElectronPDFDocumentHelperClient : public pdf::PDFDocumentHelperClient {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// pdf::PDFDocumentHelperClient
|
// pdf::PDFDocumentHelperClient
|
||||||
|
|
||||||
void UpdateContentRestrictions(content::RenderFrameHost* render_frame_host,
|
void UpdateContentRestrictions(content::RenderFrameHost* render_frame_host,
|
||||||
int content_restrictions) override {}
|
int content_restrictions) override;
|
||||||
void OnPDFHasUnsupportedFeature(content::WebContents* contents) override {}
|
void OnPDFHasUnsupportedFeature(content::WebContents* contents) override {}
|
||||||
void OnSaveURL(content::WebContents* contents) override {}
|
void OnSaveURL(content::WebContents* contents) override {}
|
||||||
void SetPluginCanSave(content::RenderFrameHost* render_frame_host,
|
void SetPluginCanSave(content::RenderFrameHost* render_frame_host,
|
||||||
|
|
|
@ -2450,13 +2450,9 @@ describe('webContents module', () => {
|
||||||
|
|
||||||
it('from an existing pdf document', async () => {
|
it('from an existing pdf document', async () => {
|
||||||
const pdfPath = path.join(fixturesPath, 'cat.pdf');
|
const pdfPath = path.join(fixturesPath, 'cat.pdf');
|
||||||
|
const readyToPrint = once(w.webContents, '-pdf-ready-to-print');
|
||||||
await w.loadFile(pdfPath);
|
await w.loadFile(pdfPath);
|
||||||
|
await readyToPrint;
|
||||||
// TODO(codebytere): the PDF plugin is not always ready immediately
|
|
||||||
// after the document is loaded, so we need to wait for it to be ready.
|
|
||||||
// We should find a better way to do this.
|
|
||||||
await setTimeout(3000);
|
|
||||||
|
|
||||||
const data = await w.webContents.printToPDF({});
|
const data = await w.webContents.printToPDF({});
|
||||||
const doc = await pdfjs.getDocument(data).promise;
|
const doc = await pdfjs.getDocument(data).promise;
|
||||||
expect(doc.numPages).to.equal(2);
|
expect(doc.numPages).to.equal(2);
|
||||||
|
|
Загрузка…
Ссылка в новой задаче