diff options
Diffstat (limited to 'Source/WebCore/page/CaptionUserPreferences.cpp')
| -rw-r--r-- | Source/WebCore/page/CaptionUserPreferences.cpp | 110 |
1 files changed, 20 insertions, 90 deletions
diff --git a/Source/WebCore/page/CaptionUserPreferences.cpp b/Source/WebCore/page/CaptionUserPreferences.cpp index fd754e76d..adb570ea7 100644 --- a/Source/WebCore/page/CaptionUserPreferences.cpp +++ b/Source/WebCore/page/CaptionUserPreferences.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013, 2015 Apple Inc. All rights reserved. + * Copyright (C) 2013 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -24,30 +24,23 @@ */ #include "config.h" -#include "CaptionUserPreferences.h" #if ENABLE(VIDEO_TRACK) -#include "AudioTrackList.h" +#include "CaptionUserPreferences.h" #include "DOMWrapperWorld.h" #include "Page.h" #include "PageGroup.h" #include "Settings.h" #include "TextTrackList.h" -#include "UserContentController.h" -#include "UserContentTypes.h" -#include "UserStyleSheet.h" #include "UserStyleSheetTypes.h" -#include <runtime/JSCellInlines.h> -#include <runtime/StructureInlines.h> -#include <wtf/NeverDestroyed.h> namespace WebCore { CaptionUserPreferences::CaptionUserPreferences(PageGroup& group) : m_pageGroup(group) , m_displayMode(ForcedOnly) - , m_timer(*this, &CaptionUserPreferences::timerFired) + , m_timer(this, &CaptionUserPreferences::timerFired) , m_testingMode(false) , m_havePreferences(false) { @@ -57,27 +50,13 @@ CaptionUserPreferences::~CaptionUserPreferences() { } -void CaptionUserPreferences::timerFired() +void CaptionUserPreferences::timerFired(Timer<CaptionUserPreferences>&) { captionPreferencesChanged(); } -void CaptionUserPreferences::beginBlockingNotifications() -{ - ++m_blockNotificationsCounter; -} - -void CaptionUserPreferences::endBlockingNotifications() -{ - ASSERT(m_blockNotificationsCounter); - --m_blockNotificationsCounter; -} - void CaptionUserPreferences::notify() { - if (m_blockNotificationsCounter) - return; - m_havePreferences = true; if (!m_timer.isActive()) m_timer.startOneShot(0); @@ -175,20 +154,6 @@ void CaptionUserPreferences::setPreferredLanguage(const String& language) notify(); } -void CaptionUserPreferences::setPreferredAudioCharacteristic(const String& characteristic) -{ - m_userPreferredAudioCharacteristic = characteristic; - notify(); -} - -Vector<String> CaptionUserPreferences::preferredAudioCharacteristics() const -{ - Vector<String> characteristics; - if (!m_userPreferredAudioCharacteristic.isEmpty()) - characteristics.append(m_userPreferredAudioCharacteristic); - return characteristics; -} - static String trackDisplayName(TextTrack* track) { if (track == TextTrack::captionMenuOffItem()) @@ -231,47 +196,22 @@ Vector<RefPtr<TextTrack>> CaptionUserPreferences::sortedTrackListForMenu(TextTra return tracksForMenu; } -static String trackDisplayName(AudioTrack* track) -{ - if (track->label().isEmpty() && track->language().isEmpty()) - return audioTrackNoLabelText(); - if (!track->label().isEmpty()) - return track->label(); - return track->language(); -} - -String CaptionUserPreferences::displayNameForTrack(AudioTrack* track) const -{ - return trackDisplayName(track); -} - -Vector<RefPtr<AudioTrack>> CaptionUserPreferences::sortedTrackListForMenu(AudioTrackList* trackList) -{ - ASSERT(trackList); - - Vector<RefPtr<AudioTrack>> tracksForMenu; - - for (unsigned i = 0, length = trackList->length(); i < length; ++i) { - AudioTrack* track = trackList->item(i); - tracksForMenu.append(track); - } - - std::sort(tracksForMenu.begin(), tracksForMenu.end(), [](const RefPtr<AudioTrack>& a, const RefPtr<AudioTrack>& b) { - return codePointCompare(trackDisplayName(a.get()), trackDisplayName(b.get())) < 0; - }); - - return tracksForMenu; -} - int CaptionUserPreferences::textTrackSelectionScore(TextTrack* track, HTMLMediaElement*) const { + int trackScore = 0; + if (track->kind() != TextTrack::captionsKeyword() && track->kind() != TextTrack::subtitlesKeyword()) - return 0; + return trackScore; if (!userPrefersSubtitles() && !userPrefersCaptions()) - return 0; + return trackScore; + + if (track->kind() == TextTrack::subtitlesKeyword() && userPrefersSubtitles()) + trackScore = 1; + else if (track->kind() == TextTrack::captionsKeyword() && userPrefersCaptions()) + trackScore = 1; - return textTrackLanguageSelectionScore(track, preferredLanguages()) + 1; + return trackScore + textTrackLanguageSelectionScore(track, preferredLanguages()); } int CaptionUserPreferences::textTrackLanguageSelectionScore(TextTrack* track, const Vector<String>& preferredLanguages) const @@ -279,15 +219,13 @@ int CaptionUserPreferences::textTrackLanguageSelectionScore(TextTrack* track, co if (track->language().isEmpty()) return 0; - bool exactMatch; - size_t languageMatchIndex = indexOfBestMatchingLanguageInList(track->language(), preferredLanguages, exactMatch); + size_t languageMatchIndex = indexOfBestMatchingLanguageInList(track->language(), preferredLanguages); if (languageMatchIndex >= preferredLanguages.size()) return 0; // Matching a track language is more important than matching track type, so this multiplier must be // greater than the maximum value returned by textTrackSelectionScore. - int bonus = exactMatch ? 1 : 0; - return (preferredLanguages.size() + bonus - languageMatchIndex) * 10; + return (preferredLanguages.size() - languageMatchIndex) * 10; } void CaptionUserPreferences::setCaptionsStyleSheetOverride(const String& override) @@ -299,24 +237,16 @@ void CaptionUserPreferences::setCaptionsStyleSheetOverride(const String& overrid void CaptionUserPreferences::updateCaptionStyleSheetOveride() { // Identify our override style sheet with a unique URL - a new scheme and a UUID. - static NeverDestroyed<URL> captionsStyleSheetURL(ParsedURLString, "user-captions-override:01F6AF12-C3B0-4F70-AF5E-A3E00234DC23"); + DEFINE_STATIC_LOCAL(URL, captionsStyleSheetURL, (ParsedURLString, "user-captions-override:01F6AF12-C3B0-4F70-AF5E-A3E00234DC23")); - auto& pages = m_pageGroup.pages(); - for (auto& page : pages) { - if (auto* pageUserContentController = page->userContentController()) - pageUserContentController->removeUserStyleSheet(mainThreadNormalWorld(), captionsStyleSheetURL); - } + m_pageGroup.removeUserStyleSheetFromWorld(mainThreadNormalWorld(), captionsStyleSheetURL); String captionsOverrideStyleSheet = captionsStyleSheetOverride(); if (captionsOverrideStyleSheet.isEmpty()) return; - for (auto& page : pages) { - if (auto* pageUserContentController = page->userContentController()) { - auto userStyleSheet = std::make_unique<UserStyleSheet>(captionsOverrideStyleSheet, captionsStyleSheetURL, Vector<String>(), Vector<String>(), InjectInAllFrames, UserStyleAuthorLevel); - pageUserContentController->addUserStyleSheet(mainThreadNormalWorld(), WTFMove(userStyleSheet), InjectInExistingDocuments); - } - } + m_pageGroup.addUserStyleSheetToWorld(mainThreadNormalWorld(), captionsOverrideStyleSheet, captionsStyleSheetURL, Vector<String>(), + Vector<String>(), InjectInAllFrames, UserStyleAuthorLevel, InjectInExistingDocuments); } String CaptionUserPreferences::primaryAudioTrackLanguageOverride() const |
