content/media/TextTrackCue.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/TextTrackCue.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,374 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 et tw=78: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef mozilla_dom_TextTrackCue_h
    1.11 +#define mozilla_dom_TextTrackCue_h
    1.12 +
    1.13 +#include "mozilla/DOMEventTargetHelper.h"
    1.14 +#include "mozilla/dom/DocumentFragment.h"
    1.15 +#include "mozilla/dom/VTTCueBinding.h"
    1.16 +#include "nsCycleCollectionParticipant.h"
    1.17 +#include "nsIWebVTTParserWrapper.h"
    1.18 +#include "mozilla/StaticPtr.h"
    1.19 +#include "nsIDocument.h"
    1.20 +#include "mozilla/dom/HTMLDivElement.h"
    1.21 +#include "mozilla/dom/UnionTypes.h"
    1.22 +#include "mozilla/dom/TextTrack.h"
    1.23 +
    1.24 +namespace mozilla {
    1.25 +namespace dom {
    1.26 +
    1.27 +class HTMLTrackElement;
    1.28 +class TextTrackRegion;
    1.29 +
    1.30 +class TextTrackCue MOZ_FINAL : public DOMEventTargetHelper
    1.31 +{
    1.32 +public:
    1.33 +  NS_DECL_ISUPPORTS_INHERITED
    1.34 +  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(TextTrackCue, DOMEventTargetHelper)
    1.35 +
    1.36 +  // TextTrackCue WebIDL
    1.37 +  // See bug 868509 about splitting out the WebVTT-specific interfaces.
    1.38 +  static already_AddRefed<TextTrackCue>
    1.39 +  Constructor(GlobalObject& aGlobal,
    1.40 +              double aStartTime,
    1.41 +              double aEndTime,
    1.42 +              const nsAString& aText,
    1.43 +              ErrorResult& aRv)
    1.44 +  {
    1.45 +    nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
    1.46 +    nsRefPtr<TextTrackCue> ttcue = new TextTrackCue(window, aStartTime,
    1.47 +                                                    aEndTime, aText, aRv);
    1.48 +    return ttcue.forget();
    1.49 +  }
    1.50 +  TextTrackCue(nsPIDOMWindow* aGlobal, double aStartTime, double aEndTime,
    1.51 +               const nsAString& aText, ErrorResult& aRv);
    1.52 +
    1.53 +  TextTrackCue(nsPIDOMWindow* aGlobal, double aStartTime, double aEndTime,
    1.54 +               const nsAString& aText, HTMLTrackElement* aTrackElement,
    1.55 +               ErrorResult& aRv);
    1.56 +
    1.57 +  virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
    1.58 +
    1.59 +  TextTrack* GetTrack() const
    1.60 +  {
    1.61 +    return mTrack;
    1.62 +  }
    1.63 +
    1.64 +  void GetId(nsAString& aId) const
    1.65 +  {
    1.66 +    aId = mId;
    1.67 +  }
    1.68 +
    1.69 +  void SetId(const nsAString& aId)
    1.70 +  {
    1.71 +    if (mId == aId) {
    1.72 +      return;
    1.73 +    }
    1.74 +
    1.75 +    mId = aId;
    1.76 +  }
    1.77 +
    1.78 +  double StartTime() const
    1.79 +  {
    1.80 +    return mStartTime;
    1.81 +  }
    1.82 +
    1.83 +  void SetStartTime(double aStartTime)
    1.84 +  {
    1.85 +    if (mStartTime == aStartTime) {
    1.86 +      return;
    1.87 +    }
    1.88 +
    1.89 +    mStartTime = aStartTime;
    1.90 +    mReset = true;
    1.91 +  }
    1.92 +
    1.93 +  double EndTime() const
    1.94 +  {
    1.95 +    return mEndTime;
    1.96 +  }
    1.97 +
    1.98 +  void SetEndTime(double aEndTime)
    1.99 +  {
   1.100 +    if (mEndTime == aEndTime) {
   1.101 +      return;
   1.102 +    }
   1.103 +
   1.104 +    mEndTime = aEndTime;
   1.105 +    mReset = true;
   1.106 +  }
   1.107 +
   1.108 +  bool PauseOnExit()
   1.109 +  {
   1.110 +    return mPauseOnExit;
   1.111 +  }
   1.112 +
   1.113 +  void SetPauseOnExit(bool aPauseOnExit)
   1.114 +  {
   1.115 +    if (mPauseOnExit == aPauseOnExit) {
   1.116 +      return;
   1.117 +    }
   1.118 +
   1.119 +    mPauseOnExit = aPauseOnExit;
   1.120 +  }
   1.121 +
   1.122 +  TextTrackRegion* GetRegion();
   1.123 +  void SetRegion(TextTrackRegion* aRegion);
   1.124 +
   1.125 +  DirectionSetting Vertical() const
   1.126 +  {
   1.127 +    return mVertical;
   1.128 +  }
   1.129 +
   1.130 +  void SetVertical(const DirectionSetting& aVertical)
   1.131 +  {
   1.132 +    if (mVertical == aVertical) {
   1.133 +      return;
   1.134 +    }
   1.135 +
   1.136 +    mReset = true;
   1.137 +    mVertical = aVertical;
   1.138 +  }
   1.139 +
   1.140 +  bool SnapToLines()
   1.141 +  {
   1.142 +    return mSnapToLines;
   1.143 +  }
   1.144 +
   1.145 +  void SetSnapToLines(bool aSnapToLines)
   1.146 +  {
   1.147 +    if (mSnapToLines == aSnapToLines) {
   1.148 +      return;
   1.149 +    }
   1.150 +
   1.151 +    mReset = true;
   1.152 +    mSnapToLines = aSnapToLines;
   1.153 +  }
   1.154 +
   1.155 +  void GetLine(OwningLongOrAutoKeyword& aLine) const
   1.156 +  {
   1.157 +    if (mLineIsAutoKeyword) {
   1.158 +      aLine.SetAsAutoKeyword() = AutoKeyword::Auto;
   1.159 +      return;
   1.160 +    }
   1.161 +    aLine.SetAsLong() = mLineLong;
   1.162 +  }
   1.163 +
   1.164 +  void SetLine(const LongOrAutoKeyword& aLine)
   1.165 +  {
   1.166 +    if (aLine.IsLong() &&
   1.167 +        (mLineIsAutoKeyword || (aLine.GetAsLong() != mLineLong))) {
   1.168 +      mLineIsAutoKeyword = false;
   1.169 +      mLineLong = aLine.GetAsLong();
   1.170 +      mReset = true;
   1.171 +      return;
   1.172 +    }
   1.173 +    if (aLine.IsAutoKeyword() && !mLineIsAutoKeyword) {
   1.174 +      mLineIsAutoKeyword = true;
   1.175 +      mReset = true;
   1.176 +    }
   1.177 +  }
   1.178 +
   1.179 +  AlignSetting LineAlign() const
   1.180 +  {
   1.181 +    return mLineAlign;
   1.182 +  }
   1.183 +
   1.184 +  void SetLineAlign(AlignSetting& aLineAlign, ErrorResult& aRv)
   1.185 +  {
   1.186 +    if (mLineAlign == aLineAlign)
   1.187 +      return;
   1.188 +
   1.189 +    if (aLineAlign == AlignSetting::Left ||
   1.190 +        aLineAlign == AlignSetting::Right) {
   1.191 +      return aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
   1.192 +    }
   1.193 +
   1.194 +    mReset = true;
   1.195 +    mLineAlign = aLineAlign;
   1.196 +  }
   1.197 +
   1.198 +  int32_t Position() const
   1.199 +  {
   1.200 +    return mPosition;
   1.201 +  }
   1.202 +
   1.203 +  void SetPosition(int32_t aPosition, ErrorResult& aRv)
   1.204 +  {
   1.205 +    if (mPosition == aPosition) {
   1.206 +      return;
   1.207 +    }
   1.208 +
   1.209 +    if (aPosition > 100 || aPosition < 0){
   1.210 +      aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
   1.211 +      return;
   1.212 +    }
   1.213 +
   1.214 +    mReset = true;
   1.215 +    mPosition = aPosition;
   1.216 +  }
   1.217 +
   1.218 +  AlignSetting PositionAlign() const
   1.219 +  {
   1.220 +    return mPositionAlign;
   1.221 +  }
   1.222 +
   1.223 +  void SetPositionAlign(AlignSetting aPositionAlign, ErrorResult& aRv)
   1.224 +  {
   1.225 +    if (mPositionAlign == aPositionAlign)
   1.226 +      return;
   1.227 +
   1.228 +    if (aPositionAlign == AlignSetting::Left ||
   1.229 +        aPositionAlign == AlignSetting::Right) {
   1.230 +      return aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
   1.231 +    }
   1.232 +
   1.233 +    mReset = true;
   1.234 +    mPositionAlign = aPositionAlign;
   1.235 +  }
   1.236 +
   1.237 +  int32_t Size() const
   1.238 +  {
   1.239 +    return mSize;
   1.240 +  }
   1.241 +
   1.242 +  void SetSize(int32_t aSize, ErrorResult& aRv)
   1.243 +  {
   1.244 +    if (mSize == aSize) {
   1.245 +      return;
   1.246 +    }
   1.247 +
   1.248 +    if (aSize < 0 || aSize > 100) {
   1.249 +      aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
   1.250 +      return;
   1.251 +    }
   1.252 +
   1.253 +    mReset = true;
   1.254 +    mSize = aSize;
   1.255 +  }
   1.256 +
   1.257 +  AlignSetting Align() const
   1.258 +  {
   1.259 +    return mAlign;
   1.260 +  }
   1.261 +
   1.262 +  void SetAlign(AlignSetting& aAlign)
   1.263 +  {
   1.264 +    if (mAlign == aAlign) {
   1.265 +      return;
   1.266 +    }
   1.267 +
   1.268 +    mReset = true;
   1.269 +    mAlign = aAlign;
   1.270 +  }
   1.271 +
   1.272 +  void GetText(nsAString& aText) const
   1.273 +  {
   1.274 +    aText = mText;
   1.275 +  }
   1.276 +
   1.277 +  void SetText(const nsAString& aText)
   1.278 +  {
   1.279 +    if (mText == aText) {
   1.280 +      return;
   1.281 +    }
   1.282 +
   1.283 +    mReset = true;
   1.284 +    mText = aText;
   1.285 +  }
   1.286 +
   1.287 +  IMPL_EVENT_HANDLER(enter)
   1.288 +  IMPL_EVENT_HANDLER(exit)
   1.289 +
   1.290 +  HTMLDivElement* GetDisplayState()
   1.291 +  {
   1.292 +    return static_cast<HTMLDivElement*>(mDisplayState.get());
   1.293 +  }
   1.294 +
   1.295 +  void SetDisplayState(HTMLDivElement* aDisplayState)
   1.296 +  {
   1.297 +    mDisplayState = aDisplayState;
   1.298 +    mReset = false;
   1.299 +  }
   1.300 +
   1.301 +  void Reset()
   1.302 +  {
   1.303 +    mReset = true;
   1.304 +  }
   1.305 +
   1.306 +  bool HasBeenReset()
   1.307 +  {
   1.308 +    return mReset;
   1.309 +  }
   1.310 +
   1.311 +  // Helper functions for implementation.
   1.312 +  bool
   1.313 +  operator==(const TextTrackCue& rhs) const
   1.314 +  {
   1.315 +    return mId.Equals(rhs.mId);
   1.316 +  }
   1.317 +
   1.318 +  const nsAString& Id() const
   1.319 +  {
   1.320 +    return mId;
   1.321 +  }
   1.322 +
   1.323 +  void SetTrack(TextTrack* aTextTrack)
   1.324 +  {
   1.325 +    mTrack = aTextTrack;
   1.326 +  }
   1.327 +
   1.328 +  /**
   1.329 +   * Produces a tree of anonymous content based on the tree of the processed
   1.330 +   * cue text.
   1.331 +   *
   1.332 +   * Returns a DocumentFragment that is the head of the tree of anonymous
   1.333 +   * content.
   1.334 +   */
   1.335 +  already_AddRefed<DocumentFragment> GetCueAsHTML();
   1.336 +
   1.337 +  void SetTrackElement(HTMLTrackElement* aTrackElement);
   1.338 +
   1.339 +private:
   1.340 +  void SetDefaultCueSettings();
   1.341 +  nsresult StashDocument();
   1.342 +
   1.343 +  nsRefPtr<nsIDocument> mDocument;
   1.344 +  nsString mText;
   1.345 +  double mStartTime;
   1.346 +  double mEndTime;
   1.347 +
   1.348 +  nsRefPtr<TextTrack> mTrack;
   1.349 +  nsRefPtr<HTMLTrackElement> mTrackElement;
   1.350 +  nsString mId;
   1.351 +  int32_t mPosition;
   1.352 +  AlignSetting mPositionAlign;
   1.353 +  int32_t mSize;
   1.354 +  bool mPauseOnExit;
   1.355 +  bool mSnapToLines;
   1.356 +  nsRefPtr<TextTrackRegion> mRegion;
   1.357 +  DirectionSetting mVertical;
   1.358 +  bool mLineIsAutoKeyword;
   1.359 +  long mLineLong;
   1.360 +  AlignSetting mAlign;
   1.361 +  AlignSetting mLineAlign;
   1.362 +
   1.363 +  // Holds the computed DOM elements that represent the parsed cue text.
   1.364 +  // http://www.whatwg.org/specs/web-apps/current-work/#text-track-cue-display-state
   1.365 +  nsRefPtr<nsGenericHTMLElement> mDisplayState;
   1.366 +  // Tells whether or not we need to recompute mDisplayState. This is set
   1.367 +  // anytime a property that relates to the display of the TextTrackCue is
   1.368 +  // changed.
   1.369 +  bool mReset;
   1.370 +
   1.371 +  static StaticRefPtr<nsIWebVTTParserWrapper> sParserWrapper;
   1.372 +};
   1.373 +
   1.374 +} // namespace dom
   1.375 +} // namespace mozilla
   1.376 +
   1.377 +#endif // mozilla_dom_TextTrackCue_h

mercurial