file_dialog_win.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/ui/file_dialog.h"
  5. #include <windows.h> // windows.h must be included first
  6. #include <atlbase.h> // atlbase.h for CComPtr
  7. #include <shlobj.h>
  8. #include <shobjidl.h>
  9. #include "atom/browser/native_window_views.h"
  10. #include "atom/browser/unresponsive_suppressor.h"
  11. #include "base/files/file_util.h"
  12. #include "base/i18n/case_conversion.h"
  13. #include "base/strings/string_split.h"
  14. #include "base/strings/string_util.h"
  15. #include "base/strings/utf_string_conversions.h"
  16. #include "base/threading/thread.h"
  17. #include "base/threading/thread_task_runner_handle.h"
  18. #include "base/win/registry.h"
  19. namespace file_dialog {
  20. DialogSettings::DialogSettings() = default;
  21. DialogSettings::DialogSettings(const DialogSettings&) = default;
  22. DialogSettings::~DialogSettings() = default;
  23. namespace {
  24. // Distinguish directories from regular files.
  25. bool IsDirectory(const base::FilePath& path) {
  26. base::File::Info file_info;
  27. return base::GetFileInfo(path, &file_info) ? file_info.is_directory
  28. : path.EndsWithSeparator();
  29. }
  30. void ConvertFilters(const Filters& filters,
  31. std::vector<std::wstring>* buffer,
  32. std::vector<COMDLG_FILTERSPEC>* filterspec) {
  33. if (filters.empty()) {
  34. COMDLG_FILTERSPEC spec = {L"All Files (*.*)", L"*.*"};
  35. filterspec->push_back(spec);
  36. return;
  37. }
  38. buffer->reserve(filters.size() * 2);
  39. for (size_t i = 0; i < filters.size(); ++i) {
  40. const Filter& filter = filters[i];
  41. COMDLG_FILTERSPEC spec;
  42. buffer->push_back(base::UTF8ToWide(filter.first));
  43. spec.pszName = buffer->back().c_str();
  44. std::vector<std::string> extensions(filter.second);
  45. for (size_t j = 0; j < extensions.size(); ++j)
  46. extensions[j].insert(0, "*.");
  47. buffer->push_back(base::UTF8ToWide(base::JoinString(extensions, ";")));
  48. spec.pszSpec = buffer->back().c_str();
  49. filterspec->push_back(spec);
  50. }
  51. }
  52. struct RunState {
  53. base::Thread* dialog_thread;
  54. scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner;
  55. };
  56. bool CreateDialogThread(RunState* run_state) {
  57. auto thread =
  58. std::make_unique<base::Thread>(ATOM_PRODUCT_NAME "FileDialogThread");
  59. thread->init_com_with_mta(false);
  60. if (!thread->Start())
  61. return false;
  62. run_state->dialog_thread = thread.release();
  63. run_state->ui_task_runner = base::ThreadTaskRunnerHandle::Get();
  64. return true;
  65. }
  66. void RunOpenDialogInNewThread(const RunState& run_state,
  67. const DialogSettings& settings,
  68. const OpenDialogCallback& callback) {
  69. std::vector<base::FilePath> paths;
  70. bool result = ShowOpenDialog(settings, &paths);
  71. run_state.ui_task_runner->PostTask(FROM_HERE,
  72. base::Bind(callback, result, paths));
  73. run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread);
  74. }
  75. void RunSaveDialogInNewThread(const RunState& run_state,
  76. const DialogSettings& settings,
  77. const SaveDialogCallback& callback) {
  78. base::FilePath path;
  79. bool result = ShowSaveDialog(settings, &path);
  80. run_state.ui_task_runner->PostTask(FROM_HERE,
  81. base::Bind(callback, result, path));
  82. run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread);
  83. }
  84. } // namespace
  85. static HRESULT GetFileNameFromShellItem(IShellItem* pShellItem,
  86. SIGDN type,
  87. LPWSTR lpstr,
  88. size_t cchLength) {
  89. assert(pShellItem != NULL);
  90. LPWSTR lpstrName = NULL;
  91. HRESULT hRet = pShellItem->GetDisplayName(type, &lpstrName);
  92. if (SUCCEEDED(hRet)) {
  93. if (wcslen(lpstrName) < cchLength) {
  94. wcscpy_s(lpstr, cchLength, lpstrName);
  95. } else {
  96. NOTREACHED();
  97. hRet = DISP_E_BUFFERTOOSMALL;
  98. }
  99. ::CoTaskMemFree(lpstrName);
  100. }
  101. return hRet;
  102. }
  103. static void SetDefaultFolder(IFileDialog* dialog,
  104. const base::FilePath file_path) {
  105. std::wstring directory =
  106. IsDirectory(file_path) ? file_path.value() : file_path.DirName().value();
  107. ATL::CComPtr<IShellItem> folder_item;
  108. HRESULT hr = SHCreateItemFromParsingName(directory.c_str(), NULL,
  109. IID_PPV_ARGS(&folder_item));
  110. if (SUCCEEDED(hr))
  111. dialog->SetFolder(folder_item);
  112. }
  113. static HRESULT ShowFileDialog(IFileDialog* dialog,
  114. const DialogSettings& settings) {
  115. atom::UnresponsiveSuppressor suppressor;
  116. HWND parent_window =
  117. settings.parent_window
  118. ? static_cast<atom::NativeWindowViews*>(settings.parent_window)
  119. ->GetAcceleratedWidget()
  120. : NULL;
  121. return dialog->Show(parent_window);
  122. }
  123. static void ApplySettings(IFileDialog* dialog, const DialogSettings& settings) {
  124. std::wstring file_part;
  125. if (!IsDirectory(settings.default_path))
  126. file_part = settings.default_path.BaseName().value();
  127. dialog->SetFileName(file_part.c_str());
  128. if (!settings.title.empty())
  129. dialog->SetTitle(base::UTF8ToUTF16(settings.title).c_str());
  130. if (!settings.button_label.empty())
  131. dialog->SetOkButtonLabel(base::UTF8ToUTF16(settings.button_label).c_str());
  132. std::vector<std::wstring> buffer;
  133. std::vector<COMDLG_FILTERSPEC> filterspec;
  134. ConvertFilters(settings.filters, &buffer, &filterspec);
  135. if (!filterspec.empty()) {
  136. dialog->SetFileTypes(filterspec.size(), filterspec.data());
  137. }
  138. // By default, *.* will be added to the file name if file type is "*.*". In
  139. // Electron, we disable it to make a better experience.
  140. //
  141. // From MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/
  142. // bb775970(v=vs.85).aspx
  143. //
  144. // If SetDefaultExtension is not called, the dialog will not update
  145. // automatically when user choose a new file type in the file dialog.
  146. //
  147. // We set file extension to the first none-wildcard extension to make
  148. // sure the dialog will update file extension automatically.
  149. for (size_t i = 0; i < filterspec.size(); ++i) {
  150. if (std::wstring(filterspec[i].pszSpec) != L"*.*") {
  151. // SetFileTypeIndex is regarded as one-based index.
  152. dialog->SetFileTypeIndex(i + 1);
  153. dialog->SetDefaultExtension(filterspec[i].pszSpec);
  154. break;
  155. }
  156. }
  157. if (settings.default_path.IsAbsolute()) {
  158. SetDefaultFolder(dialog, settings.default_path);
  159. }
  160. }
  161. bool ShowOpenDialog(const DialogSettings& settings,
  162. std::vector<base::FilePath>* paths) {
  163. ATL::CComPtr<IFileOpenDialog> file_open_dialog;
  164. HRESULT hr = file_open_dialog.CoCreateInstance(CLSID_FileOpenDialog);
  165. if (FAILED(hr))
  166. return false;
  167. DWORD options = FOS_FORCEFILESYSTEM | FOS_FILEMUSTEXIST;
  168. if (settings.properties & FILE_DIALOG_OPEN_DIRECTORY)
  169. options |= FOS_PICKFOLDERS;
  170. if (settings.properties & FILE_DIALOG_MULTI_SELECTIONS)
  171. options |= FOS_ALLOWMULTISELECT;
  172. if (settings.properties & FILE_DIALOG_SHOW_HIDDEN_FILES)
  173. options |= FOS_FORCESHOWHIDDEN;
  174. if (settings.properties & FILE_DIALOG_PROMPT_TO_CREATE)
  175. options |= FOS_CREATEPROMPT;
  176. file_open_dialog->SetOptions(options);
  177. ApplySettings(file_open_dialog, settings);
  178. hr = ShowFileDialog(file_open_dialog, settings);
  179. if (FAILED(hr))
  180. return false;
  181. ATL::CComPtr<IShellItemArray> items;
  182. hr = file_open_dialog->GetResults(&items);
  183. if (FAILED(hr))
  184. return false;
  185. ATL::CComPtr<IShellItem> item;
  186. DWORD count = 0;
  187. hr = items->GetCount(&count);
  188. if (FAILED(hr))
  189. return false;
  190. paths->reserve(count);
  191. for (DWORD i = 0; i < count; ++i) {
  192. hr = items->GetItemAt(i, &item);
  193. if (FAILED(hr))
  194. return false;
  195. wchar_t file_name[MAX_PATH];
  196. hr = GetFileNameFromShellItem(item, SIGDN_FILESYSPATH, file_name, MAX_PATH);
  197. if (FAILED(hr))
  198. return false;
  199. paths->push_back(base::FilePath(file_name));
  200. }
  201. return true;
  202. }
  203. void ShowOpenDialog(const DialogSettings& settings,
  204. const OpenDialogCallback& callback) {
  205. RunState run_state;
  206. if (!CreateDialogThread(&run_state)) {
  207. callback.Run(false, std::vector<base::FilePath>());
  208. return;
  209. }
  210. run_state.dialog_thread->task_runner()->PostTask(
  211. FROM_HERE,
  212. base::Bind(&RunOpenDialogInNewThread, run_state, settings, callback));
  213. }
  214. bool ShowSaveDialog(const DialogSettings& settings, base::FilePath* path) {
  215. ATL::CComPtr<IFileSaveDialog> file_save_dialog;
  216. HRESULT hr = file_save_dialog.CoCreateInstance(CLSID_FileSaveDialog);
  217. if (FAILED(hr))
  218. return false;
  219. file_save_dialog->SetOptions(FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST |
  220. FOS_OVERWRITEPROMPT);
  221. ApplySettings(file_save_dialog, settings);
  222. hr = ShowFileDialog(file_save_dialog, settings);
  223. if (FAILED(hr))
  224. return false;
  225. CComPtr<IShellItem> pItem;
  226. hr = file_save_dialog->GetResult(&pItem);
  227. if (FAILED(hr))
  228. return false;
  229. PWSTR result_path = nullptr;
  230. hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &result_path);
  231. if (!SUCCEEDED(hr))
  232. return false;
  233. *path = base::FilePath(result_path);
  234. CoTaskMemFree(result_path);
  235. return true;
  236. }
  237. void ShowSaveDialog(const DialogSettings& settings,
  238. const SaveDialogCallback& callback) {
  239. RunState run_state;
  240. if (!CreateDialogThread(&run_state)) {
  241. callback.Run(false, base::FilePath());
  242. return;
  243. }
  244. run_state.dialog_thread->task_runner()->PostTask(
  245. FROM_HERE,
  246. base::Bind(&RunSaveDialogInNewThread, run_state, settings, callback));
  247. }
  248. } // namespace file_dialog