atom_javascript_dialog_manager.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/atom_javascript_dialog_manager.h"
  5. #include <string>
  6. #include <vector>
  7. #include "atom/browser/api/atom_api_web_contents.h"
  8. #include "atom/browser/native_window.h"
  9. #include "atom/browser/ui/message_box.h"
  10. #include "atom/browser/web_contents_preferences.h"
  11. #include "atom/common/options_switches.h"
  12. #include "base/bind.h"
  13. #include "base/strings/utf_string_conversions.h"
  14. #include "ui/gfx/image/image_skia.h"
  15. using content::JavaScriptDialogType;
  16. namespace atom {
  17. namespace {
  18. constexpr int kUserWantsNoMoreDialogs = -1;
  19. } // namespace
  20. AtomJavaScriptDialogManager::AtomJavaScriptDialogManager(
  21. api::WebContents* api_web_contents)
  22. : api_web_contents_(api_web_contents) {}
  23. AtomJavaScriptDialogManager::~AtomJavaScriptDialogManager() = default;
  24. void AtomJavaScriptDialogManager::RunJavaScriptDialog(
  25. content::WebContents* web_contents,
  26. const GURL& origin_url,
  27. JavaScriptDialogType dialog_type,
  28. const base::string16& message_text,
  29. const base::string16& default_prompt_text,
  30. DialogClosedCallback callback,
  31. bool* did_suppress_message) {
  32. const std::string& origin = origin_url.GetOrigin().spec();
  33. if (origin_counts_[origin] == kUserWantsNoMoreDialogs) {
  34. return std::move(callback).Run(false, base::string16());
  35. }
  36. if (dialog_type != JavaScriptDialogType::JAVASCRIPT_DIALOG_TYPE_ALERT &&
  37. dialog_type != JavaScriptDialogType::JAVASCRIPT_DIALOG_TYPE_CONFIRM) {
  38. std::move(callback).Run(false, base::string16());
  39. return;
  40. }
  41. std::vector<std::string> buttons = {"OK"};
  42. if (dialog_type == JavaScriptDialogType::JAVASCRIPT_DIALOG_TYPE_CONFIRM) {
  43. buttons.push_back("Cancel");
  44. }
  45. origin_counts_[origin]++;
  46. auto* web_preferences = WebContentsPreferences::From(web_contents);
  47. std::string checkbox;
  48. if (origin_counts_[origin] > 1 && web_preferences &&
  49. web_preferences->IsEnabled("safeDialogs") &&
  50. !web_preferences->dict()->GetString("safeDialogsMessage", &checkbox)) {
  51. checkbox = "Prevent this app from creating additional dialogs";
  52. }
  53. // Don't set parent for offscreen window.
  54. NativeWindow* window = nullptr;
  55. if (web_preferences && !web_preferences->IsEnabled(options::kOffscreen)) {
  56. auto* relay = NativeWindowRelay::FromWebContents(web_contents);
  57. if (relay)
  58. window = relay->window.get();
  59. }
  60. atom::ShowMessageBox(
  61. window, atom::MessageBoxType::MESSAGE_BOX_TYPE_NONE, buttons, -1, 0,
  62. atom::MessageBoxOptions::MESSAGE_BOX_NONE, "",
  63. base::UTF16ToUTF8(message_text), "", checkbox, false, gfx::ImageSkia(),
  64. base::Bind(&AtomJavaScriptDialogManager::OnMessageBoxCallback,
  65. base::Unretained(this), base::Passed(std::move(callback)),
  66. origin));
  67. }
  68. void AtomJavaScriptDialogManager::RunBeforeUnloadDialog(
  69. content::WebContents* web_contents,
  70. bool is_reload,
  71. DialogClosedCallback callback) {
  72. bool default_prevented = api_web_contents_->Emit("will-prevent-unload");
  73. std::move(callback).Run(default_prevented, base::string16());
  74. return;
  75. }
  76. void AtomJavaScriptDialogManager::CancelDialogs(
  77. content::WebContents* web_contents,
  78. bool reset_state) {}
  79. void AtomJavaScriptDialogManager::OnMessageBoxCallback(
  80. DialogClosedCallback callback,
  81. const std::string& origin,
  82. int code,
  83. bool checkbox_checked) {
  84. if (checkbox_checked)
  85. origin_counts_[origin] = kUserWantsNoMoreDialogs;
  86. std::move(callback).Run(code == 0, base::string16());
  87. }
  88. } // namespace atom