platform_util_win.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/common/platform_util.h"
  5. #include <windows.h> // windows.h must be included first
  6. #include <atlbase.h>
  7. #include <comdef.h>
  8. #include <commdlg.h>
  9. #include <dwmapi.h>
  10. #include <objbase.h>
  11. #include <shellapi.h>
  12. #include <shlobj.h>
  13. #include <wrl/client.h>
  14. #include "base/bind.h"
  15. #include "base/bind_helpers.h"
  16. #include "base/files/file_path.h"
  17. #include "base/files/file_util.h"
  18. #include "base/logging.h"
  19. #include "base/stl_util.h"
  20. #include "base/strings/string_util.h"
  21. #include "base/strings/utf_string_conversions.h"
  22. #include "base/win/registry.h"
  23. #include "base/win/scoped_co_mem.h"
  24. #include "base/win/scoped_com_initializer.h"
  25. #include "base/win/windows_version.h"
  26. #include "ui/base/win/shell.h"
  27. #include "url/gurl.h"
  28. namespace {
  29. // Required COM implementation of IFileOperationProgressSink so we can
  30. // precheck files before deletion to make sure they can be move to the
  31. // Recycle Bin.
  32. class DeleteFileProgressSink : public IFileOperationProgressSink {
  33. public:
  34. DeleteFileProgressSink();
  35. virtual ~DeleteFileProgressSink() = default;
  36. private:
  37. ULONG STDMETHODCALLTYPE AddRef(void) override;
  38. ULONG STDMETHODCALLTYPE Release(void) override;
  39. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid,
  40. LPVOID* ppvObj) override;
  41. HRESULT STDMETHODCALLTYPE StartOperations(void) override;
  42. HRESULT STDMETHODCALLTYPE FinishOperations(HRESULT) override;
  43. HRESULT STDMETHODCALLTYPE PreRenameItem(DWORD, IShellItem*, LPCWSTR) override;
  44. HRESULT STDMETHODCALLTYPE
  45. PostRenameItem(DWORD, IShellItem*, LPCWSTR, HRESULT, IShellItem*) override;
  46. HRESULT STDMETHODCALLTYPE PreMoveItem(DWORD,
  47. IShellItem*,
  48. IShellItem*,
  49. LPCWSTR) override;
  50. HRESULT STDMETHODCALLTYPE PostMoveItem(DWORD,
  51. IShellItem*,
  52. IShellItem*,
  53. LPCWSTR,
  54. HRESULT,
  55. IShellItem*) override;
  56. HRESULT STDMETHODCALLTYPE PreCopyItem(DWORD,
  57. IShellItem*,
  58. IShellItem*,
  59. LPCWSTR) override;
  60. HRESULT STDMETHODCALLTYPE PostCopyItem(DWORD,
  61. IShellItem*,
  62. IShellItem*,
  63. LPCWSTR,
  64. HRESULT,
  65. IShellItem*) override;
  66. HRESULT STDMETHODCALLTYPE PreDeleteItem(DWORD, IShellItem*) override;
  67. HRESULT STDMETHODCALLTYPE PostDeleteItem(DWORD,
  68. IShellItem*,
  69. HRESULT,
  70. IShellItem*) override;
  71. HRESULT STDMETHODCALLTYPE PreNewItem(DWORD, IShellItem*, LPCWSTR) override;
  72. HRESULT STDMETHODCALLTYPE PostNewItem(DWORD,
  73. IShellItem*,
  74. LPCWSTR,
  75. LPCWSTR,
  76. DWORD,
  77. HRESULT,
  78. IShellItem*) override;
  79. HRESULT STDMETHODCALLTYPE UpdateProgress(UINT, UINT) override;
  80. HRESULT STDMETHODCALLTYPE ResetTimer(void) override;
  81. HRESULT STDMETHODCALLTYPE PauseTimer(void) override;
  82. HRESULT STDMETHODCALLTYPE ResumeTimer(void) override;
  83. ULONG m_cRef;
  84. };
  85. DeleteFileProgressSink::DeleteFileProgressSink() {
  86. m_cRef = 0;
  87. }
  88. HRESULT DeleteFileProgressSink::PreDeleteItem(DWORD dwFlags, IShellItem*) {
  89. if (!(dwFlags & TSF_DELETE_RECYCLE_IF_POSSIBLE)) {
  90. // TSF_DELETE_RECYCLE_IF_POSSIBLE will not be set for items that cannot be
  91. // recycled. In this case, we abort the delete operation. This bubbles
  92. // up and stops the Delete in IFileOperation.
  93. return E_ABORT;
  94. }
  95. // Returns S_OK if successful, or an error value otherwise. In the case of an
  96. // error value, the delete operation and all subsequent operations pending
  97. // from the call to IFileOperation are canceled.
  98. return S_OK;
  99. }
  100. HRESULT DeleteFileProgressSink::QueryInterface(REFIID riid, LPVOID* ppvObj) {
  101. // Always set out parameter to NULL, validating it first.
  102. if (!ppvObj)
  103. return E_INVALIDARG;
  104. *ppvObj = nullptr;
  105. if (riid == IID_IUnknown || riid == IID_IFileOperationProgressSink) {
  106. // Increment the reference count and return the pointer.
  107. *ppvObj = reinterpret_cast<IUnknown*>(this);
  108. AddRef();
  109. return NOERROR;
  110. }
  111. return E_NOINTERFACE;
  112. }
  113. ULONG DeleteFileProgressSink::AddRef() {
  114. InterlockedIncrement(&m_cRef);
  115. return m_cRef;
  116. }
  117. ULONG DeleteFileProgressSink::Release() {
  118. // Decrement the object's internal counter.
  119. ULONG ulRefCount = InterlockedDecrement(&m_cRef);
  120. if (0 == m_cRef) {
  121. delete this;
  122. }
  123. return ulRefCount;
  124. }
  125. HRESULT DeleteFileProgressSink::StartOperations() {
  126. return S_OK;
  127. }
  128. HRESULT DeleteFileProgressSink::FinishOperations(HRESULT) {
  129. return S_OK;
  130. }
  131. HRESULT DeleteFileProgressSink::PreRenameItem(DWORD, IShellItem*, LPCWSTR) {
  132. return S_OK;
  133. }
  134. HRESULT DeleteFileProgressSink::PostRenameItem(DWORD,
  135. IShellItem*,
  136. __RPC__in_string LPCWSTR,
  137. HRESULT,
  138. IShellItem*) {
  139. return E_NOTIMPL;
  140. }
  141. HRESULT DeleteFileProgressSink::PreMoveItem(DWORD,
  142. IShellItem*,
  143. IShellItem*,
  144. LPCWSTR) {
  145. return E_NOTIMPL;
  146. }
  147. HRESULT DeleteFileProgressSink::PostMoveItem(DWORD,
  148. IShellItem*,
  149. IShellItem*,
  150. LPCWSTR,
  151. HRESULT,
  152. IShellItem*) {
  153. return E_NOTIMPL;
  154. }
  155. HRESULT DeleteFileProgressSink::PreCopyItem(DWORD,
  156. IShellItem*,
  157. IShellItem*,
  158. LPCWSTR) {
  159. return E_NOTIMPL;
  160. }
  161. HRESULT DeleteFileProgressSink::PostCopyItem(DWORD,
  162. IShellItem*,
  163. IShellItem*,
  164. LPCWSTR,
  165. HRESULT,
  166. IShellItem*) {
  167. return E_NOTIMPL;
  168. }
  169. HRESULT DeleteFileProgressSink::PostDeleteItem(DWORD,
  170. IShellItem*,
  171. HRESULT,
  172. IShellItem*) {
  173. return S_OK;
  174. }
  175. HRESULT DeleteFileProgressSink::PreNewItem(DWORD dwFlags,
  176. IShellItem*,
  177. LPCWSTR) {
  178. return E_NOTIMPL;
  179. }
  180. HRESULT DeleteFileProgressSink::PostNewItem(DWORD,
  181. IShellItem*,
  182. LPCWSTR,
  183. LPCWSTR,
  184. DWORD,
  185. HRESULT,
  186. IShellItem*) {
  187. return E_NOTIMPL;
  188. }
  189. HRESULT DeleteFileProgressSink::UpdateProgress(UINT, UINT) {
  190. return S_OK;
  191. }
  192. HRESULT DeleteFileProgressSink::ResetTimer() {
  193. return S_OK;
  194. }
  195. HRESULT DeleteFileProgressSink::PauseTimer() {
  196. return S_OK;
  197. }
  198. HRESULT DeleteFileProgressSink::ResumeTimer() {
  199. return S_OK;
  200. }
  201. } // namespace
  202. namespace platform_util {
  203. bool ShowItemInFolder(const base::FilePath& full_path) {
  204. base::win::ScopedCOMInitializer com_initializer;
  205. if (!com_initializer.Succeeded())
  206. return false;
  207. base::FilePath dir = full_path.DirName().AsEndingWithSeparator();
  208. // ParseDisplayName will fail if the directory is "C:", it must be "C:\\".
  209. if (dir.empty())
  210. return false;
  211. Microsoft::WRL::ComPtr<IShellFolder> desktop;
  212. HRESULT hr = SHGetDesktopFolder(desktop.GetAddressOf());
  213. if (FAILED(hr))
  214. return false;
  215. base::win::ScopedCoMem<ITEMIDLIST> dir_item;
  216. hr = desktop->ParseDisplayName(NULL, NULL,
  217. const_cast<wchar_t*>(dir.value().c_str()),
  218. NULL, &dir_item, NULL);
  219. if (FAILED(hr)) {
  220. return ui::win::OpenFolderViaShell(dir);
  221. }
  222. base::win::ScopedCoMem<ITEMIDLIST> file_item;
  223. hr = desktop->ParseDisplayName(
  224. NULL, NULL, const_cast<wchar_t*>(full_path.value().c_str()), NULL,
  225. &file_item, NULL);
  226. if (FAILED(hr)) {
  227. return ui::win::OpenFolderViaShell(dir);
  228. }
  229. const ITEMIDLIST* highlight[] = {file_item};
  230. hr = SHOpenFolderAndSelectItems(dir_item, base::size(highlight), highlight,
  231. NULL);
  232. if (!FAILED(hr))
  233. return true;
  234. // On some systems, the above call mysteriously fails with "file not
  235. // found" even though the file is there. In these cases, ShellExecute()
  236. // seems to work as a fallback (although it won't select the file).
  237. if (hr == ERROR_FILE_NOT_FOUND) {
  238. return ui::win::OpenFolderViaShell(dir);
  239. } else {
  240. LPTSTR message = NULL;
  241. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  242. 0, hr, 0, reinterpret_cast<LPTSTR>(&message), 0, NULL);
  243. LOG(WARNING) << " " << __FUNCTION__ << "(): Can't open full_path = \""
  244. << full_path.value() << "\""
  245. << " hr = " << hr << " " << reinterpret_cast<LPTSTR>(&message);
  246. if (message)
  247. LocalFree(message);
  248. return ui::win::OpenFolderViaShell(dir);
  249. }
  250. }
  251. bool OpenItem(const base::FilePath& full_path) {
  252. if (base::DirectoryExists(full_path))
  253. return ui::win::OpenFolderViaShell(full_path);
  254. else
  255. return ui::win::OpenFileViaShell(full_path);
  256. }
  257. bool OpenExternal(const base::string16& url,
  258. const OpenExternalOptions& options) {
  259. // Quote the input scheme to be sure that the command does not have
  260. // parameters unexpected by the external program. This url should already
  261. // have been escaped.
  262. base::string16 escaped_url = L"\"" + url + L"\"";
  263. auto working_dir = options.working_dir.value();
  264. if (reinterpret_cast<ULONG_PTR>(
  265. ShellExecuteW(nullptr, L"open", escaped_url.c_str(), nullptr,
  266. working_dir.empty() ? nullptr : working_dir.c_str(),
  267. SW_SHOWNORMAL)) <= 32) {
  268. // We fail to execute the call. We could display a message to the user.
  269. // TODO(nsylvain): we should also add a dialog to warn on errors. See
  270. // bug 1136923.
  271. return false;
  272. }
  273. return true;
  274. }
  275. void OpenExternal(const base::string16& url,
  276. const OpenExternalOptions& options,
  277. OpenExternalCallback callback) {
  278. // TODO(gabriel): Implement async open if callback is specified
  279. std::move(callback).Run(OpenExternal(url, options) ? "" : "Failed to open");
  280. }
  281. bool MoveItemToTrash(const base::FilePath& path) {
  282. base::win::ScopedCOMInitializer com_initializer;
  283. if (!com_initializer.Succeeded())
  284. return false;
  285. Microsoft::WRL::ComPtr<IFileOperation> pfo;
  286. if (FAILED(::CoCreateInstance(CLSID_FileOperation, nullptr, CLSCTX_ALL,
  287. IID_PPV_ARGS(&pfo))))
  288. return false;
  289. // Elevation prompt enabled for UAC protected files. This overrides the
  290. // SILENT, NO_UI and NOERRORUI flags.
  291. if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
  292. // Windows 8 introduces the flag RECYCLEONDELETE and deprecates the
  293. // ALLOWUNDO in favor of ADDUNDORECORD.
  294. if (FAILED(pfo->SetOperationFlags(
  295. FOF_NO_UI | FOFX_ADDUNDORECORD | FOF_NOERRORUI | FOF_SILENT |
  296. FOFX_SHOWELEVATIONPROMPT | FOFX_RECYCLEONDELETE)))
  297. return false;
  298. } else {
  299. // For Windows 7 and Vista, RecycleOnDelete is the default behavior.
  300. if (FAILED(pfo->SetOperationFlags(FOF_NO_UI | FOF_ALLOWUNDO |
  301. FOF_NOERRORUI | FOF_SILENT |
  302. FOFX_SHOWELEVATIONPROMPT)))
  303. return false;
  304. }
  305. // Create an IShellItem from the supplied source path.
  306. Microsoft::WRL::ComPtr<IShellItem> delete_item;
  307. if (FAILED(SHCreateItemFromParsingName(
  308. path.value().c_str(), NULL,
  309. IID_PPV_ARGS(delete_item.GetAddressOf()))))
  310. return false;
  311. Microsoft::WRL::ComPtr<IFileOperationProgressSink> delete_sink(
  312. new DeleteFileProgressSink);
  313. if (!delete_sink)
  314. return false;
  315. // Processes the queued command DeleteItem. This will trigger
  316. // the DeleteFileProgressSink to check for Recycle Bin.
  317. return SUCCEEDED(pfo->DeleteItem(delete_item.Get(), delete_sink.Get())) &&
  318. SUCCEEDED(pfo->PerformOperations());
  319. }
  320. void Beep() {
  321. MessageBeep(MB_OK);
  322. }
  323. } // namespace platform_util