mozilla

Revision 165305 of User:Mossop

  • Revision slug: User:Mossop
  • Revision title: User:Mossop
  • Revision id: 165305
  • Created:
  • Creator: Mossop
  • Is current revision? No
  • Comment /* Observer Topics */

Revision Content

Observer Topics

Application Startup

These are the topics that you can observe on startup, in order of appearance.

Topic Description
xpcom-startup Called when xpcom is initialized. Many things are not available for use at this point. You must register in the category manager to see this topic.
app-startup General event for application startup. You must register in the category manager to see this topic.
profile-after-change This is fired after the profile has been selected. You will not be able to access user preferences, bookmarks, or anything that uses the profile folder until this event occurs.
final-ui-startup Triggered just before the first window for the application is displayed.

Application Shutdown

These are the topics that you can observe on shutdown, in order of appearance.

Topic Description
quit-application-requested Something has requested that the application be shutdown. You can cancel the shutdown from here.
quit-application-granted All observers have agreed to the shutdown.
quit-application The application is about to quit.
profile-change-teardown Part of the shutdown, profile data is still available at this point.
profile-before-change Called just before the profile is lost.
xpcom-shutdown This is the end. Many things will not be available here.

JS Threading (1.8 branch only)

Work in progress. This code seems to work in general but I found that it would hang the browser often while calling the callbacks.

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

function myThread(proxy)
{
  this.proxy = proxy;
}

myThread.prototype = {
proxy: null,

// This runs in its own thread, but cannot affect any DOM or UI except by calls on the proxy object
run: function()
{
  // long running code
  this.proxy.method1();
  // long running code
  this.proxy.method2();
  // long running code
  this.proxy.method3();
},

QueryInterface: function(iid)
{
  if (iid.equals(Ci.nsIRunnable)
   || iid.equals(Ci.nsISupports))
  {
    return this;
  }
  else
  {
    throw Components.results.NS_ERROR_NO_INTERFACE;
  }
}
}

myComponent = {

startThread: function()
{
  // Retrieves the event queue for the UI thread
  var eQs = Cc["@mozilla.org/event-queue-service;1"]
              .getService(Ci.nsIEventQueueService);
  var eQueue = eQs.getSpecialEventQueue(Ci.UI_THREAD_EVENT_QUEUE);
  
  // Creates a proxy for this object that will make calls on the UI event queue.
  var nsIPOM = Ci.nsIProxyObjectManager;
  var proxyManager = Cc["@mozilla.org/xpcomproxy;1"]
                       .getService(Ci.nsIProxyObjectManager);
  var proxy = proxyManager.getProxyForObject(eQueue, 
                                             Ci.myComponent, 
                                             this, 
                                             Ci.nsIProxyObjectManager.INVOKE_SYNC
                                              + Ci.nsIProxyObjectManager.FORCE_PROXY_CREATION);

  // Creates the thread
  var runnable = new myThread(proxy);
  var thread = Cc["@mozilla.org/thread;1"]
		 .createInstance(Ci.nsIThread);
  
  // Starts the thread
  // These settings came from some source I found, they may not be correct.
  thread.init(runnable, 0, Ci.nsIThread.PRIORITY_NORMAL, 
                           Ci.nsIThread.SCOPE_LOCAL, 
                           Ci.nsIThread.STATE_UNJOINABLE);
},

// Must be declared in the myComponent interface idl
method1: function()
{
  // This runs in the main UI thread
},

// Must be declared in the myComponent interface idl
method2: function()
{
  // This runs in the main UI thread
},

// Must be declared in the myComponent interface idl
method3: function()
{
  // This runs in the main UI thread
},

QueryInterface: function(iid)
{
  if (iid.equals(Ci.myComponent)
   || iid.equals(Ci.nsISupports))
  {
    return this;
  }
  else
  {
    throw Components.results.NS_ERROR_NO_INTERFACE;
  }
}
}

Revision Source

<h2 name="Observer_Topics"> Observer Topics </h2>
<h3 name="Application_Startup"> Application Startup </h3>
<p>These are the topics that you can observe on startup, in order of appearance.
</p>
<table class="standard-table">
  <tbody><tr>
    <th>Topic</th>
    <th>Description</th>
  </tr>
  <tr>
    <td>xpcom-startup</td>
    <td>Called when xpcom is initialized. Many things are not available for use at this point. You must register in the category manager to see this topic.</td>
  </tr>
  <tr>
    <td>app-startup</td>
    <td>General event for application startup. You must register in the category manager to see this topic.</td>
  </tr>
  <tr>
    <td>profile-after-change</td>
    <td>This is fired after the profile has been selected. You will not be able to access user preferences, bookmarks, or anything that uses the profile folder until this event occurs.</td>
  </tr>
  <tr>
    <td>final-ui-startup</td>
    <td>Triggered just before the first window for the application is displayed.</td>
  </tr>
