mozilla

Revision 166789 of Monitoring plugins

  • Revision slug: Monitoring_plugins
  • Revision title: Monitoring plugins
  • Revision id: 166789
  • Created:
  • Creator: Xrayon
  • Is current revision? No
  • Comment /* Usage */

Revision Content

{{template.Fx_minversion_header(3)}}

Summary

A new component of the plugin system is now available to measure how long it takes plugins (e.g., Macromedia Flash) to execute their calls. This component then reports the plugin runtime using the observer service to anyone registered to receive the notifications. This article discusses how developers can make use of this new feature.

Runtime Data

The runtime information reported is always in fractions of a second. Because the component measures the wall clock time it takes for blocking plugin calls to execute, the value includes both CPU time, the wait time between allocation of CPU time to the process as well as any disk I/O time. It is therefore technically incorrect to say that the runtime is a measure of CPU use, however, it is a good representation of overall resources being consumed by the plugin.

Usage

To make use of the new component you must register to receive its runtime notifications using the observer service. The notification's topic is 'experimental-notify-plugin-call'. If you are new to the Mozilla observer service, you may want to familiarize yourself with it before proceeding further. You can find more information on the observer service here and here.

Below are a number of JavaScript snippets that would be useful to developers trying to use this feature:

Registration
To register for runtime notifications with the observer service you must create a class with an 'observe' method which receives 3 parameters (subject, topic and data) as well as a 'register' method that contains the following code:

var observerService = Components.classes["@mozilla.org/observer-service;1"]
                        .getService (Components.interfaces.nsIObserverService);
observerService.addObserver(this, "experimental-notify-plugin-call", false);


Observing
As discussed above, to specify what you want done when a notification arrives your class must have an 'observe' method, receiving 3 parameters (subject, topic and data). The topic contains the notification's topic - 'experimental-notify-plugin-call' in this case, the data is the runtime in milliseconds and the subject is always 'null' and should not be used.
Here is an example that shows the runtime in an alert box to the user if the runtime exceeds half a second in length:

 observe: function(subject, topic, data) {	
   if (topic == "experimental-notify-plugin-call" ) {
     if (data > 0.500) {
       alert("Runtime is: " + data);
     }
   }
 }

NOTE: This is just a simplified example and the use of alert() is discouraged as the component can send hundreds of notifications each second and could potentially cause your browser to crash if an excessive number of alert boxes are displayed.

Also note that in the example above an if statement first checks to see that the arriving notification's topic is the correct one. This is useful in cases where your class is registered to receive notifications for more than one topic with the observer service.


Clean Up
To unregister your class with the observer service - when you no longer want to be listening to runtime notifications - your class must include an 'unregister' method that contains the following code:

var observerService = Components.classes["@mozilla.org/observer-service;1"]
                       .getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "experimental-notify-plugin-call");


Skeleton Observer Class
Below is a skeleton class that you may use to listen to runtime notifications:

 function PluginObserver() {
   this.registered = false;
   this.register();	//takes care of registering this class with observer services as an observer for plugin runtime notifications
 }
 
 PluginWatcherObserver.prototype = {
   observe: function(subject, topic, data) {	
   
     if (topic == "experimental-notify-plugin-call") //In case your class is registered to listen to other topics
       //This gets executed each time a runtime notification arrives
       // --YOUR CODE GOES HERE--        
     }
  
 
   },
   //Takes care of registering the observer services for the "experimental-notify-plugin-call" topic
   register: function() {
     if (this.registered == false) { //This check prevents double registration -- something you want to avoid!!
       var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                       .getService(Components.interfaces.nsIObserverService);
       observerService.addObserver(this, "experimental-notify-plugin-call", false);
       this.registered = true;
     }
   },
   //Unregisters from the observer services
   unregister: function() {
     if (this.registered == true) {
       var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                       .getService(Components.interfaces.nsIObserverService);
       observerService.removeObserver(this, "experimental-notify-plugin-call");
       this.registered = false;
     }
   }
 }

Additional Resources

More information on the observer service here and here.

Revision Source

