Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / platform_util_win.cc
bloba905b2f42d1fc3e381c33fe44e901b273a568011
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/platform_util.h"
7 #include <commdlg.h>
8 #include <dwmapi.h>
9 #include <shellapi.h>
10 #include <shlobj.h>
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/logging.h"
17 #include "base/metrics/field_trial.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/win/registry.h"
21 #include "base/win/scoped_co_mem.h"
22 #include "base/win/scoped_comptr.h"
23 #include "base/win/windows_version.h"
24 #include "chrome/browser/lifetime/application_lifetime.h"
25 #include "chrome/browser/platform_util_internal.h"
26 #include "chrome/browser/ui/host_desktop.h"
27 #include "chrome/common/chrome_utility_messages.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/utility_process_host.h"
30 #include "content/public/browser/utility_process_host_client.h"
31 #include "ui/base/win/shell.h"
32 #include "ui/gfx/native_widget_types.h"
33 #include "url/gurl.h"
35 using content::BrowserThread;
37 namespace platform_util {
39 namespace {
41 // TODO(asanka): Move this to ui/base/win/shell.{h,cc} and invoke it from the
42 // utility process.
43 void ShowItemInFolderOnFileThread(const base::FilePath& full_path) {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
45 base::FilePath dir = full_path.DirName().AsEndingWithSeparator();
46 // ParseDisplayName will fail if the directory is "C:", it must be "C:\\".
47 if (dir.empty())
48 return;
50 typedef HRESULT (WINAPI *SHOpenFolderAndSelectItemsFuncPtr)(
51 PCIDLIST_ABSOLUTE pidl_Folder,
52 UINT cidl,
53 PCUITEMID_CHILD_ARRAY pidls,
54 DWORD flags);
56 static SHOpenFolderAndSelectItemsFuncPtr open_folder_and_select_itemsPtr =
57 NULL;
58 static bool initialize_open_folder_proc = true;
59 if (initialize_open_folder_proc) {
60 initialize_open_folder_proc = false;
61 // The SHOpenFolderAndSelectItems API is exposed by shell32 version 6
62 // and does not exist in Win2K. We attempt to retrieve this function export
63 // from shell32 and if it does not exist, we just invoke ShellExecute to
64 // open the folder thus losing the functionality to select the item in
65 // the process.
66 HMODULE shell32_base = GetModuleHandle(L"shell32.dll");
67 if (!shell32_base) {
68 NOTREACHED() << " " << __FUNCTION__ << "(): Can't open shell32.dll";
69 return;
71 open_folder_and_select_itemsPtr =
72 reinterpret_cast<SHOpenFolderAndSelectItemsFuncPtr>
73 (GetProcAddress(shell32_base, "SHOpenFolderAndSelectItems"));
75 if (!open_folder_and_select_itemsPtr) {
76 ShellExecute(NULL, L"open", dir.value().c_str(), NULL, NULL, SW_SHOW);
77 return;
80 base::win::ScopedComPtr<IShellFolder> desktop;
81 HRESULT hr = SHGetDesktopFolder(desktop.Receive());
82 if (FAILED(hr))
83 return;
85 base::win::ScopedCoMem<ITEMIDLIST> dir_item;
86 hr = desktop->ParseDisplayName(NULL, NULL,
87 const_cast<wchar_t *>(dir.value().c_str()),
88 NULL, &dir_item, NULL);
89 if (FAILED(hr))
90 return;
92 base::win::ScopedCoMem<ITEMIDLIST> file_item;
93 hr = desktop->ParseDisplayName(NULL, NULL,
94 const_cast<wchar_t *>(full_path.value().c_str()),
95 NULL, &file_item, NULL);
96 if (FAILED(hr))
97 return;
99 const ITEMIDLIST* highlight[] = { file_item };
101 hr = (*open_folder_and_select_itemsPtr)(dir_item, arraysize(highlight),
102 highlight, NULL);
104 if (FAILED(hr)) {
105 // On some systems, the above call mysteriously fails with "file not
106 // found" even though the file is there. In these cases, ShellExecute()
107 // seems to work as a fallback (although it won't select the file).
108 if (hr == ERROR_FILE_NOT_FOUND) {
109 ShellExecute(NULL, L"open", dir.value().c_str(), NULL, NULL, SW_SHOW);
110 } else {
111 LOG(WARNING) << " " << __FUNCTION__ << "(): Can't open full_path = \""
112 << full_path.value() << "\""
113 << " hr = " << logging::SystemErrorCodeToString(hr);
118 // Old ShellExecute crashes the process when the command for a given scheme
119 // is empty. This function tells if it is.
120 bool ValidateShellCommandForScheme(const std::string& scheme) {
121 base::win::RegKey key;
122 base::string16 registry_path = base::ASCIIToUTF16(scheme) +
123 L"\\shell\\open\\command";
124 key.Open(HKEY_CLASSES_ROOT, registry_path.c_str(), KEY_READ);
125 if (!key.Valid())
126 return false;
127 DWORD size = 0;
128 key.ReadValue(NULL, NULL, &size, NULL);
129 if (size <= 2)
130 return false;
131 return true;
134 void OpenExternalOnFileThread(const GURL& url) {
135 // Quote the input scheme to be sure that the command does not have
136 // parameters unexpected by the external program. This url should already
137 // have been escaped.
138 std::string escaped_url = url.spec();
139 escaped_url.insert(0, "\"");
140 escaped_url += "\"";
142 // According to Mozilla in uriloader/exthandler/win/nsOSHelperAppService.cpp:
143 // "Some versions of windows (Win2k before SP3, Win XP before SP1) crash in
144 // ShellExecute on long URLs (bug 161357 on bugzilla.mozilla.org). IE 5 and 6
145 // support URLS of 2083 chars in length, 2K is safe."
146 const size_t kMaxUrlLength = 2048;
147 if (escaped_url.length() > kMaxUrlLength) {
148 NOTREACHED();
149 return;
152 if (base::win::GetVersion() < base::win::VERSION_WIN7) {
153 if (!ValidateShellCommandForScheme(url.scheme()))
154 return;
157 if (reinterpret_cast<ULONG_PTR>(ShellExecuteA(NULL, "open",
158 escaped_url.c_str(), NULL, NULL,
159 SW_SHOWNORMAL)) <= 32) {
160 // We fail to execute the call. We could display a message to the user.
161 // TODO(nsylvain): we should also add a dialog to warn on errors. See
162 // bug 1136923.
163 return;
167 void OpenItemViaShellInUtilityProcess(const base::FilePath& full_path,
168 OpenItemType type) {
169 base::WeakPtr<content::UtilityProcessHost> utility_process_host(
170 content::UtilityProcessHost::Create(NULL, NULL)->AsWeakPtr());
171 utility_process_host->DisableSandbox();
172 switch (type) {
173 case OPEN_FILE:
174 utility_process_host->Send(
175 new ChromeUtilityMsg_OpenFileViaShell(full_path));
176 return;
178 case OPEN_FOLDER:
179 utility_process_host->Send(
180 new ChromeUtilityMsg_OpenFolderViaShell(full_path));
181 return;
185 void ActivateDesktopIfNecessary() {
186 DCHECK_CURRENTLY_ON(BrowserThread::UI);
187 if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH)
188 chrome::ActivateDesktopHelper(chrome::ASH_KEEP_RUNNING);
191 } // namespace
193 void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) {
194 ActivateDesktopIfNecessary();
195 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
196 base::Bind(&ShowItemInFolderOnFileThread, full_path));
199 namespace internal {
201 void PlatformOpenVerifiedItem(const base::FilePath& path, OpenItemType type) {
202 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
203 base::Bind(&ActivateDesktopIfNecessary));
205 if (base::FieldTrialList::FindFullName("IsolateShellOperations") ==
206 "Enabled") {
207 BrowserThread::PostTask(
208 BrowserThread::IO, FROM_HERE,
209 base::Bind(&OpenItemViaShellInUtilityProcess, path, type));
210 } else {
211 switch (type) {
212 case OPEN_FILE:
213 ui::win::OpenFileViaShell(path);
214 break;
216 case OPEN_FOLDER:
217 ui::win::OpenFolderViaShell(path);
218 break;
223 } // namespace internal
225 void OpenExternal(Profile* profile, const GURL& url) {
226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
228 if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH &&
229 !url.SchemeIsHTTPOrHTTPS())
230 chrome::ActivateDesktopHelper(chrome::ASH_KEEP_RUNNING);
232 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
233 base::Bind(&OpenExternalOnFileThread, url));
236 } // namespace platform_util