Merge pull request #9331 from 22222/issue2579

Add 'will-prevent-unload' event for #2579.
This commit is contained in:
Kevin Sawicki 2017-05-11 09:04:40 -07:00 committed by GitHub
commit 190c9c916e
9 changed files with 88 additions and 17 deletions

View file

@ -13,6 +13,7 @@
#include "atom/browser/atom_browser_client.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_javascript_dialog_manager.h"
#include "atom/browser/child_web_contents_tracker.h"
#include "atom/browser/lib/bluetooth_chooser.h"
#include "atom/browser/native_window.h"
@ -698,6 +699,15 @@ std::unique_ptr<content::BluetoothChooser> WebContents::RunBluetoothChooser(
return std::move(bluetooth_chooser);
}
content::JavaScriptDialogManager*
WebContents::GetJavaScriptDialogManager(
content::WebContents* source) {
if (!dialog_manager_)
dialog_manager_.reset(new AtomJavaScriptDialogManager(this));
return dialog_manager_.get();
}
void WebContents::BeforeUnloadFired(const base::TimeTicks& proceed_time) {
// Do nothing, we override this method just to avoid compilation error since
// there are two virtual functions named BeforeUnloadFired.

View file

@ -41,6 +41,7 @@ namespace atom {
struct SetSizeParams;
class AtomBrowserContext;
class AtomJavaScriptDialogManager;
class WebContentsZoomController;
class WebViewGuestDelegate;
@ -296,6 +297,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
content::RenderFrameHost* frame,
const content::BluetoothChooser::EventHandler& handler) override;
content::JavaScriptDialogManager* GetJavaScriptDialogManager(
content::WebContents* source) override;
// content::WebContentsObserver:
void BeforeUnloadFired(const base::TimeTicks& proceed_time) override;
@ -388,6 +391,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
v8::Global<v8::Value> devtools_web_contents_;
v8::Global<v8::Value> debugger_;
std::unique_ptr<AtomJavaScriptDialogManager> dialog_manager_;
std::unique_ptr<WebViewGuestDelegate> guest_delegate_;
// The host webcontents that may contain this webcontents.

View file

@ -7,6 +7,7 @@
#include <string>
#include <vector>
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/browser/native_window.h"
#include "atom/browser/ui/message_box.h"
#include "base/bind.h"
@ -17,6 +18,10 @@ using content::JavaScriptDialogType;
namespace atom {
AtomJavaScriptDialogManager::AtomJavaScriptDialogManager(
api::WebContents* api_web_contents)
: api_web_contents_(api_web_contents) {}
void AtomJavaScriptDialogManager::RunJavaScriptDialog(
content::WebContents* web_contents,
const GURL& origin_url,
@ -25,7 +30,6 @@ void AtomJavaScriptDialogManager::RunJavaScriptDialog(
const base::string16& default_prompt_text,
const DialogClosedCallback& callback,
bool* did_suppress_message) {
if (dialog_type != JavaScriptDialogType::JAVASCRIPT_DIALOG_TYPE_ALERT &&
dialog_type != JavaScriptDialogType::JAVASCRIPT_DIALOG_TYPE_CONFIRM) {
callback.Run(false, base::string16());
@ -49,8 +53,9 @@ void AtomJavaScriptDialogManager::RunBeforeUnloadDialog(
content::WebContents* web_contents,
bool is_reload,
const DialogClosedCallback& callback) {
// FIXME(zcbenz): the |message_text| is removed, figure out what should we do.
callback.Run(false, base::ASCIIToUTF16("This should not be displayed"));
bool default_prevented = api_web_contents_->Emit("will-prevent-unload");
callback.Run(default_prevented, base::string16());
return;
}
void AtomJavaScriptDialogManager::CancelDialogs(

View file

@ -11,8 +11,14 @@
namespace atom {
namespace api {
class WebContents;
}
class AtomJavaScriptDialogManager : public content::JavaScriptDialogManager {
public:
explicit AtomJavaScriptDialogManager(api::WebContents* api_web_contents);
// content::JavaScriptDialogManager implementations.
void RunJavaScriptDialog(
content::WebContents* web_contents,
@ -33,6 +39,7 @@ class AtomJavaScriptDialogManager : public content::JavaScriptDialogManager {
static void OnMessageBoxCallback(const DialogClosedCallback& callback,
int code,
bool checkbox_checked);
api::WebContents* api_web_contents_;
};
} // namespace atom

View file

@ -9,7 +9,6 @@
#include <vector>
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_javascript_dialog_manager.h"
#include "atom/browser/native_window.h"
#include "atom/browser/ui/file_dialog.h"
#include "atom/browser/web_dialog_helper.h"
@ -230,15 +229,6 @@ bool CommonWebContentsDelegate::CanOverscrollContent() const {
return false;
}
content::JavaScriptDialogManager*
CommonWebContentsDelegate::GetJavaScriptDialogManager(
content::WebContents* source) {
if (!dialog_manager_)
dialog_manager_.reset(new AtomJavaScriptDialogManager);
return dialog_manager_.get();
}
content::ColorChooser* CommonWebContentsDelegate::OpenColorChooser(
content::WebContents* web_contents,
SkColor color,

View file

@ -20,7 +20,6 @@ using brightray::DevToolsFileSystemIndexer;
namespace atom {
class AtomBrowserContext;
class AtomJavaScriptDialogManager;
class NativeWindow;
class WebDialogHelper;
@ -62,8 +61,6 @@ class CommonWebContentsDelegate
content::WebContents* source,
const content::OpenURLParams& params) override;
bool CanOverscrollContent() const override;
content::JavaScriptDialogManager* GetJavaScriptDialogManager(
content::WebContents* source) override;
content::ColorChooser* OpenColorChooser(
content::WebContents* web_contents,
SkColor color,
@ -147,7 +144,6 @@ class CommonWebContentsDelegate
bool native_fullscreen_;
std::unique_ptr<WebDialogHelper> web_dialog_helper_;
std::unique_ptr<AtomJavaScriptDialogManager> dialog_manager_;
scoped_refptr<DevToolsFileSystemIndexer> devtools_file_system_indexer_;
// Make sure BrowserContext is alwasys destroyed after WebContents.