Adding files to an extension's uninstallation: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
m (added io.js reference)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Uninstall logs are no longer used or necessary since Firefox 1.5. See [https://bugzilla.mozilla.org/show_bug.cgi?id=286100 bug 286100] for more info.'''
== The uninstall log ==
== The uninstall log ==
When an extension is installed, the Extension Manager creates a log of the files which comprise that extension. This log is used by the Extension Manager to cleanly uninstall an extension when requested by the user. Here's a real log created when the [http://passwordmaker.mozdev.org PasswordMaker] extension is installed on a Windows OS:
When an extension is installed, the Extension Manager creates a log of the files which comprise that extension. This log is used by the Extension Manager to cleanly uninstall an extension when requested by the user. Here's a real log created when the [http://passwordmaker.mozdev.org PasswordMaker] extension is installed on a Windows OS:


<pre>
<pre>
add C:\Documents and Settings\Eric Jung\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
add C:\Documents and Settings\Fidel Castro\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
         {5872365e-67d1-4afd-9480-fd293bebd20d}\install.rdf
         {5872365e-67d1-4afd-9480-fd293bebd20d}\install.rdf
add C:\Documents and Settings\Eric Jung\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
add C:\Documents and Settings\Fidel Castro\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
         {5872365e-67d1-4afd-9480-fd293bebd20d}\defaults\passwordmakerdialog.ico
         {5872365e-67d1-4afd-9480-fd293bebd20d}\defaults\passwordmakerdialog.ico
add C:\Documents and Settings\Eric Jung\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
add C:\Documents and Settings\Fidel Castro\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
         {5872365e-67d1-4afd-9480-fd293bebd20d}\defaults\passwordmakerdialog.xpm
         {5872365e-67d1-4afd-9480-fd293bebd20d}\defaults\passwordmakerdialog.xpm
add C:\Documents and Settings\Eric Jung\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
add C:\Documents and Settings\Fidel Castro\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
         {5872365e-67d1-4afd-9480-fd293bebd20d}\install.js
         {5872365e-67d1-4afd-9480-fd293bebd20d}\install.js
add C:\Documents and Settings\Eric Jung\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
add C:\Documents and Settings\Fidel Castro\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
         {5872365e-67d1-4afd-9480-fd293bebd20d}\chrome\passwdmaker.jar
         {5872365e-67d1-4afd-9480-fd293bebd20d}\chrome\passwdmaker.jar
register profile package passwdmaker
register profile package passwdmaker
Line 36: Line 38:
  * Adds an "add" entry to this extension's Uninstall log.
  * Adds an "add" entry to this extension's Uninstall log.
  * The file argument can be created by DirIO.get() or FileIO.get()
  * The file argument can be created by DirIO.get() or FileIO.get()
  * in io.js. See usage example below.
  * in io.js. The guid argument is the GUID for your extension,
* including braces and dashes; e.g, "{5872365E-67D1-4AFD-9480-FD293BEBD20D}"
See usage example below.
  */
  */
function addToUninstallList(file) {
function addToUninstallList(file, guid) {
   // make sure the new file is in the extension uninstall list
   // make sure the new file is in the extension uninstall list
   var uninstallFile = DirIO.get("ProfD"); // %Profile% dir
   var uninstallFile = DirIO.get("ProfD"); // %Profile% dir
   uninstallFile.append("extensions");
   uninstallFile.append("extensions");
   uninstallFile.append("{5872365E-67D1-4AFD-9480-FD293BEBD20D}");
   uninstallFile.append(guid);
   uninstallFile.append("uninstall");
   uninstallFile.append("uninstall");
   uninstallFile.append("Uninstall");
   uninstallFile.append("Uninstall");
Line 72: Line 76:
   if (FileIO.write(myfile, "<hello-world/>", "w")) {
   if (FileIO.write(myfile, "<hello-world/>", "w")) {
     // Make sure the new file is in the extension uninstall list
     // Make sure the new file is in the extension uninstall list
     addToUninstallList(myfile);
     addToUninstallList(myfile, "{5872365E-67D1-4AFD-9480-FD293BEBD20D}");
   }
   }
</pre>
</pre>


==References==
==References==
Line 82: Line 85:
*[http://www.mozilla.org/projects/firefox/extensions/em-changes.html Enhanced Extension Installation] and why Uninstall logs may eventually disappear
*[http://www.mozilla.org/projects/firefox/extensions/em-changes.html Enhanced Extension Installation] and why Uninstall logs may eventually disappear


[[Category:Example code]]
[[Category:Obsolete]]

Latest revision as of 03:31, 25 September 2007

Uninstall logs are no longer used or necessary since Firefox 1.5. See bug 286100 for more info.

The uninstall log

When an extension is installed, the Extension Manager creates a log of the files which comprise that extension. This log is used by the Extension Manager to cleanly uninstall an extension when requested by the user. Here's a real log created when the PasswordMaker extension is installed on a Windows OS:

add	C:\Documents and Settings\Fidel Castro\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
        {5872365e-67d1-4afd-9480-fd293bebd20d}\install.rdf
add	C:\Documents and Settings\Fidel Castro\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
        {5872365e-67d1-4afd-9480-fd293bebd20d}\defaults\passwordmakerdialog.ico
add	C:\Documents and Settings\Fidel Castro\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
        {5872365e-67d1-4afd-9480-fd293bebd20d}\defaults\passwordmakerdialog.xpm
add	C:\Documents and Settings\Fidel Castro\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
        {5872365e-67d1-4afd-9480-fd293bebd20d}\install.js
add	C:\Documents and Settings\Fidel Castro\Application Data\Mozilla\Firefox\Profiles\ifk9k760.dev9\extensions\
        {5872365e-67d1-4afd-9480-fd293bebd20d}\chrome\passwdmaker.jar
register	profile	package	passwdmaker
register	profile	skin	classic/1.0

An extension's log is written to the file %Profile%/extensions/{GUID}/uninstall/Uninstall. Note the lack of an extension on the filename is not a typographical error.

"Add" entries

Each add entry in the log contains the full name and path of a file which was written to disk when this extension was installed. In the example above, the add entries have line breaks for clarity, but they are written without these line break in the real log file. The general format of an add entry is:

add\tpath\filename\n

where \t is a tab character and \n is a newline character.

When an extension is uninstalled, the Extension Manager reads the extension's log and subsequently deletes each file that has an add entry.

Adding entries to the uninstall log

If an extension creates file(s) during its lifetime, and the extension developer would like those file(s) automatically deleted when the extension is uninstalled, add entries must be written to the log for each file to be deleted. There is a feature request in BugZilla to provide a programmatic interface on the Extension Manager with which to manipulate the Uninstall log, but until that request is fulfilled, the following code can be used. It makes use of io.js variables and functions:

/**
 * Adds an "add" entry to this extension's Uninstall log.
 * The file argument can be created by DirIO.get() or FileIO.get()
 * in io.js. The guid argument is the GUID for your extension,
 * including braces and dashes; e.g, "{5872365E-67D1-4AFD-9480-FD293BEBD20D}"
 *  See usage example below.
 */
function addToUninstallList(file, guid) {
  // make sure the new file is in the extension uninstall list
  var uninstallFile = DirIO.get("ProfD"); // %Profile% dir
  uninstallFile.append("extensions");
  uninstallFile.append(guid);
  uninstallFile.append("uninstall");
  uninstallFile.append("Uninstall");
  var data = FileIO.read(uninstallFile);
  var r = new RegEx("^add\s.*" + file.path", "m");
  if (!data || r.test(data)) {
    // Uninstall file doesn't exist or filename is
    // already listed in it.
    return;
  }
  // Add new file to the Uninstall list.
  // Note we're not using the FileIO.path() function
  // because it returns an URL-encoded path (e.g., a space equals %20)
  var newLine = "add\t" + file.path + "\n";
  FileIO.write(uninstallFile, newLine, "a");
}

Sample use of the function:

// Write a file. If successful, update the Uninstall log
// so the file is automatically deleted when our extension
// is uninstalled by the user.  
var myfile = DirIO.get("ProfD"); // %Profile% dir
  myfile.append("extensions");
  myfile.append("{5872365E-67D1-4AFD-9480-FD293BEBD20D}");
  myfile.append("myfile.xml"); 
  if (FileIO.write(myfile, "<hello-world/>", "w")) {
    // Make sure the new file is in the extension uninstall list
    addToUninstallList(myfile, "{5872365E-67D1-4AFD-9480-FD293BEBD20D}");
  }

References