javascriptfirefox-addonprogramdata

Writing to a database in the ProgramData folder


I am making a firefox extension that allows me to write to a database that is then accessed by a c# program. I found this to be the easiest way for the two to communicate. Now I need the database to reside in the programdata folder so that everything works well when installed on a clients PC. I am not sure how to go about doing that. I know it's simple from c# but I don't know how from javascript.


Solution

  • You want this path from Firefox addon:

    var path_progData = Services.dirsvc.get('CmPrgs', Ci.nsIFile).path;
    

    This will give you:

    You might prefer CmAppData which gives you just C:\ProgramData on Win8.1

    Project to collect Firefox special paths on all platforms

    So a couple months ago I went through all the special path keywords in Firefox source, I tried to catch them all, I might have missed some, if I did please mention it so we can improve this. My goal was to write something so I can run on all platforms to and see what the special paths were.

    This is the code I came up with, can copy paste and run from scratchpad in browser environment: (btw the paths differ based on if your profile is relative to Profiles directory or if it is an absolute path profile) (also if you use the portable version of Firefox the paths differ) (not all paths just some, but in case of portable they are a bit crazy) (portable Firefox only available for Windows)

    var DSP = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
    var keywords = [];
    var str = [];
    
    
    function doit() {
        for (var i=0; i<keywords.length; i++) {
            str.push('Keyword: "' + keywords[i] + '"');
            try {
               var methodFU = FileUtils.getFile(keywords[i], ['']);
                str[str.length-1] += '|FU = "' + methodFU.path + '"';
            } catch(ex) {
                str[str.length-1] += '|FU = "' + ex.message + '"';
            }
            try {
               var methodDS = DSP.get(keywords[i], Ci.nsIFile);
                str[str.length-1] += '|DS = "' + methodDS.path + '"';
            } catch(ex) {
                str[str.length-1] += '|DS = "' + ex.message + '"';
            }
        }
    var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
    gClipboardHelper.copyString(str.join('\n'))
        console.log(str.join('\n'));
    }
    
    var keywords = [
        //http://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsAppDirectoryServiceDefs.h
        'AppRegF',
        'AppRegD',
        'DefRt',
        'PrfDef',
        'current',
        'default',
        'DefProfRt',
        'DefProfLRt',
        'ARes',
        'AChrom',
        'APlugns',
        'SrchPlugns',
        'AChromDL',
        'APluginsDL',
        'SrchPluginsDL',
        'SHARED',
        'PrefD',
        'PrefF',
        'MetroPrefF',
        'PrefDL',
        'ExtPrefDL',
        'PrefDOverride',
        'ProfD',
        'ProfLD',
        'UChrm',
        'UsrSrchPlugns',
        'LclSt',
        'UPnls',
        'UMimTyp',
        'cachePDir',
        'BMarks',
        'DLoads',
        'SrchF',
        'XPIClnupD',
        'indexedDBPDir',
        'permissionDBPDir',
        //http://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsDirectoryServiceDefs.h
        'Home',
        'TmpD',
        'CurWorkD',
        'Home',
        'Desk',
        'CurProcD',
        'XCurProcD',
        'XpcomLib',
        'GreD',
        'SysD',
        'Trsh',
        'Strt',
        'Shdwn',
        'ApplMenu',
        'CntlPnl',
        'Exts',
        'Fnts',
        'Prfs',
        'Docs',
        'ISrch',
        'DfltDwnld',
        'ULibDir',
        'UsrDsk',
        'LocDsk',
        'UsrApp',
        'LocApp',
        'UsrDocs',
        'LocDocs',
        'UsrIntrntPlgn',
        'LoclIntrntPlgn',
        'UsrFrmwrks',
        'LocFrmwrks',
        'UsrPrfs',
        'LocPrfs',
        'Pct',
        'Mov',
        'Music',
        'IntrntSts',
        'WinD',
        'ProgF',
        'DeskV',
        'Progs',
        'Cntls',
        'Prnts',
        'Pers',
        'Favs',
        'Strt',
        'Rcnt',
        'SndTo',
        'Buckt',
        'Strt',
        'DeskP',
        'Drivs',
        'NetW',
        'netH',
        'Fnts',
        'Tmpls',
        'CmStrt',
        'CmPrgs',
        'CmDeskP',
        'CmAppData',
        'AppData',
        'LocalAppData',
        'PrntHd',
        'CookD',
        'DfltDwnld',
        'Docs',
        'Pict',
        'Music',
        'Vids',
        'Locl',
        'LibD',
        'XDGDesk',
        'XDGDocs',
        'XDGDwnld',
        'XDGMusic',
        'XDGPict',
        'XDGPubSh',
        'XDGTempl',
        'XDGVids',
        'DfltDwnld',
        'DrvD',
        //http://mxr.mozilla.org/mozilla-release/source/toolkit/mozapps/update/nsUpdateService.js#76
        'GreD',
        'UpdRootD',
        'XREExeF',
        //http://mxr.mozilla.org/mozilla-release/source/toolkit/mozapps/update/tests/shared.js#55
        'ProfDS',
        'ProfD',
        'GreD',
        'XCurProcD',
        'XREExeF',
        'UpdRootD',
        //http://mxr.mozilla.org/mozilla-release/source/xpcom/build/nsXULAppAPI.h
        'UAppData',
        'XREExtDL',
        'XREExeF',
        'started',
        'ProfDS',
        'started',
        'ProfLDS',
        'XRESysLExtPD',
        'XRESysSExtPD',
        'XREUSysExt',
        'XREAppDist',
        'UpdRootD',
        'UpdArchD',
        'OSUpdApplyToD'
    ];
    
    doit();
    

    So the special path words are in the array keywords. It would be awesome to know all the paths for all platforms.

    This code above yields this on Win8.1, it copies to clipboard so can just run and paste here.

    Relative Profile (regular method)

    I only pasted Win8.1 for relative path profile above, for the rest I'm collecting them here in the comments: https://gist.github.com/Noitidart/715840fa5008ee032017 as Stack Overflow doesnt allow this much text.