1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_ 6 #define UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_ 7 8 #include <map> 9 #include <set> 10 11 #include "base/compiler_specific.h" 12 #include "ui/compositor/compositor_export.h" 13 #include "ui/compositor/layer_animation_element.h" 14 15 namespace ui { 16 17 namespace test { 18 class CountCheckingLayerAnimationObserver; 19 class LayerAnimationObserverTestApi; 20 } // namespace test 21 22 class LayerAnimationSequence; 23 class ScopedLayerAnimationSettings; 24 class ImplicitAnimationObserver; 25 26 // LayerAnimationObservers are notified when animations complete. 27 class COMPOSITOR_EXPORT LayerAnimationObserver { 28 public: 29 // Called when the |sequence| starts. 30 virtual void OnLayerAnimationStarted(LayerAnimationSequence* sequence); 31 32 // Called when the |sequence| ends. Not called if |sequence| is aborted. 33 virtual void OnLayerAnimationEnded( 34 LayerAnimationSequence* sequence) = 0; 35 36 // Called if |sequence| is aborted for any reason. Should never do anything 37 // that may cause another animation to be started. 38 virtual void OnLayerAnimationAborted( 39 LayerAnimationSequence* sequence) = 0; 40 41 // Called when the animation is scheduled. 42 virtual void OnLayerAnimationScheduled( 43 LayerAnimationSequence* sequence) = 0; 44 45 protected: 46 typedef std::set<LayerAnimationSequence*> AttachedSequences; 47 48 LayerAnimationObserver(); 49 virtual ~LayerAnimationObserver(); 50 51 // If the LayerAnimator is destroyed during an animation, the animations are 52 // aborted. The resulting NotifyAborted notifications will NOT be sent to 53 // this observer if this function returns false. An observer who wants to 54 // receive the NotifyAborted notifications during destruction can override 55 // this function to return true. 56 // 57 // *** IMPORTANT ***: If a class overrides this function to return true and 58 // that class is a direct or indirect owner of the LayerAnimationSequence 59 // being observed, then the class must explicitly remove itself as an 60 // observer during destruction of the LayerAnimationObserver! This is to 61 // ensure that a partially destroyed observer isn't notified with an 62 // OnLayerAnimationAborted() call when the LayerAnimator is destroyed. 63 // 64 // This opt-in pattern is used because it is common for a class to be the 65 // observer of a LayerAnimationSequence that it owns indirectly because it 66 // owns the Layer which owns the LayerAnimator which owns the 67 // LayerAnimationSequence. 68 virtual bool RequiresNotificationWhenAnimatorDestroyed() const; 69 70 // Called when |this| is added to |sequence|'s observer list. 71 virtual void OnAttachedToSequence(LayerAnimationSequence* sequence); 72 73 // Called when |this| is removed to |sequence|'s observer list. 74 virtual void OnDetachedFromSequence(LayerAnimationSequence* sequence); 75 76 // Detaches this observer from all sequences it is currently observing. 77 void StopObserving(); 78 attached_sequences()79 const AttachedSequences& attached_sequences() const { 80 return attached_sequences_; 81 } 82 83 private: 84 friend class LayerAnimationSequence; 85 friend class test::CountCheckingLayerAnimationObserver; 86 friend class test::LayerAnimationObserverTestApi; 87 88 // Called when |this| is added to |sequence|'s observer list. 89 void AttachedToSequence(LayerAnimationSequence* sequence); 90 91 // Called when |this| is removed to |sequence|'s observer list. 92 // This will only result in notifications if |send_notification| is true. 93 void DetachedFromSequence(LayerAnimationSequence* sequence, 94 bool send_notification); 95 96 AttachedSequences attached_sequences_; 97 }; 98 99 // An implicit animation observer is intended to be used in conjunction with a 100 // ScopedLayerAnimationSettings object in order to receive a notification when 101 // all implicit animations complete. 102 // TODO(bruthig): Unify the ImplicitAnimationObserver with the 103 // CallbackLayerAnimationObserver. (See www.crbug.com/542825). 104 class COMPOSITOR_EXPORT ImplicitAnimationObserver 105 : public LayerAnimationObserver { 106 public: 107 ImplicitAnimationObserver(); 108 ~ImplicitAnimationObserver() override; 109 110 // Called when the first animation sequence has started. OnImplicitAnimationsScheduled()111 virtual void OnImplicitAnimationsScheduled() {} 112 113 virtual void OnImplicitAnimationsCompleted() = 0; 114 115 protected: 116 // Deactivates the observer and clears the collection of animations it is 117 // waiting for. 118 void StopObservingImplicitAnimations(); 119 120 // Returns whether animation for |property| was aborted. 121 // Note that if the property wasn't animated, then it couldn't have been 122 // aborted, so this will return false for that property. 123 bool WasAnimationAbortedForProperty( 124 LayerAnimationElement::AnimatableProperty property) const; 125 126 // Returns whether animation for |property| was completed successfully. 127 // Note that if the property wasn't animated, then it couldn't have been 128 // completed, so this will return false for that property. 129 bool WasAnimationCompletedForProperty( 130 LayerAnimationElement::AnimatableProperty property) const; 131 132 private: 133 enum AnimationStatus { 134 ANIMATION_STATUS_UNKNOWN, 135 ANIMATION_STATUS_COMPLETED, 136 ANIMATION_STATUS_ABORTED, 137 }; 138 139 friend class ScopedLayerAnimationSettings; 140 141 // LayerAnimationObserver implementation 142 void OnLayerAnimationEnded(LayerAnimationSequence* sequence) override; 143 void OnLayerAnimationAborted(LayerAnimationSequence* sequence) override; 144 void OnLayerAnimationScheduled(LayerAnimationSequence* sequence) override; 145 void OnAttachedToSequence(LayerAnimationSequence* sequence) override; 146 void OnDetachedFromSequence(LayerAnimationSequence* sequence) override; 147 148 // OnImplicitAnimationsCompleted is not fired unless the observer is active. active()149 bool active() const { return active_; } 150 void SetActive(bool active); 151 152 void CheckCompleted(); 153 154 void UpdatePropertyAnimationStatus(LayerAnimationSequence* sequence, 155 AnimationStatus status); 156 AnimationStatus AnimationStatusForProperty( 157 LayerAnimationElement::AnimatableProperty property) const; 158 159 bool active_; 160 161 // Set to true in the destructor (if non-NULL). Used to detect deletion while 162 // calling out. 163 bool* destroyed_; 164 165 typedef std::map<LayerAnimationElement::AnimatableProperty, 166 AnimationStatus> PropertyAnimationStatusMap; 167 PropertyAnimationStatusMap property_animation_status_; 168 169 // True if OnLayerAnimationScheduled() has been called at least once. 170 bool first_sequence_scheduled_; 171 }; 172 173 } // namespace ui 174 175 #endif // UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_ 176