Skip to content

Commit

Permalink
Merge pull request #23489 from brave/missing-print-preview-pages
Browse files Browse the repository at this point in the history
Make sure PrintPreviewExtraction emit beforeprint and afterprint correctly
  • Loading branch information
darkdh committed May 9, 2024
2 parents 13a5af7 + 56eb3d0 commit bf88d6a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
6 changes: 5 additions & 1 deletion browser/ui/webui/ai_chat/print_preview_extractor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ void PrintPreviewExtractor::DidGetDefaultPageLayout(
void PrintPreviewExtractor::DidStartPreview(
printing::mojom::DidStartPreviewParamsPtr params,
int32_t request_id) {
DVLOG(3) << __func__ << ": id=" << request_id;
DVLOG(3) << __func__ << ": id=" << request_id
<< " , page count: " << params->page_count;
}

void PrintPreviewExtractor::OnPrepareForDocumentToPdfDone(
Expand Down Expand Up @@ -395,6 +396,9 @@ void PrintPreviewExtractor::PreviewCleanup() {
return;
}
PrintPreviewDataService::GetInstance()->RemoveEntry(*print_preview_ui_id_);
if (!is_pdf_) {
print_render_frame_->OnPrintPreviewDialogClosed();
}
DisconnectPrintPrieviewUI();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ void PrintRenderFrameHelper::InitiatePrintPreview(
print_preview_context_.InitWithNode(plugin);
} else {
print_preview_context_.InitWithFrame(frame);
print_preview_context_.DispatchBeforePrintEvent(
weak_ptr_factory_.GetWeakPtr());
}
print_in_progress_ = false;
// Print Preview resets `print_in_progress_` when the dialog closes.
}

void PrintRenderFrameHelper::SetIsPrintPreviewExtraction(bool value) {
Expand Down
41 changes: 22 additions & 19 deletions test/data/leo/extra_long_canvas.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@
<body>
<script>
// Here we create 21 pages to test kMaxPreviewPages which is 20.
for (let i = 0; i < 21; i++) {
const div = document.createElement('div');
div.style.width = '559px';
div.style.height = '794px';
// These pages will be created only after receiving the beforeprint event.
window.addEventListener('beforeprint', () => {
for (let i = 0; i < 21; i++) {
const div = document.createElement('div');
div.style.width = '559px';
div.style.height = '794px';

const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = 559
canvas.height = 794
context.font = '30px Arial';
if (i == 19) {
// This should be the last page because of the truncation.
context.fillText('This is the way.', 50, 100);
} else if (i == 20) {
// This will be truncated.
context.fillText('There is no way.', 50, 100);
}
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = 559
canvas.height = 794
context.font = '30px Arial';
if (i == 19) {
// This should be the last page because of the truncation.
context.fillText('This is the way.', 50, 100);
} else if (i == 20) {
// This will be truncated.
context.fillText('There is no way.', 50, 100);
}

div.appendChild(canvas);
document.body.appendChild(div);
}
div.appendChild(canvas);
document.body.appendChild(div);
}
});
</script>
</body>
</html>
Expand Down

0 comments on commit bf88d6a

Please sign in to comment.