Bug 1657701 - Localize the media control Firefox is now playing fallback text r=alwu,zbraniecki

Differential Revision: https://phabricator.services.mozilla.com/D91514
This commit is contained in:
Tom Schuster 2020-10-02 10:02:00 +00:00
Родитель 9dab095b52
Коммит 10fcf4e476
7 изменённых файлов: 56 добавлений и 7 удалений

Просмотреть файл

@ -223,6 +223,9 @@ var whitelist = [
// services/fxaccounts/RustFxAccount.js // services/fxaccounts/RustFxAccount.js
{ file: "resource://gre/modules/RustFxAccount.js" }, { file: "resource://gre/modules/RustFxAccount.js" },
// dom/media/mediacontrol/MediaControlService.cpp
{ file: "resource://gre/localization/en-US/dom/media.ftl" },
]; ];
if (AppConstants.NIGHTLY_BUILD && AppConstants.platform != "win") { if (AppConstants.NIGHTLY_BUILD && AppConstants.platform != "win") {

Просмотреть файл

@ -0,0 +1,5 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
mediastatus-fallback-title = { -brand-short-name } is playing media

Просмотреть файл

@ -3,6 +3,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this # License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
[localization] @AB_CD@.jar:
dom (%dom/**/*.ftl)
@AB_CD@.jar: @AB_CD@.jar:
locale/@AB_CD@/global/css.properties (%chrome/layout/css.properties) locale/@AB_CD@/global/css.properties (%chrome/layout/css.properties)

Просмотреть файл

@ -6,14 +6,16 @@
#include "MediaController.h" #include "MediaController.h"
#include "MediaControlUtils.h" #include "MediaControlUtils.h"
#include "mozilla/Assertions.h" #include "mozilla/Assertions.h"
#include "mozilla/intl/Localization.h"
#include "mozilla/Logging.h" #include "mozilla/Logging.h"
#include "mozilla/Services.h" #include "mozilla/Services.h"
#include "mozilla/StaticPtr.h" #include "mozilla/StaticPtr.h"
#include "nsIObserverService.h" #include "nsIObserverService.h"
#include "nsXULAppAPI.h" #include "nsXULAppAPI.h"
using mozilla::intl::Localization;
#undef LOG #undef LOG
#define LOG(msg, ...) \ #define LOG(msg, ...) \
MOZ_LOG(gMediaControlLog, LogLevel::Debug, \ MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
@ -111,6 +113,24 @@ void MediaControlService::Init() {
MOZ_ASSERT(mMediaControlKeyManager->IsOpened()); MOZ_ASSERT(mMediaControlKeyManager->IsOpened());
mMediaControlKeyManager->AddListener(mMediaKeysHandler.get()); mMediaControlKeyManager->AddListener(mMediaKeysHandler.get());
mControllerManager = MakeUnique<ControllerManager>(this); mControllerManager = MakeUnique<ControllerManager>(this);
// Initialize the fallback title
nsCOMPtr<nsIGlobalObject> global =
xpc::NativeGlobal(xpc::PrivilegedJunkScope());
RefPtr<Localization> l10n = Localization::Create(global, true, {});
l10n->AddResourceId(u"branding/brand.ftl"_ns);
l10n->AddResourceId(u"dom/media.ftl"_ns);
{
AutoSafeJSContext cx;
nsAutoCString translation;
ErrorResult rv;
l10n->FormatValueSync(cx, "mediastatus-fallback-title"_ns, {}, translation,
rv);
if (!rv.Failed()) {
mFallbackTitle = NS_ConvertUTF8toUTF16(translation);
}
}
} }
MediaControlService::~MediaControlService() { MediaControlService::~MediaControlService() {
@ -262,6 +282,10 @@ MediaSessionPlaybackState MediaControlService::GetMainControllerPlaybackState()
: MediaSessionPlaybackState::None; : MediaSessionPlaybackState::None;
} }
nsString MediaControlService::GetFallbackTitle() const {
return mFallbackTitle;
}
// Following functions belong to ControllerManager // Following functions belong to ControllerManager
MediaControlService::ControllerManager::ControllerManager( MediaControlService::ControllerManager::ControllerManager(
MediaControlService* aService) MediaControlService* aService)

Просмотреть файл

@ -82,6 +82,11 @@ class MediaControlService final : public nsIObserver {
MediaMetadataBase GetMainControllerMediaMetadata() const; MediaMetadataBase GetMainControllerMediaMetadata() const;
MediaSessionPlaybackState GetMainControllerPlaybackState() const; MediaSessionPlaybackState GetMainControllerPlaybackState() const;
// Media title that should be used as a fallback. This commonly used
// when playing media in private browsing mode and we are trying to avoid
// exposing potentially sensitive titles.
nsString GetFallbackTitle() const;
private: private:
MediaControlService(); MediaControlService();
~MediaControlService(); ~MediaControlService();
@ -166,6 +171,7 @@ class MediaControlService final : public nsIObserver {
RefPtr<MediaControlKeyListener> mMediaKeysHandler; RefPtr<MediaControlKeyListener> mMediaKeysHandler;
MediaEventProducer<uint64_t> mMediaControllerAmountChangedEvent; MediaEventProducer<uint64_t> mMediaControllerAmountChangedEvent;
UniquePtr<ControllerManager> mControllerManager; UniquePtr<ControllerManager> mControllerManager;
nsString mFallbackTitle;
}; };
} // namespace dom } // namespace dom

Просмотреть файл

@ -198,9 +198,8 @@ MediaMetadataBase MediaStatusManager::CreateDefaultMetadata() const {
} }
nsString MediaStatusManager::GetDefaultTitle() const { nsString MediaStatusManager::GetDefaultTitle() const {
// TODO : maybe need l10n? (bug1657701) RefPtr<MediaControlService> service = MediaControlService::GetService();
nsString defaultTitle; nsString defaultTitle = service->GetFallbackTitle();
defaultTitle.AssignLiteral("Firefox is playing media");
RefPtr<CanonicalBrowsingContext> bc = RefPtr<CanonicalBrowsingContext> bc =
CanonicalBrowsingContext::Get(mTopLevelBrowsingContextId); CanonicalBrowsingContext::Get(mTopLevelBrowsingContextId);

Просмотреть файл

@ -220,13 +220,23 @@ function isCurrentMetadataEqualTo(metadata) {
* the `options`. * the `options`.
*/ */
async function isGivenTabUsingDefaultMetadata(tab, options = {}) { async function isGivenTabUsingDefaultMetadata(tab, options = {}) {
const localization = new Localization([
"branding/brand.ftl",
"dom/media.ftl",
]);
const fallbackTitle = await localization.formatValue(
"mediastatus-fallback-title"
);
ok(fallbackTitle.length > 0, "l10n fallback title is not empty");
const metadata = tab.linkedBrowser.browsingContext.mediaController.getMetadata(); const metadata = tab.linkedBrowser.browsingContext.mediaController.getMetadata();
await SpecialPowers.spawn( await SpecialPowers.spawn(
tab.linkedBrowser, tab.linkedBrowser,
[metadata.title, options.isPrivateBrowsing], [metadata.title, fallbackTitle, options.isPrivateBrowsing],
(title, isPrivateBrowsing) => { (title, fallbackTitle, isPrivateBrowsing) => {
if (isPrivateBrowsing || !content.document.title.length) { if (isPrivateBrowsing || !content.document.title.length) {
is(title, "Firefox is playing media", "Using a generic default title"); is(title, fallbackTitle, "Using a generic default fallback title");
} else { } else {
is( is(
title, title,