Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash on print cancellation and silent print settings #19598

Merged
merged 3 commits into from Aug 7, 2019

Conversation

codebytere
Copy link
Member

@codebytere codebytere commented Aug 2, 2019

Description of Change

Fixes #19122.
Fixes #17077.
Fixes #19108.
Fixes #19474.

In an earlier PR, I introduced the ability to set custom print options in webContents.print().

However, this meant that the base::DictionaryValue passed in with these settings would never be null, because if even one option is set than they all have to be set with a default to prevent a DCHECK that gets hit if this function returns false. As a result, only the PrintHostMsg_UpdatePrintSettings IPC message would be sent, and printing would silently fail as no defaults were properly initialized. More specifically, since no defaults were set, when a new PrinterQuery was created it would have no idea about previously existing or defaulted settings, since settings were reset at the beginning of print updates.

This fixes that by changing codepaths to intentionally only send the IPC message to update print settings (PrintHostMsg_UpdatePrintSettings), and then each time pursuing the following codepath:

  1. Reset settings from previous job
  2. Call printing_context_->UseDefaultSettings() in order to set the settings_ member with default settings
  3. Call printing_context_->UpdatePrintSettings() with our new settings to only change the options that were passed in.

These custom settings should work now for both silent and non-silent printing: this can be seeing by calling:

win.webContents.print({ copies: 3 }, (success, reason) => {
    console.log(`printing ${success ? 'did' : 'did not'} succeed: ${reason}`)
})

to see that '3' is automatically placed in the UI settings of the native print dialog.

Secondary Fix:

Due to changes in the lifetime of PrinterQuery, a crash occurred if we posted a task to notify of print cancellation and did not also call the callback. This removes that gating and now will call the callback regardless of cancellation status.

I chose not to put this secondary fix in a standalone PR because the primary issue above meant that it isn't even being surfaced to users as it causes a silent failure before a UI printing dialog is ever shown.

cc @jkleinsc @deepak1556

Checklist

Release Notes

Notes: Fixed a crash on manual print cancellation as well as an issue with deviceName not working.

@codebytere codebytere requested a review from a team as a code owner August 2, 2019 19:09
@electron-cation electron-cation bot added new-pr 🌱 PR opened in the last 24 hours and removed new-pr 🌱 PR opened in the last 24 hours labels Aug 2, 2019
@fleabitten232
Copy link

I'm currently using your changes in a small, heavily print dependent project that needs to silently print to both a standard A4 size/laser printer combination and a notoriously temperamental 6x4inch thermal label printer. I'm happy to share my experiences if you would find that helpful -- just let me know where if here isn't appropriate.

I've now mostly finished moving from electron@latest to electron@beta which broke a label design that was previously working. In latest, it would correctly print landscape (albeit not silently).

In the beta (without any code/css changes), the same label was adamant that it wanted to be portrait. It would print massively scaled down so that the landscape design would fit to the width of a portrait page. This happened with 'silent' sent to both true and false.

I couldn't immediately fix the problem through any print dialog settings and unfortunately I ran out of labels before I could explore deeper. The last thing I noticed was that setting the page to 'landscape' would actually rotate it 180 degrees, rather than 90. Although the printer does have a setting to rotate labels 180 degrees, I'm wondering if the page is somehow being 'landscaped' twice.

I'll have some more labels in the next 24 hours so I'll keep you updated. Unfortunately the project has a looming deadline so if I can't get it working soon I'll have to just rotate the label design 90deg using CSS as a hack and call it done.

Copy link
Contributor

@jkleinsc jkleinsc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, just one question on updating the test.

spec/ts-smoke/electron/main.ts Outdated Show resolved Hide resolved
Copy link
Member

@deepak1556 deepak1556 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am -1 on landing this fix for two reasons,

I introduced the ability to set custom print options in webContents.print(), with the intent that they only be used in silent printing mode.

#18984 that implemented this feature never stated about specifics to silent mode.

  • If the settings were not optional at the Printing layer but the implementation is making it optional for users then we should provide the defaults. Which we already seem to do except for some options, we can fix this and make it explicit in the documentation.

  • These options are provided to emulate the printing UI provided by chrome, so I don't think it has to be specific to silent printing.

With that said I would propose refactoring the PR which preferred defaults that avoids the crash.

@codebytere
Copy link
Member Author

codebytere commented Aug 6, 2019

@deepak1556 i've managed to fix the root issue, and have updated the PR body accordingly :)

@codebytere codebytere requested a review from brenca August 6, 2019 01:57
@codebytere codebytere changed the title fix: crash on print cancellation fix: crash on print cancellation and silent print settings Aug 6, 2019
Copy link
Member

@deepak1556 deepak1556 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with some clarifications.

patches/chromium/printing.patch Outdated Show resolved Hide resolved
shell/browser/api/atom_api_web_contents.cc Show resolved Hide resolved
patches/chromium/printing.patch Show resolved Hide resolved
@codebytere
Copy link
Member Author

CI failures are unrelated and will be fixed by: #19652

Copy link
Contributor

@brenca brenca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Copy link
Member

@deepak1556 deepak1556 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@jkleinsc jkleinsc merged commit 9c7a216 into master Aug 7, 2019
@release-clerk
Copy link

release-clerk bot commented Aug 7, 2019

Release Notes Persisted

Fixed a crash on manual print cancellation as well as an issue with deviceName not working.

@trop
Copy link
Contributor

trop bot commented Aug 7, 2019

I have automatically backported this PR to "7-0-x", please check out #19668

@jkleinsc jkleinsc deleted the fix-print-settings branch August 7, 2019 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
7.2.x
Fixed in 7.0.0-beta.2
5 participants