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