Chromium Code Reviews| 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); | |
|
gab
2014/01/02 21:01:46
I really like supporting all properties here, but
huangs
2014/01/02 23:22:37
Done.
| |
| 180 base::ThreadRestrictions::AssertIOAllowed(); | |
| 181 | |
| 182 ScopedComPtr<IShellLink> i_shell_link; | |
| 183 | |
| 184 // Get pointer to the IShellLink interface. | |
| 185 if (FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL, | |
| 186 CLSCTX_INPROC_SERVER))) { | |
| 187 return false; | |
| 188 } | |
| 189 | |
| 190 ScopedComPtr<IPersistFile> persist; | |
| 191 // Query IShellLink for the IPersistFile interface. | |
| 192 if (FAILED(persist.QueryFrom(i_shell_link))) | |
| 193 return false; | |
| 194 | |
| 195 // Load the shell link. | |
| 196 if (FAILED(persist->Load(shortcut_path.value().c_str(), STGM_READ))) | |
| 197 return false; | |
| 198 | |
| 199 // Reset |properties|. | |
| 200 properties->options = 0; | |
| 201 | |
| 202 WCHAR temp[MAX_PATH]; | |
| 203 if (options & ShortcutProperties::PROPERTIES_TARGET) { | |
| 204 // Try to find the target of a shortcut. | |
| 205 if (FAILED(i_shell_link->Resolve(0, SLR_NO_UI | SLR_NOSEARCH))) | |
| 206 return false; | |
| 207 if (FAILED(i_shell_link->GetPath(temp, MAX_PATH, NULL, SLGP_UNCPRIORITY))) | |
| 208 return false; | |
| 209 properties->set_target(FilePath(temp)); | |
| 210 } | |
| 211 | |
| 212 if (options & ShortcutProperties::PROPERTIES_WORKING_DIR) { | |
| 213 if (FAILED(i_shell_link->GetWorkingDirectory(temp, MAX_PATH))) | |
| 214 return false; | |
| 215 properties->set_working_dir(FilePath(temp)); | |
| 216 } | |
| 217 | |
| 218 if (options & ShortcutProperties::PROPERTIES_ARGUMENTS) { | |
| 219 if (FAILED(i_shell_link->GetArguments(temp, MAX_PATH))) | |
| 220 return false; | |
| 221 properties->set_arguments(temp); | |
| 222 } | |
| 223 | |
| 224 if (options & ShortcutProperties::PROPERTIES_DESCRIPTION) { | |
| 225 // Note: description length constrained by MAX_PATH. | |
| 226 if (FAILED(i_shell_link->GetDescription(temp, MAX_PATH))) | |
| 227 return false; | |
| 228 properties->set_description(temp); | |
| 229 } | |
| 230 | |
| 231 if (options & ShortcutProperties::PROPERTIES_ICON) { | |
| 232 int temp_index; | |
| 233 if (FAILED(i_shell_link->GetIconLocation(temp, MAX_PATH, &temp_index))) | |
| 234 return false; | |
| 235 properties->set_icon(FilePath(temp), temp_index); | |
| 236 } | |
| 237 | |
| 238 // Windows 7+ options, avoiding unnecessary work. | |
| 239 const uint32 win7_options = ShortcutProperties::PROPERTIES_APP_ID | | |
| 240 ShortcutProperties::PROPERTIES_DUAL_MODE; | |
| 241 if ((options & win7_options) && GetVersion() >= VERSION_WIN7) { | |
| 242 ScopedComPtr<IPropertyStore> property_store; | |
| 243 if (FAILED(property_store.QueryFrom(i_shell_link))) | |
| 244 return false; | |
| 245 | |
| 246 if (options & ShortcutProperties::PROPERTIES_APP_ID) { | |
| 247 ScopedPropVariant pv_app_id; | |
| 248 if (S_OK != property_store->GetValue(PKEY_AppUserModel_ID, | |
|
gab
2014/01/02 21:01:46
Put constant on RHS of equality-check (here and be
huangs
2014/01/02 23:22:37
Done.
| |
| 249 pv_app_id.Receive())) { | |
| 250 return false; | |
| 251 } | |
| 252 properties->set_app_id(pv_app_id.get().vt == VT_LPWSTR ? | |
|
gab
2014/01/02 21:01:46
Use switch statement instead, on VT_EMPTY, set to
huangs
2014/01/02 23:22:37
Done.
| |
| 253 pv_app_id.get().pwszVal : L""); | |
| 254 } | |
| 255 | |
| 256 if (options & ShortcutProperties::PROPERTIES_DUAL_MODE) { | |
| 257 ScopedPropVariant pv_dual_mode; | |
| 258 if (S_OK != property_store->GetValue(PKEY_AppUserModel_IsDualMode, | |
| 259 pv_dual_mode.Receive())) { | |
| 260 return false; | |
| 261 } | |
| 262 properties->set_dual_mode(pv_dual_mode.get().vt == VT_BOOL && | |
| 263 pv_dual_mode.get().boolVal == VARIANT_TRUE); | |
| 264 } | |
| 265 } | |
| 266 | |
| 267 return true; | |
| 268 } | |
| 269 | |
| 175 bool ResolveShortcut(const FilePath& shortcut_path, | 270 bool ResolveShortcut(const FilePath& shortcut_path, |
| 176 FilePath* target_path, | 271 FilePath* target_path, |
| 177 string16* args) { | 272 string16* args) { |
| 178 base::ThreadRestrictions::AssertIOAllowed(); | 273 uint32 options = 0; |
| 274 if (target_path) | |
| 275 options |= ShortcutProperties::PROPERTIES_TARGET; | |
| 276 if (args) | |
| 277 options |= ShortcutProperties::PROPERTIES_ARGUMENTS; | |
| 278 DCHECK(options); | |
| 179 | 279 |
| 180 HRESULT result; | 280 ShortcutProperties properties; |
| 181 ScopedComPtr<IShellLink> i_shell_link; | 281 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; | 282 return false; |
| 188 | 283 |
| 189 ScopedComPtr<IPersistFile> persist; | 284 if (target_path) |
| 190 // Query IShellLink for the IPersistFile interface. | 285 *target_path = properties.target; |
| 191 result = persist.QueryFrom(i_shell_link); | 286 if (args) |
| 192 if (FAILED(result)) | 287 *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; | 288 return true; |
| 222 } | 289 } |
| 223 | 290 |
| 224 bool TaskbarPinShortcutLink(const wchar_t* shortcut) { | 291 bool TaskbarPinShortcutLink(const wchar_t* shortcut) { |
| 225 base::ThreadRestrictions::AssertIOAllowed(); | 292 base::ThreadRestrictions::AssertIOAllowed(); |
| 226 | 293 |
| 227 // "Pin to taskbar" is only supported after Win7. | 294 // "Pin to taskbar" is only supported after Win7. |
| 228 if (GetVersion() < VERSION_WIN7) | 295 if (GetVersion() < VERSION_WIN7) |
| 229 return false; | 296 return false; |
| 230 | 297 |
| 231 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarpin", shortcut, | 298 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarpin", shortcut, |
| 232 NULL, NULL, 0)); | 299 NULL, NULL, 0)); |
| 233 return result > 32; | 300 return result > 32; |
| 234 } | 301 } |
| 235 | 302 |
| 236 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) { | 303 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) { |
| 237 base::ThreadRestrictions::AssertIOAllowed(); | 304 base::ThreadRestrictions::AssertIOAllowed(); |
| 238 | 305 |
| 239 // "Unpin from taskbar" is only supported after Win7. | 306 // "Unpin from taskbar" is only supported after Win7. |
| 240 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 307 if (GetVersion() < VERSION_WIN7) |
| 241 return false; | 308 return false; |
| 242 | 309 |
| 243 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarunpin", | 310 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarunpin", |
| 244 shortcut, NULL, NULL, 0)); | 311 shortcut, NULL, NULL, 0)); |
| 245 return result > 32; | 312 return result > 32; |
| 246 } | 313 } |
| 247 | 314 |
| 248 } // namespace win | 315 } // namespace win |
| 249 } // namespace base | 316 } // namespace base |
| OLD | NEW |