Skip to content

Add 'will-prevent-unload' event for #2579. - #9331

Merged
kevinsawicki merged 2 commits into
electron:masterfrom
22222:issue2579
May 11, 2017
Merged

kevinsawicki merged 2 commits into
electron:masterfrom
22222:issue2579

Conversation

@22222

@22222 22222 commented Apr 30, 2017

Copy link
Copy Markdown
Contributor

This change is based on my comment in issue 2579. I have very little experience with the Electron code, so I hope that "help/beginner" tag still applies.

Restructure

The biggest part of this change is getting access to the event emitting methods of the atom::api::WebContents class from the atom::AtomJavaScriptDialogManager class. That requirement led me to shuffling around a couple dependencies.

I based the changes to atom::AtomJavaScriptDialogManager on the existing atom::BluetoothChooser class: have a constructor that accepts an api::WebContents* object, save a reference to it, and use its Emit method later.

The second part of that change is in constructing the atom::AtomJavaScriptDialogManager object. Right now creating and managing that dialog_manager object happens in the atom::CommonWebContentsDelegate base class, which isn't ideal for this change since it doesn't have access to any of the Emit methods. So I moved that (including all references to atom::AtomJavaScriptDialogManager) into the atom::api::WebContents class.

One thing that makes me a little nervous about that change is this comment from common_web_contents_delegate.h:

  // The stored InspectableWebContents object.
  // Notice that web_contents_ must be placed after dialog_manager_, so we can
  // make sure web_contents_ is destroyed before dialog_manager_, otherwise a
  // crash would happen.
  std::unique_ptr<brightray::InspectableWebContents> web_contents_;

The dialog_manager_ field is in a subclass now, so it should still be destroyed before web_contents_ field. But this change does affect the destruction order, so if there are any other dependencies on the destruction order then it's possible that some other problem could come from this.

Reloads

One additional piece of information that the RunBeforeUnloadDialog method has is an is_reload flag. The Chromium browser uses that to choose a different title for the confirmation dialog if the unload is for a reload (Do you want to reload this site? instead of Do you want to leave this site?).

Originally I planned to make that is_reload flag available in the new will-prevent-unload event. But in my testing the flag was always false, even for a reload (using the reload menu option from the default app). So at least for now, I left the is_reload flag out since it would be misleading to provide it if it doesn't work, and it would probably be possible to add later if there's a demand for it.

@kevinsawicki kevinsawicki self-assigned this May 9, 2017

@kevinsawicki kevinsawicki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks good, left one minor comment, also could you resolve the conflicts in this branch? Thanks.

#include "atom/browser/ui/message_box.h"
#include "base/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/l10n/l10n_util.h"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think this include is needed.

@22222

22222 commented May 9, 2017

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing this.

I made the requested change and resolved the conflict. Let me know if you need any other changes.

* const-ing
* mainWindow -> win
* add dialog require
@kevinsawicki

Copy link
Copy Markdown
Contributor

Thanks so much for this 👍 🚢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants