Index: media/base/state_machine.h |
diff --git a/media/base/state_machine.h b/media/base/state_machine.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bb16c118dce33f154307cc4741b457a8e00cb986 |
--- /dev/null |
+++ b/media/base/state_machine.h |
@@ -0,0 +1,69 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef MEDIA_BASE_STATE_MACHINE_H_ |
+#define MEDIA_BASE_STATE_MACHINE_H_ |
+ |
+#include <vector> |
+ |
+#include "media/base/media_export.h" |
+#include "media/base/state.h" |
+ |
+namespace media { |
+ |
+// Stateless implementation of a state machine. The subclasses must handle |
+// being given the State objects, since they'll probably want concrete |
+// subclasses anyway. Subclasses should call UsingState to record a ref to the |
+// state, and automatically get updates for it. |
+class MEDIA_EXPORT StateMachine : public State::Client { |
+ public: |
+ ~StateMachine() override; |
+ |
+ // Subclasses will find out about State objects (or concrete subclasses of |
+ // state) via methods on the StateMachine subclass / constructor / or |
+ // whatever they like. |
+ |
+ // Thing that owns / runs the state machine (e.g., StateMachineDriver). |
+ class Owner { |
+ public: |
+ // Request that our owner run us, but not re-entrantly. It is unclear |
+ // if it may run before returning from this or not, assuming that |us| |
+ // is not running currently. It must not run immediately if this is |
+ // called recursively from |us->Run()|. |
+ // Probably this is never allowed to run before returning. |
+ virtual void RequestRun(StateMachine* us) = 0; |
+ }; |
+ |
+ // Set the owner that we'll use. |
+ void SetOwner(Owner* owner); |
+ |
+ // Run the state machine once. |
+ virtual void Run() const = 0; |
+ |
+ // State::Client |
+ // If any state notifies us that it has changed. We'll request that our |
+ // owner runs us via owner_->RequestRun(this). |
+ void OnStateChanged() override; |
+ |
+ protected: |
+ StateMachine(); |
+ |
+ // Adds |state| to the set that we're using. This will cause us to register |
+ // as a client with them, and probably keep a ref (which the caller likely |
+ // also has too, to the subclass type). By registering state, we will |
+ // automatically request a run when it changes. Alternatively, if it makes |
+ // things cleaner, the state could keep a list of (machine, driver) pairs |
+ // that it notifies, but that seems unlikely to improve it. |
+ void UsingState(scoped_refptr<State> state); |
+ |
+ private: |
+ Owner* owner_; |
+ std::vector<scoped_refptr<State>> states_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(StateMachine); |
+}; |
+ |
+} // namespace media |
+ |
+#endif // MEDIA_BASE_STATE_MACHINE_H_ |