Difference between revisions of "PWCore"
(→Registration) |
(→Usage) |
||
Line 25: | Line 25: | ||
===Observing=== | ===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) that gets called with each notification. The topic contains PW's notification topic - 'experimental-notify-plugin-call', the data is the runtime in milliseconds and the subject is always 'null' and should not be used. | + | 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) that gets called with each notification. The topic contains PW's notification topic - 'experimental-notify-plugin-call', 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: | Here is an example that shows the runtime in an alert box to the user: | ||
Line 35: | Line 35: | ||
} | } | ||
− | NOTE: This example should never be used as PW sends hundreds of notifications each second and will cause your browser to crash with the excessive number of alert boxes. | + | - NOTE: This is just a simplified example and should never be used as PW sends hundreds of notifications each second and will cause your browser to crash with the excessive number of alert boxes. |
+ | |||
+ | 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=== | ===Clean Up=== | ||
− | To unregister with the Observer service when you no longer want to be listening to PW's notifications | + | To unregister your class with the Observer service - when you no longer want to be listening to PW's 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"); |
Revision as of 16:51, 24 April 2008
Contents
PW Core
Summery
PW Core is the core component of PluginWatcher, which resides at the plugin core of the Firefox browser. The intended purpose of PW Core is to time how long it takes plugins (such as Macromedia Flash, Windows Media Player, Acrobat Reader etc...) to execute their calls and then report the runtime (in milliseconds) using the Mozilla Observer service to anyone registered to receive PluginWatcher's notifications.
Release Information
PW Core is part of Mozilla 1.9 and will be released along with Firefox 3. If you are interested in seeing the code please visit this bug.
Usage
To make use of PW Core you must register to receive its runtime notifications using Mozilla's Observer service. To do this you need to know the notification's topic which 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 work with PW Core:
Registration
To register for PW 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) that gets called with each notification. The topic contains PW's notification topic - 'experimental-notify-plugin-call', 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:
observe: function(subject, topic, data) { if (topic == "experimental-notify-plugin-call" ) { alert("Runtime is:" + data); } }
- NOTE: This is just a simplified example and should never be used as PW sends hundreds of notifications each second and will cause your browser to crash with the excessive number of alert boxes.
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 PW's 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");