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
{ 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") {

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

@ -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
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
[localization] @AB_CD@.jar:
dom (%dom/**/*.ftl)
@AB_CD@.jar:
locale/@AB_CD@/global/css.properties (%chrome/layout/css.properties)

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

@ -6,14 +6,16 @@
#include "MediaController.h"
#include "MediaControlUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/intl/Localization.h"
#include "mozilla/Logging.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPtr.h"
#include "nsIObserverService.h"
#include "nsXULAppAPI.h"
using mozilla::intl::Localization;
#undef LOG
#define LOG(msg, ...) \
MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
@ -111,6 +113,24 @@ void MediaControlService::Init() {
MOZ_ASSERT(mMediaControlKeyManager->IsOpened());
mMediaControlKeyManager->AddListener(mMediaKeysHandler.get());
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() {
@ -262,6 +282,10 @@ MediaSessionPlaybackState MediaControlService::GetMainControllerPlaybackState()
: MediaSessionPlaybackState::None;
}
nsString MediaControlService::GetFallbackTitle() const {
return mFallbackTitle;
}
// Following functions belong to ControllerManager
MediaControlService::ControllerManager::ControllerManager(
MediaControlService* aService)

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

@ -82,6 +82,11 @@ class MediaControlService final : public nsIObserver {
MediaMetadataBase GetMainControllerMediaMetadata() 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:
MediaControlService();
~MediaControlService();
@ -166,6 +171,7 @@ class MediaControlService final : public nsIObserver {
RefPtr<MediaControlKeyListener> mMediaKeysHandler;
MediaEventProducer<uint64_t> mMediaControllerAmountChangedEvent;
UniquePtr<ControllerManager> mControllerManager;
nsString mFallbackTitle;
};
} // namespace dom

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

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

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

@ -220,13 +220,23 @@ function isCurrentMetadataEqualTo(metadata) {
* the `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();
await SpecialPowers.spawn(
tab.linkedBrowser,
[metadata.title, options.isPrivateBrowsing],
(title, isPrivateBrowsing) => {
[metadata.title, fallbackTitle, options.isPrivateBrowsing],
(title, fallbackTitle, isPrivateBrowsing) => {
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 {
is(
title,