File: timing_input.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (278 lines) | stat: -rw-r--r-- 11,234 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/animation/timing_input.h"

#include "third_party/blink/renderer/bindings/core/v8/v8_effect_timing.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_keyframe_animation_options.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_keyframe_effect_options.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_optional_effect_timing.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_timeline_range.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_timeline_range_offset.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_cssnumericvalue_string_unrestricteddouble.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_keyframeanimationoptions_unrestricteddouble.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_keyframeeffectoptions_unrestricteddouble.h"
#include "third_party/blink/renderer/core/animation/animation_effect.h"
#include "third_party/blink/renderer/core/animation/animation_input_helpers.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/css/css_value_list.h"
#include "third_party/blink/renderer/core/css/cssom/css_unit_values.h"
#include "third_party/blink/renderer/core/css/properties/css_property.h"
#include "third_party/blink/renderer/core/execution_context/security_context.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/geometry/calculation_value.h"

namespace blink {
namespace {

Timing::PlaybackDirection ConvertPlaybackDirection(
    V8PlaybackDirection::Enum direction) {
  switch (direction) {
    case V8PlaybackDirection::Enum::kReverse:
      return Timing::PlaybackDirection::REVERSE;
    case V8PlaybackDirection::Enum::kAlternate:
      return Timing::PlaybackDirection::ALTERNATE_NORMAL;
    case V8PlaybackDirection::Enum::kAlternateReverse:
      return Timing::PlaybackDirection::ALTERNATE_REVERSE;
    case V8PlaybackDirection::Enum::kNormal:
      return Timing::PlaybackDirection::NORMAL;
  }
}

std::optional<AnimationTimeDelta> ConvertIterationDuration(
    const V8UnionCSSNumericValueOrStringOrUnrestrictedDouble* duration) {
  if (duration->IsUnrestrictedDouble()) {
    return ANIMATION_TIME_DELTA_FROM_MILLISECONDS(
        duration->GetAsUnrestrictedDouble());
  }
  return std::nullopt;
}

Timing::Delay ConvertDelay(const Timing::V8Delay* delay,
                           double default_percent,
                           ExceptionState& exception_state) {
  Timing::Delay result;
  if (delay->IsDouble()) {
    double delay_in_ms = delay->GetAsDouble();
    DCHECK(std::isfinite(delay_in_ms));
    result.time_delay = ANIMATION_TIME_DELTA_FROM_MILLISECONDS(delay_in_ms);
  } else {
    CSSNumericValue* numeric_value = delay->GetAsCSSNumericValue();
    CSSUnitValue* unit_value =
        numeric_value->to(CSSPrimitiveValue::UnitType::kPercentage);
    if (!unit_value) {
      exception_state.ThrowTypeError(
          "Delay must be a finite double or percentage for animation delay.");
      return result;
    }
    result.relative_delay = 0.01 * unit_value->value();
  }
  return result;
}

Timing ConvertEffectTiming(const EffectTiming* timing_input,
                           Document* document,
                           ExceptionState& exception_state) {
  Timing timing_output;
  TimingInput::Update(timing_output, timing_input, document, exception_state);
  if (!exception_state.HadException()) {
    timing_output.AssertValid();
  }
  return timing_output;
}

template <class V>
bool UpdateValueIfChanged(V& lhs, const V& rhs) {
  if (lhs != rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

}  // namespace

Timing TimingInput::Convert(
    const V8UnionKeyframeEffectOptionsOrUnrestrictedDouble* options,
    Document* document,
    ExceptionState& exception_state) {
  if (!options) {
    return Timing();
  }

  switch (options->GetContentType()) {
    case V8UnionKeyframeEffectOptionsOrUnrestrictedDouble::ContentType::
        kKeyframeEffectOptions:
      return ConvertEffectTiming(options->GetAsKeyframeEffectOptions(),
                                 document, exception_state);
    case V8UnionKeyframeEffectOptionsOrUnrestrictedDouble::ContentType::
        kUnrestrictedDouble: {
      // https://drafts.csswg.org/web-animations-1/#dom-keyframeeffect-keyframeeffect
      // If options is a double,
      //   Let timing input be a new EffectTiming object with all members set to
      //   their default values and duration set to options.
      EffectTiming* timing_input = EffectTiming::Create();
      timing_input->setDuration(
          MakeGarbageCollected<
              V8UnionCSSNumericValueOrStringOrUnrestrictedDouble>(
              options->GetAsUnrestrictedDouble()));
      return ConvertEffectTiming(timing_input, document, exception_state);
    }
  }
  NOTREACHED();
}

Timing TimingInput::Convert(
    const V8UnionKeyframeAnimationOptionsOrUnrestrictedDouble* options,
    Document* document,
    ExceptionState& exception_state) {
  if (!options) {
    return Timing();
  }

  switch (options->GetContentType()) {
    case V8UnionKeyframeAnimationOptionsOrUnrestrictedDouble::ContentType::
        kKeyframeAnimationOptions:
      return ConvertEffectTiming(options->GetAsKeyframeAnimationOptions(),
                                 document, exception_state);
    case V8UnionKeyframeAnimationOptionsOrUnrestrictedDouble::ContentType::
        kUnrestrictedDouble: {
      // https://drafts.csswg.org/web-animations-1/#dom-keyframeeffect-keyframeeffect
      // If options is a double,
      //   Let timing input be a new EffectTiming object with all members set to
      //   their default values and duration set to options.
      EffectTiming* timing_input = EffectTiming::Create();
      timing_input->setDuration(
          MakeGarbageCollected<
              V8UnionCSSNumericValueOrStringOrUnrestrictedDouble>(
              options->GetAsUnrestrictedDouble()));
      return ConvertEffectTiming(timing_input, document, exception_state);
    }
  }
  NOTREACHED();
}

template <class InputTiming>
bool TimingInput::Update(Timing& timing,
                         const InputTiming* input,
                         Document* document,
                         ExceptionState& exception_state) {
  // 1. If the iterationStart member of input is present and less than zero,
  // throw a TypeError and abort this procedure.
  if (input->hasIterationStart() && input->iterationStart() < 0) {
    exception_state.ThrowTypeError("iterationStart must be non-negative");
    return false;
  }

  // 2. If the iterations member of input is present, and less than zero or is
  // the value NaN, throw a TypeError and abort this procedure.
  if (input->hasIterations() &&
      (std::isnan(input->iterations()) || input->iterations() < 0)) {
    exception_state.ThrowTypeError("iterationCount must be non-negative");
    return false;
  }

  // 3. If the duration member of input is present, and less than zero or is the
  // value NaN, throw a TypeError and abort this procedure.
  //
  // We also throw if the value is a string but not 'auto', as per
  // https://github.com/w3c/csswg-drafts/issues/247 .
  if (input->hasDuration()) {
    const char* error_message = "duration must be non-negative or auto";
    switch (input->duration()->GetContentType()) {
      case V8UnionCSSNumericValueOrStringOrUnrestrictedDouble::ContentType::
          kCSSNumericValue:
        exception_state.ThrowTypeError(
            "Setting duration using CSSNumericValue is not supported.");
        return false;
      case V8UnionCSSNumericValueOrStringOrUnrestrictedDouble::ContentType::
          kString:
        if (input->duration()->GetAsString() != "auto") {
          exception_state.ThrowTypeError(error_message);
          return false;
        }
        break;
      case V8UnionCSSNumericValueOrStringOrUnrestrictedDouble::ContentType::
          kUnrestrictedDouble: {
        double duration = input->duration()->GetAsUnrestrictedDouble();
        if (std::isnan(duration) || duration < 0) {
          exception_state.ThrowTypeError(error_message);
          return false;
        }
        break;
      }
    }
  }

  // 4. If the easing member of input is present but cannot be parsed using the
  // <timing-function> production  [CSS-TIMING-1], throw a TypeError and abort
  // this procedure.
  scoped_refptr<TimingFunction> timing_function;
  if (input->hasEasing()) {
    timing_function = AnimationInputHelpers::ParseTimingFunction(
        input->easing(), document, exception_state);
    if (!timing_function) {
      DCHECK(exception_state.HadException());
      return false;
    }
  }

  // 5. Assign each member present in input to the corresponding timing property
  // of effect as follows:
  bool changed = false;
  if (input->hasDelay()) {
    changed |= UpdateValueIfChanged(
        timing.start_delay, ConvertDelay(input->delay(), 0, exception_state));
    timing.SetTimingOverride(Timing::kOverrideStartDelay);
  }
  if (input->hasEndDelay()) {
    changed |= UpdateValueIfChanged(
        timing.end_delay,
        ConvertDelay(input->endDelay(), 100, exception_state));
    timing.SetTimingOverride(Timing::kOverrideEndDelay);
  }
  if (input->hasFill()) {
    changed |= UpdateValueIfChanged(
        timing.fill_mode, Timing::EnumToFillMode(input->fill().AsEnum()));
    timing.SetTimingOverride(Timing::kOverideFillMode);
  }
  if (input->hasIterationStart()) {
    changed |=
        UpdateValueIfChanged(timing.iteration_start, input->iterationStart());
    timing.SetTimingOverride(Timing::kOverrideIterationStart);
  }
  if (input->hasIterations()) {
    changed |=
        UpdateValueIfChanged(timing.iteration_count, input->iterations());
    timing.SetTimingOverride(Timing::kOverrideIterationCount);
  }
  if (input->hasDuration()) {
    changed |= UpdateValueIfChanged(
        timing.iteration_duration, ConvertIterationDuration(input->duration()));
    timing.SetTimingOverride(Timing::kOverrideDuration);
  }
  if (input->hasDirection()) {
    changed |= UpdateValueIfChanged(
        timing.direction,
        ConvertPlaybackDirection(input->direction().AsEnum()));
    timing.SetTimingOverride(Timing::kOverrideDirection);
  }
  if (timing_function) {
    // We need to compare the timing functions by underlying value to see if
    // they have really changed, but update the scoped_refptr, so cant use
    // UpdateValueIfChanged.
    changed |= (*timing.timing_function != *timing_function);
    timing.timing_function = timing_function;
    timing.SetTimingOverride(Timing::kOverrideTimingFunction);
  }
  return changed;
}

// Export the OptionalEffectTiming version for AnimationEffect::updateTiming.
template CORE_EXPORT bool TimingInput::Update(Timing&,
                                              const OptionalEffectTiming*,
                                              Document*,
                                              ExceptionState&);

}  // namespace blink