content/media/TextTrackCue.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim:set ts=2 sw=2 et tw=78: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef mozilla_dom_TextTrackCue_h
     8 #define mozilla_dom_TextTrackCue_h
    10 #include "mozilla/DOMEventTargetHelper.h"
    11 #include "mozilla/dom/DocumentFragment.h"
    12 #include "mozilla/dom/VTTCueBinding.h"
    13 #include "nsCycleCollectionParticipant.h"
    14 #include "nsIWebVTTParserWrapper.h"
    15 #include "mozilla/StaticPtr.h"
    16 #include "nsIDocument.h"
    17 #include "mozilla/dom/HTMLDivElement.h"
    18 #include "mozilla/dom/UnionTypes.h"
    19 #include "mozilla/dom/TextTrack.h"
    21 namespace mozilla {
    22 namespace dom {
    24 class HTMLTrackElement;
    25 class TextTrackRegion;
    27 class TextTrackCue MOZ_FINAL : public DOMEventTargetHelper
    28 {
    29 public:
    30   NS_DECL_ISUPPORTS_INHERITED
    31   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(TextTrackCue, DOMEventTargetHelper)
    33   // TextTrackCue WebIDL
    34   // See bug 868509 about splitting out the WebVTT-specific interfaces.
    35   static already_AddRefed<TextTrackCue>
    36   Constructor(GlobalObject& aGlobal,
    37               double aStartTime,
    38               double aEndTime,
    39               const nsAString& aText,
    40               ErrorResult& aRv)
    41   {
    42     nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
    43     nsRefPtr<TextTrackCue> ttcue = new TextTrackCue(window, aStartTime,
    44                                                     aEndTime, aText, aRv);
    45     return ttcue.forget();
    46   }
    47   TextTrackCue(nsPIDOMWindow* aGlobal, double aStartTime, double aEndTime,
    48                const nsAString& aText, ErrorResult& aRv);
    50   TextTrackCue(nsPIDOMWindow* aGlobal, double aStartTime, double aEndTime,
    51                const nsAString& aText, HTMLTrackElement* aTrackElement,
    52                ErrorResult& aRv);
    54   virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
    56   TextTrack* GetTrack() const
    57   {
    58     return mTrack;
    59   }
    61   void GetId(nsAString& aId) const
    62   {
    63     aId = mId;
    64   }
    66   void SetId(const nsAString& aId)
    67   {
    68     if (mId == aId) {
    69       return;
    70     }
    72     mId = aId;
    73   }
    75   double StartTime() const
    76   {
    77     return mStartTime;
    78   }
    80   void SetStartTime(double aStartTime)
    81   {
    82     if (mStartTime == aStartTime) {
    83       return;
    84     }
    86     mStartTime = aStartTime;
    87     mReset = true;
    88   }
    90   double EndTime() const
    91   {
    92     return mEndTime;
    93   }
    95   void SetEndTime(double aEndTime)
    96   {
    97     if (mEndTime == aEndTime) {
    98       return;
    99     }
   101     mEndTime = aEndTime;
   102     mReset = true;
   103   }
   105   bool PauseOnExit()
   106   {
   107     return mPauseOnExit;
   108   }
   110   void SetPauseOnExit(bool aPauseOnExit)
   111   {
   112     if (mPauseOnExit == aPauseOnExit) {
   113       return;
   114     }
   116     mPauseOnExit = aPauseOnExit;
   117   }
   119   TextTrackRegion* GetRegion();
   120   void SetRegion(TextTrackRegion* aRegion);
   122   DirectionSetting Vertical() const
   123   {
   124     return mVertical;
   125   }
   127   void SetVertical(const DirectionSetting& aVertical)
   128   {
   129     if (mVertical == aVertical) {
   130       return;
   131     }
   133     mReset = true;
   134     mVertical = aVertical;
   135   }
   137   bool SnapToLines()
   138   {
   139     return mSnapToLines;
   140   }
   142   void SetSnapToLines(bool aSnapToLines)
   143   {
   144     if (mSnapToLines == aSnapToLines) {
   145       return;
   146     }
   148     mReset = true;
   149     mSnapToLines = aSnapToLines;
   150   }
   152   void GetLine(OwningLongOrAutoKeyword& aLine) const
   153   {
   154     if (mLineIsAutoKeyword) {
   155       aLine.SetAsAutoKeyword() = AutoKeyword::Auto;
   156       return;
   157     }
   158     aLine.SetAsLong() = mLineLong;
   159   }
   161   void SetLine(const LongOrAutoKeyword& aLine)
   162   {
   163     if (aLine.IsLong() &&
   164         (mLineIsAutoKeyword || (aLine.GetAsLong() != mLineLong))) {
   165       mLineIsAutoKeyword = false;
   166       mLineLong = aLine.GetAsLong();
   167       mReset = true;
   168       return;
   169     }
   170     if (aLine.IsAutoKeyword() && !mLineIsAutoKeyword) {
   171       mLineIsAutoKeyword = true;
   172       mReset = true;
   173     }
   174   }
   176   AlignSetting LineAlign() const
   177   {
   178     return mLineAlign;
   179   }
   181   void SetLineAlign(AlignSetting& aLineAlign, ErrorResult& aRv)
   182   {
   183     if (mLineAlign == aLineAlign)
   184       return;
   186     if (aLineAlign == AlignSetting::Left ||
   187         aLineAlign == AlignSetting::Right) {
   188       return aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
   189     }
   191     mReset = true;
   192     mLineAlign = aLineAlign;
   193   }
   195   int32_t Position() const
   196   {
   197     return mPosition;
   198   }
   200   void SetPosition(int32_t aPosition, ErrorResult& aRv)
   201   {
   202     if (mPosition == aPosition) {
   203       return;
   204     }
   206     if (aPosition > 100 || aPosition < 0){
   207       aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
   208       return;
   209     }
   211     mReset = true;
   212     mPosition = aPosition;
   213   }
   215   AlignSetting PositionAlign() const
   216   {
   217     return mPositionAlign;
   218   }
   220   void SetPositionAlign(AlignSetting aPositionAlign, ErrorResult& aRv)
   221   {
   222     if (mPositionAlign == aPositionAlign)
   223       return;
   225     if (aPositionAlign == AlignSetting::Left ||
   226         aPositionAlign == AlignSetting::Right) {
   227       return aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
   228     }
   230     mReset = true;
   231     mPositionAlign = aPositionAlign;
   232   }
   234   int32_t Size() const
   235   {
   236     return mSize;
   237   }
   239   void SetSize(int32_t aSize, ErrorResult& aRv)
   240   {
   241     if (mSize == aSize) {
   242       return;
   243     }
   245     if (aSize < 0 || aSize > 100) {
   246       aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
   247       return;
   248     }
   250     mReset = true;
   251     mSize = aSize;
   252   }
   254   AlignSetting Align() const
   255   {
   256     return mAlign;
   257   }
   259   void SetAlign(AlignSetting& aAlign)
   260   {
   261     if (mAlign == aAlign) {
   262       return;
   263     }
   265     mReset = true;
   266     mAlign = aAlign;
   267   }
   269   void GetText(nsAString& aText) const
   270   {
   271     aText = mText;
   272   }
   274   void SetText(const nsAString& aText)
   275   {
   276     if (mText == aText) {
   277       return;
   278     }
   280     mReset = true;
   281     mText = aText;
   282   }
   284   IMPL_EVENT_HANDLER(enter)
   285   IMPL_EVENT_HANDLER(exit)
   287   HTMLDivElement* GetDisplayState()
   288   {
   289     return static_cast<HTMLDivElement*>(mDisplayState.get());
   290   }
   292   void SetDisplayState(HTMLDivElement* aDisplayState)
   293   {
   294     mDisplayState = aDisplayState;
   295     mReset = false;
   296   }
   298   void Reset()
   299   {
   300     mReset = true;
   301   }
   303   bool HasBeenReset()
   304   {
   305     return mReset;
   306   }
   308   // Helper functions for implementation.
   309   bool
   310   operator==(const TextTrackCue& rhs) const
   311   {
   312     return mId.Equals(rhs.mId);
   313   }
   315   const nsAString& Id() const
   316   {
   317     return mId;
   318   }
   320   void SetTrack(TextTrack* aTextTrack)
   321   {
   322     mTrack = aTextTrack;
   323   }
   325   /**
   326    * Produces a tree of anonymous content based on the tree of the processed
   327    * cue text.
   328    *
   329    * Returns a DocumentFragment that is the head of the tree of anonymous
   330    * content.
   331    */
   332   already_AddRefed<DocumentFragment> GetCueAsHTML();
   334   void SetTrackElement(HTMLTrackElement* aTrackElement);
   336 private:
   337   void SetDefaultCueSettings();
   338   nsresult StashDocument();
   340   nsRefPtr<nsIDocument> mDocument;
   341   nsString mText;
   342   double mStartTime;
   343   double mEndTime;
   345   nsRefPtr<TextTrack> mTrack;
   346   nsRefPtr<HTMLTrackElement> mTrackElement;
   347   nsString mId;
   348   int32_t mPosition;
   349   AlignSetting mPositionAlign;
   350   int32_t mSize;
   351   bool mPauseOnExit;
   352   bool mSnapToLines;
   353   nsRefPtr<TextTrackRegion> mRegion;
   354   DirectionSetting mVertical;
   355   bool mLineIsAutoKeyword;
   356   long mLineLong;
   357   AlignSetting mAlign;
   358   AlignSetting mLineAlign;
   360   // Holds the computed DOM elements that represent the parsed cue text.
   361   // http://www.whatwg.org/specs/web-apps/current-work/#text-track-cue-display-state
   362   nsRefPtr<nsGenericHTMLElement> mDisplayState;
   363   // Tells whether or not we need to recompute mDisplayState. This is set
   364   // anytime a property that relates to the display of the TextTrackCue is
   365   // changed.
   366   bool mReset;
   368   static StaticRefPtr<nsIWebVTTParserWrapper> sParserWrapper;
   369 };
   371 } // namespace dom
   372 } // namespace mozilla
   374 #endif // mozilla_dom_TextTrackCue_h

mercurial