</tbody></table>
<h3 name="Application_Shutdown"> Application Shutdown </h3>
<p>These are the topics that you can observe on shutdown, in order of appearance.
</p>
<table class="standard-table">
  <tbody><tr>
    <th>Topic</th>
    <th>Description</th>
  </tr>
  <tr>
    <td>quit-application-requested</td>
    <td>Something has requested that the application be shutdown. You can cancel the shutdown from here.</td>
  </tr>
  <tr>
    <td>quit-application-granted</td>
    <td>All observers have agreed to the shutdown.</td>
  </tr>
  <tr>
    <td>quit-application</td>
    <td>The application is about to quit.</td>
  </tr>
  <tr>
    <td>profile-change-teardown</td>
    <td>Part of the shutdown, profile data is still available at this point.</td>
  </tr>
  <tr>
    <td>profile-before-change</td>
    <td>Called just before the profile is lost.</td>
  </tr>
  <tr>
    <td>xpcom-shutdown</td>
    <td>This is the end. Many things will not be available here.</td>
  </tr>
</tbody></table>
<h2 name="JS_Threading_.281.8_branch_only.29"> JS Threading (1.8 branch only) </h2>
<p>Work in progress. This code seems to work in general but I found that it would hang the browser often while calling the callbacks.
</p>
<pre>const Cc = Components.classes;
const Ci = Components.interfaces;

function myThread(proxy)
{
  this.proxy = proxy;
}

myThread.prototype = {
proxy: null,

// This runs in its own thread, but cannot affect any DOM or UI except by calls on the proxy object
run: function()
{
  // long running code
  this.proxy.method1();
  // long running code
  this.proxy.method2();
  // long running code
  this.proxy.method3();
},

QueryInterface: function(iid)
{
  if (iid.equals(Ci.nsIRunnable)
   || iid.equals(Ci.nsISupports))
  {
    return this;
  }
  else
  {
    throw Components.results.NS_ERROR_NO_INTERFACE;
  }
}
}

myComponent = {

startThread: function()
{
  // Retrieves the event queue for the UI thread
  var eQs = Cc["@mozilla.org/event-queue-service;1"]
              .getService(Ci.nsIEventQueueService);
  var eQueue = eQs.getSpecialEventQueue(Ci.UI_THREAD_EVENT_QUEUE);
  
  // Creates a proxy for this object that will make calls on the UI event queue.
  var nsIPOM = Ci.nsIProxyObjectManager;
  var proxyManager = Cc["@mozilla.org/xpcomproxy;1"]
                       .getService(Ci.nsIProxyObjectManager);
  var proxy = proxyManager.getProxyForObject(eQueue, 
                                             Ci.myComponent, 
                                             this, 
                                             Ci.nsIProxyObjectManager.INVOKE_SYNC
                                              + Ci.nsIProxyObjectManager.FORCE_PROXY_CREATION);

  // Creates the thread
  var runnable = new myThread(proxy);
  var thread = Cc["@mozilla.org/thread;1"]
		 .createInstance(Ci.nsIThread);
  
  // Starts the thread
  // These settings came from some source I found, they may not be correct.
  thread.init(runnable, 0, Ci.nsIThread.PRIORITY_NORMAL, 
                           Ci.nsIThread.SCOPE_LOCAL, 
                           Ci.nsIThread.STATE_UNJOINABLE);
},

// Must be declared in the myComponent interface idl
method1: function()
{
  // This runs in the main UI thread
},

// Must be declared in the myComponent interface idl
method2: function()
{
  // This runs in the main UI thread
},

// Must be declared in the myComponent interface idl
method3: function()
{
  // This runs in the main UI thread
},

QueryInterface: function(iid)
{
  if (iid.equals(Ci.myComponent)
   || iid.equals(Ci.nsISupports))
  {
    return this;
  }
  else
  {
    throw Components.results.NS_ERROR_NO_INTERFACE;
  }
}
}

</pre>
Revert to this revision