CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / toolkit / mozapps / extensions / LightweightThemeManager.jsm
blob39aafe986f9adad1168311f7a63653310ed9e05d
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is the Extension Manager.
15  *
16  * The Initial Developer of the Original Code is
17  * the Mozilla Foundation.
18  * Portions created by the Initial Developer are Copyright (C) 2009
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Dão Gottwald <dao@mozilla.com>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
38 var EXPORTED_SYMBOLS = ["LightweightThemeManager"];
40 const Cc = Components.classes;
41 const Ci = Components.interfaces;
43 Components.utils.import("resource://gre/modules/AddonManager.jsm");
44 Components.utils.import("resource://gre/modules/Services.jsm");
46 const ID_SUFFIX              = "@personas.mozilla.org";
47 const PREF_LWTHEME_TO_SELECT = "extensions.lwThemeToSelect";
48 const PREF_GENERAL_SKINS_SELECTEDSKIN = "general.skins.selectedSkin";
49 const PREF_EM_DSS_ENABLED    = "extensions.dss.enabled";
50 const ADDON_TYPE             = "theme";
52 const DEFAULT_MAX_USED_THEMES_COUNT = 30;
54 const MAX_PREVIEW_SECONDS = 30;
56 const MANDATORY = ["id", "name", "headerURL"];
57 const OPTIONAL = ["footerURL", "textcolor", "accentcolor", "iconURL",
58                   "previewURL", "author", "description", "homepageURL",
59                   "updateURL", "version"];
61 const PERSIST_ENABLED = true;
62 const PERSIST_BYPASS_CACHE = false;
63 const PERSIST_FILES = {
64   headerURL: "lightweighttheme-header",
65   footerURL: "lightweighttheme-footer"
68 __defineGetter__("_prefs", function () {
69   delete this._prefs;
70   return this._prefs = Services.prefs.getBranch("lightweightThemes.")
71                                      .QueryInterface(Ci.nsIPrefBranch2);
72 });
74 __defineGetter__("_maxUsedThemes", function() {
75   delete this._maxUsedThemes;
76   try {
77     this._maxUsedThemes = _prefs.getIntPref("maxUsedThemes");
78   }
79   catch (e) {
80     this._maxUsedThemes = DEFAULT_MAX_USED_THEMES_COUNT;
81   }
82   return this._maxUsedThemes;
83 });
85 __defineSetter__("_maxUsedThemes", function(aVal) {
86   delete this._maxUsedThemes;
87   return this._maxUsedThemes = aVal;
88 });
90 // Holds the ID of the theme being enabled while sending out the events so
91 // cached AddonWrapper instances can return correct values for permissions and
92 // pendingOperations
93 var _themeIDBeingEnabled = null;
95 var LightweightThemeManager = {
96   get usedThemes () {
97     try {
98       return JSON.parse(_prefs.getComplexValue("usedThemes",
99                                                Ci.nsISupportsString).data);
100     } catch (e) {
101       return [];
102     }
103   },
105   get currentTheme () {
106     try {
107       if (_prefs.getBoolPref("isThemeSelected"))
108         var data = this.usedThemes[0];
109     } catch (e) {}
111     return data || null;
112   },
114   get currentThemeForDisplay () {
115     var data = this.currentTheme;
117     if (data && PERSIST_ENABLED) {
118       for (let key in PERSIST_FILES) {
119         try {
120           if (data[key] && _prefs.getBoolPref("persisted." + key))
121             data[key] = _getLocalImageURI(PERSIST_FILES[key]).spec
122                         + "?" + data.id + ";" + _version(data);
123         } catch (e) {}
124       }
125     }
127     return data;
128   },
130   set currentTheme (aData) {
131     return _setCurrentTheme(aData, false);
132   },
134   setLocalTheme: function (aData) {
135     _setCurrentTheme(aData, true);
136   },
138   getUsedTheme: function (aId) {
139     var usedThemes = this.usedThemes;
140     for (let i = 0; i < usedThemes.length; i++) {
141       if (usedThemes[i].id == aId)
142         return usedThemes[i];
143     }
144     return null;
145   },
147   forgetUsedTheme: function (aId) {
148     let theme = this.getUsedTheme(aId);
149     if (!theme)
150       return;
152     let wrapper = new AddonWrapper(theme);
153     AddonManagerPrivate.callAddonListeners("onUninstalling", wrapper, false);
155     var currentTheme = this.currentTheme;
156     if (currentTheme && currentTheme.id == aId) {
157       this.themeChanged(null);
158       AddonManagerPrivate.notifyAddonChanged(null, ADDON_TYPE, false);
159     }
161     _updateUsedThemes(_usedThemesExceptId(aId));
162     AddonManagerPrivate.callAddonListeners("onUninstalled", wrapper);
163   },
165   previewTheme: function (aData) {
166     if (!aData)
167       return;
169     let cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
170     cancel.data = false;
171     Services.obs.notifyObservers(cancel, "lightweight-theme-preview-requested",
172                                  JSON.stringify(aData));
173     if (cancel.data)
174       return;
176     if (_previewTimer)
177       _previewTimer.cancel();
178     else
179       _previewTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
180     _previewTimer.initWithCallback(_previewTimerCallback,
181                                    MAX_PREVIEW_SECONDS * 1000,
182                                    _previewTimer.TYPE_ONE_SHOT);
184     _notifyWindows(aData);
185   },
187   resetPreview: function () {
188     if (_previewTimer) {
189       _previewTimer.cancel();
190       _previewTimer = null;
191       _notifyWindows(this.currentThemeForDisplay);
192     }
193   },
195   parseTheme: function (aString, aBaseURI) {
196     try {
197       return _sanitizeTheme(JSON.parse(aString), aBaseURI, false);
198     } catch (e) {
199       return null;
200     }
201   },
203   updateCurrentTheme: function () {
204     try {
205       if (!_prefs.getBoolPref("update.enabled"))
206         return;
207     } catch (e) {
208       return;
209     }
211     var theme = this.currentTheme;
212     if (!theme || !theme.updateURL)
213       return;
215     var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
216                 .createInstance(Ci.nsIXMLHttpRequest);
218     req.mozBackgroundRequest = true;
219     req.overrideMimeType("text/plain");
220     req.open("GET", theme.updateURL, true);
222     var self = this;
223     req.onload = function () {
224       if (req.status != 200)
225         return;
227       let newData = self.parseTheme(req.responseText, theme.updateURL);
228       if (!newData ||
229           newData.id != theme.id ||
230           _version(newData) == _version(theme))
231         return;
233       var currentTheme = self.currentTheme;
234       if (currentTheme && currentTheme.id == theme.id)
235         self.currentTheme = newData;
236     };
238     req.send(null);
239   },
241   /**
242    * Switches to a new lightweight theme.
243    *
244    * @param  aData
245    *         The lightweight theme to switch to
246    */
247   themeChanged: function(aData) {
248     if (_previewTimer) {
249       _previewTimer.cancel();
250       _previewTimer = null;
251     }
253     if (aData) {
254       let usedThemes = _usedThemesExceptId(aData.id);
255       usedThemes.unshift(aData);
256       _updateUsedThemes(usedThemes);
257       if (PERSIST_ENABLED)
258         _persistImages(aData);
259     }
261     _prefs.setBoolPref("isThemeSelected", aData != null);
262     _notifyWindows(aData);
263     Services.obs.notifyObservers(null, "lightweight-theme-changed", null);
264   },
266   /**
267    * Starts the Addons provider and enables the new lightweight theme if
268    * necessary.
269    */
270   startup: function() {
271     if (Services.prefs.prefHasUserValue(PREF_LWTHEME_TO_SELECT)) {
272       let id = Services.prefs.getCharPref(PREF_LWTHEME_TO_SELECT);
273       if (id)
274         this.themeChanged(this.getUsedTheme(id));
275       else
276         this.themeChanged(null);
277       Services.prefs.clearUserPref(PREF_LWTHEME_TO_SELECT);
278     }
280     _prefs.addObserver("", _prefObserver, false);
281   },
283   /**
284    * Shuts down the provider.
285    */
286   shutdown: function() {
287     _prefs.removeObserver("", _prefObserver);
288   },
290   /**
291    * Called when a new add-on has been enabled when only one add-on of that type
292    * can be enabled.
293    *
294    * @param  aId
295    *         The ID of the newly enabled add-on
296    * @param  aType
297    *         The type of the newly enabled add-on
298    * @param  aPendingRestart
299    *         true if the newly enabled add-on will only become enabled after a
300    *         restart
301    */
302   addonChanged: function(aId, aType, aPendingRestart) {
303     if (aType != ADDON_TYPE)
304       return;
306     let id = _getInternalID(aId);
307     let current = this.currentTheme;
309     try {
310       let next = Services.prefs.getCharPref(PREF_LWTHEME_TO_SELECT);
311       if (id == next && aPendingRestart)
312         return;
314       Services.prefs.clearUserPref(PREF_LWTHEME_TO_SELECT);
315       if (next) {
316         AddonManagerPrivate.callAddonListeners("onOperationCancelled",
317                                                new AddonWrapper(this.getUsedTheme(next)));
318       }
319       else {
320         if (id == current.id) {
321           AddonManagerPrivate.callAddonListeners("onOperationCancelled",
322                                                  new AddonWrapper(current));
323           return;
324         }
325       }
326     }
327     catch (e) {
328     }
330     if (current) {
331       if (current.id == id)
332         return;
333       let wrapper = new AddonWrapper(current);
334       if (aPendingRestart) {
335         Services.prefs.setCharPref(PREF_LWTHEME_TO_SELECT, "");
336         AddonManagerPrivate.callAddonListeners("onDisabling", wrapper, true);
337       }
338       else {
339         AddonManagerPrivate.callAddonListeners("onDisabling", wrapper, false);
340         this.themeChanged(null);
341         AddonManagerPrivate.callAddonListeners("onDisabled", wrapper);
342       }
343     }
345     if (id) {
346       let theme = this.getUsedTheme(id);
347       _themeIDBeingEnabled = id;
348       let wrapper = new AddonWrapper(theme);
349       if (aPendingRestart) {
350         AddonManagerPrivate.callAddonListeners("onEnabling", wrapper, true);
351         Services.prefs.setCharPref(PREF_LWTHEME_TO_SELECT, id);
353         // Flush the preferences to disk so they survive any crash
354         Services.prefs.savePrefFile(null);
355       }
356       else {
357         AddonManagerPrivate.callAddonListeners("onEnabling", wrapper, false);
358         this.themeChanged(theme);
359         AddonManagerPrivate.callAddonListeners("onEnabled", wrapper);
360       }
361       _themeIDBeingEnabled = null;
362     }
363   },
365   /**
366    * Called to get an Addon with a particular ID.
367    *
368    * @param  aId
369    *         The ID of the add-on to retrieve
370    * @param  aCallback
371    *         A callback to pass the Addon to
372    */
373   getAddonByID: function(aId, aCallback) {
374     let id = _getInternalID(aId);
375     if (!id) {
376       aCallback(null);
377       return;
378      }
380     let theme = this.getUsedTheme(id);
381     if (!theme) {
382       aCallback(null);
383       return;
384     }
386     aCallback(new AddonWrapper(theme));
387   },
389   /**
390    * Called to get Addons of a particular type.
391    *
392    * @param  aTypes
393    *         An array of types to fetch. Can be null to get all types.
394    * @param  aCallback
395    *         A callback to pass an array of Addons to
396    */
397   getAddonsByTypes: function(aTypes, aCallback) {
398     if (aTypes && aTypes.indexOf(ADDON_TYPE) == -1) {
399       aCallback([]);
400       return;
401     }
403     aCallback([new AddonWrapper(a) for each (a in this.usedThemes)]);
404   },
408  * The AddonWrapper wraps lightweight theme to provide the data visible to
409  * consumers of the AddonManager API.
410  */
411 function AddonWrapper(aTheme) {
412   this.__defineGetter__("id", function() aTheme.id + ID_SUFFIX);
413   this.__defineGetter__("type", function() ADDON_TYPE);
414   this.__defineGetter__("isActive", function() {
415     let current = LightweightThemeManager.currentTheme;
416     if (current)
417       return aTheme.id == current.id;
418     return false;
419   });
421   this.__defineGetter__("name", function() aTheme.name);
422   this.__defineGetter__("version", function() {
423     return "version" in aTheme ? aTheme.version : "";
424   });
426   ["description", "homepageURL", "iconURL"].forEach(function(prop) {
427     this.__defineGetter__(prop, function() {
428       return prop in aTheme ? aTheme[prop] : null;
429     });
430   }, this);
432   ["installDate", "updateDate"].forEach(function(prop) {
433     this.__defineGetter__(prop, function() {
434       return prop in aTheme ? new Date(aTheme[prop]) : null;
435     });
436   }, this);
438   this.__defineGetter__("creator", function() {
439     return new AddonManagerPrivate.AddonAuthor(aTheme.author);
440   });
442   this.__defineGetter__("screenshots", function() {
443     let url = aTheme.previewURL;
444     return [new AddonManagerPrivate.AddonScreenshot(url)];
445   });
447   this.__defineGetter__("pendingOperations", function() {
448     let pending = AddonManager.PENDING_NONE;
449     if (this.isActive == this.userDisabled)
450       pending |= this.isActive ? AddonManager.PENDING_DISABLE : AddonManager.PENDING_ENABLE;
451     return pending;
452   });
454   this.__defineGetter__("operationsRequiringRestart", function() {
455     // If a non-default theme is in use then a restart will be required to
456     // enable lightweight themes unless dynamic theme switching is enabled
457     if (Services.prefs.prefHasUserValue(PREF_GENERAL_SKINS_SELECTEDSKIN)) {
458       try {
459         if (Services.prefs.getBoolPref(PREF_EM_DSS_ENABLED))
460           return AddonManager.OP_NEEDS_RESTART_NONE;
461       }
462       catch (e) {
463       }
464       return AddonManager.OP_NEEDS_RESTART_ENABLE;
465     }
467     return AddonManager.OP_NEEDS_RESTART_NONE;
468   });
470   this.__defineGetter__("size", function() {
471     // The size changes depending on whether the theme is in use or not, this is
472     // probably not worth exposing.
473     return null;
474   });
476   this.__defineGetter__("permissions", function() {
477     let permissions = AddonManager.PERM_CAN_UNINSTALL;
478     if (this.userDisabled)
479       permissions |= AddonManager.PERM_CAN_ENABLE;
480     return permissions;
481   });
483   this.__defineGetter__("userDisabled", function() {
484     if (_themeIDBeingEnabled == aTheme.id)
485       return false;
487     try {
488       let toSelect = Services.prefs.getCharPref(PREF_LWTHEME_TO_SELECT);
489       return aTheme.id != toSelect;
490     }
491     catch (e) {
492       let current = LightweightThemeManager.currentTheme;
493       return !current || current.id != aTheme.id;
494     }
495   });
497   this.__defineSetter__("userDisabled", function(val) {
498     if (val == this.userDisabled)
499       return val;
501     if (val)
502       LightweightThemeManager.currentTheme = null;
503     else
504       LightweightThemeManager.currentTheme = aTheme;
506     return val;
507   });
509   this.uninstall = function() {
510     LightweightThemeManager.forgetUsedTheme(aTheme.id);
511   };
513   this.cancelUninstall = function() {
514     throw new Error("Theme is not marked to be uninstalled");
515   };
517   this.findUpdates = function(listener, reason, appVersion, platformVersion) {
518     if ("onNoCompatibilityUpdateAvailable" in listener)
519       listener.onNoCompatibilityUpdateAvailable(this);
520     if ("onNoUpdateAvailable" in listener)
521       listener.onNoUpdateAvailable(this);
522     if ("onUpdateFinished" in listener)
523       listener.onUpdateFinished(this);
524   };
527 AddonWrapper.prototype = {
528   // Lightweight themes are never disabled by the application
529   get appDisabled() {
530     return false;
531   },
533   // Lightweight themes are always compatible
534   get isCompatible() {
535     return true;
536   },
538   get isPlatformCompatible() {
539     return true;
540   },
542   get scope() {
543     return AddonManager.SCOPE_PROFILE;
544   },
546   // Lightweight themes are always compatible
547   isCompatibleWith: function(appVersion, platformVersion) {
548     return true;
549   },
551   // Lightweight themes are always securely updated
552   get providesUpdatesSecurely() {
553     return true;
554   },
556   // Lightweight themes are never blocklisted
557   get blocklistState() {
558     return Ci.nsIBlocklistService.STATE_NOT_BLOCKED;
559   }
563  * Converts the ID used by the public AddonManager API to an lightweight theme
564  * ID.
566  * @param   id
567  *          The ID to be converted
569  * @return  the lightweight theme ID or null if the ID was not for a lightweight
570  *          theme.
571  */
572 function _getInternalID(id) {
573   if (!id)
574     return null;
575   let len = id.length - ID_SUFFIX.length;
576   if (len > 0 && id.substring(len) == ID_SUFFIX)
577     return id.substring(0, len);
578   return null;
581 function _setCurrentTheme(aData, aLocal) {
582   aData = _sanitizeTheme(aData, null, aLocal);
584   let needsRestart = (ADDON_TYPE == "theme") &&
585                      Services.prefs.prefHasUserValue(PREF_GENERAL_SKINS_SELECTEDSKIN);
587   let cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
588   cancel.data = false;
589   Services.obs.notifyObservers(cancel, "lightweight-theme-change-requested",
590                                JSON.stringify(aData));
592   if (aData) {
593     let theme = LightweightThemeManager.getUsedTheme(aData.id);
594     let isInstall = !theme || theme.version != aData.version;
595     if (isInstall) {
596       aData.updateDate = Date.now();
597       if (theme && "installDate" in theme)
598         aData.installDate = theme.installDate;
599       else
600         aData.installDate = aData.updateDate;
602       var oldWrapper = theme ? new AddonWrapper(theme) : null;
603       var wrapper = new AddonWrapper(aData);
604       AddonManagerPrivate.callInstallListeners("onExternalInstall", null,
605                                                wrapper, oldWrapper, false);
606       AddonManagerPrivate.callAddonListeners("onInstalling", wrapper, false);
607     }
609     let current = LightweightThemeManager.currentTheme;
610     let usedThemes = _usedThemesExceptId(aData.id);
611     if (current && current.id != aData.id)
612       usedThemes.splice(1, 0, aData);
613     else
614       usedThemes.unshift(aData);
615     _updateUsedThemes(usedThemes);
617     if (isInstall)
618        AddonManagerPrivate.callAddonListeners("onInstalled", wrapper);
619   }
621   if (cancel.data)
622     return null;
624   AddonManagerPrivate.notifyAddonChanged(aData ? aData.id + ID_SUFFIX : null,
625                                          ADDON_TYPE, needsRestart);
627   return LightweightThemeManager.currentTheme;
630 function _sanitizeTheme(aData, aBaseURI, aLocal) {
631   if (!aData || typeof aData != "object")
632     return null;
634   var resourceProtocols = ["http", "https"];
635   if (aLocal)
636     resourceProtocols.push("file");
637   var resourceProtocolExp = new RegExp("^(" + resourceProtocols.join("|") + "):");
639   function sanitizeProperty(prop) {
640     if (!(prop in aData))
641       return null;
642     if (typeof aData[prop] != "string")
643       return null;
644     let val = aData[prop].trim();
645     if (!val)
646       return null;
648     if (!/URL$/.test(prop))
649       return val;
651     try {
652       val = _makeURI(val, aBaseURI ? _makeURI(aBaseURI) : null).spec;
653       if ((prop == "updateURL" ? /^https:/ : resourceProtocolExp).test(val))
654         return val;
655       return null;
656     }
657     catch (e) {
658       return null;
659     }
660   }
662   let result = {};
663   for (let i = 0; i < MANDATORY.length; i++) {
664     let val = sanitizeProperty(MANDATORY[i]);
665     if (!val)
666       throw Components.results.NS_ERROR_INVALID_ARG;
667     result[MANDATORY[i]] = val;
668   }
670   for (let i = 0; i < OPTIONAL.length; i++) {
671     let val = sanitizeProperty(OPTIONAL[i]);
672     if (!val)
673       continue;
674     result[OPTIONAL[i]] = val;
675   }
677   return result;
680 function _usedThemesExceptId(aId)
681   LightweightThemeManager.usedThemes.filter(function (t) "id" in t && t.id != aId);
683 function _version(aThemeData)
684   aThemeData.version || "";
686 function _makeURI(aURL, aBaseURI)
687   Services.io.newURI(aURL, null, aBaseURI);
689 function _updateUsedThemes(aList) {
690   // Send uninstall events for all themes that need to be removed.
691   while (aList.length > _maxUsedThemes) {
692     let wrapper = new AddonWrapper(aList[aList.length - 1]);
693     AddonManagerPrivate.callAddonListeners("onUninstalling", wrapper, false);
694     aList.pop();
695     AddonManagerPrivate.callAddonListeners("onUninstalled", wrapper);
696   }
698   var str = Cc["@mozilla.org/supports-string;1"]
699               .createInstance(Ci.nsISupportsString);
700   str.data = JSON.stringify(aList);
701   _prefs.setComplexValue("usedThemes", Ci.nsISupportsString, str);
703   Services.obs.notifyObservers(null, "lightweight-theme-list-changed", null);
706 function _notifyWindows(aThemeData) {
707   Services.obs.notifyObservers(null, "lightweight-theme-styling-update",
708                                JSON.stringify(aThemeData));
711 var _previewTimer;
712 var _previewTimerCallback = {
713   notify: function () {
714     LightweightThemeManager.resetPreview();
715   }
719  * Called when any of the lightweightThemes preferences are changed.
720  */
721 function _prefObserver(aSubject, aTopic, aData) {
722   switch (aData) {
723     case "maxUsedThemes":
724       try {
725         _maxUsedThemes = _prefs.getIntPref(aData);
726       }
727       catch (e) {
728         _maxUsedThemes = DEFAULT_MAX_USED_THEMES_COUNT;
729       }
730       // Update the theme list to remove any themes over the number we keep
731       _updateUsedThemes(LightweightThemeManager.usedThemes);
732       break;
733   }
736 function _persistImages(aData) {
737   function onSuccess(key) function () {
738     let current = LightweightThemeManager.currentTheme;
739     if (current && current.id == aData.id)
740       _prefs.setBoolPref("persisted." + key, true);
741   };
743   for (let key in PERSIST_FILES) {
744     _prefs.setBoolPref("persisted." + key, false);
745     if (aData[key])
746       _persistImage(aData[key], PERSIST_FILES[key], onSuccess(key));
747   }
750 function _getLocalImageURI(localFileName) {
751   var localFile = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
752   localFile.append(localFileName);
753   return Services.io.newFileURI(localFile);
756 function _persistImage(sourceURL, localFileName, successCallback) {
757   if (/^file:/.test(sourceURL))
758     return;
760   var targetURI = _getLocalImageURI(localFileName);
761   var sourceURI = _makeURI(sourceURL);
763   var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
764                   .createInstance(Ci.nsIWebBrowserPersist);
766   persist.persistFlags =
767     Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
768     Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION |
769     (PERSIST_BYPASS_CACHE ?
770        Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE :
771        Ci.nsIWebBrowserPersist.PERSIST_FLAGS_FROM_CACHE);
773   persist.progressListener = new _persistProgressListener(successCallback);
775   persist.saveURI(sourceURI, null, null, null, null, targetURI);
778 function _persistProgressListener(successCallback) {
779   this.onLocationChange = function () {};
780   this.onProgressChange = function () {};
781   this.onStatusChange   = function () {};
782   this.onSecurityChange = function () {};
783   this.onStateChange    = function (aWebProgress, aRequest, aStateFlags, aStatus) {
784     if (aRequest &&
785         aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK &&
786         aStateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
787       try {
788         if (aRequest.QueryInterface(Ci.nsIHttpChannel).requestSucceeded) {
789           // success
790           successCallback();
791           return;
792         }
793       } catch (e) { }
794       // failure
795     }
796   };
799 AddonManagerPrivate.registerProvider(LightweightThemeManager);