<p>{{template.Fx_minversion_header(3)}}
</p>
<h3 name="Summary">Summary</h3>
<p>A new component of the plugin system is now available to measure how long it takes plugins (e.g., Macromedia Flash) to execute their calls. This component then reports the plugin runtime using the <a href="en/NsIObserverService">observer service</a> to anyone registered to receive the notifications. This article discusses how developers can make use of this new feature.
</p>
<h3 name="Runtime_Data">Runtime Data</h3>
<p>The runtime information reported is always in fractions of a second.  Because the component measures the wall clock time it takes for blocking plugin calls to execute, the value includes both CPU time, the wait time between allocation of CPU time to the process as well as any disk I/O time. It is therefore technically incorrect to say that the runtime is a measure of CPU use, however, it is a good representation of overall resources being consumed by the plugin.
</p>
<h3 name="Usage">Usage</h3>
<p>To make use of the new component you must register to receive its runtime notifications using the <a href="en/NsIObserverService">observer service</a>. The notification's topic is 'experimental-notify-plugin-call'. If you are new to the Mozilla observer service, you may want to familiarize yourself with it before proceeding further. You can find more information on the observer service <a href="en/NsIObserverService"> here</a> and <a href="en/NsIObserver"> here</a>.
</p><p>Below are a number of JavaScript snippets that would be useful to developers trying to use this feature:
</p>
<dl><dt>Registration
</dt></dl>
<dl><dd>To register for runtime notifications with the observer service you must create a class with an 'observe' method which receives 3 parameters (subject, topic and data) as well as a 'register' method that contains the following code:
</dd></dl>
<p><code>
</code></p>
<pre class="eval">var observerService = Components.classes["@mozilla.org/observer-service;1"]
                        .getService (Components.interfaces.nsIObserverService);
observerService.addObserver(this, "experimental-notify-plugin-call", false);
</pre>
<p>
</p><p><br>
</p>
<dl><dt>Observing
</dt></dl>
<dl><dd>As discussed above, to specify what you want done when a notification arrives your class must have an 'observe' method, receiving 3 parameters (subject, topic and data). The topic contains the notification's topic - 'experimental-notify-plugin-call' in this case, the data is the runtime in milliseconds and the subject is always 'null' and should not be used.  
</dd></dl>
<dl><dd>Here is an example that shows the runtime in an alert box to the user if the runtime exceeds half a second in length:
</dd></dl>
<p><code>
</code></p>
<pre class="eval"> observe: function(subject, topic, data) {	
   if (topic == "experimental-notify-plugin-call" ) {
     if (data &gt; 0.500) {
       alert("Runtime is: " + data);
     }
   }
 }
</pre>
<p>
</p>
<dl><dd><b>NOTE: This is just a simplified example and the use of alert() is discouraged as the component can send hundreds of notifications each second and could potentially cause your browser to crash if an excessive number of alert boxes are displayed.</b>
</dd></dl>
<p>
</p>
<dl><dd>Also note that in the example above an if statement first checks to see that the arriving notification's topic is the correct one. This is useful in cases where your class is registered to receive notifications for more than one topic with the observer service.
</dd></dl>
<p><br>
</p>
<dl><dt>Clean Up
</dt></dl>
<dl><dd>To unregister your class with the observer service - when you no longer want to be listening to runtime notifications - your class must include an 'unregister' method that contains the following code:
</dd></dl>
<p><code>
</code></p>
<pre class="eval">var observerService = Components.classes["@mozilla.org/observer-service;1"]
                       .getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "experimental-notify-plugin-call");
</pre>
<p>
</p><p><br>
</p>
<dl><dt>Skeleton Observer Class
</dt></dl>
<dl><dd>Below is a skeleton class that you may use to listen to runtime notifications:
</dd></dl>
<p><code>
</code></p>
<pre class="eval"> function PluginObserver() {
   this.registered = false;
   this.register();	//takes care of registering this class with observer services as an observer for plugin runtime notifications
 }
 
 PluginWatcherObserver.prototype = {
   observe: function(subject, topic, data) {	
   
     if (topic == "experimental-notify-plugin-call") //In case your class is registered to listen to other topics
       //This gets executed each time a runtime notification arrives
       // --YOUR CODE GOES HERE--        
     }
  
 
   },
   //Takes care of registering the observer services for the "experimental-notify-plugin-call" topic
   register: function() {
     if (this.registered == false) { //This check prevents double registration -- something you want to avoid!!
       var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                       .getService(Components.interfaces.nsIObserverService);
       observerService.addObserver(this, "experimental-notify-plugin-call", false);
       this.registered = true;
     }
   },
   //Unregisters from the observer services
   unregister: function() {
     if (this.registered == true) {
       var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                       .getService(Components.interfaces.nsIObserverService);
       observerService.removeObserver(this, "experimental-notify-plugin-call");
       this.registered = false;
     }
   }
 }
</pre>
<p>
</p>
<h3 name="Additional_Resources">Additional Resources</h3>
<p>More information on the observer service <a href="en/NsIObserverService"> here</a> and <a href="en/NsIObserver"> here</a>.
</p>
Revert to this revision