electron/atom/browser/atom_javascript_dialog_manager.cc

106 lines
3.4 KiB
C++
Raw Normal View History

// 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
// found in the LICENSE file.
2014-03-16 00:30:26 +00:00
#include "atom/browser/atom_javascript_dialog_manager.h"
#include <string>
#include <vector>
2017-04-28 00:28:48 +00:00
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/browser/native_window.h"
#include "atom/browser/ui/message_box.h"
#include "atom/browser/web_contents_preferences.h"
#include "base/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/gfx/image/image_skia.h"
2017-04-04 04:50:44 +00:00
using content::JavaScriptDialogType;
namespace {
constexpr int USER_WANTS_NO_MORE_DIALOGS = -1;
}
namespace atom {
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,
2014-03-01 11:53:50 +00:00
const DialogClosedCallback& callback,
bool* did_suppress_message) {
const std::string origin = origin_url.GetOrigin().spec();
if (origin_counts_.find(origin) == origin_counts_.end()) {
origin_counts_[origin] = 0;
}
if (origin_counts_[origin] == USER_WANTS_NO_MORE_DIALOGS) {
return callback.Run(false, base::string16());
}
2017-04-04 04:50:44 +00:00
if (dialog_type != JavaScriptDialogType::JAVASCRIPT_DIALOG_TYPE_ALERT &&
dialog_type != JavaScriptDialogType::JAVASCRIPT_DIALOG_TYPE_CONFIRM) {
callback.Run(false, base::string16());
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) {
buttons.push_back("Cancel");
}
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)) {
if (!WebContentsPreferences::GetString("safeDialogsMessage",
&checkbox_string, web_contents)) {
checkbox_string = "Prevent this app from creating additional dialogs";
}
}
atom::ShowMessageBox(NativeWindow::FromWebContents(web_contents),
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(&OnMessageBoxCallback, callback, origin,
&origin_counts_));
2014-03-01 11:53:50 +00:00
}
void AtomJavaScriptDialogManager::RunBeforeUnloadDialog(
content::WebContents* web_contents,
bool is_reload,
const DialogClosedCallback& callback) {
2017-04-28 00:28:48 +00:00
bool default_prevented = api_web_contents_->Emit("will-prevent-unload");
callback.Run(default_prevented, base::string16());
return;
}
2017-01-24 03:18:56 +00:00
void AtomJavaScriptDialogManager::CancelDialogs(
content::WebContents* web_contents,
bool reset_state) {
}
// static
void AtomJavaScriptDialogManager::OnMessageBoxCallback(
const DialogClosedCallback& callback,
const std::string& origin,
std::map<std::string, int>* origin_counts_,
int code,
bool checkbox_checked) {
if (checkbox_checked) {
(*origin_counts_)[origin] = USER_WANTS_NO_MORE_DIALOGS;
}
callback.Run(code == 0, base::string16());
}
} // namespace atom