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"
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
14 #include "base/files/file_path.h"
15 #include "base/logging.h"
16 #include "base/metrics/field_trial.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/win/registry.h"
20 #include "base/win/scoped_co_mem.h"
21 #include "base/win/scoped_comptr.h"
22 #include "base/win/windows_version.h"
23 #include "chrome/browser/lifetime/application_lifetime.h"
24 #include "chrome/browser/ui/host_desktop.h"
25 #include "chrome/common/chrome_utility_messages.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/utility_process_host.h"
28 #include "ui/base/win/shell.h"
29 #include "ui/gfx/native_widget_types.h"
32 using content::BrowserThread
;
36 void ShowItemInFolderOnFileThread(const base::FilePath
& full_path
) {
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
38 base::FilePath dir
= full_path
.DirName().AsEndingWithSeparator();
39 // ParseDisplayName will fail if the directory is "C:", it must be "C:\\".
43 typedef HRESULT (WINAPI
*SHOpenFolderAndSelectItemsFuncPtr
)(
44 PCIDLIST_ABSOLUTE pidl_Folder
,
46 PCUITEMID_CHILD_ARRAY pidls
,
49 static SHOpenFolderAndSelectItemsFuncPtr open_folder_and_select_itemsPtr
=
51 static bool initialize_open_folder_proc
= true;
52 if (initialize_open_folder_proc
) {
53 initialize_open_folder_proc
= false;
54 // The SHOpenFolderAndSelectItems API is exposed by shell32 version 6
55 // and does not exist in Win2K. We attempt to retrieve this function export
56 // from shell32 and if it does not exist, we just invoke ShellExecute to
57 // open the folder thus losing the functionality to select the item in
59 HMODULE shell32_base
= GetModuleHandle(L
"shell32.dll");
61 NOTREACHED() << " " << __FUNCTION__
<< "(): Can't open shell32.dll";
64 open_folder_and_select_itemsPtr
=
65 reinterpret_cast<SHOpenFolderAndSelectItemsFuncPtr
>
66 (GetProcAddress(shell32_base
, "SHOpenFolderAndSelectItems"));
68 if (!open_folder_and_select_itemsPtr
) {
69 ShellExecute(NULL
, L
"open", dir
.value().c_str(), NULL
, NULL
, SW_SHOW
);
73 base::win::ScopedComPtr
<IShellFolder
> desktop
;
74 HRESULT hr
= SHGetDesktopFolder(desktop
.Receive());
78 base::win::ScopedCoMem
<ITEMIDLIST
> dir_item
;
79 hr
= desktop
->ParseDisplayName(NULL
, NULL
,
80 const_cast<wchar_t *>(dir
.value().c_str()),
81 NULL
, &dir_item
, NULL
);
85 base::win::ScopedCoMem
<ITEMIDLIST
> file_item
;
86 hr
= desktop
->ParseDisplayName(NULL
, NULL
,
87 const_cast<wchar_t *>(full_path
.value().c_str()),
88 NULL
, &file_item
, NULL
);
92 const ITEMIDLIST
* highlight
[] = { file_item
};
94 hr
= (*open_folder_and_select_itemsPtr
)(dir_item
, arraysize(highlight
),
98 // On some systems, the above call mysteriously fails with "file not
99 // found" even though the file is there. In these cases, ShellExecute()
100 // seems to work as a fallback (although it won't select the file).
101 if (hr
== ERROR_FILE_NOT_FOUND
) {
102 ShellExecute(NULL
, L
"open", dir
.value().c_str(), NULL
, NULL
, SW_SHOW
);
104 LOG(WARNING
) << " " << __FUNCTION__
105 << "(): Can't open full_path = \""
106 << full_path
.value() << "\""
107 << " hr = " << logging::SystemErrorCodeToString(hr
);
112 // Old ShellExecute crashes the process when the command for a given scheme
113 // is empty. This function tells if it is.
114 bool ValidateShellCommandForScheme(const std::string
& scheme
) {
115 base::win::RegKey key
;
116 std::wstring registry_path
= base::ASCIIToWide(scheme
) +
117 L
"\\shell\\open\\command";
118 key
.Open(HKEY_CLASSES_ROOT
, registry_path
.c_str(), KEY_READ
);
122 key
.ReadValue(NULL
, NULL
, &size
, NULL
);
128 void OpenExternalOnFileThread(const GURL
& url
) {
129 // Quote the input scheme to be sure that the command does not have
130 // parameters unexpected by the external program. This url should already
131 // have been escaped.
132 std::string escaped_url
= url
.spec();
133 escaped_url
.insert(0, "\"");
136 // According to Mozilla in uriloader/exthandler/win/nsOSHelperAppService.cpp:
137 // "Some versions of windows (Win2k before SP3, Win XP before SP1) crash in
138 // ShellExecute on long URLs (bug 161357 on bugzilla.mozilla.org). IE 5 and 6
139 // support URLS of 2083 chars in length, 2K is safe."
140 const size_t kMaxUrlLength
= 2048;
141 if (escaped_url
.length() > kMaxUrlLength
) {
146 if (base::win::GetVersion() < base::win::VERSION_WIN7
) {
147 if (!ValidateShellCommandForScheme(url
.scheme()))
151 if (reinterpret_cast<ULONG_PTR
>(ShellExecuteA(NULL
, "open",
152 escaped_url
.c_str(), NULL
, NULL
,
153 SW_SHOWNORMAL
)) <= 32) {
154 // We fail to execute the call. We could display a message to the user.
155 // TODO(nsylvain): we should also add a dialog to warn on errors. See
161 void OpenItemViaShellInUtilityProcess(const base::FilePath
& full_path
) {
162 base::WeakPtr
<content::UtilityProcessHost
> utility_process_host(
163 content::UtilityProcessHost::Create(NULL
, NULL
)->AsWeakPtr());
164 utility_process_host
->DisableSandbox();
165 utility_process_host
->Send(new ChromeUtilityMsg_OpenItemViaShell(full_path
));
170 namespace platform_util
{
172 void ShowItemInFolder(Profile
* profile
, const base::FilePath
& full_path
) {
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
175 if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH
)
176 chrome::ActivateDesktopHelper(chrome::ASH_KEEP_RUNNING
);
178 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE
,
179 base::Bind(&ShowItemInFolderOnFileThread
, full_path
));
182 void OpenItem(Profile
* profile
, const base::FilePath
& full_path
) {
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
185 if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH
)
186 chrome::ActivateDesktopHelper(chrome::ASH_KEEP_RUNNING
);
188 if (base::FieldTrialList::FindFullName("IsolateShellOperations") ==
190 BrowserThread::PostTask(
193 base::Bind(&OpenItemViaShellInUtilityProcess
, full_path
));
195 BrowserThread::PostTask(
198 base::Bind(base::IgnoreResult(&ui::win::OpenItemViaShell
), full_path
));
202 void OpenExternal(Profile
* profile
, const GURL
& url
) {
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
205 if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH
&&
206 !url
.SchemeIsHTTPOrHTTPS())
207 chrome::ActivateDesktopHelper(chrome::ASH_KEEP_RUNNING
);
209 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE
,
210 base::Bind(&OpenExternalOnFileThread
, url
));
213 #if !defined(USE_AURA)
214 gfx::NativeWindow
GetTopLevel(gfx::NativeView view
) {
215 return ::GetAncestor(view
, GA_ROOT
);
218 gfx::NativeView
GetParent(gfx::NativeView view
) {
219 return ::GetParent(view
);
222 bool IsWindowActive(gfx::NativeWindow window
) {
223 return ::GetForegroundWindow() == window
;
226 void ActivateWindow(gfx::NativeWindow window
) {
227 ::SetForegroundWindow(window
);
230 bool IsVisible(gfx::NativeView view
) {
231 // MSVC complains if we don't include != 0.
232 return ::IsWindowVisible(view
) != 0;
236 } // namespace platform_util