platform_util_win.cc 13 KB

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