action_processor.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Copyright (C) 2011 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #ifndef UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_
  17. #define UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_
  18. #include <deque>
  19. #include <memory>
  20. #include <vector>
  21. #include <base/macros.h>
  22. #include <brillo/errors/error.h>
  23. #include "update_engine/common/error_code.h"
  24. #include <gtest/gtest_prod.h>
  25. // The structure of these classes (Action, ActionPipe, ActionProcessor, etc.)
  26. // is based on the KSAction* classes from the Google Update Engine code at
  27. // http://code.google.com/p/update-engine/ . The author of this file sends
  28. // a big thanks to that team for their high quality design, implementation,
  29. // and documentation.
  30. // See action.h for an overview of this class and other Action* classes.
  31. // An ActionProcessor keeps a queue of Actions and processes them in order.
  32. namespace chromeos_update_engine {
  33. class AbstractAction;
  34. class ActionProcessorDelegate;
  35. class ActionProcessor {
  36. public:
  37. ActionProcessor() = default;
  38. virtual ~ActionProcessor();
  39. // Starts processing the first Action in the queue. If there's a delegate,
  40. // when all processing is complete, ProcessingDone() will be called on the
  41. // delegate.
  42. virtual void StartProcessing();
  43. // Aborts processing. If an Action is running, it will have
  44. // TerminateProcessing() called on it. The Action that was running and all the
  45. // remaining actions will be lost and must be re-enqueued if this Processor is
  46. // to use it.
  47. void StopProcessing();
  48. // Suspend the processing. If an Action is running, it will have the
  49. // SuspendProcessing() called on it, and it should suspend operations until
  50. // ResumeProcessing() is called on this class to continue. While suspended,
  51. // no new actions will be started. Calling SuspendProcessing while the
  52. // processing is suspended or not running this method performs no action.
  53. void SuspendProcessing();
  54. // Resume the suspended processing. If the ActionProcessor is not suspended
  55. // or not running in the first place this method performs no action.
  56. void ResumeProcessing();
  57. // Returns true iff the processing was started but not yet completed nor
  58. // stopped.
  59. bool IsRunning() const;
  60. // Adds another Action to the end of the queue.
  61. virtual void EnqueueAction(std::unique_ptr<AbstractAction> action);
  62. // Sets/gets the current delegate. Set to null to remove a delegate.
  63. ActionProcessorDelegate* delegate() const { return delegate_; }
  64. void set_delegate(ActionProcessorDelegate* delegate) { delegate_ = delegate; }
  65. // Returns a pointer to the current Action that's processing.
  66. AbstractAction* current_action() const { return current_action_.get(); }
  67. // Called by an action to notify processor that it's done. Caller passes self.
  68. // But this call deletes the action if there no other object has a reference
  69. // to it, so in that case, the caller should not try to access any of its
  70. // member variables after this call.
  71. void ActionComplete(AbstractAction* actionptr, ErrorCode code);
  72. private:
  73. FRIEND_TEST(ActionProcessorTest, ChainActionsTest);
  74. // Continue processing actions (if any) after the last action terminated with
  75. // the passed error code. If there are no more actions to process, the
  76. // processing will terminate.
  77. void StartNextActionOrFinish(ErrorCode code);
  78. // Actions that have not yet begun processing, in the order in which
  79. // they'll be processed.
  80. std::deque<std::unique_ptr<AbstractAction>> actions_;
  81. // A pointer to the currently processing Action, if any.
  82. std::unique_ptr<AbstractAction> current_action_;
  83. // The ErrorCode reported by an action that was suspended but finished while
  84. // being suspended. This error code is stored here to be reported back to the
  85. // delegate once the processor is resumed.
  86. ErrorCode suspended_error_code_{ErrorCode::kSuccess};
  87. // Whether the action processor is or should be suspended.
  88. bool suspended_{false};
  89. // A pointer to the delegate, or null if none.
  90. ActionProcessorDelegate* delegate_{nullptr};
  91. DISALLOW_COPY_AND_ASSIGN(ActionProcessor);
  92. };
  93. // A delegate object can be used to be notified of events that happen
  94. // in an ActionProcessor. An instance of this class can be passed to an
  95. // ActionProcessor to register itself.
  96. class ActionProcessorDelegate {
  97. public:
  98. virtual ~ActionProcessorDelegate() = default;
  99. // Called when all processing in an ActionProcessor has completed. A pointer
  100. // to the ActionProcessor is passed. |code| is set to the exit code of the
  101. // last completed action.
  102. virtual void ProcessingDone(const ActionProcessor* processor,
  103. ErrorCode code) {}
  104. // Called when processing has stopped. Does not mean that all Actions have
  105. // completed. If/when all Actions complete, ProcessingDone() will be called.
  106. virtual void ProcessingStopped(const ActionProcessor* processor) {}
  107. // Called whenever an action has finished processing, either successfully
  108. // or otherwise.
  109. virtual void ActionCompleted(ActionProcessor* processor,
  110. AbstractAction* action,
  111. ErrorCode code) {}
  112. };
  113. } // namespace chromeos_update_engine
  114. #endif // UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_