1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/common/platform_util.h"
#include <commdlg.h>
#include <dwmapi.h>
#include <shellapi.h>
#include <shlobj.h>
#include "app/gfx/native_widget_types.h"
#include "app/win_util.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/registry.h"
#include "base/scoped_comptr_win.h"
#include "base/string_util.h"
#include "chrome/installer/util/google_update_constants.h"
#include "googleurl/src/gurl.h"
namespace platform_util {
void ShowItemInFolder(const FilePath& full_path) {
FilePath dir = full_path.DirName();
// ParseDisplayName will fail if the directory is "C:", it must be "C:\\".
if (dir.value() == L"" || !file_util::EnsureEndsWithSeparator(&dir))
return;
typedef HRESULT (WINAPI *SHOpenFolderAndSelectItemsFuncPtr)(
PCIDLIST_ABSOLUTE pidl_Folder,
UINT cidl,
PCUITEMID_CHILD_ARRAY pidls,
DWORD flags);
static SHOpenFolderAndSelectItemsFuncPtr open_folder_and_select_itemsPtr =
NULL;
static bool initialize_open_folder_proc = true;
if (initialize_open_folder_proc) {
initialize_open_folder_proc = false;
// The SHOpenFolderAndSelectItems API is exposed by shell32 version 6
// and does not exist in Win2K. We attempt to retrieve this function export
// from shell32 and if it does not exist, we just invoke ShellExecute to
// open the folder thus losing the functionality to select the item in
// the process.
HMODULE shell32_base = GetModuleHandle(L"shell32.dll");
if (!shell32_base) {
NOTREACHED();
return;
}
open_folder_and_select_itemsPtr =
reinterpret_cast<SHOpenFolderAndSelectItemsFuncPtr>
(GetProcAddress(shell32_base, "SHOpenFolderAndSelectItems"));
}
if (!open_folder_and_select_itemsPtr) {
ShellExecute(NULL, _T("open"), dir.value().c_str(), NULL, NULL, SW_SHOW);
return;
}
ScopedComPtr<IShellFolder> desktop;
HRESULT hr = SHGetDesktopFolder(desktop.Receive());
if (FAILED(hr))
return;
win_util::CoMemReleaser<ITEMIDLIST> dir_item;
hr = desktop->ParseDisplayName(NULL, NULL,
const_cast<wchar_t *>(dir.value().c_str()),
NULL, &dir_item, NULL);
if (FAILED(hr))
return;
win_util::CoMemReleaser<ITEMIDLIST> file_item;
hr = desktop->ParseDisplayName(NULL, NULL,
const_cast<wchar_t *>(full_path.value().c_str()),
NULL, &file_item, NULL);
if (FAILED(hr))
return;
const ITEMIDLIST* highlight[] = {
{file_item},
};
(*open_folder_and_select_itemsPtr)(dir_item, arraysize(highlight),
highlight, NULL);
}
void OpenItem(const FilePath& full_path) {
win_util::OpenItemViaShell(full_path);
}
void OpenExternal(const GURL& url) {
// Quote the input scheme to be sure that the command does not have
// parameters unexpected by the external program. This url should already
// have been escaped.
std::string escaped_url = url.spec();
escaped_url.insert(0, "\"");
escaped_url += "\"";
// According to Mozilla in uriloader/exthandler/win/nsOSHelperAppService.cpp:
// "Some versions of windows (Win2k before SP3, Win XP before SP1) crash in
// ShellExecute on long URLs (bug 161357 on bugzilla.mozilla.org). IE 5 and 6
// support URLS of 2083 chars in length, 2K is safe."
const size_t kMaxUrlLength = 2048;
if (escaped_url.length() > kMaxUrlLength) {
NOTREACHED();
return;
}
RegKey key;
std::wstring registry_path = ASCIIToWide(url.scheme()) +
L"\\shell\\open\\command";
key.Open(HKEY_CLASSES_ROOT, registry_path.c_str());
if (key.Valid()) {
DWORD size = 0;
key.ReadValue(NULL, NULL, &size);
if (size <= 2) {
// ShellExecute crashes the process when the command is empty.
// We check for "2" because it always returns the trailing NULL.
// TODO(nsylvain): we should also add a dialog to warn on errors. See
// bug 1136923.
return;
}
}
if (reinterpret_cast<ULONG_PTR>(ShellExecuteA(NULL, "open",
escaped_url.c_str(), NULL, NULL,
SW_SHOWNORMAL)) <= 32) {
// We fail to execute the call. We could display a message to the user.
// TODO(nsylvain): we should also add a dialog to warn on errors. See
// bug 1136923.
return;
}
}
gfx::NativeWindow GetTopLevel(gfx::NativeView view) {
return GetAncestor(view, GA_ROOT);
}
bool IsWindowActive(gfx::NativeWindow window) {
return ::GetForegroundWindow() == window;
}
bool IsVisible(gfx::NativeView view) {
// MSVC complains if we don't include != 0.
return ::IsWindowVisible(view) != 0;
}
void SimpleErrorBox(gfx::NativeWindow parent,
const string16& title,
const string16& message) {
win_util::MessageBox(parent, message, title, MB_OK | MB_SETFOREGROUND);
}
namespace {
std::wstring CurrentChromeChannel() {
// Start by seeing if we can find the Clients key on the HKLM branch.
// For each search, require confirmation by looking for "name"
// inside it. We've noticed problems cleaning up the registry on
// uninstall or upgrade (http://crbug.com/33532,
// http://crbug.com/33534), and have other reports of inconsistency
// (http://crbug.com/32479).
HKEY registry_hive = HKEY_LOCAL_MACHINE;
std::wstring key = google_update::kRegPathClients + std::wstring(L"\\") +
google_update::kChromeUpgradeCode;
RegKey google_update_hklm(registry_hive, key.c_str(), KEY_READ);
if (!google_update_hklm.Valid() ||
!google_update_hklm.ValueExists(google_update::kRegNameField)) {
// HKLM failed us, try the same for the HKCU branch.
registry_hive = HKEY_CURRENT_USER;
RegKey google_update_hkcu(registry_hive, key.c_str(), KEY_READ);
if (!google_update_hkcu.Valid() ||
!google_update_hkcu.ValueExists(google_update::kRegNameField)) {
// Unknown.
registry_hive = 0;
}
}
std::wstring update_branch(L"unknown"); // the default.
if (registry_hive != 0) {
// Now that we know which hive to use, read the 'ap' key from it.
std::wstring key = google_update::kRegPathClientState +
std::wstring(L"\\") + google_update::kChromeUpgradeCode;
RegKey client_state(registry_hive, key.c_str(), KEY_READ);
client_state.ReadValue(google_update::kRegApField, &update_branch);
// If the parent folder exists (we have a valid install) but the
// 'ap' key is empty, we necessarily are the stable channel.
// So we print nothing.
if (update_branch.empty() && client_state.Valid()) {
update_branch = L"";
}
}
// Map to something pithy for human consumption. There are no rules as to
// what the ap string can contain, but generally it will contain a number
// followed by a dash followed by the branch name (and then some random
// suffix). We fall back on empty string in case we fail to parse.
// Only ever return "", "unknown", "dev" or "beta".
if (update_branch.find(L"-beta") != std::wstring::npos)
update_branch = L"beta";
else if (update_branch.find(L"-dev") != std::wstring::npos)
update_branch = L"dev";
else if (!update_branch.empty())
update_branch = L"unknown";
return update_branch;
}
} // namespace
string16 GetVersionStringModifier() {
#if defined(GOOGLE_CHROME_BUILD)
return CurrentChromeChannel();
#else
return string16();
#endif
}
} // namespace platform_util
|