AppSandboxFileAccess.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // AppSandboxFileAccess.h
  3. // AppSandboxFileAccess
  4. //
  5. // Created by Leigh McCulloch on 23/11/2013.
  6. //
  7. // Copyright (c) 2013, Leigh McCulloch
  8. // All rights reserved.
  9. //
  10. // BSD-2-Clause License: http://opensource.org/licenses/BSD-2-Clause
  11. //
  12. // Redistribution and use in source and binary forms, with or without
  13. // modification, are permitted provided that the following conditions are
  14. // met:
  15. //
  16. // 1. Redistributions of source code must retain the above copyright
  17. // notice, this list of conditions and the following disclaimer.
  18. //
  19. // 2. Redistributions in binary form must reproduce the above copyright
  20. // notice, this list of conditions and the following disclaimer in the
  21. // documentation and/or other materials provided with the distribution.
  22. //
  23. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24. // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  25. // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  26. // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  29. // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. //
  35. #import <Foundation/Foundation.h>
  36. #import <AppKit/AppKit.h>
  37. //@import AppKit;
  38. #pragma mark -
  39. #pragma mark AppSandboxFileAccessProtocol
  40. @protocol AppSandboxFileAccessProtocol<NSObject>
  41. @required
  42. - (NSData *)bookmarkDataForURL:(NSURL *)url;
  43. - (void)setBookmarkData:(NSData *)data forURL:(NSURL *)url;
  44. - (void)clearBookmarkDataForURL:(NSURL *)url;
  45. @end
  46. #pragma mark -
  47. #pragma mark AppSandboxFileAccess
  48. typedef void (^AppSandboxFileAccessBlock)(void);
  49. typedef void (^AppSandboxFileSecurityScopeBlock)(NSURL *securityScopedFileURL, NSData *bookmarkData);
  50. @interface AppSandboxFileAccess : NSObject
  51. /*! @brief The title of the NSOpenPanel displayed when asking permission to access a file.
  52. Default: "Allow Access"
  53. */
  54. @property (readwrite, copy, nonatomic) NSString *title;
  55. /*! @brief The message contained on the the NSOpenPanel displayed when asking permission to access a file.
  56. Default: "[Application Name] needs to access this path to continue. Click Allow to continue."
  57. */
  58. @property (readwrite, copy, nonatomic) NSString *message;
  59. /*! @brief The prompt button on the the NSOpenPanel displayed when asking permission to access a file.
  60. Default: "Allow"
  61. */
  62. @property (readwrite, copy, nonatomic) NSString *prompt;
  63. /*! @brief This is an optional delegate object that can be provided to customize the persistance of bookmark data (e.g. in a Core Data database).
  64. Default: nil (Default uses the AppSandboxFileAccessPersist class.)
  65. */
  66. @property (nonatomic, weak) id <AppSandboxFileAccessProtocol> bookmarkPersistanceDelegate;
  67. /*! @brief Create the object with the default values. */
  68. + (AppSandboxFileAccess *)fileAccess;
  69. /*! @brief Initialise the object with the default values. */
  70. - (instancetype)init;
  71. /*! @brief Access a file path to read or write, automatically gaining permission from the user with NSOpenPanel if required
  72. and using persisted permissions if possible.
  73. @see accessFile:persistPermission:withBlock:
  74. @see securityScopedURLForFilePath:persistPermission:bookmark:
  75. @param path A file path, either a file or folder, that the caller needs access to.
  76. @param persist If YES will save the permission for future calls.
  77. @param block The block that will be given access to the file or folder.
  78. @return YES if permission was granted or already available, NO otherwise.
  79. */
  80. - (BOOL)accessFilePath:(NSString *)path persistPermission:(BOOL)persist withBlock:(AppSandboxFileAccessBlock)block;
  81. /*!
  82. @warning Deprecated.
  83. @see accessFilePath:persistPermission:withBlock:
  84. @param path A file path, either a file or folder, that the caller needs access to.
  85. @param block The block that will be given access to the file or folder.
  86. @param persist If YES will save the permission for future calls.
  87. @return YES if permission was granted or already available, NO otherwise.
  88. */
  89. - (BOOL)accessFilePath:(NSString *)path withBlock:(AppSandboxFileAccessBlock)block persistPermission:(BOOL)persist __attribute__((deprecated("Use 'accessFilePath:persistPermission:withBlock:' instead.")));
  90. /*! @brief Access a file URL to read or write, automatically gaining permission from the user with NSOpenPanel if required
  91. and using persisted permissions if possible.
  92. @see requestAccessPermissionsForFileURL:persistPermission:withBlock:
  93. @discussion Internally calls `requestAccessPermissionsForFileURL:persistPermission:withBlock:` and accesses the returned scoped URL if successful.
  94. @discussion See `requestAccessPermissionsForFileURL:persistPermission:withBlock:` for detailed behaviour.
  95. @param fileURL A file URL, either a file or folder, that the caller needs access to.
  96. @param persist If YES will save the permission for future calls.
  97. @param block The block that will be given access to the file or folder.
  98. @return YES if permission was granted or already available, NO otherwise.
  99. */
  100. - (BOOL)accessFileURL:(NSURL *)fileURL persistPermission:(BOOL)persist withBlock:(AppSandboxFileAccessBlock)block;
  101. /*!
  102. @warning Deprecated.
  103. @see accessFileURL:persistPermission:withBlock:
  104. @param fileURL A file URL, either a file or folder, that the caller needs access to.
  105. @param persist If YES will save the permission for future calls.
  106. @param block The block that will be given access to the file or folder.
  107. @return YES if permission was granted or already available, NO otherwise.
  108. */
  109. - (BOOL)accessFileURL:(NSURL *)fileURL withBlock:(AppSandboxFileAccessBlock)block persistPermission:(BOOL)persist __attribute__((deprecated("Use 'accessFileURL:persistPermission:withBlock:' instead.")));
  110. /*! @brief Request access permission for a file path to read or write, automatically with NSOpenPanel if required
  111. and using persisted permissions if possible.
  112. @see securityScopedURLForFilePath:persistPermission:bookmark:
  113. @param path A file path, either a file or folder, that the caller needs access to.
  114. @param persist If YES will save the permission for future calls.
  115. @return YES if permission was granted or already available, NO otherwise.
  116. */
  117. - (BOOL)requestAccessPermissionsForFilePath:(NSString *)filePath persistPermission:(BOOL)persist withBlock:(AppSandboxFileSecurityScopeBlock)block;
  118. /*! @brief Request access permission for a file path to read or write, automatically with NSOpenPanel if required
  119. and using persisted permissions if possible.
  120. @discussion Use this function to access a file URL to either read or write in an application restricted by the App Sandbox.
  121. This function will ask the user for permission if necessary using a well formed NSOpenPanel. The user will
  122. have the option of approving access to the URL you specify, or a parent path for that URL. If persist is YES
  123. the permission will be stored as a bookmark in NSUserDefaults and further calls to this function will
  124. load the saved permission and not ask for permission again.
  125. @discussion If the file URL does not exist, it's parent directory will be asked for permission instead, since permission
  126. to the directory will be required to write the file. If the parent directory doesn't exist, it will ask for
  127. permission of whatever part of the parent path exists.
  128. @discussion Note: If the caller has permission to access a file because it was dropped onto the application or introduced
  129. to the application in some other way, this function will not be aware of that permission and still prompt
  130. the user. To prevent this, use the persistPermission function to persist a permission you've been given
  131. whenever a user introduces a file to the application. E.g. when dropping a file onto the application window
  132. or dock or when using an NSOpenPanel.
  133. @param fileURL A file URL, either a file or folder, that the caller needs access to.
  134. @param persist If YES will save the permission for future calls.
  135. @param block The block that will be given access to the file or folder.
  136. @return YES if permission was granted or already available, NO otherwise.
  137. */
  138. - (BOOL)requestAccessPermissionsForFileURL:(NSURL *)fileURL persistPermission:(BOOL)persist withBlock:(AppSandboxFileSecurityScopeBlock)block;
  139. /*! @brief Persist a security bookmark for the given path. The calling application must already have permission.
  140. @see persistPermissionURL:
  141. @param path The path with permission that will be persisted.
  142. @return Bookmark data if permission was granted or already available, nil otherwise.
  143. */
  144. - (NSData *)persistPermissionPath:(NSString *)path;
  145. /*! @brief Persist a security bookmark for the given URL. The calling application must already have permission.
  146. @discussion Use this function to persist permission of a URL that has already been granted when a user introduced
  147. a file to the calling application. E.g. by dropping the file onto the application window, or dock icon,
  148. or when using an NSOpenPanel.
  149. Note: If the calling application does not have access to this file, this call will do nothing.
  150. @param url The URL with permission that will be persisted.
  151. @return Bookmark data if permission was granted or already available, nil otherwise.
  152. */
  153. - (NSData *)persistPermissionURL:(NSURL *)url;
  154. @end