OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/win/shortcut.h" | 5 #include "base/win/shortcut.h" |
6 | 6 |
7 #include <shellapi.h> | 7 #include <shellapi.h> |
8 #include <shlobj.h> | 8 #include <shlobj.h> |
9 #include <propkey.h> | 9 #include <propkey.h> |
10 | 10 |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
13 #include "base/win/scoped_comptr.h" | 13 #include "base/win/scoped_comptr.h" |
14 #include "base/win/scoped_propvariant.h" | |
14 #include "base/win/win_util.h" | 15 #include "base/win/win_util.h" |
15 #include "base/win/windows_version.h" | 16 #include "base/win/windows_version.h" |
16 | 17 |
17 namespace base { | 18 namespace base { |
18 namespace win { | 19 namespace win { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 // Initializes |i_shell_link| and |i_persist_file| (releasing them first if they | 23 // Initializes |i_shell_link| and |i_persist_file| (releasing them first if they |
23 // are already initialized). | 24 // are already initialized). |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); | 166 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); |
166 } else { | 167 } else { |
167 SHChangeNotify(SHCNE_CREATE, SHCNF_PATH, shortcut_path.value().c_str(), | 168 SHChangeNotify(SHCNE_CREATE, SHCNF_PATH, shortcut_path.value().c_str(), |
168 NULL); | 169 NULL); |
169 } | 170 } |
170 } | 171 } |
171 | 172 |
172 return succeeded; | 173 return succeeded; |
173 } | 174 } |
174 | 175 |
176 bool ResolveShortcutProperties(const FilePath& shortcut_path, | |
177 uint32 options, | |
178 ShortcutProperties* properties) { | |
179 DCHECK(options && properties); | |
180 base::ThreadRestrictions::AssertIOAllowed(); | |
181 | |
182 if (options & ~ShortcutProperties::PROPERTIES_ALL) { | |
183 NOTREACHED() << "Unhandled property is used."; | |
184 } | |
gab
2014/01/03 18:25:52
No {}
huangs
2014/01/03 20:19:35
Done.
| |
185 | |
186 ScopedComPtr<IShellLink> i_shell_link; | |
187 | |
188 // Get pointer to the IShellLink interface. | |
189 if (FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL, | |
190 CLSCTX_INPROC_SERVER))) { | |
191 return false; | |
192 } | |
193 | |
194 ScopedComPtr<IPersistFile> persist; | |
195 // Query IShellLink for the IPersistFile interface. | |
196 if (FAILED(persist.QueryFrom(i_shell_link))) | |
197 return false; | |
198 | |
199 // Load the shell link. | |
200 if (FAILED(persist->Load(shortcut_path.value().c_str(), STGM_READ))) | |
201 return false; | |
202 | |
203 // Reset |properties|. | |
204 properties->options = 0; | |
205 | |
206 WCHAR temp[MAX_PATH]; | |
207 if (options & ShortcutProperties::PROPERTIES_TARGET) { | |
208 // Try to find the target of a shortcut. | |
209 if (FAILED(i_shell_link->Resolve(0, SLR_NO_UI | SLR_NOSEARCH))) | |
210 return false; | |
211 if (FAILED(i_shell_link->GetPath(temp, MAX_PATH, NULL, SLGP_UNCPRIORITY))) | |
212 return false; | |
213 properties->set_target(FilePath(temp)); | |
214 } | |
215 | |
216 if (options & ShortcutProperties::PROPERTIES_WORKING_DIR) { | |
217 if (FAILED(i_shell_link->GetWorkingDirectory(temp, MAX_PATH))) | |
218 return false; | |
219 properties->set_working_dir(FilePath(temp)); | |
220 } | |
221 | |
222 if (options & ShortcutProperties::PROPERTIES_ARGUMENTS) { | |
223 if (FAILED(i_shell_link->GetArguments(temp, MAX_PATH))) | |
224 return false; | |
225 properties->set_arguments(temp); | |
226 } | |
227 | |
228 if (options & ShortcutProperties::PROPERTIES_DESCRIPTION) { | |
229 // Note: description length constrained by MAX_PATH. | |
230 if (FAILED(i_shell_link->GetDescription(temp, MAX_PATH))) | |
231 return false; | |
232 properties->set_description(temp); | |
233 } | |
234 | |
235 if (options & ShortcutProperties::PROPERTIES_ICON) { | |
236 int temp_index; | |
237 if (FAILED(i_shell_link->GetIconLocation(temp, MAX_PATH, &temp_index))) | |
238 return false; | |
239 properties->set_icon(FilePath(temp), temp_index); | |
240 } | |
241 | |
242 // Windows 7+ options, avoiding unnecessary work. | |
243 if ((options & ShortcutProperties::PROPERTIES_WIN7) && | |
244 GetVersion() >= VERSION_WIN7) { | |
245 ScopedComPtr<IPropertyStore> property_store; | |
246 if (FAILED(property_store.QueryFrom(i_shell_link))) | |
247 return false; | |
248 | |
249 if (options & ShortcutProperties::PROPERTIES_APP_ID) { | |
250 ScopedPropVariant pv_app_id; | |
251 if (property_store->GetValue(PKEY_AppUserModel_ID, pv_app_id.Receive()) | |
252 != S_OK) { | |
gab
2014/01/03 18:25:52
It's more readable to wrap in a way that the != is
huangs
2014/01/03 20:19:35
Done.
| |
253 return false; | |
254 } | |
255 switch (pv_app_id.get().vt) { | |
256 case VT_EMPTY: | |
257 properties->set_app_id(L""); | |
258 break; | |
259 case VT_LPWSTR: | |
260 properties->set_app_id(pv_app_id.get().pwszVal); | |
261 break; | |
262 default: | |
263 NOTREACHED() << "Unexpected variant type: " << pv_app_id.get().vt; | |
264 } | |
265 } | |
266 | |
267 if (options & ShortcutProperties::PROPERTIES_DUAL_MODE) { | |
268 ScopedPropVariant pv_dual_mode; | |
269 if (property_store->GetValue(PKEY_AppUserModel_IsDualMode, | |
270 pv_dual_mode.Receive()) != S_OK) { | |
271 return false; | |
272 } | |
273 switch (pv_dual_mode.get().vt) { | |
274 case VT_EMPTY: | |
275 properties->set_dual_mode(false); | |
276 break; | |
277 case VT_BOOL: | |
278 properties->set_dual_mode(pv_dual_mode.get().boolVal == VARIANT_TRUE); | |
279 break; | |
280 default: | |
281 NOTREACHED() << "Unexpected variant type: " << pv_dual_mode.get().vt; | |
282 } | |
283 } | |
284 | |
285 // Implement new properties go here. | |
gab
2014/01/03 18:25:52
I don't think this comment is required anymore now
huangs
2014/01/03 20:19:35
Done; removed.
| |
286 } | |
287 | |
288 return true; | |
289 } | |
290 | |
175 bool ResolveShortcut(const FilePath& shortcut_path, | 291 bool ResolveShortcut(const FilePath& shortcut_path, |
176 FilePath* target_path, | 292 FilePath* target_path, |
177 string16* args) { | 293 string16* args) { |
178 base::ThreadRestrictions::AssertIOAllowed(); | 294 uint32 options = 0; |
295 if (target_path) | |
296 options |= ShortcutProperties::PROPERTIES_TARGET; | |
297 if (args) | |
298 options |= ShortcutProperties::PROPERTIES_ARGUMENTS; | |
299 DCHECK(options); | |
179 | 300 |
180 HRESULT result; | 301 ShortcutProperties properties; |
181 ScopedComPtr<IShellLink> i_shell_link; | 302 if (!ResolveShortcutProperties(shortcut_path, options, &properties)) |
182 | |
183 // Get pointer to the IShellLink interface. | |
184 result = i_shell_link.CreateInstance(CLSID_ShellLink, NULL, | |
185 CLSCTX_INPROC_SERVER); | |
186 if (FAILED(result)) | |
187 return false; | 303 return false; |
188 | 304 |
189 ScopedComPtr<IPersistFile> persist; | 305 if (target_path) |
190 // Query IShellLink for the IPersistFile interface. | 306 *target_path = properties.target; |
191 result = persist.QueryFrom(i_shell_link); | 307 if (args) |
192 if (FAILED(result)) | 308 *args = properties.arguments; |
193 return false; | |
194 | |
195 // Load the shell link. | |
196 result = persist->Load(shortcut_path.value().c_str(), STGM_READ); | |
197 if (FAILED(result)) | |
198 return false; | |
199 | |
200 WCHAR temp[MAX_PATH]; | |
201 if (target_path) { | |
202 // Try to find the target of a shortcut. | |
203 result = i_shell_link->Resolve(0, SLR_NO_UI | SLR_NOSEARCH); | |
204 if (FAILED(result)) | |
205 return false; | |
206 | |
207 result = i_shell_link->GetPath(temp, MAX_PATH, NULL, SLGP_UNCPRIORITY); | |
208 if (FAILED(result)) | |
209 return false; | |
210 | |
211 *target_path = FilePath(temp); | |
212 } | |
213 | |
214 if (args) { | |
215 result = i_shell_link->GetArguments(temp, MAX_PATH); | |
216 if (FAILED(result)) | |
217 return false; | |
218 | |
219 *args = string16(temp); | |
220 } | |
221 return true; | 309 return true; |
222 } | 310 } |
223 | 311 |
224 bool TaskbarPinShortcutLink(const wchar_t* shortcut) { | 312 bool TaskbarPinShortcutLink(const wchar_t* shortcut) { |
225 base::ThreadRestrictions::AssertIOAllowed(); | 313 base::ThreadRestrictions::AssertIOAllowed(); |
226 | 314 |
227 // "Pin to taskbar" is only supported after Win7. | 315 // "Pin to taskbar" is only supported after Win7. |
228 if (GetVersion() < VERSION_WIN7) | 316 if (GetVersion() < VERSION_WIN7) |
229 return false; | 317 return false; |
230 | 318 |
231 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarpin", shortcut, | 319 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarpin", shortcut, |
232 NULL, NULL, 0)); | 320 NULL, NULL, 0)); |
233 return result > 32; | 321 return result > 32; |
234 } | 322 } |
235 | 323 |
236 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) { | 324 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) { |
237 base::ThreadRestrictions::AssertIOAllowed(); | 325 base::ThreadRestrictions::AssertIOAllowed(); |
238 | 326 |
239 // "Unpin from taskbar" is only supported after Win7. | 327 // "Unpin from taskbar" is only supported after Win7. |
240 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 328 if (GetVersion() < VERSION_WIN7) |
241 return false; | 329 return false; |
242 | 330 |
243 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarunpin", | 331 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarunpin", |
244 shortcut, NULL, NULL, 0)); | 332 shortcut, NULL, NULL, 0)); |
245 return result > 32; | 333 return result > 32; |
246 } | 334 } |
247 | 335 |
248 } // namespace win | 336 } // namespace win |
249 } // namespace base | 337 } // namespace base |
OLD | NEW |