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 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); | 165 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); |
| 166 } else { | 166 } else { |
| 167 SHChangeNotify(SHCNE_CREATE, SHCNF_PATH, shortcut_path.value().c_str(), | 167 SHChangeNotify(SHCNE_CREATE, SHCNF_PATH, shortcut_path.value().c_str(), |
| 168 NULL); | 168 NULL); |
| 169 } | 169 } |
| 170 } | 170 } |
| 171 | 171 |
| 172 return succeeded; | 172 return succeeded; |
| 173 } | 173 } |
| 174 | 174 |
| 175 bool ResolveShortcutProperties(const FilePath& shortcut_path, | |
| 176 uint32 options, | |
| 177 ShortcutProperties* properties) { | |
| 178 DCHECK(properties); | |
| 179 DCHECK(options); | |
|
gab
2014/01/02 15:51:46
Switch order to match argument order (and/or group
huangs
2014/01/02 19:58:22
Going with && for implicit cast to bool.
| |
| 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 properties->options = 0; | |
|
gab
2014/01/02 15:51:46
Empty line below (otherwise it feels like this is
huangs
2014/01/02 19:58:22
Done.
| |
| 200 WCHAR temp[MAX_PATH]; | |
| 201 if (options & ShortcutProperties::PROPERTIES_TARGET) { | |
| 202 // Try to find the target of a shortcut. | |
| 203 if (FAILED(i_shell_link->Resolve(0, SLR_NO_UI | SLR_NOSEARCH))) | |
| 204 return false; | |
| 205 | |
| 206 if (FAILED(i_shell_link->GetPath(temp, MAX_PATH, NULL, SLGP_UNCPRIORITY))) | |
| 207 return false; | |
| 208 | |
| 209 properties->set_target(FilePath(temp)); | |
| 210 } | |
| 211 | |
| 212 if (options & ShortcutProperties::PROPERTIES_ARGUMENTS) { | |
| 213 if (FAILED(i_shell_link->GetArguments(temp, MAX_PATH))) | |
| 214 return false; | |
| 215 | |
| 216 properties->set_arguments(string16(temp)); | |
| 217 } | |
| 218 | |
| 219 if (options & ShortcutProperties::PROPERTIES_ICON) { | |
| 220 int temp_index; | |
| 221 if (FAILED(i_shell_link->GetIconLocation(temp, MAX_PATH, &temp_index))) | |
| 222 return false; | |
| 223 | |
| 224 properties->set_icon(FilePath(temp), temp_index); | |
| 225 } | |
| 226 | |
| 227 // Implement other options if the need arise. | |
|
gab
2014/01/02 15:51:46
Add to the comment in the header to mention which
huangs
2014/01/02 19:58:22
I realized that base\test\test_shortcut_win.cc: Va
| |
| 228 | |
| 229 return true; | |
| 230 } | |
| 231 | |
| 175 bool ResolveShortcut(const FilePath& shortcut_path, | 232 bool ResolveShortcut(const FilePath& shortcut_path, |
| 176 FilePath* target_path, | 233 FilePath* target_path, |
| 177 string16* args) { | 234 string16* args) { |
| 178 base::ThreadRestrictions::AssertIOAllowed(); | 235 uint32 options = 0; |
| 236 if (target_path) | |
| 237 options |= ShortcutProperties::PROPERTIES_TARGET; | |
| 238 if (args) | |
| 239 options |= ShortcutProperties::PROPERTIES_ARGUMENTS; | |
| 240 if (!options) | |
| 241 return true; // Vacuous success if we want nothing. | |
|
gab
2014/01/02 15:51:46
I don't think it makes sense to call ResolveShortc
huangs
2014/01/02 19:58:22
Done, but adding DCHECK in this routine.
| |
| 179 | 242 |
| 180 HRESULT result; | 243 ShortcutProperties p; |
|
gab
2014/01/02 15:51:46
s/p/properties
huangs
2014/01/02 19:58:22
Done.
| |
| 181 ScopedComPtr<IShellLink> i_shell_link; | 244 if (!ResolveShortcutProperties(shortcut_path, options, &p)) |
| 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; | 245 return false; |
| 188 | 246 |
| 189 ScopedComPtr<IPersistFile> persist; | 247 if (target_path) |
| 190 // Query IShellLink for the IPersistFile interface. | 248 *target_path = p.target; |
| 191 result = persist.QueryFrom(i_shell_link); | 249 if (args) |
| 192 if (FAILED(result)) | 250 *args = p.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; | 251 return true; |
| 222 } | 252 } |
| 223 | 253 |
| 224 bool TaskbarPinShortcutLink(const wchar_t* shortcut) { | 254 bool TaskbarPinShortcutLink(const wchar_t* shortcut) { |
| 225 base::ThreadRestrictions::AssertIOAllowed(); | 255 base::ThreadRestrictions::AssertIOAllowed(); |
| 226 | 256 |
| 227 // "Pin to taskbar" is only supported after Win7. | 257 // "Pin to taskbar" is only supported after Win7. |
| 228 if (GetVersion() < VERSION_WIN7) | 258 if (GetVersion() < VERSION_WIN7) |
| 229 return false; | 259 return false; |
| 230 | 260 |
| 231 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarpin", shortcut, | 261 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarpin", shortcut, |
| 232 NULL, NULL, 0)); | 262 NULL, NULL, 0)); |
| 233 return result > 32; | 263 return result > 32; |
| 234 } | 264 } |
| 235 | 265 |
| 236 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) { | 266 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) { |
| 237 base::ThreadRestrictions::AssertIOAllowed(); | 267 base::ThreadRestrictions::AssertIOAllowed(); |
| 238 | 268 |
| 239 // "Unpin from taskbar" is only supported after Win7. | 269 // "Unpin from taskbar" is only supported after Win7. |
| 240 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 270 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 241 return false; | 271 return false; |
| 242 | 272 |
| 243 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarunpin", | 273 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarunpin", |
| 244 shortcut, NULL, NULL, 0)); | 274 shortcut, NULL, NULL, 0)); |
| 245 return result > 32; | 275 return result > 32; |
| 246 } | 276 } |
| 247 | 277 |
| 248 } // namespace win | 278 } // namespace win |
| 249 } // namespace base | 279 } // namespace base |
| OLD | NEW |