/core/externals/update-engine/Common/KSAction.h

http://macfuse.googlecode.com/ · C Header · 147 lines · 18 code · 14 blank · 115 comment · 0 complexity · 2eb8ed9ac2f10a888e4ea6fe6a62d93d MD5 · raw file

  1. // Copyright 2008 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <Foundation/Foundation.h>
  15. @class KSActionProcessor, KSActionPipe;
  16. // KSAction (Abstract)
  17. //
  18. // Abstract class that encapsulates a unit of work--an Action--to be performed.
  19. // Examples of possible KSAction subclasses might be a "KSDownloadAction" or a
  20. // "KSInstallAction", that each do the obvious thing that their name implies.
  21. // Concrete KSAction instances are run by adding them to a KSActionProcessor's
  22. // queue, then starting the KSActionProcessor. KSAction instances may perform
  23. // their task synchronously or asynchronously. The only requirement is that they
  24. // must tell their owning KSActionProcessor when they are finished by sending
  25. // the KSActionProcessor a -finishedProcessing:successfully: message.
  26. //
  27. // When a KSAction is added to a KSActionProcessor, the KSActionProcessor will
  28. // call -setProcessor: on the KSAction to indicate that the action is "owned" by
  29. // the KSActionProcessor. Note that this happens when the action is enqueued,
  30. // which may be significantly before the action is told to run. Also note that
  31. // the by the time the action is told to run via the -performAction message, the
  32. // -processor method is guaranteed to return a valid KSActionProcessor.
  33. //
  34. // Input-Output:
  35. //
  36. // Every KSAction has one KSActionPipe for input and one for output. These pipes
  37. // are always present and are never nil. However, they can be replaced, and
  38. // reset at any time. Action pipes are one way for actions to communicate with
  39. // one another. One action may store its output in a KSActionPipe, and another
  40. // action may use that same pipe for its input; thus connecting the two actions.
  41. //
  42. // KSActionPipes should be viewed as filling the same role as typical pipes on
  43. // a Unix command line: connecting the output of one job to the input of
  44. // another. This is true even though KSActionPipes are at a higher-level (an
  45. // "object" level) than the stream-of-bytes level of typical Unix pipe(2)s.
  46. //
  47. // Subclassing:
  48. //
  49. // KSAction subclasses may be created and configured as necessary to enable them
  50. // to perform their action. They should not actually perform their action until
  51. // they are sent the -performAction message from a KSActionProcessor. If the
  52. // action runs asynchronously, it should also implement the -terminateAction
  53. // method, which should stop the action and only return once the action has been
  54. // terminated. Subclasses will typically only need to override two methods:
  55. // -performAction, and possibly, -terminateAction. The KSAction implementation
  56. // of all other methods should be sufficient for nearly all KSAction subclasses.
  57. //
  58. // The lifecycle of a KSAction looks like the following:
  59. //
  60. // 1. KSAction is created like normal (e.g., alloc/init)
  61. // 2. KSAction is added to a KSActionProcessor, at which point the KSAction
  62. // is sent a -setProcessor: message.
  63. // 3. Eventually, the action is sent the -performAction message indicating
  64. // that the action should do its work
  65. // 4. The action may or may not be sent a -terminateAction message indicating
  66. // that all work it is doing should be immediately stopped
  67. // 5. Once the action completes successfully or otherwise, it is sent a
  68. // -setProcessor:nil message to indicate that it was removed from the
  69. // KSActionProcessor.
  70. @interface KSAction : NSObject {
  71. @private
  72. KSActionProcessor *processor_;
  73. KSActionPipe *inpipe_;
  74. KSActionPipe *outpipe_;
  75. }
  76. // Returns the KSActionProcessor instance that owns this KSAction.
  77. - (KSActionProcessor *)processor;
  78. // Sets the KSActionProcessor that owns this KSAction. Note that a KSAction may
  79. // only be in one KSActionProcessor at a time.
  80. - (void)setProcessor:(KSActionProcessor *)processor;
  81. // Returns the KSActionPipe for this action to use for input. This method never
  82. // returns nil.
  83. - (KSActionPipe *)inPipe;
  84. // Sets the KSActionPipe for this action to use for input. If |inpipe| is nil,
  85. // the input pipe will be re-set to a new, empty KSActionPipe.
  86. - (void)setInPipe:(KSActionPipe *)inpipe;
  87. // Returns the KSActionPipe for this action to use for output. This method never
  88. // returns nil.
  89. - (KSActionPipe *)outPipe;
  90. // Sets the KSActionPipe for this action to use for output. If |outpipe| is nil,
  91. // the output pipe will be re-set to a new, empty KSActionPipe.
  92. - (void)setOutPipe:(KSActionPipe *)outpipe;
  93. // Returns YES if the action is currently running, NO otherwise. Subclasses
  94. // should NOT need to override this method. It returns YES if this action (self)
  95. // is the "current action" on its |processor|, NO otherwise. This should be the
  96. // correct "isRunning" status for the majority of KSAction subclasses.
  97. - (BOOL)isRunning;
  98. //
  99. // Methods that subclasses are likely to need to override.
  100. //
  101. // This message is sent by a KSActionProcessor when the action is supposed to
  102. // begin. This method may be synchronous or asynchronous. When the action is
  103. // complete, the -[KSActionProcessor finishedProcessing:successfully:] message
  104. // should be sent to the KSActionProcessor on which this action is running (as
  105. // obtained from the -processor message).
  106. //
  107. // Note that the -finishedProcessing:successfully: message should always be sent
  108. // to the processor instance eventually. This is the only way the processor
  109. // will know that the action has finished. The only caveat to this rule is if
  110. // your action is terminated by the -terminateAction method, then you should NOT
  111. // send -finishedProcessing:successfully: to the action processor.
  112. //
  113. // Also, keep in mind that, as with all Google code, this method is NOT allowed
  114. // to throw any exceptions. If you use an API that is documented to throw (or
  115. // is likely to throw), such as NSTask, it is your responsibility to catch that
  116. // and handle it appropriately.
  117. //
  118. // *** Note that a KSAction may only be on one KSActionProcessor at a time. ***
  119. - (void)performAction;
  120. // This message is sent by the KSActionProcessor when it needs to prematurely
  121. // terminate a running action. When this message is sent you should immediately
  122. // stop all processing before returning from this method.
  123. //
  124. // This method is optional. If your action doesn't need to do any cleanup, or
  125. // if it's synchronous (meaning, it will be done when -performAction
  126. // returns), this method does not need to be overridden.
  127. //
  128. // Subclasses should NOT send -finishedProcessing:successfully: to the action
  129. // processor from this method. If this method is called, the action processor
  130. // already knows that you're finishing, and therefore, it's unnecessary to
  131. // send that message.
  132. - (void)terminateAction;
  133. @end