1 // Copyright 2020 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 THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_CSS_CSS_SCROLL_TIMELINE_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_CSS_CSS_SCROLL_TIMELINE_H_
7 
8 #include "base/optional.h"
9 #include "third_party/blink/renderer/core/animation/scroll_timeline.h"
10 #include "third_party/blink/renderer/core/dom/id_target_observer.h"
11 
12 namespace blink {
13 
14 class Document;
15 class Element;
16 class StyleRuleScrollTimeline;
17 
18 // A CSSScrollTimeline is like a ScrollTimeline, except it originates from
19 // an @scroll-timeline rule.
20 class CORE_EXPORT CSSScrollTimeline : public ScrollTimeline {
21  public:
22   struct Options {
23     STACK_ALLOCATED();
24 
25    public:
26     Options(Element*, StyleRuleScrollTimeline&);
27 
28     // TODO(crbug.com/1097041): Support 'auto' value.
IsValidOptions29     bool IsValid() const { return time_range_.has_value(); }
30 
31    private:
32     friend class CSSScrollTimeline;
33 
34     Element* source_;
35     ScrollTimeline::ScrollDirection direction_;
36     HeapVector<Member<ScrollTimelineOffset>>* offsets_;
37     base::Optional<double> time_range_;
38     StyleRuleScrollTimeline* rule_;
39   };
40 
41   CSSScrollTimeline(Document*, const Options&);
42 
43   const AtomicString& Name() const;
44 
45   bool Matches(const Options&) const;
46 
47   // AnimationTimeline implementation.
IsCSSScrollTimeline()48   bool IsCSSScrollTimeline() const override { return true; }
49   void AnimationAttached(Animation*) override;
50   void AnimationDetached(Animation*) override;
51 
52   // If a CSSScrollTimeline matching |options| already exists, return that
53   // timeline. Otherwise returns nullptr.
54   static CSSScrollTimeline* FindMatchingTimeline(const Options&);
55 
56   void Trace(Visitor*) const override;
57 
58  private:
59   void SetObservers(HeapVector<Member<IdTargetObserver>>);
60 
61   Member<StyleRuleScrollTimeline> rule_;
62   HeapVector<Member<IdTargetObserver>> observers_;
63 };
64 
65 template <>
66 struct DowncastTraits<CSSScrollTimeline> {
67   static bool AllowFrom(const AnimationTimeline& value) {
68     return value.IsCSSScrollTimeline();
69   }
70 };
71 
72 }  // namespace blink
73 
74 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_CSS_CSS_SCROLL_TIMELINE_H_
75