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.
4
5 #include "chrome/browser/platform_util.h"
6
7 #include <commdlg.h>
8 #include <dwmapi.h>
9 #include <shellapi.h>
10 #include <shlobj.h>
11
12 #include "app/win/scoped_co_mem.h"
13 #include "app/win/shell.h"
14 #include "base/file_path.h"
15 #include "base/file_util.h"
16 #include "base/logging.h"
17 #include "base/path_service.h"
18 #include "base/string_util.h"
19 #include "base/utf_string_conversions.h"
20 #include "base/win/registry.h"
21 #include "base/win/scoped_comptr.h"
22 #include "chrome/installer/util/browser_distribution.h"
23 #include "chrome/installer/util/google_update_constants.h"
24 #include "chrome/installer/util/google_update_settings.h"
25 #include "chrome/installer/util/install_util.h"
26 #include "googleurl/src/gurl.h"
27 #include "ui/base/message_box_win.h"
28 #include "ui/gfx/native_widget_types.h"
29
30 namespace platform_util {
31
ShowItemInFolder(const FilePath & full_path)32 void ShowItemInFolder(const FilePath& full_path) {
33 FilePath dir = full_path.DirName();
34 // ParseDisplayName will fail if the directory is "C:", it must be "C:\\".
35 if (dir.value() == L"" || !file_util::EnsureEndsWithSeparator(&dir))
36 return;
37
38 typedef HRESULT (WINAPI *SHOpenFolderAndSelectItemsFuncPtr)(
39 PCIDLIST_ABSOLUTE pidl_Folder,
40 UINT cidl,
41 PCUITEMID_CHILD_ARRAY pidls,
42 DWORD flags);
43
44 static SHOpenFolderAndSelectItemsFuncPtr open_folder_and_select_itemsPtr =
45 NULL;
46 static bool initialize_open_folder_proc = true;
47 if (initialize_open_folder_proc) {
48 initialize_open_folder_proc = false;
49 // The SHOpenFolderAndSelectItems API is exposed by shell32 version 6
50 // and does not exist in Win2K. We attempt to retrieve this function export
51 // from shell32 and if it does not exist, we just invoke ShellExecute to
52 // open the folder thus losing the functionality to select the item in
53 // the process.
54 HMODULE shell32_base = GetModuleHandle(L"shell32.dll");
55 if (!shell32_base) {
56 NOTREACHED() << " " << __FUNCTION__ << "(): Can't open shell32.dll";
57 return;
58 }
59 open_folder_and_select_itemsPtr =
60 reinterpret_cast<SHOpenFolderAndSelectItemsFuncPtr>
61 (GetProcAddress(shell32_base, "SHOpenFolderAndSelectItems"));
62 }
63 if (!open_folder_and_select_itemsPtr) {
64 ShellExecute(NULL, L"open", dir.value().c_str(), NULL, NULL, SW_SHOW);
65 return;
66 }
67
68 base::win::ScopedComPtr<IShellFolder> desktop;
69 HRESULT hr = SHGetDesktopFolder(desktop.Receive());
70 if (FAILED(hr))
71 return;
72
73 app::win::ScopedCoMem<ITEMIDLIST> dir_item;
74 hr = desktop->ParseDisplayName(NULL, NULL,
75 const_cast<wchar_t *>(dir.value().c_str()),
76 NULL, &dir_item, NULL);
77 if (FAILED(hr))
78 return;
79
80 app::win::ScopedCoMem<ITEMIDLIST> file_item;
81 hr = desktop->ParseDisplayName(NULL, NULL,
82 const_cast<wchar_t *>(full_path.value().c_str()),
83 NULL, &file_item, NULL);
84 if (FAILED(hr))
85 return;
86
87 const ITEMIDLIST* highlight[] = {
88 {file_item},
89 };
90
91 hr = (*open_folder_and_select_itemsPtr)(dir_item, arraysize(highlight),
92 highlight, NULL);
93
94 if (FAILED(hr)) {
95 // On some systems, the above call mysteriously fails with "file not
96 // found" even though the file is there. In these cases, ShellExecute()
97 // seems to work as a fallback (although it won't select the file).
98 if (hr == ERROR_FILE_NOT_FOUND) {
99 ShellExecute(NULL, L"open", dir.value().c_str(), NULL, NULL, SW_SHOW);
100 } else {
101 LPTSTR message = NULL;
102 DWORD message_length = FormatMessage(
103 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
104 0, hr, 0, reinterpret_cast<LPTSTR>(&message), 0, NULL);
105 LOG(WARNING) << " " << __FUNCTION__
106 << "(): Can't open full_path = \""
107 << full_path.value() << "\""
108 << " hr = " << hr
109 << " " << reinterpret_cast<LPTSTR>(&message);
110 if (message)
111 LocalFree(message);
112 }
113 }
114 }
115
OpenItem(const FilePath & full_path)116 void OpenItem(const FilePath& full_path) {
117 app::win::OpenItemViaShell(full_path);
118 }
119
OpenExternal(const GURL & url)120 void OpenExternal(const GURL& url) {
121 // Quote the input scheme to be sure that the command does not have
122 // parameters unexpected by the external program. This url should already
123 // have been escaped.
124 std::string escaped_url = url.spec();
125 escaped_url.insert(0, "\"");
126 escaped_url += "\"";
127
128 // According to Mozilla in uriloader/exthandler/win/nsOSHelperAppService.cpp:
129 // "Some versions of windows (Win2k before SP3, Win XP before SP1) crash in
130 // ShellExecute on long URLs (bug 161357 on bugzilla.mozilla.org). IE 5 and 6
131 // support URLS of 2083 chars in length, 2K is safe."
132 const size_t kMaxUrlLength = 2048;
133 if (escaped_url.length() > kMaxUrlLength) {
134 NOTREACHED();
135 return;
136 }
137
138 base::win::RegKey key;
139 std::wstring registry_path = ASCIIToWide(url.scheme()) +
140 L"\\shell\\open\\command";
141 key.Open(HKEY_CLASSES_ROOT, registry_path.c_str(), KEY_READ);
142 if (key.Valid()) {
143 DWORD size = 0;
144 key.ReadValue(NULL, NULL, &size, NULL);
145 if (size <= 2) {
146 // ShellExecute crashes the process when the command is empty.
147 // We check for "2" because it always returns the trailing NULL.
148 // TODO(nsylvain): we should also add a dialog to warn on errors. See
149 // bug 1136923.
150 return;
151 }
152 }
153
154 if (reinterpret_cast<ULONG_PTR>(ShellExecuteA(NULL, "open",
155 escaped_url.c_str(), NULL, NULL,
156 SW_SHOWNORMAL)) <= 32) {
157 // We fail to execute the call. We could display a message to the user.
158 // TODO(nsylvain): we should also add a dialog to warn on errors. See
159 // bug 1136923.
160 return;
161 }
162 }
163
GetTopLevel(gfx::NativeView view)164 gfx::NativeWindow GetTopLevel(gfx::NativeView view) {
165 return ::GetAncestor(view, GA_ROOT);
166 }
167
GetParent(gfx::NativeView view)168 gfx::NativeView GetParent(gfx::NativeView view) {
169 return ::GetParent(view);
170 }
171
IsWindowActive(gfx::NativeWindow window)172 bool IsWindowActive(gfx::NativeWindow window) {
173 return ::GetForegroundWindow() == window;
174 }
175
ActivateWindow(gfx::NativeWindow window)176 void ActivateWindow(gfx::NativeWindow window) {
177 ::SetForegroundWindow(window);
178 }
179
IsVisible(gfx::NativeView view)180 bool IsVisible(gfx::NativeView view) {
181 // MSVC complains if we don't include != 0.
182 return ::IsWindowVisible(view) != 0;
183 }
184
SimpleErrorBox(gfx::NativeWindow parent,const string16 & title,const string16 & message)185 void SimpleErrorBox(gfx::NativeWindow parent,
186 const string16& title,
187 const string16& message) {
188 ui::MessageBox(parent, message, title,
189 MB_OK | MB_SETFOREGROUND | MB_ICONWARNING | MB_TOPMOST);
190 }
191
SimpleYesNoBox(gfx::NativeWindow parent,const string16 & title,const string16 & message)192 bool SimpleYesNoBox(gfx::NativeWindow parent,
193 const string16& title,
194 const string16& message) {
195 return ui::MessageBox(parent, message.c_str(), title.c_str(),
196 MB_YESNO | MB_ICONWARNING | MB_SETFOREGROUND) == IDYES;
197 }
198
GetVersionStringModifier()199 std::string GetVersionStringModifier() {
200 #if defined(GOOGLE_CHROME_BUILD)
201 FilePath module;
202 string16 channel;
203 if (PathService::Get(base::FILE_MODULE, &module)) {
204 bool is_system_install =
205 !InstallUtil::IsPerUserInstall(module.value().c_str());
206
207 GoogleUpdateSettings::GetChromeChannel(is_system_install, &channel);
208 }
209 return UTF16ToASCII(channel);
210 #else
211 return std::string();
212 #endif
213 }
214
CanSetAsDefaultBrowser()215 bool CanSetAsDefaultBrowser() {
216 return BrowserDistribution::GetDistribution()->CanSetAsDefault();
217 }
218
219 } // namespace platform_util
220