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 <objbase.h> | 7 #include <objbase.h> |
8 #include <shellapi.h> | 8 #include <shellapi.h> |
9 #include <shlobj.h> | 9 #include <shlobj.h> |
10 #include <propkey.h> | 10 #include <propkey.h> |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 if ((properties.options & ShortcutProperties::PROPERTIES_ICON) && | 130 if ((properties.options & ShortcutProperties::PROPERTIES_ICON) && |
131 FAILED(i_shell_link->SetIconLocation(properties.icon.value().c_str(), | 131 FAILED(i_shell_link->SetIconLocation(properties.icon.value().c_str(), |
132 properties.icon_index))) { | 132 properties.icon_index))) { |
133 return false; | 133 return false; |
134 } | 134 } |
135 | 135 |
136 bool has_app_id = | 136 bool has_app_id = |
137 (properties.options & ShortcutProperties::PROPERTIES_APP_ID) != 0; | 137 (properties.options & ShortcutProperties::PROPERTIES_APP_ID) != 0; |
138 bool has_dual_mode = | 138 bool has_dual_mode = |
139 (properties.options & ShortcutProperties::PROPERTIES_DUAL_MODE) != 0; | 139 (properties.options & ShortcutProperties::PROPERTIES_DUAL_MODE) != 0; |
140 if ((has_app_id || has_dual_mode) && | 140 if (has_app_id || has_dual_mode) { |
141 GetVersion() >= VERSION_WIN7) { | |
142 ScopedComPtr<IPropertyStore> property_store; | 141 ScopedComPtr<IPropertyStore> property_store; |
143 if (FAILED(i_shell_link.CopyTo(property_store.GetAddressOf())) || | 142 if (FAILED(i_shell_link.CopyTo(property_store.GetAddressOf())) || |
144 !property_store.Get()) | 143 !property_store.Get()) |
145 return false; | 144 return false; |
146 | 145 |
147 if (has_app_id && | 146 if (has_app_id && |
148 !SetAppIdForPropertyStore(property_store.Get(), | 147 !SetAppIdForPropertyStore(property_store.Get(), |
149 properties.app_id.c_str())) { | 148 properties.app_id.c_str())) { |
150 return false; | 149 return false; |
151 } | 150 } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 properties->set_description(temp); | 240 properties->set_description(temp); |
242 } | 241 } |
243 | 242 |
244 if (options & ShortcutProperties::PROPERTIES_ICON) { | 243 if (options & ShortcutProperties::PROPERTIES_ICON) { |
245 int temp_index; | 244 int temp_index; |
246 if (FAILED(i_shell_link->GetIconLocation(temp, MAX_PATH, &temp_index))) | 245 if (FAILED(i_shell_link->GetIconLocation(temp, MAX_PATH, &temp_index))) |
247 return false; | 246 return false; |
248 properties->set_icon(FilePath(temp), temp_index); | 247 properties->set_icon(FilePath(temp), temp_index); |
249 } | 248 } |
250 | 249 |
251 // Windows 7+ options, avoiding unnecessary work. | 250 if (options & ShortcutProperties::PROPERTIES_WIN7) { |
252 if ((options & ShortcutProperties::PROPERTIES_WIN7) && | |
253 GetVersion() >= VERSION_WIN7) { | |
254 ScopedComPtr<IPropertyStore> property_store; | 251 ScopedComPtr<IPropertyStore> property_store; |
255 if (FAILED(i_shell_link.CopyTo(property_store.GetAddressOf()))) | 252 if (FAILED(i_shell_link.CopyTo(property_store.GetAddressOf()))) |
256 return false; | 253 return false; |
257 | 254 |
258 if (options & ShortcutProperties::PROPERTIES_APP_ID) { | 255 if (options & ShortcutProperties::PROPERTIES_APP_ID) { |
259 ScopedPropVariant pv_app_id; | 256 ScopedPropVariant pv_app_id; |
260 if (property_store->GetValue(PKEY_AppUserModel_ID, | 257 if (property_store->GetValue(PKEY_AppUserModel_ID, |
261 pv_app_id.Receive()) != S_OK) { | 258 pv_app_id.Receive()) != S_OK) { |
262 return false; | 259 return false; |
263 } | 260 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 return false; | 309 return false; |
313 | 310 |
314 if (target_path) | 311 if (target_path) |
315 *target_path = properties.target; | 312 *target_path = properties.target; |
316 if (args) | 313 if (args) |
317 *args = properties.arguments; | 314 *args = properties.arguments; |
318 return true; | 315 return true; |
319 } | 316 } |
320 | 317 |
321 bool CanPinShortcutToTaskbar() { | 318 bool CanPinShortcutToTaskbar() { |
322 // "Pin to taskbar" appeared in Windows 7 and stopped being supported in | 319 // "Pin to taskbar" stopped being supported in Windows 10. |
323 // Windows 10. | 320 return GetVersion() < VERSION_WIN10; |
324 return GetVersion() >= VERSION_WIN7 && GetVersion() < VERSION_WIN10; | |
325 } | 321 } |
326 | 322 |
327 bool PinShortcutToTaskbar(const FilePath& shortcut) { | 323 bool PinShortcutToTaskbar(const FilePath& shortcut) { |
328 base::ThreadRestrictions::AssertIOAllowed(); | 324 base::ThreadRestrictions::AssertIOAllowed(); |
329 DCHECK(CanPinShortcutToTaskbar()); | 325 DCHECK(CanPinShortcutToTaskbar()); |
330 | 326 |
331 intptr_t result = reinterpret_cast<intptr_t>(ShellExecute( | 327 intptr_t result = reinterpret_cast<intptr_t>(ShellExecute( |
332 NULL, L"taskbarpin", shortcut.value().c_str(), NULL, NULL, 0)); | 328 NULL, L"taskbarpin", shortcut.value().c_str(), NULL, NULL, 0)); |
333 return result > 32; | 329 return result > 32; |
334 } | 330 } |
335 | 331 |
336 bool UnpinShortcutFromTaskbar(const FilePath& shortcut) { | 332 bool UnpinShortcutFromTaskbar(const FilePath& shortcut) { |
337 base::ThreadRestrictions::AssertIOAllowed(); | 333 base::ThreadRestrictions::AssertIOAllowed(); |
338 | 334 |
339 // "Unpin from taskbar" is only supported after Win7. It is possible to remove | |
340 // a shortcut pinned by a user on Windows 10+. | |
341 if (GetVersion() < VERSION_WIN7) | |
342 return false; | |
343 | |
344 intptr_t result = reinterpret_cast<intptr_t>(ShellExecute( | 335 intptr_t result = reinterpret_cast<intptr_t>(ShellExecute( |
345 NULL, L"taskbarunpin", shortcut.value().c_str(), NULL, NULL, 0)); | 336 NULL, L"taskbarunpin", shortcut.value().c_str(), NULL, NULL, 0)); |
346 return result > 32; | 337 return result > 32; |
347 } | 338 } |
348 | 339 |
349 } // namespace win | 340 } // namespace win |
350 } // namespace base | 341 } // namespace base |
OLD | NEW |