2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-30 15:56:50 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/atom_javascript_dialog_manager.h"
|
2013-04-30 15:56:50 +00:00
|
|
|
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <string>
|
2017-01-05 00:46:50 +00:00
|
|
|
#include <vector>
|
2014-03-16 01:13:06 +00:00
|
|
|
|
2017-04-28 00:28:48 +00:00
|
|
|
#include "atom/browser/api/atom_api_web_contents.h"
|
2017-01-05 00:46:50 +00:00
|
|
|
#include "atom/browser/native_window.h"
|
|
|
|
#include "atom/browser/ui/message_box.h"
|
2018-01-10 06:07:56 +00:00
|
|
|
#include "atom/browser/web_contents_preferences.h"
|
2017-01-05 00:46:50 +00:00
|
|
|
#include "base/bind.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2017-01-05 00:46:50 +00:00
|
|
|
#include "ui/gfx/image/image_skia.h"
|
|
|
|
|
2017-04-04 04:50:44 +00:00
|
|
|
using content::JavaScriptDialogType;
|
2013-04-30 15:56:50 +00:00
|
|
|
|
2018-03-06 02:31:56 +00:00
|
|
|
namespace atom {
|
|
|
|
|
2018-02-15 02:07:49 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-03-06 02:31:56 +00:00
|
|
|
constexpr int kUserWantsNoMoreDialogs = -1;
|
|
|
|
|
|
|
|
} // namespace
|
2013-04-30 15:56:50 +00:00
|
|
|
|
2017-04-28 00:28:48 +00:00
|
|
|
AtomJavaScriptDialogManager::AtomJavaScriptDialogManager(
|
|
|
|
api::WebContents* api_web_contents)
|
|
|
|
: api_web_contents_(api_web_contents) {}
|
|
|
|
|
2014-03-01 11:53:50 +00:00
|
|
|
void AtomJavaScriptDialogManager::RunJavaScriptDialog(
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
const GURL& origin_url,
|
2017-04-04 04:50:44 +00:00
|
|
|
JavaScriptDialogType dialog_type,
|
2014-06-28 11:36:57 +00:00
|
|
|
const base::string16& message_text,
|
|
|
|
const base::string16& default_prompt_text,
|
2017-12-18 01:24:13 +00:00
|
|
|
DialogClosedCallback callback,
|
2014-03-01 11:53:50 +00:00
|
|
|
bool* did_suppress_message) {
|
2018-01-10 06:07:56 +00:00
|
|
|
const std::string origin = origin_url.GetOrigin().spec();
|
2018-03-06 02:31:56 +00:00
|
|
|
if (origin_counts_[origin] == kUserWantsNoMoreDialogs) {
|
2018-01-25 02:50:00 +00:00
|
|
|
return callback.Run(false, base::string16());
|
|
|
|
}
|
2018-01-10 06:07:56 +00:00
|
|
|
|
2017-04-04 04:50:44 +00:00
|
|
|
if (dialog_type != JavaScriptDialogType::JAVASCRIPT_DIALOG_TYPE_ALERT &&
|
|
|
|
dialog_type != JavaScriptDialogType::JAVASCRIPT_DIALOG_TYPE_CONFIRM) {
|
2017-12-18 01:31:00 +00:00
|
|
|
std::move(callback).Run(false, base::string16());
|
2017-01-05 00:46:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:53:06 +00:00
|
|
|
std::vector<std::string> buttons = {"OK"};
|
2017-04-04 04:50:44 +00:00
|
|
|
if (dialog_type == JavaScriptDialogType::JAVASCRIPT_DIALOG_TYPE_CONFIRM) {
|
2017-01-05 00:46:50 +00:00
|
|
|
buttons.push_back("Cancel");
|
|
|
|
}
|
|
|
|
|
2018-01-10 06:07:56 +00:00
|
|
|
origin_counts_[origin]++;
|
|
|
|
|
|
|
|
std::string checkbox_string;
|
|
|
|
if (origin_counts_[origin] > 1 &&
|
2018-02-15 03:06:49 +00:00
|
|
|
WebContentsPreferences::IsPreferenceEnabled("safeDialogs",
|
|
|
|
web_contents)) {
|
2018-01-10 06:07:56 +00:00
|
|
|
if (!WebContentsPreferences::GetString("safeDialogsMessage",
|
|
|
|
&checkbox_string, web_contents)) {
|
|
|
|
checkbox_string = "Prevent this app from creating additional dialogs";
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 06:18:45 +00:00
|
|
|
|
|
|
|
auto* relay = NativeWindowRelay::FromWebContents(web_contents);
|
2018-03-06 02:35:53 +00:00
|
|
|
atom::ShowMessageBox(
|
2018-03-06 06:18:45 +00:00
|
|
|
relay ? relay->window.get() : nullptr,
|
2018-03-06 02:35:53 +00:00
|
|
|
atom::MessageBoxType::MESSAGE_BOX_TYPE_NONE, buttons, -1, 0,
|
|
|
|
atom::MessageBoxOptions::MESSAGE_BOX_NONE, "",
|
|
|
|
base::UTF16ToUTF8(message_text), "", checkbox_string,
|
|
|
|
false, gfx::ImageSkia(),
|
|
|
|
base::Bind(&AtomJavaScriptDialogManager::OnMessageBoxCallback,
|
|
|
|
base::Unretained(this), callback, origin));
|
2014-03-01 11:53:50 +00:00
|
|
|
}
|
|
|
|
|
2013-04-30 15:56:50 +00:00
|
|
|
void AtomJavaScriptDialogManager::RunBeforeUnloadDialog(
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
bool is_reload,
|
2017-12-18 01:24:13 +00:00
|
|
|
DialogClosedCallback callback) {
|
2017-04-28 00:28:48 +00:00
|
|
|
bool default_prevented = api_web_contents_->Emit("will-prevent-unload");
|
2017-12-18 01:31:00 +00:00
|
|
|
std::move(callback).Run(default_prevented, base::string16());
|
2017-04-28 00:28:48 +00:00
|
|
|
return;
|
2013-04-30 15:56:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 03:18:56 +00:00
|
|
|
void AtomJavaScriptDialogManager::CancelDialogs(
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
bool reset_state) {
|
|
|
|
}
|
|
|
|
|
2017-01-05 00:46:50 +00:00
|
|
|
void AtomJavaScriptDialogManager::OnMessageBoxCallback(
|
2017-02-06 15:35:36 +00:00
|
|
|
const DialogClosedCallback& callback,
|
2018-01-10 06:07:56 +00:00
|
|
|
const std::string& origin,
|
2017-02-06 15:35:36 +00:00
|
|
|
int code,
|
|
|
|
bool checkbox_checked) {
|
2018-03-06 02:35:53 +00:00
|
|
|
if (checkbox_checked)
|
|
|
|
origin_counts_[origin] = kUserWantsNoMoreDialogs;
|
2017-01-05 00:46:50 +00:00
|
|
|
callback.Run(code == 0, base::string16());
|
|
|
|
}
|
|
|
|
|
2013-04-30 15:56:50 +00:00
|
|
|
} // namespace atom
|