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 | |
185 ScopedComPtr<IShellLink> i_shell_link; | |
186 | |
187 // Get pointer to the IShellLink interface. | |
188 if (FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL, | |
189 CLSCTX_INPROC_SERVER))) { | |
190 return false; | |
191 } | |
192 | |
193 ScopedComPtr<IPersistFile> persist; | |
194 // Query IShellLink for the IPersistFile interface. | |
195 if (FAILED(persist.QueryFrom(i_shell_link))) | |
196 return false; | |
197 | |
198 // Load the shell link. | |
199 if (FAILED(persist->Load(shortcut_path.value().c_str(), STGM_READ))) | |
200 return false; | |
201 | |
202 // Reset |properties|. | |
203 properties->options = 0; | |
204 | |
205 WCHAR temp[MAX_PATH]; | |
cpu_(ooo_6.6-7.5)
2014/01/08 02:11:59
use wchar_t like the rest of this file.
huangs
2014/01/08 03:26:31
Done.
| |
206 if (options & ShortcutProperties::PROPERTIES_TARGET) { | |
207 // Try to find the target of a shortcut. | |
208 if (FAILED(i_shell_link->Resolve(0, SLR_NO_UI | SLR_NOSEARCH))) | |
209 return false; | |
210 if (FAILED(i_shell_link->GetPath(temp, MAX_PATH, NULL, SLGP_UNCPRIORITY))) | |
211 return false; | |
212 properties->set_target(FilePath(temp)); | |
213 } | |
214 | |
215 if (options & ShortcutProperties::PROPERTIES_WORKING_DIR) { | |
216 if (FAILED(i_shell_link->GetWorkingDirectory(temp, MAX_PATH))) | |
217 return false; | |
218 properties->set_working_dir(FilePath(temp)); | |
219 } | |
220 | |
221 if (options & ShortcutProperties::PROPERTIES_ARGUMENTS) { | |
222 if (FAILED(i_shell_link->GetArguments(temp, MAX_PATH))) | |
223 return false; | |
224 properties->set_arguments(temp); | |
225 } | |
226 | |
227 if (options & ShortcutProperties::PROPERTIES_DESCRIPTION) { | |
228 // Note: description length constrained by MAX_PATH. | |
229 if (FAILED(i_shell_link->GetDescription(temp, MAX_PATH))) | |
230 return false; | |
231 properties->set_description(temp); | |
232 } | |
233 | |
234 if (options & ShortcutProperties::PROPERTIES_ICON) { | |
235 int temp_index; | |
236 if (FAILED(i_shell_link->GetIconLocation(temp, MAX_PATH, &temp_index))) | |
237 return false; | |
238 properties->set_icon(FilePath(temp), temp_index); | |
239 } | |
240 | |
241 // Windows 7+ options, avoiding unnecessary work. | |
242 if ((options & ShortcutProperties::PROPERTIES_WIN7) && | |
243 GetVersion() >= VERSION_WIN7) { | |
244 ScopedComPtr<IPropertyStore> property_store; | |
245 if (FAILED(property_store.QueryFrom(i_shell_link))) | |
246 return false; | |
247 | |
248 if (options & ShortcutProperties::PROPERTIES_APP_ID) { | |
249 ScopedPropVariant pv_app_id; | |
250 if (property_store->GetValue(PKEY_AppUserModel_ID, | |
251 pv_app_id.Receive()) != S_OK) { | |
252 return false; | |
253 } | |
254 switch (pv_app_id.get().vt) { | |
255 case VT_EMPTY: | |
256 properties->set_app_id(L""); | |
257 break; | |
258 case VT_LPWSTR: | |
259 properties->set_app_id(pv_app_id.get().pwszVal); | |
260 break; | |
261 default: | |
262 NOTREACHED() << "Unexpected variant type: " << pv_app_id.get().vt; | |
cpu_(ooo_6.6-7.5)
2014/01/08 02:11:59
feels like we should return false for default, sin
huangs
2014/01/08 03:26:31
Done; also applied below.
| |
263 } | |
264 } | |
265 | |
266 if (options & ShortcutProperties::PROPERTIES_DUAL_MODE) { | |
267 ScopedPropVariant pv_dual_mode; | |
268 if (property_store->GetValue(PKEY_AppUserModel_IsDualMode, | |
269 pv_dual_mode.Receive()) != S_OK) { | |
270 return false; | |
271 } | |
272 switch (pv_dual_mode.get().vt) { | |
273 case VT_EMPTY: | |
274 properties->set_dual_mode(false); | |
275 break; | |
276 case VT_BOOL: | |
277 properties->set_dual_mode(pv_dual_mode.get().boolVal == VARIANT_TRUE); | |
278 break; | |
279 default: | |
280 NOTREACHED() << "Unexpected variant type: " << pv_dual_mode.get().vt; | |
281 } | |
282 } | |
283 } | |
284 | |
285 return true; | |
286 } | |
287 | |
175 bool ResolveShortcut(const FilePath& shortcut_path, | 288 bool ResolveShortcut(const FilePath& shortcut_path, |
176 FilePath* target_path, | 289 FilePath* target_path, |
177 string16* args) { | 290 string16* args) { |
178 base::ThreadRestrictions::AssertIOAllowed(); | 291 uint32 options = 0; |
292 if (target_path) | |
293 options |= ShortcutProperties::PROPERTIES_TARGET; | |
294 if (args) | |
295 options |= ShortcutProperties::PROPERTIES_ARGUMENTS; | |
296 DCHECK(options); | |
179 | 297 |
180 HRESULT result; | 298 ShortcutProperties properties; |
181 ScopedComPtr<IShellLink> i_shell_link; | 299 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; | 300 return false; |
188 | 301 |
189 ScopedComPtr<IPersistFile> persist; | 302 if (target_path) |
190 // Query IShellLink for the IPersistFile interface. | 303 *target_path = properties.target; |
191 result = persist.QueryFrom(i_shell_link); | 304 if (args) |
192 if (FAILED(result)) | 305 *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; | 306 return true; |
222 } | 307 } |
223 | 308 |
224 bool TaskbarPinShortcutLink(const wchar_t* shortcut) { | 309 bool TaskbarPinShortcutLink(const wchar_t* shortcut) { |
225 base::ThreadRestrictions::AssertIOAllowed(); | 310 base::ThreadRestrictions::AssertIOAllowed(); |
226 | 311 |
227 // "Pin to taskbar" is only supported after Win7. | 312 // "Pin to taskbar" is only supported after Win7. |
228 if (GetVersion() < VERSION_WIN7) | 313 if (GetVersion() < VERSION_WIN7) |
229 return false; | 314 return false; |
230 | 315 |
231 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarpin", shortcut, | 316 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarpin", shortcut, |
232 NULL, NULL, 0)); | 317 NULL, NULL, 0)); |
233 return result > 32; | 318 return result > 32; |
234 } | 319 } |
235 | 320 |
236 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) { | 321 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) { |
237 base::ThreadRestrictions::AssertIOAllowed(); | 322 base::ThreadRestrictions::AssertIOAllowed(); |
238 | 323 |
239 // "Unpin from taskbar" is only supported after Win7. | 324 // "Unpin from taskbar" is only supported after Win7. |
240 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 325 if (GetVersion() < VERSION_WIN7) |
241 return false; | 326 return false; |
242 | 327 |
243 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarunpin", | 328 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarunpin", |
244 shortcut, NULL, NULL, 0)); | 329 shortcut, NULL, NULL, 0)); |
245 return result > 32; | 330 return result > 32; |
246 } | 331 } |
247 | 332 |
248 } // namespace win | 333 } // namespace win |
249 } // namespace base | 334 } // namespace base |
OLD | NEW |