Bug 920686 - [OS.File] Add some constants to OS.Constants.Path for WebappsInstaller.jsm. r=yoric

This commit is contained in:
Marco Castelluccio 2013-09-30 10:59:41 -04:00
parent e3ecce0148
commit 9245146ce3
4 changed files with 130 additions and 10 deletions

View File

@ -75,6 +75,38 @@ struct Paths {
nsString tmpDir;
nsString profileDir;
nsString localProfileDir;
/**
* The user's home directory
*/
nsString homeDir;
/**
* The user's desktop directory, if there is one. Otherwise this is
* the same as homeDir.
*/
nsString desktopDir;
#if defined(XP_WIN)
/**
* The user's application data directory.
*/
nsString winAppDataDir;
/**
* The programs subdirectory in the user's start menu directory.
*/
nsString winStartMenuProgsDir;
#endif // defined(XP_WIN)
#if defined(XP_MACOSX)
/**
* The user's Library directory.
*/
nsString macUserLibDir;
/**
* The Application directory, that stores applications installed in the
* system.
*/
nsString macLocalApplicationsDir;
#endif // defined(XP_MACOSX)
Paths()
{
@ -82,6 +114,18 @@ struct Paths {
tmpDir.SetIsVoid(true);
profileDir.SetIsVoid(true);
localProfileDir.SetIsVoid(true);
homeDir.SetIsVoid(true);
desktopDir.SetIsVoid(true);
#if defined(XP_WIN)
winAppDataDir.SetIsVoid(true);
winStartMenuProgsDir.SetIsVoid(true);
#endif // defined(XP_WIN)
#if defined(XP_MACOSX)
macUserLibDir.SetIsVoid(true);
macLocalApplicationsDir.SetIsVoid(true);
#endif // defined(XP_MACOSX)
}
};
@ -209,6 +253,18 @@ nsresult InitOSFileConstants()
// some platforms or in non-Firefox embeddings of Gecko).
GetPathToSpecialDir(NS_OS_TEMP_DIR, paths->tmpDir);
GetPathToSpecialDir(NS_OS_HOME_DIR, paths->homeDir);
GetPathToSpecialDir(NS_OS_DESKTOP_DIR, paths->desktopDir);
#if defined(XP_WIN)
GetPathToSpecialDir(NS_WIN_APPDATA_DIR, paths->winAppDataDir);
GetPathToSpecialDir(NS_WIN_PROGRAMS_DIR, paths->winStartMenuProgsDir);
#endif // defined(XP_WIN)
#if defined(XP_MACOSX)
GetPathToSpecialDir(NS_MAC_USER_LIB_DIR, paths->macUserLibDir);
GetPathToSpecialDir(NS_OSX_LOCAL_APPLICATIONS_DIR, paths->macLocalApplicationsDir);
#endif // defined(XP_MACOSX)
gPaths = paths.forget();
return NS_OK;
@ -769,6 +825,34 @@ bool DefineOSFileConstants(JSContext *cx, JS::Handle<JSObject*> global)
return false;
}
if (!SetStringProperty(cx, objPath, "homeDir", gPaths->homeDir)) {
return false;
}
if (!SetStringProperty(cx, objPath, "desktopDir", gPaths->desktopDir)) {
return false;
}
#if defined(XP_WIN)
if (!SetStringProperty(cx, objPath, "winAppDataDir", gPaths->winAppDataDir)) {
return false;
}
if (!SetStringProperty(cx, objPath, "winStartMenuProgsDir", gPaths->winStartMenuProgsDir)) {
return false;
}
#endif // defined(XP_WIN)
#if defined(XP_MACOSX)
if (!SetStringProperty(cx, objPath, "macUserLibDir", gPaths->macUserLibDir)) {
return false;
}
if (!SetStringProperty(cx, objPath, "macLocalApplicationsDir", gPaths->macLocalApplicationsDir)) {
return false;
}
#endif // defined(XP_MACOSX)
return true;
}

View File

@ -22,16 +22,6 @@ let main = this;
function test() {
info("test_osfile_front.xul: Starting test");
// Test OS.Constants.Path
Components.classes["@mozilla.org/net/osfileconstantsservice;1"].
getService(Components.interfaces.nsIOSFileConstantsService).
init();
Components.utils.import("resource://gre/modules/Services.jsm");
is(OS.Constants.Path.tmpDir, Services.dirsvc.get("TmpD", Components.interfaces.nsIFile).path, "OS.Constants.Path.tmpDir is correct");
is(OS.Constants.Path.profileDir, Services.dirsvc.get("ProfD", Components.interfaces.nsIFile).path, "OS.Constants.Path.profileDir is correct");
// Test the OS.File worker
worker = new ChromeWorker("worker_test_osfile_front.js");

View File

@ -0,0 +1,45 @@
/* 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/. */
"use strict";
Components.utils.import("resource://gre/modules/osfile.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
function run_test() {
run_next_test();
}
function compare_paths(ospath, key) {
let file;
try {
file = Services.dirsvc.get(key, Components.interfaces.nsIFile);
} catch(ex) {}
if (file) {
do_check_true(!!ospath);
do_check_eq(ospath, file.path);
} else {
do_check_false(!!ospath);
}
}
add_test(function() {
do_check_true(!!OS.Constants.Path.tmpDir);
do_check_eq(OS.Constants.Path.tmpDir, Services.dirsvc.get("TmpD", Components.interfaces.nsIFile).path);
do_check_true(!!OS.Constants.Path.homeDir);
do_check_eq(OS.Constants.Path.homeDir, Services.dirsvc.get("Home", Components.interfaces.nsIFile).path);
do_check_true(!!OS.Constants.Path.desktopDir);
do_check_eq(OS.Constants.Path.desktopDir, Services.dirsvc.get("Desk", Components.interfaces.nsIFile).path);
compare_paths(OS.Constants.Path.winAppDataDir, "AppData");
compare_paths(OS.Constants.Path.winStartMenuProgsDir, "Progs");
compare_paths(OS.Constants.Path.macUserLibDir, "ULibDir");
compare_paths(OS.Constants.Path.macLocalApplicationsDir, "LocApp");
run_next_test();
});

View File

@ -8,3 +8,4 @@ tail =
[test_profiledir.js]
[test_logging.js]
[test_creationDate.js]
[test_path_constants.js]