shortcut.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (c) 2012 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. #ifndef BASE_WIN_SHORTCUT_H_
  5. #define BASE_WIN_SHORTCUT_H_
  6. #include <windows.h>
  7. #include <stdint.h>
  8. #include "base/base_export.h"
  9. #include "base/check.h"
  10. #include "base/files/file_path.h"
  11. namespace base {
  12. namespace win {
  13. enum ShortcutOperation {
  14. // Create a new shortcut (overwriting if necessary).
  15. SHORTCUT_CREATE_ALWAYS = 0,
  16. // Overwrite an existing shortcut (fails if the shortcut doesn't exist).
  17. // If the arguments are not specified on the new shortcut, keep the old
  18. // shortcut's arguments.
  19. SHORTCUT_REPLACE_EXISTING,
  20. // Update specified properties only on an existing shortcut.
  21. SHORTCUT_UPDATE_EXISTING,
  22. };
  23. // Properties for shortcuts. Properties set will be applied to the shortcut on
  24. // creation/update, others will be ignored.
  25. // Callers are encouraged to use the setters provided which take care of
  26. // setting |options| as desired.
  27. struct BASE_EXPORT ShortcutProperties {
  28. enum IndividualProperties {
  29. PROPERTIES_TARGET = 1U << 0,
  30. PROPERTIES_WORKING_DIR = 1U << 1,
  31. PROPERTIES_ARGUMENTS = 1U << 2,
  32. PROPERTIES_DESCRIPTION = 1U << 3,
  33. PROPERTIES_ICON = 1U << 4,
  34. PROPERTIES_APP_ID = 1U << 5,
  35. PROPERTIES_DUAL_MODE = 1U << 6,
  36. PROPERTIES_TOAST_ACTIVATOR_CLSID = 1U << 7,
  37. // Be sure to update the values below when adding a new property.
  38. PROPERTIES_ALL = PROPERTIES_TARGET | PROPERTIES_WORKING_DIR |
  39. PROPERTIES_ARGUMENTS | PROPERTIES_DESCRIPTION |
  40. PROPERTIES_ICON | PROPERTIES_APP_ID |
  41. PROPERTIES_DUAL_MODE | PROPERTIES_TOAST_ACTIVATOR_CLSID
  42. };
  43. ShortcutProperties();
  44. ShortcutProperties(const ShortcutProperties& other);
  45. ~ShortcutProperties();
  46. void set_target(const FilePath& target_in) {
  47. target = target_in;
  48. options |= PROPERTIES_TARGET;
  49. }
  50. void set_working_dir(const FilePath& working_dir_in) {
  51. working_dir = working_dir_in;
  52. options |= PROPERTIES_WORKING_DIR;
  53. }
  54. void set_arguments(const std::wstring& arguments_in) {
  55. // Size restriction as per MSDN at http://goo.gl/TJ7q5.
  56. DCHECK(arguments_in.size() < MAX_PATH);
  57. arguments = arguments_in;
  58. options |= PROPERTIES_ARGUMENTS;
  59. }
  60. void set_description(const std::wstring& description_in) {
  61. // Size restriction as per MSDN at http://goo.gl/OdNQq.
  62. DCHECK(description_in.size() < MAX_PATH);
  63. description = description_in;
  64. options |= PROPERTIES_DESCRIPTION;
  65. }
  66. void set_icon(const FilePath& icon_in, int icon_index_in) {
  67. icon = icon_in;
  68. icon_index = icon_index_in;
  69. options |= PROPERTIES_ICON;
  70. }
  71. void set_app_id(const std::wstring& app_id_in) {
  72. app_id = app_id_in;
  73. options |= PROPERTIES_APP_ID;
  74. }
  75. void set_dual_mode(bool dual_mode_in) {
  76. dual_mode = dual_mode_in;
  77. options |= PROPERTIES_DUAL_MODE;
  78. }
  79. void set_toast_activator_clsid(const CLSID& toast_activator_clsid_in) {
  80. toast_activator_clsid = toast_activator_clsid_in;
  81. options |= PROPERTIES_TOAST_ACTIVATOR_CLSID;
  82. }
  83. // The target to launch from this shortcut. This is mandatory when creating
  84. // a shortcut.
  85. FilePath target;
  86. // The name of the working directory when launching the shortcut.
  87. FilePath working_dir;
  88. // The arguments to be applied to |target| when launching from this shortcut.
  89. // The length of this string must be less than MAX_PATH.
  90. std::wstring arguments;
  91. // The localized description of the shortcut.
  92. // The length of this string must be less than MAX_PATH.
  93. std::wstring description;
  94. // The path to the icon (can be a dll or exe, in which case |icon_index| is
  95. // the resource id).
  96. FilePath icon;
  97. int icon_index = -1;
  98. // The app model id for the shortcut.
  99. std::wstring app_id;
  100. // Whether this is a dual mode shortcut (Win8+).
  101. bool dual_mode = false;
  102. // The CLSID of the COM object registered with the OS via the shortcut. This
  103. // is for app activation via user interaction with a toast notification in the
  104. // Action Center. (Win10 version 1607, build 14393, and beyond).
  105. CLSID toast_activator_clsid;
  106. // Bitfield made of IndividualProperties. Properties set in |options| will be
  107. // set on the shortcut, others will be ignored.
  108. uint32_t options = 0U;
  109. };
  110. // This method creates (or updates) a shortcut link at |shortcut_path| using the
  111. // information given through |properties|.
  112. // Ensure you have initialized COM before calling into this function.
  113. // |operation|: a choice from the ShortcutOperation enum.
  114. // If |operation| is SHORTCUT_REPLACE_EXISTING or SHORTCUT_UPDATE_EXISTING and
  115. // |shortcut_path| does not exist, this method is a no-op and returns false.
  116. BASE_EXPORT bool CreateOrUpdateShortcutLink(
  117. const FilePath& shortcut_path,
  118. const ShortcutProperties& properties,
  119. ShortcutOperation operation);
  120. // Resolves Windows shortcut (.LNK file).
  121. // This methods tries to resolve selected properties of a shortcut .LNK file.
  122. // The path of the shortcut to resolve is in |shortcut_path|. |options| is a bit
  123. // field composed of ShortcutProperties::IndividualProperties, to specify which
  124. // properties to read. It should be non-0. The resulting data are read into
  125. // |properties|, which must not be NULL. Note: PROPERTIES_TARGET will retrieve
  126. // the target path as stored in the shortcut but won't attempt to resolve that
  127. // path so it may not be valid. The function returns true if all requested
  128. // properties are successfully read. Otherwise some reads have failed and
  129. // intermediate values written to |properties| should be ignored.
  130. BASE_EXPORT bool ResolveShortcutProperties(const FilePath& shortcut_path,
  131. uint32_t options,
  132. ShortcutProperties* properties);
  133. // Resolves Windows shortcut (.LNK file).
  134. // This is a wrapper to ResolveShortcutProperties() to handle the common use
  135. // case of resolving target and arguments. |target_path| and |args| are
  136. // optional output variables that are ignored if NULL (but at least one must be
  137. // non-NULL). The function returns true if all requested fields are found
  138. // successfully. Callers can safely use the same variable for both
  139. // |shortcut_path| and |target_path|.
  140. BASE_EXPORT bool ResolveShortcut(const FilePath& shortcut_path,
  141. FilePath* target_path,
  142. std::wstring* args);
  143. // Pin to taskbar is only supported on Windows 7 and Windows 8. Returns true on
  144. // those platforms.
  145. BASE_EXPORT bool CanPinShortcutToTaskbar();
  146. // Pins a shortcut to the taskbar on Windows 7 and 8. The |shortcut| file must
  147. // already exist and be a shortcut that points to an executable. The app id of
  148. // the shortcut is used to group windows and must be set correctly.
  149. BASE_EXPORT bool PinShortcutToTaskbar(const FilePath& shortcut);
  150. // Unpins a shortcut from the Windows 7+ taskbar. The |shortcut| must exist and
  151. // already be pinned to the taskbar. The app id of the shortcut is used as the
  152. // identifier for the taskbar item to remove and must be set correctly.
  153. BASE_EXPORT bool UnpinShortcutFromTaskbar(const FilePath& shortcut);
  154. } // namespace win
  155. } // namespace base
  156. #endif // BASE_WIN_SHORTCUT_H_