Bug 1402279 - Part 1 - Move the DownloadPaths module to the jsdownloads folder. r=mak

MozReview-Commit-ID: 91ZqKef6NIE

--HG--
rename : toolkit/mozapps/downloads/DownloadPaths.jsm => toolkit/components/jsdownloads/src/DownloadPaths.jsm
rename : toolkit/mozapps/downloads/tests/unit/test_DownloadPaths.js => toolkit/components/jsdownloads/test/unit/test_DownloadPaths.js
extra : rebase_source : 7505715770e8fa383a42e0f8a18258c08e8e0f3a
This commit is contained in:
Paolo Amadini 2017-09-22 15:45:39 +01:00
Родитель ba9335f101
Коммит 8418fc1ba5
6 изменённых файлов: 34 добавлений и 55 удалений

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

@ -1,30 +1,18 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
/* This Source Code Form is subject to the terms of the Mozilla Public /* 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 * 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/. */
/**
* Provides methods for giving names and paths to files being downloaded.
*/
"use strict";
this.EXPORTED_SYMBOLS = [ this.EXPORTED_SYMBOLS = [
"DownloadPaths", "DownloadPaths",
]; ];
/** const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
* This module provides the DownloadPaths object which contains methods for
* giving names and paths to files being downloaded.
*
* List of methods:
*
* nsIFile
* createNiceUniqueFile(nsIFile aLocalFile)
*
* [string base, string ext]
* splitBaseNameAndExtension(string aLeafName)
*/
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cr = Components.results;
this.DownloadPaths = { this.DownloadPaths = {
/** /**
@ -36,17 +24,18 @@ this.DownloadPaths = {
* NS_ERROR_FILE_TOO_BIG. Other exceptions, like NS_ERROR_FILE_ACCESS_DENIED, * NS_ERROR_FILE_TOO_BIG. Other exceptions, like NS_ERROR_FILE_ACCESS_DENIED,
* can also be expected. * can also be expected.
* *
* @param aTemplateFile * @param templateFile
* nsIFile whose leaf name is going to be used as a template. The * nsIFile whose leaf name is going to be used as a template. The
* provided object is not modified. * provided object is not modified.
* @returns A new instance of an nsIFile object pointing to the newly *
* created empty file. On platforms that support permission bits, the * @return A new instance of an nsIFile object pointing to the newly created
* file is created with permissions 644. * empty file. On platforms that support permission bits, the file is
* created with permissions 644.
*/ */
createNiceUniqueFile: function DP_createNiceUniqueFile(aTemplateFile) { createNiceUniqueFile(templateFile) {
// Work on a clone of the provided template file object. // Work on a clone of the provided template file object.
var curFile = aTemplateFile.clone().QueryInterface(Ci.nsIFile); let curFile = templateFile.clone().QueryInterface(Ci.nsIFile);
var [base, ext] = DownloadPaths.splitBaseNameAndExtension(curFile.leafName); let [base, ext] = DownloadPaths.splitBaseNameAndExtension(curFile.leafName);
// Try other file names, for example "base(1).txt" or "base(1).tar.gz", // Try other file names, for example "base(1).txt" or "base(1).tar.gz",
// only if the file name initially set already exists. // only if the file name initially set already exists.
for (let i = 1; i < 10000 && curFile.exists(); i++) { for (let i = 1; i < 10000 && curFile.exists(); i++) {
@ -64,26 +53,27 @@ this.DownloadPaths = {
/** /**
* Separates the base name from the extension in a file name, recognizing some * Separates the base name from the extension in a file name, recognizing some
* double extensions like ".tar.gz". * double extensions like ".tar.gz".
* *
* @param aLeafName * @param leafName
* The full leaf name to be parsed. Be careful when processing names * The full leaf name to be parsed. Be careful when processing names
* containing leading or trailing dots or spaces. * containing leading or trailing dots or spaces.
* @returns [base, ext] *
* The base name of the file, which can be empty, and its extension, * @return [base, ext]
* which always includes the leading dot unless it's an empty string. * The base name of the file, which can be empty, and its extension,
* Concatenating the two items always results in the original name. * which always includes the leading dot unless it's an empty string.
* Concatenating the two items always results in the original name.
*/ */
splitBaseNameAndExtension: function DP_splitBaseNameAndExtension(aLeafName) { splitBaseNameAndExtension(leafName) {
// The following regular expression is built from these key parts: // The following regular expression is built from these key parts:
// .*? Matches the base name non-greedily. // .*? Matches the base name non-greedily.
// \.[A-Z0-9]{1,3} Up to three letters or numbers preceding a // \.[A-Z0-9]{1,3} Up to three letters or numbers preceding a
// double extension. // double extension.
// \.(?:gz|bz2|Z) The second part of common double extensions. // \.(?:gz|bz2|Z) The second part of common double extensions.
// \.[^.]* Matches any extension or a single trailing dot. // \.[^.]* Matches any extension or a single trailing dot.
var [, base, ext] = /(.*?)(\.[A-Z0-9]{1,3}\.(?:gz|bz2|Z)|\.[^.]*)?$/i let [, base, ext] = /(.*?)(\.[A-Z0-9]{1,3}\.(?:gz|bz2|Z)|\.[^.]*)?$/i
.exec(aLeafName); .exec(leafName);
// Return an empty string instead of undefined if no extension is found. // Return an empty string instead of undefined if no extension is found.
return [base, ext || ""]; return [base, ext || ""];
} },
}; };

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

@ -17,6 +17,7 @@ EXTRA_JS_MODULES += [
'DownloadCore.jsm', 'DownloadCore.jsm',
'DownloadIntegration.jsm', 'DownloadIntegration.jsm',
'DownloadList.jsm', 'DownloadList.jsm',
'DownloadPaths.jsm',
'Downloads.jsm', 'Downloads.jsm',
'DownloadStore.jsm', 'DownloadStore.jsm',
'DownloadUIHelper.jsm', 'DownloadUIHelper.jsm',

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

@ -1,23 +1,10 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ /* Any copyright is dedicated to the Public Domain.
/* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */ * http://creativecommons.org/publicdomain/zero/1.0/ */
/* ***** BEGIN LICENSE BLOCK *****
*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*
* ***** END LICENSE BLOCK ***** */
/** /**
* Tests for the "DownloadPaths.jsm" JavaScript module. * Tests for the "DownloadPaths.jsm" JavaScript module.
*/ */
var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;
var Cr = Components.results;
Cu.import("resource://gre/modules/DownloadPaths.jsm");
/** /**
* Provides a temporary save directory. * Provides a temporary save directory.
* *
@ -54,7 +41,7 @@ function testCreateNiceUniqueFile(aTempFile, aExpectedLeafName) {
do_check_eq(createdFile.leafName, aExpectedLeafName); do_check_eq(createdFile.leafName, aExpectedLeafName);
} }
function run_test() { add_task(async function test_splitBaseNameAndExtension() {
// Usual file names. // Usual file names.
testSplitBaseNameAndExtension("base", ["base", ""]); testSplitBaseNameAndExtension("base", ["base", ""]);
testSplitBaseNameAndExtension("base.ext", ["base", ".ext"]); testSplitBaseNameAndExtension("base.ext", ["base", ".ext"]);
@ -88,7 +75,9 @@ function run_test() {
testSplitBaseNameAndExtension(" .ext", [" ", ".ext"]); testSplitBaseNameAndExtension(" .ext", [" ", ".ext"]);
testSplitBaseNameAndExtension(" .ext. ", [" .ext", ". "]); testSplitBaseNameAndExtension(" .ext. ", [" .ext", ". "]);
testSplitBaseNameAndExtension(" .ext.gz ", [" .ext", ".gz "]); testSplitBaseNameAndExtension(" .ext.gz ", [" .ext", ".gz "]);
});
add_task(async function test_createNiceUniqueFile() {
var destDir = createTemporarySaveDirectory(); var destDir = createTemporarySaveDirectory();
try { try {
// Single extension. // Single extension.
@ -124,4 +113,4 @@ function run_test() {
// Clean up the temporary directory. // Clean up the temporary directory.
destDir.remove(true); destDir.remove(true);
} }
} });

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

@ -12,6 +12,7 @@ support-files =
[test_DownloadIntegration.js] [test_DownloadIntegration.js]
[test_DownloadLegacy.js] [test_DownloadLegacy.js]
[test_DownloadList.js] [test_DownloadList.js]
[test_DownloadPaths.js]
[test_Downloads.js] [test_Downloads.js]
[test_DownloadStore.js] [test_DownloadStore.js]
[test_PrivateTemp.js] [test_PrivateTemp.js]

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

@ -16,7 +16,6 @@ EXTRA_COMPONENTS += [
EXTRA_JS_MODULES += [ EXTRA_JS_MODULES += [
'DownloadLastDir.jsm', 'DownloadLastDir.jsm',
'DownloadPaths.jsm',
'DownloadUtils.jsm', 'DownloadUtils.jsm',
] ]

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

@ -1,7 +1,6 @@
[DEFAULT] [DEFAULT]
head = head_downloads.js head = head_downloads.js
[test_DownloadPaths.js]
[test_DownloadUtils.js] [test_DownloadUtils.js]
[test_lowMinutes.js] [test_lowMinutes.js]
[test_syncedDownloadUtils.js] [test_syncedDownloadUtils.js]