Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Bug 616907 - Convert private-browsing events to style described in bu…
Browse files Browse the repository at this point in the history
…g 593921. r=myk
  • Loading branch information
0c0w3 committed Dec 6, 2010
1 parent 68b79ee commit 9d92356
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
21 changes: 8 additions & 13 deletions packages/addon-kit/docs/private-browsing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,29 @@
<!-- edited by Noelle Murata [fiveinchpixie@gmail.com] -->
<!-- contributed by Irakli Gozalishvili [gozala@mozilla.com] -->


The `private-browsing` module allows you to access the private browsing service
- detecting if it is active and adding callbacks for transitioning into and out
of private browsing mode.

Private browsing is a singleton, so in most cases it will be easiest to store it
in a variable.

var pb = require("private-browsing");
The `private-browsing` module allows you to access Firefox's private browsing
mode, detecting if it is active and when its state changes.

## Events ##

When the browser starts or stops private browsing mode, the following events
are emitted:
are emitted. In each case, listeners are passed an event object that has a
single property `emitter` whose value is the private browsing module itself.

### start ###
Emitted when the browser starts private browsing mode.

pb.on("start", function() {
var pb = require("private-browsing");
pb.on("start", function(event) {
// Do something when the browser starts private browsing mode.
});


### stop ###
Emitted when the browser stops private browsing mode.


pb.on("stop", function() {
var pb = require("private-browsing");
pb.on("stop", function(event) {
// Do something when the browser stops private browsing mode.
});

Expand Down
5 changes: 3 additions & 2 deletions packages/addon-kit/lib/private-browsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if (require("xul-app").is("Firefox")) {
const privateBrowsing = EventEmitter.compose({
constructor: function PrivateBrowsing() {
// Binding method to instance since it will be used with `setTimeout`.
this._emit = this._emit.bind(this);
this._emitEventObject = this._emitEventObject.bind(this);
this.unload = this.unload.bind(this);
// Report unhandled errors from listeners
this.on("error", console.exception.bind(console));
Expand All @@ -74,7 +74,8 @@ const privateBrowsing = EventEmitter.compose({
// We don't need to do anything with cancel here.
onTransition: function onTransition() {
let isActive = this._isActive = pbService.privateBrowsingEnabled;
setTimeout(this._emit, 0, isActive ? ON_START : ON_STOP);
setTimeout(this._emitEventObject, 0, isActive ? ON_START : ON_STOP, {},
exports);
},
get isActive() this._isActive,
set isActive(value) {
Expand Down
12 changes: 10 additions & 2 deletions packages/addon-kit/tests/test-private-browsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ if (pbService) {

exports.testStart = function(test) {
test.waitUntilDone();
pb.on("start", function onStart() {
pb.on("start", function onStart(evt) {
test.assertEqual(this, pb, "`this` should be private-browsing module");
test.assert(!!evt, "Listener should be passed event object");
test.assertEqual(evt.emitter, pb,
"event.emitter should be private-browsing module");
test.assert(pbService.privateBrowsingEnabled,
'private mode is active when "start" event is emitted');
test.assert(pb.isActive,
Expand All @@ -86,7 +90,11 @@ if (pbService) {

exports.testStop = function(test) {
test.waitUntilDone();
pb.on("stop", function onStop() {
pb.on("stop", function onStop(evt) {
test.assertEqual(this, pb, "`this` should be private-browsing module");
test.assert(!!evt, "Listener should be passed event object");
test.assertEqual(evt.emitter, pb,
"event.emitter should be private-browsing module");
test.assertEqual(pbService.privateBrowsingEnabled, false,
"private mode is disabled when stop event is emitted");
test.assertEqual(pb.isActive, false,
Expand Down

0 comments on commit 9d92356

Please sign in to comment.