Use "= default" to denote default constructor or destructor
[WebKit-https.git] / Source / WebCore / page / CaptionUserPreferences.cpp
index 1e96a12..b1cc701 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (C) 2013, 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2013-2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -30,6 +30,8 @@
 
 #include "AudioTrackList.h"
 #include "DOMWrapperWorld.h"
 
 #include "AudioTrackList.h"
 #include "DOMWrapperWorld.h"
+#include "LocalizedStrings.h"
+#include "MediaSelectionOption.h"
 #include "Page.h"
 #include "PageGroup.h"
 #include "Settings.h"
 #include "Page.h"
 #include "PageGroup.h"
 #include "Settings.h"
 #include "UserContentTypes.h"
 #include "UserStyleSheet.h"
 #include "UserStyleSheetTypes.h"
 #include "UserContentTypes.h"
 #include "UserStyleSheet.h"
 #include "UserStyleSheetTypes.h"
+#include <heap/HeapInlines.h>
 #include <runtime/JSCellInlines.h>
 #include <runtime/StructureInlines.h>
 #include <runtime/JSCellInlines.h>
 #include <runtime/StructureInlines.h>
-#include <wtf/NeverDestroyed.h>
+#include <wtf/Language.h>
 
 namespace WebCore {
 
 
 namespace WebCore {
 
@@ -48,14 +51,10 @@ CaptionUserPreferences::CaptionUserPreferences(PageGroup& group)
     : m_pageGroup(group)
     , m_displayMode(ForcedOnly)
     , m_timer(*this, &CaptionUserPreferences::timerFired)
     : m_pageGroup(group)
     , m_displayMode(ForcedOnly)
     , m_timer(*this, &CaptionUserPreferences::timerFired)
-    , m_testingMode(false)
-    , m_havePreferences(false)
 {
 }
 
 {
 }
 
-CaptionUserPreferences::~CaptionUserPreferences()
-{
-}
+CaptionUserPreferences::~CaptionUserPreferences() = default;
 
 void CaptionUserPreferences::timerFired()
 {
 
 void CaptionUserPreferences::timerFired()
 {
@@ -80,7 +79,7 @@ void CaptionUserPreferences::notify()
 
     m_havePreferences = true;
     if (!m_timer.isActive())
 
     m_havePreferences = true;
     if (!m_timer.isActive())
-        m_timer.startOneShot(0);
+        m_timer.startOneShot(0_s);
 }
 
 CaptionUserPreferences::CaptionDisplayMode CaptionUserPreferences::captionDisplayMode() const
 }
 
 CaptionUserPreferences::CaptionDisplayMode CaptionUserPreferences::captionDisplayMode() const
@@ -98,9 +97,17 @@ void CaptionUserPreferences::setCaptionDisplayMode(CaptionUserPreferences::Capti
     notify();
 }
 
     notify();
 }
 
+Page* CaptionUserPreferences::currentPage() const
+{
+    if (m_pageGroup.pages().isEmpty())
+        return nullptr;
+
+    return *(m_pageGroup.pages().begin());
+}
+
 bool CaptionUserPreferences::userPrefersCaptions() const
 {
 bool CaptionUserPreferences::userPrefersCaptions() const
 {
-    Page* page = *(m_pageGroup.pages().begin());
+    Page* page = currentPage();
     if (!page)
         return false;
 
     if (!page)
         return false;
 
@@ -109,7 +116,7 @@ bool CaptionUserPreferences::userPrefersCaptions() const
 
 void CaptionUserPreferences::setUserPrefersCaptions(bool preference)
 {
 
 void CaptionUserPreferences::setUserPrefersCaptions(bool preference)
 {
-    Page* page = *(m_pageGroup.pages().begin());
+    Page* page = currentPage();
     if (!page)
         return;
 
     if (!page)
         return;
 
@@ -119,7 +126,7 @@ void CaptionUserPreferences::setUserPrefersCaptions(bool preference)
 
 bool CaptionUserPreferences::userPrefersSubtitles() const
 {
 
 bool CaptionUserPreferences::userPrefersSubtitles() const
 {
-    Page* page = *(pageGroup().pages().begin());
+    Page* page = currentPage();
     if (!page)
         return false;
 
     if (!page)
         return false;
 
@@ -128,7 +135,7 @@ bool CaptionUserPreferences::userPrefersSubtitles() const
 
 void CaptionUserPreferences::setUserPrefersSubtitles(bool preference)
 {
 
 void CaptionUserPreferences::setUserPrefersSubtitles(bool preference)
 {
-    Page* page = *(m_pageGroup.pages().begin());
+    Page* page = currentPage();
     if (!page)
         return;
 
     if (!page)
         return;
 
@@ -138,7 +145,7 @@ void CaptionUserPreferences::setUserPrefersSubtitles(bool preference)
 
 bool CaptionUserPreferences::userPrefersTextDescriptions() const
 {
 
 bool CaptionUserPreferences::userPrefersTextDescriptions() const
 {
-    Page* page = *(m_pageGroup.pages().begin());
+    Page* page = currentPage();
     if (!page)
         return false;
     
     if (!page)
         return false;
     
@@ -147,7 +154,7 @@ bool CaptionUserPreferences::userPrefersTextDescriptions() const
 
 void CaptionUserPreferences::setUserPrefersTextDescriptions(bool preference)
 {
 
 void CaptionUserPreferences::setUserPrefersTextDescriptions(bool preference)
 {
-    Page* page = *(m_pageGroup.pages().begin());
+    Page* page = currentPage();
     if (!page)
         return;
     
     if (!page)
         return;
     
@@ -196,17 +203,27 @@ static String trackDisplayName(TextTrack* track)
     if (track == TextTrack::captionMenuAutomaticItem())
         return textTrackAutomaticMenuItemText();
 
     if (track == TextTrack::captionMenuAutomaticItem())
         return textTrackAutomaticMenuItemText();
 
-    if (track->label().isEmpty() && track->language().isEmpty())
+    if (track->label().isEmpty() && track->validBCP47Language().isEmpty())
         return textTrackNoLabelText();
     if (!track->label().isEmpty())
         return track->label();
         return textTrackNoLabelText();
     if (!track->label().isEmpty())
         return track->label();
-    return track->language();
+    return track->validBCP47Language();
 }
 
 String CaptionUserPreferences::displayNameForTrack(TextTrack* track) const
 {
     return trackDisplayName(track);
 }
 }
 
 String CaptionUserPreferences::displayNameForTrack(TextTrack* track) const
 {
     return trackDisplayName(track);
 }
+
+MediaSelectionOption CaptionUserPreferences::mediaSelectionOptionForTrack(TextTrack* track) const
+{
+    auto type = MediaSelectionOption::Type::Regular;
+    if (track == TextTrack::captionMenuOffItem())
+        type = MediaSelectionOption::Type::LegibleOff;
+    else if (track == TextTrack::captionMenuAutomaticItem())
+        type = MediaSelectionOption::Type::LegibleAuto;
+    return { displayNameForTrack(track), type };
+}
     
 Vector<RefPtr<TextTrack>> CaptionUserPreferences::sortedTrackListForMenu(TextTrackList* trackList)
 {
     
 Vector<RefPtr<TextTrack>> CaptionUserPreferences::sortedTrackListForMenu(TextTrackList* trackList)
 {
@@ -221,7 +238,7 @@ Vector<RefPtr<TextTrack>> CaptionUserPreferences::sortedTrackListForMenu(TextTra
             tracksForMenu.append(track);
     }
 
             tracksForMenu.append(track);
     }
 
-    std::sort(tracksForMenu.begin(), tracksForMenu.end(), [](const RefPtr<TextTrack>& a, const RefPtr<TextTrack>& b) {
+    std::sort(tracksForMenu.begin(), tracksForMenu.end(), [](auto& a, auto& b) {
         return codePointCompare(trackDisplayName(a.get()), trackDisplayName(b.get())) < 0;
     });
 
         return codePointCompare(trackDisplayName(a.get()), trackDisplayName(b.get())) < 0;
     });
 
@@ -233,11 +250,11 @@ Vector<RefPtr<TextTrack>> CaptionUserPreferences::sortedTrackListForMenu(TextTra
 
 static String trackDisplayName(AudioTrack* track)
 {
 
 static String trackDisplayName(AudioTrack* track)
 {
-    if (track->label().isEmpty() && track->language().isEmpty())
+    if (track->label().isEmpty() && track->validBCP47Language().isEmpty())
         return audioTrackNoLabelText();
     if (!track->label().isEmpty())
         return track->label();
         return audioTrackNoLabelText();
     if (!track->label().isEmpty())
         return track->label();
-    return track->language();
+    return track->validBCP47Language();
 }
 
 String CaptionUserPreferences::displayNameForTrack(AudioTrack* track) const
 }
 
 String CaptionUserPreferences::displayNameForTrack(AudioTrack* track) const
@@ -245,6 +262,11 @@ String CaptionUserPreferences::displayNameForTrack(AudioTrack* track) const
     return trackDisplayName(track);
 }
 
     return trackDisplayName(track);
 }
 
+MediaSelectionOption CaptionUserPreferences::mediaSelectionOptionForTrack(AudioTrack* track) const
+{
+    return { displayNameForTrack(track), MediaSelectionOption::Type::Regular };
+}
+
 Vector<RefPtr<AudioTrack>> CaptionUserPreferences::sortedTrackListForMenu(AudioTrackList* trackList)
 {
     ASSERT(trackList);
 Vector<RefPtr<AudioTrack>> CaptionUserPreferences::sortedTrackListForMenu(AudioTrackList* trackList)
 {
     ASSERT(trackList);
@@ -256,7 +278,7 @@ Vector<RefPtr<AudioTrack>> CaptionUserPreferences::sortedTrackListForMenu(AudioT
         tracksForMenu.append(track);
     }
 
         tracksForMenu.append(track);
     }
 
-    std::sort(tracksForMenu.begin(), tracksForMenu.end(), [](const RefPtr<AudioTrack>& a, const RefPtr<AudioTrack>& b) {
+    std::sort(tracksForMenu.begin(), tracksForMenu.end(), [](auto& a, auto& b) {
         return codePointCompare(trackDisplayName(a.get()), trackDisplayName(b.get())) < 0;
     });
 
         return codePointCompare(trackDisplayName(a.get()), trackDisplayName(b.get())) < 0;
     });
 
@@ -276,11 +298,11 @@ int CaptionUserPreferences::textTrackSelectionScore(TextTrack* track, HTMLMediaE
 
 int CaptionUserPreferences::textTrackLanguageSelectionScore(TextTrack* track, const Vector<String>& preferredLanguages) const
 {
 
 int CaptionUserPreferences::textTrackLanguageSelectionScore(TextTrack* track, const Vector<String>& preferredLanguages) const
 {
-    if (track->language().isEmpty())
+    if (track->validBCP47Language().isEmpty())
         return 0;
 
     bool exactMatch;
         return 0;
 
     bool exactMatch;
-    size_t languageMatchIndex = indexOfBestMatchingLanguageInList(track->language(), preferredLanguages, exactMatch);
+    size_t languageMatchIndex = indexOfBestMatchingLanguageInList(track->validBCP47Language(), preferredLanguages, exactMatch);
     if (languageMatchIndex >= preferredLanguages.size())
         return 0;
 
     if (languageMatchIndex >= preferredLanguages.size())
         return 0;