atom_api_dialog.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <string>
  5. #include <utility>
  6. #include <vector>
  7. #include "atom/browser/api/atom_api_browser_window.h"
  8. #include "atom/browser/native_window.h"
  9. #include "atom/browser/ui/certificate_trust.h"
  10. #include "atom/browser/ui/file_dialog.h"
  11. #include "atom/browser/ui/message_box.h"
  12. #include "atom/common/native_mate_converters/callback.h"
  13. #include "atom/common/native_mate_converters/file_dialog_converter.h"
  14. #include "atom/common/native_mate_converters/file_path_converter.h"
  15. #include "atom/common/native_mate_converters/image_converter.h"
  16. #include "atom/common/native_mate_converters/net_converter.h"
  17. #include "atom/common/node_includes.h"
  18. #include "atom/common/promise_util.h"
  19. #include "native_mate/dictionary.h"
  20. namespace {
  21. int ShowMessageBoxSync(int type,
  22. const std::vector<std::string>& buttons,
  23. int default_id,
  24. int cancel_id,
  25. int options,
  26. const std::string& title,
  27. const std::string& message,
  28. const std::string& detail,
  29. const std::string& checkbox_label,
  30. bool checkbox_checked,
  31. const gfx::ImageSkia& icon,
  32. atom::NativeWindow* window) {
  33. return atom::ShowMessageBoxSync(
  34. window, static_cast<atom::MessageBoxType>(type), buttons, default_id,
  35. cancel_id, options, title, message, detail, icon);
  36. }
  37. void ResolvePromiseObject(atom::util::Promise promise,
  38. int result,
  39. bool checkbox_checked) {
  40. mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate());
  41. dict.Set("response", result);
  42. dict.Set("checkboxChecked", checkbox_checked);
  43. promise.Resolve(dict.GetHandle());
  44. }
  45. v8::Local<v8::Promise> ShowMessageBox(int type,
  46. const std::vector<std::string>& buttons,
  47. int default_id,
  48. int cancel_id,
  49. int options,
  50. const std::string& title,
  51. const std::string& message,
  52. const std::string& detail,
  53. const std::string& checkbox_label,
  54. bool checkbox_checked,
  55. const gfx::ImageSkia& icon,
  56. atom::NativeWindow* window,
  57. mate::Arguments* args) {
  58. v8::Isolate* isolate = args->isolate();
  59. atom::util::Promise promise(isolate);
  60. v8::Local<v8::Promise> handle = promise.GetHandle();
  61. atom::ShowMessageBox(
  62. window, static_cast<atom::MessageBoxType>(type), buttons, default_id,
  63. cancel_id, options, title, message, detail, checkbox_label,
  64. checkbox_checked, icon,
  65. base::BindOnce(&ResolvePromiseObject, std::move(promise)));
  66. return handle;
  67. }
  68. void ShowOpenDialogSync(const file_dialog::DialogSettings& settings,
  69. mate::Arguments* args) {
  70. std::vector<base::FilePath> paths;
  71. if (file_dialog::ShowOpenDialogSync(settings, &paths))
  72. args->Return(paths);
  73. }
  74. v8::Local<v8::Promise> ShowOpenDialog(
  75. const file_dialog::DialogSettings& settings,
  76. mate::Arguments* args) {
  77. atom::util::Promise promise(args->isolate());
  78. v8::Local<v8::Promise> handle = promise.GetHandle();
  79. file_dialog::ShowOpenDialog(settings, std::move(promise));
  80. return handle;
  81. }
  82. void ShowSaveDialogSync(const file_dialog::DialogSettings& settings,
  83. mate::Arguments* args) {
  84. base::FilePath path;
  85. if (file_dialog::ShowSaveDialogSync(settings, &path))
  86. args->Return(path);
  87. }
  88. v8::Local<v8::Promise> ShowSaveDialog(
  89. const file_dialog::DialogSettings& settings,
  90. mate::Arguments* args) {
  91. atom::util::Promise promise(args->isolate());
  92. v8::Local<v8::Promise> handle = promise.GetHandle();
  93. file_dialog::ShowSaveDialog(settings, std::move(promise));
  94. return handle;
  95. }
  96. void Initialize(v8::Local<v8::Object> exports,
  97. v8::Local<v8::Value> unused,
  98. v8::Local<v8::Context> context,
  99. void* priv) {
  100. mate::Dictionary dict(context->GetIsolate(), exports);
  101. dict.SetMethod("showMessageBoxSync", &ShowMessageBoxSync);
  102. dict.SetMethod("showMessageBox", &ShowMessageBox);
  103. dict.SetMethod("showErrorBox", &atom::ShowErrorBox);
  104. dict.SetMethod("showOpenDialogSync", &ShowOpenDialogSync);
  105. dict.SetMethod("showOpenDialog", &ShowOpenDialog);
  106. dict.SetMethod("showSaveDialogSync", &ShowSaveDialogSync);
  107. dict.SetMethod("showSaveDialog", &ShowSaveDialog);
  108. #if defined(OS_MACOSX) || defined(OS_WIN)
  109. dict.SetMethod("showCertificateTrustDialog",
  110. &certificate_trust::ShowCertificateTrust);
  111. #endif
  112. }
  113. } // namespace
  114. NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_dialog, Initialize)