Bug 1284548 - Put logic classifying <video> element based on content in a function and reuse it. r=?
MozReview-Commit-ID: 8ken4L8fEqq
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -579,16 +579,29 @@ var BrowserApp = {
},
initContextMenu: function () {
// We pass a thunk in place of a raw label string. This allows the
// context menu to automatically accommodate locale changes without
// having to be rebuilt.
let stringGetter = name => () => Strings.browser.GetStringFromName(name);
+ function videoClassifier(aTarget) {
+ if (aTarget.readyState == aTarget.HAVE_NOTHING) {
+ // We don't know if the height/width of the video,
+ // show a generic string.
+ return "Media";
+ } else if (aTarget.videoWidth == 0 || aTarget.videoHeight == 0) {
+ // If a video element is zero width or height, it's essentially
+ // an HTMLAudioElement.
+ return "Audio";
+ }
+ return "Video";
+ }
+
// TODO: These should eventually move into more appropriate classes
NativeWindow.contextmenus.add(stringGetter("contextmenu.openInNewTab"),
NativeWindow.contextmenus.linkOpenableNonPrivateContext,
function(aTarget) {
UITelemetry.addEvent("action.1", "contextmenu", null, "web_open_new_tab");
UITelemetry.addEvent("loadurl.1", "contextmenu", null);
let url = NativeWindow.contextmenus._getLinkURL(aTarget);
@@ -895,48 +908,36 @@ var BrowserApp = {
type: "Image:SetAs",
url: src
});
});
NativeWindow.contextmenus.add(
function(aTarget) {
if (aTarget instanceof HTMLVideoElement) {
- if (aTarget.readyState == aTarget.HAVE_NOTHING) {
- // We don't know if the height/width of the video,
- // show a generic string.
- return Strings.browser.GetStringFromName("contextmenu.saveMedia");
- } else if (aTarget.videoWidth == 0 || aTarget.videoHeight == 0) {
- // If a video element is zero width or height, its essentially
- // an HTMLAudioElement.
- return Strings.browser.GetStringFromName("contextmenu.saveAudio");
- }
- return Strings.browser.GetStringFromName("contextmenu.saveVideo");
+ return Strings.browser.GetStringFromName(
+ `contextmenu.save${videoClassifier(aTarget)}`
+ );
} else if (aTarget instanceof HTMLAudioElement) {
return Strings.browser.GetStringFromName("contextmenu.saveAudio");
}
return Strings.browser.GetStringFromName("contextmenu.saveVideo");
}, NativeWindow.contextmenus.mediaSaveableContext,
function(aTarget) {
UITelemetry.addEvent("action.1", "contextmenu", null, "web_save_media");
UITelemetry.addEvent("save.1", "contextmenu", null, "media");
WindowEventDispatcher.sendRequest({
type: "Mma:web_save_media",
});
let url = aTarget.currentSrc || aTarget.src;
let filePickerTitleKey;
if (aTarget instanceof HTMLVideoElement) {
- if (aTarget.readyState == aTarget.HAVE_NOTHING) {
- filePickerTitleKey = "SaveMediaTitle";
- } else if (aTarget.videoWidth == 0 || aTarget.videoHeight == 0) {
- filePickerTitleKey = "SaveAudioTitle";
- }
- filePickerTitleKey = "SaveVideoTitle";
+ filePickerTitleKey = `Save${videoClassifier(aTarget)}Title`;
} else {
filePickerTitleKey = "SaveAudioTitle";
}
// Skipped trying to pull MIME type out of cache for now
ContentAreaUtils.internalSave(url, null, null, null, null, false,
filePickerTitleKey, null, aTarget.ownerDocument.documentURIObject,
aTarget.ownerDocument, true, null);