OLD | NEW |
---|---|
(Empty) | |
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 | |
5 #ifndef BASE_WIN_SHORTCUT_H_ | |
6 #define BASE_WIN_SHORTCUT_H_ | |
7 | |
8 #include <windows.h> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/file_path.h" | |
12 #include "base/string16.h" | |
13 | |
14 namespace base { | |
15 namespace win { | |
16 | |
17 enum ShortcutOperation { | |
18 // Create a new shortcut (overwriting if necessary). | |
19 SHORTCUT_CREATE_ALWAYS = 0, | |
20 // Update specified (non-null) properties only on an existing shortcut. | |
21 SHORTCUT_UPDATE_EXISTING, | |
22 }; | |
23 | |
24 // Properties for shortcuts. Properties set will be applied to the shortcut on | |
25 // creation/update, others will be ignored. | |
26 // Callers are encouraged to use the setters provided which take care of | |
27 // setting |options| as desired. | |
28 struct ShortcutProperties { | |
29 enum IndividualProperties { | |
30 PROPERTIES_TARGET = 1 << 0, | |
31 PROPERTIES_WORKING_DIR = 1 << 1, | |
32 PROPERTIES_ARGUMENTS = 1 << 2, | |
33 PROPERTIES_DESCRIPTION = 1 << 3, | |
34 PROPERTIES_ICON = 1 << 4, | |
35 PROPERTIES_APP_ID = 1 << 5, | |
36 PROPERTIES_DUAL_MODE = 1 << 6, | |
37 }; | |
38 | |
39 ShortcutProperties() : options(0U) {} | |
40 | |
41 void set_target(const FilePath& target_in) { | |
42 target = target_in; | |
43 options |= PROPERTIES_TARGET; | |
44 } | |
45 | |
46 void set_working_dir(const FilePath& working_dir_in) { | |
47 working_dir = working_dir_in; | |
48 options |= PROPERTIES_WORKING_DIR; | |
49 } | |
50 | |
51 void set_arguments(const string16& arguments_in) { | |
52 // Size restriction as per MSDN at http://goo.gl/TJ7q5. | |
53 DCHECK(arguments_in.size() < MAX_PATH); | |
54 arguments = arguments_in; | |
55 options |= PROPERTIES_ARGUMENTS; | |
56 } | |
57 | |
58 void set_description(const string16& description_in) { | |
59 // Size restriction as per MSDN at http://goo.gl/OdNQq. | |
60 DCHECK(description_in.size() < MAX_PATH); | |
61 description = description_in; | |
62 options |= PROPERTIES_DESCRIPTION; | |
63 } | |
64 | |
65 void set_icon(const FilePath& icon_in, int icon_index_in) { | |
66 icon = icon_in; | |
67 icon_index = icon_index_in; | |
68 options |= PROPERTIES_ICON; | |
69 } | |
70 | |
71 void set_app_id(const string16& app_id_in) { | |
72 app_id = app_id_in; | |
73 options |= PROPERTIES_APP_ID; | |
74 } | |
75 | |
76 void set_dual_mode(bool dual_mode_in) { | |
77 dual_mode = dual_mode_in; | |
78 options |= PROPERTIES_DUAL_MODE; | |
79 } | |
80 | |
81 // The target to launch from this shortcut. This is mandatory when creating | |
82 // a shortcut. | |
83 FilePath target; | |
84 // The name of the working directory when launching the shortcut. | |
85 FilePath working_dir; | |
86 // The arguments to be applied to |target| when launching from this shortcut. | |
87 // The length of this string must be less than MAX_PATH. | |
88 string16 arguments; | |
89 // The localized description of the shortcut. | |
90 // The length of this string must be less than MAX_PATH. | |
91 string16 description; | |
92 // The path to the icon (can be a dll or exe, in which case |icon_index| is | |
93 // the resource id). | |
94 FilePath icon; | |
95 int icon_index; | |
96 // The app model id for the shortcut (Win7+). | |
97 string16 app_id; | |
98 // Whether this is a dual mode shortcut (Win8+). | |
99 // For now this property can only be set to true (i.e. once set it cannot be | |
100 // unset). | |
101 // TODO (gab): Make it possible to set this property to false. | |
102 bool dual_mode; | |
103 // Bitfield made of IndividualProperties. Properties set in |options| will be | |
104 // set on the shortcut, others will be ignored. | |
105 uint32 options; | |
106 }; | |
107 | |
108 // This method creates (or updates) a shortcut link at |shortcut_path| using the | |
109 // information given through |properties|. | |
110 // Ensure you have initialized COM before calling into this function. | |
111 // |operation|: a choice from the ShortcutOperation enum. | |
112 // If |operation| is SHORTCUT_UPDATE_EXISTING and |shortcut_path| does not | |
113 // exist, this method is a no-op and returns false. | |
114 BASE_EXPORT bool CreateOrUpdateShortcutLink( | |
115 const FilePath& shortcut_path, | |
116 const ShortcutProperties& properties, | |
117 ShortcutOperation operation); | |
118 | |
119 // Resolve Windows shortcut (.LNK file) | |
120 // This methods tries to resolve a shortcut .LNK file. The path of the shortcut | |
121 // to resolve is in |shortcut_path|. If |target_path| is not NULL, the target | |
122 // will be resolved and placed in |target_path|. If |args| is not NULL, the | |
123 // arguments will be retrieved and placed in |args|. The function returns true | |
124 // if all requested fields are found successfully. | |
125 // Callers can safely use the same variable for both |shortcut_path| and | |
126 // |target_path|. | |
127 BASE_EXPORT bool ResolveShortcut(const FilePath& shortcut_path, | |
128 FilePath* target_path, | |
129 string16* args); | |
130 | |
131 // Pins a shortcut to the Windows 7 taskbar. The shortcut file must already | |
132 // exist and be a shortcut that points to an executable. | |
133 BASE_EXPORT bool TaskbarPinShortcutLink(const wchar_t* shortcut); | |
134 | |
135 // Unpins a shortcut from the Windows 7 taskbar. The shortcut must exist and | |
136 // already be pinned to the taskbar. | |
137 BASE_EXPORT bool TaskbarUnpinShortcutLink(const wchar_t* shortcut); | |
138 | |
139 } // namespace Win | |
grt (UTC plus 2)
2012/09/10 17:46:59
nit: Win -> win
gab
2012/09/10 17:55:38
Good catch (also in another file), done in both pl
| |
140 } // namespace base | |
141 | |
142 #endif // BASE_WIN_SHORTCUT_H_ | |
OLD | NEW |