activity.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #ifndef __XPC_ACTIVITY_H__
  2. #define __XPC_ACTIVITY_H__
  3. #ifndef __XPC_INDIRECT__
  4. #error "Please #include <xpc/xpc.h> instead of this file directly."
  5. // For HeaderDoc.
  6. #include <xpc/base.h>
  7. #endif // __XPC_INDIRECT__
  8. #ifdef __BLOCKS__
  9. __BEGIN_DECLS
  10. /*
  11. * The following are a collection of keys and values used to set an activity's
  12. * execution criteria.
  13. */
  14. /*!
  15. * @constant XPC_ACTIVITY_INTERVAL
  16. * An integer property indicating the desired time interval (in seconds) of the
  17. * activity. The activity will not be run more than once per time interval.
  18. * Due to the nature of XPC Activity finding an opportune time to run
  19. * the activity, any two occurrences may be more or less than 'interval'
  20. * seconds apart, but on average will be 'interval' seconds apart.
  21. * The presence of this key implies the following, unless overridden:
  22. * - XPC_ACTIVITY_REPEATING with a value of true
  23. * - XPC_ACTIVITY_DELAY with a value of half the 'interval'
  24. * The delay enforces a minimum distance between any two occurrences.
  25. * - XPC_ACTIVITY_GRACE_PERIOD with a value of half the 'interval'.
  26. * The grace period is the amount of time allowed to pass after the end of
  27. * the interval before more aggressive scheduling occurs. The grace period
  28. * does not increase the size of the interval.
  29. */
  30. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  31. XPC_EXPORT
  32. const char *XPC_ACTIVITY_INTERVAL;
  33. /*!
  34. * @constant XPC_ACTIVITY_REPEATING
  35. * A boolean property indicating whether this is a repeating activity.
  36. */
  37. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  38. XPC_EXPORT
  39. const char *XPC_ACTIVITY_REPEATING;
  40. /*!
  41. * @constant XPC_ACTIVITY_DELAY
  42. * An integer property indicating the number of seconds to delay before
  43. * beginning the activity.
  44. */
  45. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  46. XPC_EXPORT
  47. const char *XPC_ACTIVITY_DELAY;
  48. /*!
  49. * @constant XPC_ACTIVITY_GRACE_PERIOD
  50. * An integer property indicating the number of seconds to allow as a grace
  51. * period before the scheduling of the activity becomes more aggressive.
  52. */
  53. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  54. XPC_EXPORT
  55. const char *XPC_ACTIVITY_GRACE_PERIOD;
  56. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  57. XPC_EXPORT
  58. const int64_t XPC_ACTIVITY_INTERVAL_1_MIN;
  59. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  60. XPC_EXPORT
  61. const int64_t XPC_ACTIVITY_INTERVAL_5_MIN;
  62. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  63. XPC_EXPORT
  64. const int64_t XPC_ACTIVITY_INTERVAL_15_MIN;
  65. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  66. XPC_EXPORT
  67. const int64_t XPC_ACTIVITY_INTERVAL_30_MIN;
  68. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  69. XPC_EXPORT
  70. const int64_t XPC_ACTIVITY_INTERVAL_1_HOUR;
  71. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  72. XPC_EXPORT
  73. const int64_t XPC_ACTIVITY_INTERVAL_4_HOURS;
  74. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  75. XPC_EXPORT
  76. const int64_t XPC_ACTIVITY_INTERVAL_8_HOURS;
  77. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  78. XPC_EXPORT
  79. const int64_t XPC_ACTIVITY_INTERVAL_1_DAY;
  80. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  81. XPC_EXPORT
  82. const int64_t XPC_ACTIVITY_INTERVAL_7_DAYS;
  83. /*!
  84. * @constant XPC_ACTIVITY_PRIORITY
  85. * A string property indicating the priority of the activity.
  86. */
  87. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  88. XPC_EXPORT
  89. const char *XPC_ACTIVITY_PRIORITY;
  90. /*!
  91. * @constant XPC_ACTIVITY_PRIORITY_MAINTENANCE
  92. * A string indicating activity is maintenance priority.
  93. * Maintenance priority is intended for user-invisible maintenance tasks
  94. * such as garbage collection or optimization.
  95. */
  96. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  97. XPC_EXPORT
  98. const char *XPC_ACTIVITY_PRIORITY_MAINTENANCE;
  99. /*!
  100. * @constant XPC_ACTIVITY_PRIORITY_UTILITY
  101. * A string indicating activity is utility priority.
  102. * Utility priority is intended for user-visible tasks such as fetching data
  103. * from the network, copying files, or importing data.
  104. */
  105. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  106. XPC_EXPORT
  107. const char *XPC_ACTIVITY_PRIORITY_UTILITY;
  108. /*!
  109. * @constant XPC_ACTIVITY_ALLOW_BATTERY
  110. * A Boolean value indicating whether the activity should be allowed to run
  111. * while the computer is on battery power. The default value is false for
  112. * maintenance priority activity and true for utility priority activity.
  113. */
  114. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  115. XPC_EXPORT
  116. const char *XPC_ACTIVITY_ALLOW_BATTERY;
  117. /*!
  118. * @constant XPC_ACTIVITY_REQUIRE_SCREEN_SLEEP
  119. * A Boolean value indicating whether the activity should only be performed
  120. * while the primary screen is in sleep mode. Defaults to false.
  121. */
  122. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  123. XPC_EXPORT
  124. const char *XPC_ACTIVITY_REQUIRE_SCREEN_SLEEP; // bool
  125. /*!
  126. * @constant XPC_ACTIVITY_REQUIRE_BATTERY_LEVEL
  127. * An integer percentage of minimum battery charge required to allow the
  128. * activity to run. A default minimum battery level is determined by the
  129. * system.
  130. */
  131. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  132. XPC_EXPORT
  133. const char *XPC_ACTIVITY_REQUIRE_BATTERY_LEVEL; // int (%)
  134. /*!
  135. * @constant XPC_ACTIVITY_REQUIRE_HDD_SPINNING
  136. * A Boolean value indicating whether the activity should only be performed
  137. * while the hard disk drive (HDD) is spinning. Computers with flash storage
  138. * are considered to be equivalent to HDD spinning. Defaults to false.
  139. */
  140. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  141. XPC_EXPORT
  142. const char *XPC_ACTIVITY_REQUIRE_HDD_SPINNING; // bool
  143. /*!
  144. * @define XPC_TYPE_ACTIVITY
  145. * A type representing a connection to a named service. This connection is
  146. * bidirectional and can be used to both send and receive messages. A
  147. * connection carries the credentials of the remote service provider.
  148. */
  149. #define XPC_TYPE_ACTIVITY (&_xpc_type_activity)
  150. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  151. XPC_EXPORT
  152. XPC_TYPE(_xpc_type_activity);
  153. /*!
  154. * @typedef xpc_activity_t
  155. *
  156. * @abstract
  157. * An XPC activity object.
  158. *
  159. * @discussion
  160. * This object represents a set of execution criteria and a current execution
  161. * state for background activity on the system. Once an activity is registered,
  162. * the system will evaluate its criteria to determine whether the activity is
  163. * eligible to run under current system conditions. When an activity becomes
  164. * eligible to run, its execution state will be updated and an invocation of
  165. * its handler block will be made.
  166. */
  167. XPC_DECL(xpc_activity);
  168. /*!
  169. * @typedef xpc_activity_handler_t
  170. *
  171. * @abstract
  172. * A block that is called when an XPC activity becomes eligible to run.
  173. */
  174. typedef void (^xpc_activity_handler_t)(xpc_activity_t activity);
  175. /*!
  176. * @constant XPC_ACTIVITY_CHECK_IN
  177. * This constant may be passed to xpc_activity_register() as the criteria
  178. * dictionary in order to check in with the system for previously registered
  179. * activity using the same identifier (for example, an activity taken from a
  180. * launchd property list).
  181. */
  182. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  183. XPC_EXPORT
  184. const xpc_object_t XPC_ACTIVITY_CHECK_IN;
  185. /*!
  186. * @function xpc_activity_register
  187. *
  188. * @abstract
  189. * Registers an activity with the system.
  190. *
  191. * @discussion
  192. * Registers a new activity with the system. The criteria of the activity are
  193. * described by the dictionary passed to this function. If an activity with the
  194. * same identifier already exists, the criteria provided override the existing
  195. * criteria unless the special dictionary XPC_ACTIVITY_CHECK_IN is used. The
  196. * XPC_ACTIVITY_CHECK_IN dictionary instructs the system to first look up an
  197. * existing activity without modifying its criteria. Once the existing activity
  198. * is found (or a new one is created with an empty set of criteria) the handler
  199. * will be called with an activity object in the XPC_ACTIVITY_STATE_CHECK_IN
  200. * state.
  201. *
  202. * @param identifier
  203. * A unique identifier for the activity. Each application has its own namespace.
  204. *
  205. * @param criteria
  206. * A dictionary of criteria for the activity.
  207. *
  208. * @param handler
  209. * The handler block to be called when the activity changes state to one of the
  210. * following states:
  211. * - XPC_ACTIVITY_STATE_CHECK_IN (optional)
  212. * - XPC_ACTIVITY_STATE_RUN
  213. *
  214. * The handler block is never invoked reentrantly. It will be invoked on a
  215. * dispatch queue with an appropriate priority to perform the activity.
  216. */
  217. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  218. XPC_EXPORT XPC_NONNULL1 XPC_NONNULL2 XPC_NONNULL3
  219. void
  220. xpc_activity_register(const char *identifier, xpc_object_t criteria,
  221. xpc_activity_handler_t handler);
  222. /*!
  223. * @function xpc_activity_copy_criteria
  224. *
  225. * @abstract
  226. * Returns an XPC dictionary describing the execution criteria of an activity.
  227. * This will return NULL in cases where the activity has already completed, e.g.
  228. * when checking in to an event that finished and was not rescheduled.
  229. */
  230. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  231. XPC_EXPORT XPC_WARN_RESULT XPC_RETURNS_RETAINED
  232. xpc_object_t
  233. xpc_activity_copy_criteria(xpc_activity_t activity);
  234. /*!
  235. * @function xpc_activity_set_criteria
  236. *
  237. * @abstract
  238. * Modifies the execution criteria of an activity.
  239. */
  240. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  241. XPC_EXPORT XPC_NONNULL1 XPC_NONNULL2
  242. void
  243. xpc_activity_set_criteria(xpc_activity_t activity, xpc_object_t criteria);
  244. /*!
  245. * @enum xpc_activity_state_t
  246. * An activity is defined to be in one of the following states. Applications
  247. * may check the current state of the activity using xpc_activity_get_state()
  248. * in the handler block provided to xpc_activity_register().
  249. *
  250. * The application can modify the state of the activity by calling
  251. * xpc_activity_set_state() with one of the following:
  252. * - XPC_ACTIVITY_STATE_DEFER
  253. * - XPC_ACTIVITY_STATE_CONTINUE
  254. * - XPC_ACTIVITY_STATE_DONE
  255. *
  256. * @constant XPC_ACTIVITY_STATE_CHECK_IN
  257. * An activity in this state has just completed a checkin with the system after
  258. * XPC_ACTIVITY_CHECK_IN was provided as the criteria dictionary to
  259. * xpc_activity_register. The state gives the application an opportunity to
  260. * inspect and modify the activity's criteria.
  261. *
  262. * @constant XPC_ACTIVITY_STATE_WAIT
  263. * An activity in this state is waiting for an opportunity to run. This value
  264. * is never returned within the activity's handler block, as the block is
  265. * invoked in response to XPC_ACTIVITY_STATE_CHECK_IN or XPC_ACTIVITY_STATE_RUN.
  266. *
  267. * Note:
  268. * A launchd job may idle exit while an activity is in the wait state and be
  269. * relaunched in response to the activity becoming runnable. The launchd job
  270. * simply needs to re-register for the activity on its next launch by passing
  271. * XPC_ACTIVITY_STATE_CHECK_IN to xpc_activity_register().
  272. *
  273. * @constant XPC_ACTIVITY_STATE_RUN
  274. * An activity in this state is eligible to run based on its criteria.
  275. *
  276. * @constant XPC_ACTIVITY_STATE_DEFER
  277. * An application may pass this value to xpc_activity_set_state() to indicate
  278. * that the activity should be deferred (placed back into the WAIT state) until
  279. * a time when its criteria are met again. Deferring an activity does not reset
  280. * any of its time-based criteria (in other words, it will remain past due).
  281. * This should be done in response to observing xpc_activity_should_defer().
  282. *
  283. * @constant XPC_ACTIVITY_STATE_CONTINUE
  284. * An application may pass this value to xpc_activity_set_state() to indicate
  285. * that the activity will continue its operation beyond the return of its
  286. * handler block. This can be used to extend an activity to include asynchronous
  287. * operations. The activity's handler block will not be invoked again until the
  288. * state has been updated to either XPC_ACTIVITY_STATE_DEFER or, in the case
  289. * of repeating activity, XPC_ACTIVITY_STATE_DONE.
  290. *
  291. * @constant XPC_ACTIVITY_STATE_DONE
  292. * An application may pass this value to xpc_activity_set_state() to indicate
  293. * that the activity has completed. For non-repeating activity, the resources
  294. * associated with the activity will be automatically released upon return from
  295. * the handler block. For repeating activity, timers present in the activity's
  296. * criteria will be reset.
  297. */
  298. enum {
  299. XPC_ACTIVITY_STATE_CHECK_IN,
  300. XPC_ACTIVITY_STATE_WAIT,
  301. XPC_ACTIVITY_STATE_RUN,
  302. XPC_ACTIVITY_STATE_DEFER,
  303. XPC_ACTIVITY_STATE_CONTINUE,
  304. XPC_ACTIVITY_STATE_DONE,
  305. };
  306. typedef long xpc_activity_state_t;
  307. /*!
  308. * @function xpc_activity_get_state
  309. *
  310. * @abstract
  311. * Returns the current state of an activity.
  312. */
  313. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  314. XPC_EXPORT XPC_WARN_RESULT XPC_NONNULL1
  315. xpc_activity_state_t
  316. xpc_activity_get_state(xpc_activity_t activity);
  317. /*!
  318. * @function xpc_activity_set_state
  319. *
  320. * @abstract
  321. * Updates the current state of an activity.
  322. *
  323. * @return
  324. * Returns true if the state was successfully updated; otherwise, returns
  325. * false if the requested state transition is not valid.
  326. */
  327. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  328. XPC_EXPORT XPC_WARN_RESULT XPC_NONNULL1
  329. bool
  330. xpc_activity_set_state(xpc_activity_t activity, xpc_activity_state_t state);
  331. /*!
  332. * @function xpc_activity_should_defer
  333. *
  334. * @abstract
  335. * Test whether an activity should be deferred.
  336. *
  337. * @discussion
  338. * This function may be used to test whether the criteria of a long-running
  339. * activity are still satisfied. If not, the system indicates that the
  340. * application should defer the activity. The application may acknowledge the
  341. * deferral by calling xpc_activity_set_state() with XPC_ACTIVITY_STATE_DEFER.
  342. * Once deferred, the system will place the activity back into the WAIT state
  343. * and re-invoke the handler block at the earliest opportunity when the criteria
  344. * are once again satisfied.
  345. *
  346. * @return
  347. * Returns true if the activity should be deferred.
  348. */
  349. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  350. XPC_EXPORT
  351. bool
  352. xpc_activity_should_defer(xpc_activity_t activity);
  353. /*!
  354. * @function xpc_activity_unregister
  355. *
  356. * @abstract
  357. * Unregisters an activity found by its identifier.
  358. *
  359. * @discussion
  360. * A dynamically registered activity will be deleted in response to this call.
  361. * Statically registered activity (from a launchd property list) will be
  362. * reverted to its original criteria if any modifications were made.
  363. *
  364. * Unregistering an activity has no effect on any outstanding xpc_activity_t
  365. * objects or any currently executing xpc_activity_handler_t blocks; however,
  366. * no new handler block invocations will be made after it is unregistered.
  367. *
  368. * @param identifier
  369. * The identifier of the activity to unregister.
  370. */
  371. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0)
  372. XPC_EXPORT XPC_NONNULL1
  373. void
  374. xpc_activity_unregister(const char *identifier);
  375. __END_DECLS
  376. #endif // __BLOCKS__
  377. #endif // __XPC_ACTIVITY_H__