Bug 1571342 - unify save paths for console and netmonitor and fix the latter to not do docshell loads, r=Honza,nchevobbe

Differential Revision: https://phabricator.services.mozilla.com/D41858

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Gijs Kruitbosch 2019-08-16 11:13:24 +00:00
Родитель a1cc09d360
Коммит a829e24657
9 изменённых файлов: 31 добавлений и 130 удалений

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

@ -5,7 +5,7 @@
"use strict";
const Services = require("Services");
const FileSaver = require("devtools/client/shared/file-saver");
const DevToolsUtils = require("devtools/shared/DevToolsUtils");
const JSZip = require("devtools/client/shared/vendor/jszip");
const clipboardHelper = require("devtools/shared/platform/clipboard");
const { HarBuilder } = require("./har-builder");
@ -86,16 +86,14 @@ const HarExporter = {
.generateAsync({
compression: "DEFLATE",
platform: Services.appinfo.OS === "WINNT" ? "DOS" : "UNIX",
type: "blob",
type: "uint8array",
});
} else {
data = new TextEncoder().encode(data);
}
fileName = `${fileName}${compress ? ".zip" : ""}`;
const blob = compress
? data
: new Blob([data], { type: "application/json" });
FileSaver.saveAs(blob, fileName, document);
DevToolsUtils.saveAs(window, data, fileName);
},
formatDate(date) {

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

@ -15,12 +15,7 @@ const {
} = require("../utils/request-utils");
loader.lazyRequireGetter(this, "Curl", "devtools/client/shared/curl", true);
loader.lazyRequireGetter(
this,
"saveAs",
"devtools/client/shared/file-saver",
true
);
loader.lazyRequireGetter(this, "saveAs", "devtools/shared/DevToolsUtils", true);
loader.lazyRequireGetter(
this,
"copyString",
@ -610,9 +605,9 @@ class RequestListContextMenu {
data[i] = decoded.charCodeAt(i);
}
} else {
data = text;
data = new TextEncoder().encode(text);
}
saveAs(new Blob([data]), fileName, document);
saveAs(window, data, fileName);
}
/**

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

@ -1,30 +0,0 @@
/* 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/. */
/* eslint-env browser */
"use strict";
/**
* HTML5 file saver to provide a standard download interface with a "Save As"
* dialog
*
* @param {object} blob - A blob object will be downloaded
* @param {string} filename - Given a file name which will display in "Save As" dialog
* @param {object} document - Optional. A HTML document for creating a temporary anchor
* for triggering a file download.
*/
function saveAs(blob, filename = "", doc = document) {
const url = URL.createObjectURL(blob);
const a = doc.createElement("a");
doc.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
a.remove();
}
exports.saveAs = saveAs;

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

@ -34,7 +34,6 @@ DevToolsModules(
'DOMHelpers.jsm',
'enum.js',
'events.js',
'file-saver.js',
'focus.js',
'getjson.js',
'inplace-editor.js',

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

@ -155,6 +155,7 @@ async function exportAllToFile(hud, message) {
]);
MockFilePicker.setFiles([nsiFile]);
exportFile.click();
info("Exporting to file");
// The file may not be ready yet.
await waitFor(() => OS.File.exists(nsiFile.path));

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

@ -14,18 +14,7 @@ const { MESSAGE_SOURCE } = require("devtools/client/webconsole/constants");
const clipboardHelper = require("devtools/shared/platform/clipboard");
const { l10n } = require("devtools/client/webconsole/utils/messages");
loader.lazyRequireGetter(
this,
"showSaveFileDialog",
"devtools/shared/DevToolsUtils",
true
);
loader.lazyRequireGetter(
this,
"saveFileStream",
"devtools/shared/DevToolsUtils",
true
);
loader.lazyRequireGetter(this, "saveAs", "devtools/shared/DevToolsUtils", true);
loader.lazyRequireGetter(
this,
"openContentLink",
@ -255,22 +244,16 @@ function createContextMenu(
id: "console-menu-export-file",
label: l10n.getStr("webconsole.menu.exportSubmenu.exportFile.label"),
disabled: false,
click: async () => {
// Note: not async, but returns a promise for the actual save.
click: () => {
const date = new Date();
const suggestedName =
`console-export-${date.getFullYear()}-` +
`${date.getMonth() + 1}-${date.getDate()}_${date.getHours()}-` +
`${date.getMinutes()}-${date.getSeconds()}.txt`;
const returnFile = await showSaveFileDialog(win, suggestedName);
const converter = Cc[
"@mozilla.org/intl/scriptableunicodeconverter"
].createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
const webconsoleOutput = parentNode.querySelector(".webconsole-output");
const istream = converter.convertToInputStream(
getElementText(webconsoleOutput)
);
return saveFileStream(returnFile, istream);
const data = new TextEncoder().encode(getElementText(webconsoleOutput));
return saveAs(window, data, suggestedName);
},
})
);

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

@ -781,21 +781,25 @@ exports.openFileStream = function(filePath) {
};
/**
* Open the file at the given path for writing.
* Save the given data to disk after asking the user where to do so.
*
* @param {String} filePath
* @param {Window} parentWindow
* The parent window to use to display the filepicker.
* @param {UInt8Array} dataArray
* The data to write to the file.
* @param {String} fileName
* The suggested filename.
*/
exports.saveFileStream = function(filePath, istream) {
return new Promise((resolve, reject) => {
const ostream = FileUtils.openSafeFileOutputStream(filePath);
NetUtil.asyncCopy(istream, ostream, status => {
if (!components.isSuccessCode(status)) {
reject(new Error(`Could not save "${filePath}"`));
return;
}
FileUtils.closeSafeFileOutputStream(ostream);
resolve();
});
exports.saveAs = async function(parentWindow, dataArray, fileName = "") {
let returnFile;
try {
returnFile = await exports.showSaveFileDialog(parentWindow, fileName);
} catch (ex) {
return;
}
await OS.File.writeAtomic(returnFile.path, dataArray, {
tmpPath: returnFile.path + ".tmp",
});
};

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

@ -1,48 +0,0 @@
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// Tests for DevToolsUtils.saveFileStream file:
const { FileUtils } = ChromeUtils.import(
"resource://gre/modules/FileUtils.jsm"
);
/**
* Tests that a file is properly saved using the saveFileStream function
*/
add_task(async function test_save_file() {
const testText = "This is a text";
const fileName = "test_console_save-file-" + Math.random();
const file = FileUtils.getFile("TmpD", [fileName]);
info("Test creating temporary file: " + file.path);
await DevToolsUtils.saveFileStream(file, convertToInputStream(testText));
Assert.ok(file.exists(), "Checking if test file exists");
const { content } = await DevToolsUtils.fetch(file.path);
deepEqual(content, testText, "The content was correct.");
cleanup(fileName);
});
/**
* Converts a string to an input stream.
* @param String content
* @return nsIInputStream
*/
function convertToInputStream(content) {
const converter = Cc[
"@mozilla.org/intl/scriptableunicodeconverter"
].createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
return converter.convertToInputStream(content);
}
/**
* Removes the temporary file after the test completes.
*/
function cleanup(fileName) {
const file = FileUtils.getFile("TmpD", [fileName]);
registerCleanupFunction(() => {
file.remove(false);
});
}

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

@ -33,7 +33,6 @@ run-if = nightly_build
[test_safeErrorString.js]
[test_defineLazyPrototypeGetter.js]
[test_console_filtering.js]
[test_console_save-file.js]
[test_pluralForm-english.js]
[test_pluralForm-makeGetter.js]
[test_prettifyCSS.js]