views:

159

answers:

1

Thanks to everyone in advance -

I need to load a preference before any windows are loaded at startup. Below is some /component code I have been working with. The SetPreference method seems to fail when it is called (nothing executes afterwords either) - I am assuming because the resources that it needs are not available at the time of execution...or I am doing something wrong. Any suggestions with this code or another approach to setting a preference at startup?

Thanks again,

Sam

For some reason the code formatting for SO is not working properly - here is a link to the code as well - http://samingrassia.com/_FILES/startup.js

Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');

const Cc = Components.classes;
const Ci = Components.interfaces;

const ObserverService = Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService);

function MyStartupService() {};

MyStartupService.prototype = {
  observe : function(aSubject, aTopic, aData) {
    switch (aTopic) {
      case 'xpcom-startup':
        this.SetPreference("my.extension.is_running", "false");
        break;
      case 'app-startup':
        this.SetPreference("my.extension.is_running", "false");
        ObserverService.addObserver(this, 'final-ui-startup', false);
        break;
      case 'final-ui-startup':

        //make sure is_running is set to false
        this.SetPreference("my.extension.is_running", "false");

        ObserverService.removeObserver(this, 'final-ui-startup');
        const WindowWatcher = Cc['@mozilla.org/embedcomp/window-watcher;1'].getService(Ci.nsIWindowWatcher);
        WindowWatcher.registerNotification(this);
        break;
      case 'domwindowopened':
        this.initWindow(aSubject);
        break;
    }
  },
  SetPreference : function(Token, Value) {
    var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
    var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
    str.data = Value;
    prefs.setComplexValue(Token, Components.interfaces.nsISupportsString, str);

    //save preferences
    var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
    prefService.savePrefFile(null);  
  },
  initWindow : function(aWindow) {
    if (aWindow != '[object ChromeWindow]') return;
    aWindow.addEventListener('load', function() {
      aWindow.removeEventListener('load', arguments.callee, false);
      aWindow.document.title = 'domwindowopened!';
      // for browser windows
      var root = aWindow.document.documentElement;
      root.setAttribute('title', aWindow.document.title);
      root.setAttribute('titlemodifier', aWindow.document.title);
    }, false);
  },
  classDescription : 'My Startup Service',
  contractID : '@mystartupservice.com/startup;1',
  classID : Components.ID('{770825e7-b39c-4654-94bc-008e5d6d57b7}'),
  QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver]),
  _xpcom_categories : [{ category : 'app-startup', service : true }]
};

function NSGetModule(aCompMgr, aFileSpec) {
  return XPCOMUtils.generateModule([MyStartupService]);
}
+1  A: 

To answer your real question, which is

I have code that loads on every window load and I need to make sure that only gets executed once every time firefox starts up.

..you should just use a module, in the load handler that you wish to execute once, check a flag on the object exported from (i.e. "living in") the module, then after running the code you need, set the flag.

Since the module is shared across all windows, the flag will remain set until you close Firefox.

As for your intermediate problem, I'd suggest wrapping the code inside observe() in a try { ... } catch(e) {dump(e)} (you'll need to set a pref and run Firefox in a special way in order to see the output) and check the error returned.

I guess xpcom-startup and app-startup is too early to mess with preferences (I think you need a profile for that), note that you don't register to get xpcom-startup notification anyway. You probably want to register for profile-after-change instead.

Nickolay
muchos gracias!
Sam Ingrassia