Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: base/file_util.h

Issue 10914109: Refactoring and tests for the highly undertested file_util::CreateOrUpdateShortcutLink() method. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: better path matching Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/file_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // This file contains utility functions for dealing with the local 5 // This file contains utility functions for dealing with the local
6 // filesystem. 6 // filesystem.
7 7
8 #ifndef BASE_FILE_UTIL_H_ 8 #ifndef BASE_FILE_UTIL_H_
9 #define BASE_FILE_UTIL_H_ 9 #define BASE_FILE_UTIL_H_
10 10
11 #include "build/build_config.h" 11 #include "build/build_config.h"
12 12
13 #if defined(OS_WIN) 13 #if defined(OS_WIN)
14 #include <windows.h> 14 #include <windows.h>
15 #elif defined(OS_POSIX) 15 #elif defined(OS_POSIX)
16 #include <sys/stat.h> 16 #include <sys/stat.h>
17 #include <unistd.h> 17 #include <unistd.h>
18 #endif 18 #endif
19 19
20 #include <stdio.h> 20 #include <stdio.h>
21 21
22 #include <set> 22 #include <set>
23 #include <stack> 23 #include <stack>
24 #include <string> 24 #include <string>
25 #include <vector> 25 #include <vector>
26 26
27 #include "base/base_export.h" 27 #include "base/base_export.h"
28 #include "base/basictypes.h" 28 #include "base/basictypes.h"
29 #include "base/file_path.h" 29 #include "base/file_path.h"
30 #include "base/logging.h"
30 #include "base/memory/scoped_ptr.h" 31 #include "base/memory/scoped_ptr.h"
31 #include "base/platform_file.h" 32 #include "base/platform_file.h"
32 #include "base/string16.h" 33 #include "base/string16.h"
33 34
34 #if defined(OS_POSIX) 35 #if defined(OS_POSIX)
35 #include "base/eintr_wrapper.h" 36 #include "base/eintr_wrapper.h"
36 #include "base/file_descriptor_posix.h" 37 #include "base/file_descriptor_posix.h"
37 #include "base/logging.h"
38 #endif 38 #endif
39 39
40 namespace base { 40 namespace base {
41 class Time; 41 class Time;
42 } 42 }
43 43
44 namespace file_util { 44 namespace file_util {
45 45
46 extern bool g_bug108724_debug; 46 extern bool g_bug108724_debug;
47 47
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 // a file which the symlink points to. 219 // a file which the symlink points to.
220 BASE_EXPORT bool GetPosixFilePermissions(const FilePath& path, 220 BASE_EXPORT bool GetPosixFilePermissions(const FilePath& path,
221 int* mode); 221 int* mode);
222 // Sets the permission of the given |path|. If |path| is symbolic link, sets 222 // Sets the permission of the given |path|. If |path| is symbolic link, sets
223 // the permission of a file which the symlink points to. 223 // the permission of a file which the symlink points to.
224 BASE_EXPORT bool SetPosixFilePermissions(const FilePath& path, 224 BASE_EXPORT bool SetPosixFilePermissions(const FilePath& path,
225 int mode); 225 int mode);
226 #endif // defined(OS_POSIX) 226 #endif // defined(OS_POSIX)
227 227
228 #if defined(OS_WIN) 228 #if defined(OS_WIN)
229 enum ShortcutOptions { 229 enum ShortcutOperation {
230 SHORTCUT_NO_OPTIONS = 0,
231 // Set DualMode property for Windows 8 Metro-enabled shortcuts.
232 SHORTCUT_DUAL_MODE = 1 << 0,
233 // Create a new shortcut (overwriting if necessary). 230 // Create a new shortcut (overwriting if necessary).
234 SHORTCUT_CREATE_ALWAYS = 1 << 1, 231 SHORTCUT_CREATE_ALWAYS = 0,
232 // Update specified (non-null) properties only on an existing shortcut.
233 SHORTCUT_UPDATE_EXISTING,
234 };
235
236 // Properties for shortcuts. Properties set will be applied to the shortcut on
237 // creation/update, others will be ignored.
238 // Callers are encouraged to use the setters provided which take care of
239 // setting |options| as desired.
240 struct ShortcutProperties {
241 enum IndividualProperties {
242 PROPERTIES_TARGET = 1 << 0,
243 PROPERTIES_WORKING_DIR = 1 << 1,
244 PROPERTIES_ARGUMENTS = 1 << 2,
245 PROPERTIES_DESCRIPTION = 1 << 3,
246 PROPERTIES_ICON = 1 << 4,
247 PROPERTIES_APP_ID = 1 << 5,
248 PROPERTIES_DUAL_MODE = 1 << 6,
249 };
250
251 ShortcutProperties() : options(0U) {}
252
253 void set_target(const string16& target_in) {
254 target = target_in;
255 options |= PROPERTIES_TARGET;
256 }
257
258 void set_working_dir(const string16& working_dir_in) {
259 working_dir = working_dir_in;
260 options |= PROPERTIES_WORKING_DIR;
261 }
262
263 void set_arguments(const string16& arguments_in) {
264 // Size restriction as per MSDN at http://goo.gl/TJ7q5.
265 DCHECK(arguments_in.size() < MAX_PATH);
266 arguments = arguments_in;
267 options |= PROPERTIES_ARGUMENTS;
268 }
269
270 void set_description(const string16& description_in) {
271 // Size restriction as per MSDN at http://goo.gl/OdNQq.
272 DCHECK(description_in.size() < MAX_PATH);
273 description = description_in;
274 options |= PROPERTIES_DESCRIPTION;
275 }
276
277 void set_icon(const string16& icon_in, int icon_index_in) {
278 icon = icon_in;
279 icon_index = icon_index_in;
280 options |= PROPERTIES_ICON;
281 }
282
283 void set_app_id(const string16& app_id_in) {
284 app_id = app_id_in;
285 options |= PROPERTIES_APP_ID;
286 }
287
288 void set_dual_mode(bool dual_mode_in) {
289 dual_mode = dual_mode_in;
290 options |= PROPERTIES_DUAL_MODE;
291 }
292
293 // The target to launch from this shortcut. This is mandatory when creating
294 // a shortcut.
295 string16 target;
brettw 2012/09/08 18:28:48 Should some of these be FilePaths instead? On Wind
gab 2012/09/10 17:35:34 Done.
296 // The name of the working directory when launching the shortcut.
297 string16 working_dir;
298 // The arguments to be applied to |target| when launching from this shortcut.
299 // The length of this string must be less than MAX_PATH.
300 string16 arguments;
301 // The localized description of the shortcut.
302 // The length of this string must be less than MAX_PATH.
303 string16 description;
304 // The path to the icon (can be a dll or exe, in which case |icon_index| is
305 // the resource id).
306 string16 icon;
307 int icon_index;
308 // The app model id for the shortcut (Win7+).
309 string16 app_id;
310 // Whether this is a dual mode shortcut (Win8+).
311 // For now this property can only be set to true (i.e. once set it cannot be
312 // unset).
313 // TODO (gab): Make it possible to set this property to false.
314 bool dual_mode;
315 // Bitfield made of IndividualProperties. Properties set in |options| will be
316 // set on the shortcut, others will be ignored.
317 uint32 options;
235 }; 318 };
236 319
237 // Resolve Windows shortcut (.LNK file) 320 // Resolve Windows shortcut (.LNK file)
238 // This methods tries to resolve a shortcut .LNK file. The path of the shortcut 321 // This methods tries to resolve a shortcut .LNK file. The path of the shortcut
239 // to resolve is in |shortcut_path|. If |target_path| is not NULL, the target 322 // to resolve is in |shortcut_path|. If |target_path| is not NULL, the target
240 // will be resolved and placed in |target_path|. If |args| is not NULL, the 323 // will be resolved and placed in |target_path|. If |args| is not NULL, the
241 // arguments will be retrieved and placed in |args|. The function returns true 324 // arguments will be retrieved and placed in |args|. The function returns true
242 // if all requested fields are are found successfully. 325 // if all requested fields are are found successfully.
243 // Callers can safely use the same variable for both |shortcut_path| and 326 // Callers can safely use the same variable for both |shortcut_path| and
244 // |target_path|. 327 // |target_path|.
245 BASE_EXPORT bool ResolveShortcut(const FilePath& shortcut_path, 328 BASE_EXPORT bool ResolveShortcut(const FilePath& shortcut_path,
246 FilePath* target_path, 329 FilePath* target_path,
247 string16* args); 330 string16* args);
248 331
249 // Creates (or updates) a Windows shortcut (.LNK file) 332 // This method creates (or updates) a shortcut link at |shortcut_path| using the
250 // This method creates (or updates) a shortcut link using the information given. 333 // information given through |properties|.
251 // Ensure you have initialized COM before calling into this function. 334 // Ensure you have initialized COM before calling into this function.
252 // |destination| is required. |source| is required when SHORTCUT_CREATE_ALWAYS 335 // |operation|: a choice from the ShortcutOperation enum.
253 // is specified in |options|. All other parameters are optional and may be NULL. 336 // If |operation| is SHORTCUT_UPDATE_EXISTING and |shortcut_path| does not
254 // |source| is the existing file, |destination| is the new link file to be
255 // created; for best results pass the filename with the .lnk extension.
256 // The |icon| can specify a dll or exe in which case the icon index is the
257 // resource id. |app_id| is the app model id for the shortcut on Win7.
258 // |options|: bitfield for which the options come from ShortcutOptions.
259 // If SHORTCUT_CREATE_ALWAYS is not set in |options|, only specified (non-null)
260 // properties on an existing shortcut will be modified. If the shortcut does not
261 // exist, this method is a no-op and returns false. 337 // exist, this method is a no-op and returns false.
262 BASE_EXPORT bool CreateOrUpdateShortcutLink(const wchar_t *source, 338 BASE_EXPORT bool CreateOrUpdateShortcutLink(
263 const wchar_t *destination, 339 const string16& shortcut_path,
264 const wchar_t *working_dir, 340 const ShortcutProperties& properties,
265 const wchar_t *arguments, 341 ShortcutOperation operation);
266 const wchar_t *description,
267 const wchar_t *icon,
268 int icon_index,
269 const wchar_t* app_id,
270 uint32 options);
271 342
272 // Pins a shortcut to the Windows 7 taskbar. The shortcut file must already 343 // Pins a shortcut to the Windows 7 taskbar. The shortcut file must already
273 // exist and be a shortcut that points to an executable. 344 // exist and be a shortcut that points to an executable.
274 BASE_EXPORT bool TaskbarPinShortcutLink(const wchar_t* shortcut); 345 BASE_EXPORT bool TaskbarPinShortcutLink(const wchar_t* shortcut);
275 346
276 // Unpins a shortcut from the Windows 7 taskbar. The shortcut must exist and 347 // Unpins a shortcut from the Windows 7 taskbar. The shortcut must exist and
277 // already be pinned to the taskbar. 348 // already be pinned to the taskbar.
278 BASE_EXPORT bool TaskbarUnpinShortcutLink(const wchar_t* shortcut); 349 BASE_EXPORT bool TaskbarUnpinShortcutLink(const wchar_t* shortcut);
279 350
280 // Copy from_path to to_path recursively and then delete from_path recursively. 351 // Copy from_path to to_path recursively and then delete from_path recursively.
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 }; 742 };
672 743
673 // Attempts determine the FileSystemType for |path|. 744 // Attempts determine the FileSystemType for |path|.
674 // Returns false if |path| doesn't exist. 745 // Returns false if |path| doesn't exist.
675 BASE_EXPORT bool GetFileSystemType(const FilePath& path, FileSystemType* type); 746 BASE_EXPORT bool GetFileSystemType(const FilePath& path, FileSystemType* type);
676 #endif 747 #endif
677 748
678 } // namespace file_util 749 } // namespace file_util
679 750
680 #endif // BASE_FILE_UTIL_H_ 751 #endif // BASE_FILE_UTIL_H_
OLDNEW
« no previous file with comments | « no previous file | base/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698