1 // Copyright 2012 The Chromium Authors
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 "base/win/shortcut.h"
6
7 #include <objbase.h>
8 #include <propkey.h>
9 #include <shlobj.h>
10 #include <wrl/client.h>
11
12 #include "base/files/file_util.h"
13 #include "base/logging.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/scoped_blocking_call.h"
17 #include "base/win/scoped_propvariant.h"
18 #include "base/win/win_util.h"
19 #include "base/win/windows_version.h"
20
21 namespace base {
22 namespace win {
23
24 namespace {
25
26 using Microsoft::WRL::ComPtr;
27
28 // Initializes |i_shell_link| and |i_persist_file| (releasing them first if they
29 // are already initialized).
30 // If |shortcut| is not NULL, loads |shortcut| into |i_persist_file|.
31 // If any of the above steps fail, both |i_shell_link| and |i_persist_file| will
32 // be released.
InitializeShortcutInterfaces(const wchar_t * shortcut,ComPtr<IShellLink> * i_shell_link,ComPtr<IPersistFile> * i_persist_file)33 void InitializeShortcutInterfaces(const wchar_t* shortcut,
34 ComPtr<IShellLink>* i_shell_link,
35 ComPtr<IPersistFile>* i_persist_file) {
36 // Reset in the inverse order of acquisition.
37 i_persist_file->Reset();
38 i_shell_link->Reset();
39
40 ComPtr<IShellLink> shell_link;
41 if (FAILED(::CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER,
42 IID_PPV_ARGS(&shell_link)))) {
43 return;
44 }
45 ComPtr<IPersistFile> persist_file;
46 if (FAILED(shell_link.As(&persist_file)))
47 return;
48 if (shortcut && FAILED(persist_file->Load(shortcut, STGM_READWRITE)))
49 return;
50 i_shell_link->Swap(shell_link);
51 i_persist_file->Swap(persist_file);
52 }
53
54 } // namespace
55
56 ShortcutProperties::ShortcutProperties() = default;
57
58 ShortcutProperties::ShortcutProperties(const ShortcutProperties& other) =
59 default;
60
61 ShortcutProperties::~ShortcutProperties() = default;
62
set_description(const std::wstring & description_in)63 void ShortcutProperties::set_description(const std::wstring& description_in) {
64 // Size restriction as per MSDN at http://goo.gl/OdNQq.
65 DCHECK_LE(description_in.size(), static_cast<size_t>(INFOTIPSIZE));
66 description = description_in;
67 options |= PROPERTIES_DESCRIPTION;
68 }
69
CreateOrUpdateShortcutLink(const FilePath & shortcut_path,const ShortcutProperties & properties,ShortcutOperation operation)70 bool CreateOrUpdateShortcutLink(const FilePath& shortcut_path,
71 const ShortcutProperties& properties,
72 ShortcutOperation operation) {
73 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
74
75 // Make sure the parent directories exist when creating the shortcut.
76 if (operation == ShortcutOperation::kCreateAlways &&
77 !base::CreateDirectory(shortcut_path.DirName())) {
78 DLOG(ERROR) << "CreateDirectory " << shortcut_path.DirName() << " failed";
79 return false;
80 }
81 // A target is required unless |operation| is kUpdateExisting.
82 if (operation != ShortcutOperation::kUpdateExisting &&
83 !(properties.options & ShortcutProperties::PROPERTIES_TARGET)) {
84 NOTREACHED();
85 return false;
86 }
87
88 bool shortcut_existed = PathExists(shortcut_path);
89
90 // Interfaces to the old shortcut when replacing an existing shortcut.
91 ComPtr<IShellLink> old_i_shell_link;
92 ComPtr<IPersistFile> old_i_persist_file;
93
94 // Interfaces to the shortcut being created/updated.
95 ComPtr<IShellLink> i_shell_link;
96 ComPtr<IPersistFile> i_persist_file;
97 switch (operation) {
98 case ShortcutOperation::kCreateAlways:
99 InitializeShortcutInterfaces(nullptr, &i_shell_link, &i_persist_file);
100 break;
101 case ShortcutOperation::kUpdateExisting:
102 InitializeShortcutInterfaces(shortcut_path.value().c_str(), &i_shell_link,
103 &i_persist_file);
104 break;
105 case ShortcutOperation::kReplaceExisting:
106 InitializeShortcutInterfaces(shortcut_path.value().c_str(),
107 &old_i_shell_link, &old_i_persist_file);
108 // Confirm |shortcut_path| exists and is a shortcut by verifying
109 // |old_i_persist_file| was successfully initialized in the call above. If
110 // so, initialize the interfaces to begin writing a new shortcut (to
111 // overwrite the current one if successful).
112 if (old_i_persist_file.Get())
113 InitializeShortcutInterfaces(nullptr, &i_shell_link, &i_persist_file);
114 break;
115 default:
116 NOTREACHED();
117 }
118
119 // Return false immediately upon failure to initialize shortcut interfaces.
120 if (!i_persist_file.Get())
121 return false;
122
123 if ((properties.options & ShortcutProperties::PROPERTIES_TARGET) &&
124 FAILED(i_shell_link->SetPath(properties.target.value().c_str()))) {
125 return false;
126 }
127
128 if ((properties.options & ShortcutProperties::PROPERTIES_WORKING_DIR) &&
129 FAILED(i_shell_link->SetWorkingDirectory(
130 properties.working_dir.value().c_str()))) {
131 return false;
132 }
133
134 if (properties.options & ShortcutProperties::PROPERTIES_ARGUMENTS) {
135 if (FAILED(i_shell_link->SetArguments(properties.arguments.c_str())))
136 return false;
137 } else if (old_i_persist_file.Get()) {
138 wchar_t current_arguments[MAX_PATH] = {0};
139 if (SUCCEEDED(
140 old_i_shell_link->GetArguments(current_arguments, MAX_PATH))) {
141 i_shell_link->SetArguments(current_arguments);
142 }
143 }
144
145 if ((properties.options & ShortcutProperties::PROPERTIES_DESCRIPTION) &&
146 FAILED(i_shell_link->SetDescription(properties.description.c_str()))) {
147 return false;
148 }
149
150 if ((properties.options & ShortcutProperties::PROPERTIES_ICON) &&
151 FAILED(i_shell_link->SetIconLocation(properties.icon.value().c_str(),
152 properties.icon_index))) {
153 return false;
154 }
155
156 bool has_app_id =
157 (properties.options & ShortcutProperties::PROPERTIES_APP_ID) != 0;
158 bool has_dual_mode =
159 (properties.options & ShortcutProperties::PROPERTIES_DUAL_MODE) != 0;
160 bool has_toast_activator_clsid =
161 (properties.options &
162 ShortcutProperties::PROPERTIES_TOAST_ACTIVATOR_CLSID) != 0;
163 if (has_app_id || has_dual_mode || has_toast_activator_clsid) {
164 ComPtr<IPropertyStore> property_store;
165 if (FAILED(i_shell_link.As(&property_store)) || !property_store.Get())
166 return false;
167
168 if (has_app_id && !SetAppIdForPropertyStore(property_store.Get(),
169 properties.app_id.c_str())) {
170 return false;
171 }
172 if (has_dual_mode && !SetBooleanValueForPropertyStore(
173 property_store.Get(), PKEY_AppUserModel_IsDualMode,
174 properties.dual_mode)) {
175 return false;
176 }
177 if (has_toast_activator_clsid &&
178 !SetClsidForPropertyStore(property_store.Get(),
179 PKEY_AppUserModel_ToastActivatorCLSID,
180 properties.toast_activator_clsid)) {
181 return false;
182 }
183 }
184
185 // Release the interfaces to the old shortcut to make sure it doesn't prevent
186 // overwriting it if needed.
187 old_i_persist_file.Reset();
188 old_i_shell_link.Reset();
189
190 HRESULT result = i_persist_file->Save(shortcut_path.value().c_str(), TRUE);
191
192 // Release the interfaces in case the SHChangeNotify call below depends on
193 // the operations above being fully completed.
194 i_persist_file.Reset();
195 i_shell_link.Reset();
196
197 // If we successfully created/updated the icon, notify the shell that we have
198 // done so.
199 if (!SUCCEEDED(result))
200 return false;
201
202 SHChangeNotify(shortcut_existed ? SHCNE_UPDATEITEM : SHCNE_CREATE,
203 SHCNF_PATH | SHCNF_FLUSH, shortcut_path.value().c_str(),
204 nullptr);
205
206 return true;
207 }
208
ResolveShortcutProperties(const FilePath & shortcut_path,uint32_t options,ShortcutProperties * properties)209 bool ResolveShortcutProperties(const FilePath& shortcut_path,
210 uint32_t options,
211 ShortcutProperties* properties) {
212 DCHECK(options && properties);
213 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
214
215 if (options & ~ShortcutProperties::PROPERTIES_ALL)
216 NOTREACHED() << "Unhandled property is used.";
217
218 ComPtr<IShellLink> i_shell_link;
219
220 // Get pointer to the IShellLink interface.
221 if (FAILED(::CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER,
222 IID_PPV_ARGS(&i_shell_link)))) {
223 return false;
224 }
225
226 ComPtr<IPersistFile> persist;
227 // Query IShellLink for the IPersistFile interface.
228 if (FAILED(i_shell_link.As(&persist)))
229 return false;
230
231 // Load the shell link.
232 if (FAILED(persist->Load(shortcut_path.value().c_str(), STGM_READ)))
233 return false;
234
235 // Reset |properties|.
236 properties->options = 0;
237
238 wchar_t temp[MAX_PATH];
239 if (options & ShortcutProperties::PROPERTIES_TARGET) {
240 if (FAILED(
241 i_shell_link->GetPath(temp, MAX_PATH, nullptr, SLGP_UNCPRIORITY))) {
242 return false;
243 }
244 properties->set_target(FilePath(temp));
245 }
246
247 if (options & ShortcutProperties::PROPERTIES_WORKING_DIR) {
248 if (FAILED(i_shell_link->GetWorkingDirectory(temp, MAX_PATH)))
249 return false;
250 properties->set_working_dir(FilePath(temp));
251 }
252
253 if (options & ShortcutProperties::PROPERTIES_ARGUMENTS) {
254 if (FAILED(i_shell_link->GetArguments(temp, MAX_PATH)))
255 return false;
256 properties->set_arguments(temp);
257 }
258
259 if (options & ShortcutProperties::PROPERTIES_DESCRIPTION) {
260 // Note: description length constrained by MAX_PATH.
261 if (FAILED(i_shell_link->GetDescription(temp, MAX_PATH)))
262 return false;
263 properties->set_description(temp);
264 }
265
266 if (options & ShortcutProperties::PROPERTIES_ICON) {
267 int temp_index;
268 if (FAILED(i_shell_link->GetIconLocation(temp, MAX_PATH, &temp_index))) {
269 return false;
270 }
271 properties->set_icon(FilePath(temp), temp_index);
272 }
273
274 if (options & (ShortcutProperties::PROPERTIES_APP_ID |
275 ShortcutProperties::PROPERTIES_DUAL_MODE |
276 ShortcutProperties::PROPERTIES_TOAST_ACTIVATOR_CLSID)) {
277 ComPtr<IPropertyStore> property_store;
278 if (FAILED(i_shell_link.As(&property_store)))
279 return false;
280
281 if (options & ShortcutProperties::PROPERTIES_APP_ID) {
282 ScopedPropVariant pv_app_id;
283 if (property_store->GetValue(PKEY_AppUserModel_ID, pv_app_id.Receive()) !=
284 S_OK) {
285 return false;
286 }
287 switch (pv_app_id.get().vt) {
288 case VT_EMPTY:
289 properties->set_app_id(std::wstring());
290 break;
291 case VT_LPWSTR:
292 properties->set_app_id(pv_app_id.get().pwszVal);
293 break;
294 default:
295 NOTREACHED() << "Unexpected variant type: " << pv_app_id.get().vt;
296 return false;
297 }
298 }
299
300 if (options & ShortcutProperties::PROPERTIES_DUAL_MODE) {
301 ScopedPropVariant pv_dual_mode;
302 if (property_store->GetValue(PKEY_AppUserModel_IsDualMode,
303 pv_dual_mode.Receive()) != S_OK) {
304 return false;
305 }
306 switch (pv_dual_mode.get().vt) {
307 case VT_EMPTY:
308 properties->set_dual_mode(false);
309 break;
310 case VT_BOOL:
311 properties->set_dual_mode(pv_dual_mode.get().boolVal == VARIANT_TRUE);
312 break;
313 default:
314 NOTREACHED() << "Unexpected variant type: " << pv_dual_mode.get().vt;
315 return false;
316 }
317 }
318
319 if (options & ShortcutProperties::PROPERTIES_TOAST_ACTIVATOR_CLSID) {
320 ScopedPropVariant pv_toast_activator_clsid;
321 if (property_store->GetValue(PKEY_AppUserModel_ToastActivatorCLSID,
322 pv_toast_activator_clsid.Receive()) !=
323 S_OK) {
324 return false;
325 }
326 switch (pv_toast_activator_clsid.get().vt) {
327 case VT_EMPTY:
328 properties->set_toast_activator_clsid(CLSID_NULL);
329 break;
330 case VT_CLSID:
331 properties->set_toast_activator_clsid(
332 *(pv_toast_activator_clsid.get().puuid));
333 break;
334 default:
335 NOTREACHED() << "Unexpected variant type: "
336 << pv_toast_activator_clsid.get().vt;
337 return false;
338 }
339 }
340 }
341
342 return true;
343 }
344
ResolveShortcut(const FilePath & shortcut_path,FilePath * target_path,std::wstring * args)345 bool ResolveShortcut(const FilePath& shortcut_path,
346 FilePath* target_path,
347 std::wstring* args) {
348 uint32_t options = 0;
349 if (target_path)
350 options |= ShortcutProperties::PROPERTIES_TARGET;
351 if (args)
352 options |= ShortcutProperties::PROPERTIES_ARGUMENTS;
353 DCHECK(options);
354
355 ShortcutProperties properties;
356 if (!ResolveShortcutProperties(shortcut_path, options, &properties))
357 return false;
358
359 if (target_path)
360 *target_path = properties.target;
361 if (args)
362 *args = properties.arguments;
363 return true;
364 }
365
366 } // namespace win
367 } // namespace base
368