User:Ehsan/PrivateBrowsingForExtensions: Difference between revisions

Document the private-browsing-request notification
(Remove the private-browsing-query notification)
(Document the private-browsing-request notification)
 
Line 21: Line 21:


The second code sample below implements a full blown listener for the private browsing mode, which can both be used to query its current status, and set a watcher to watch for entering and/or leaving the private mode.
The second code sample below implements a full blown listener for the private browsing mode, which can both be used to query its current status, and set a watcher to watch for entering and/or leaving the private mode.
Some extensions might benefit from preventing the user from entering the private browsing mode.  An example would be a download manager extension, which needs to ask the user if he wants to switch the private mode if a download is currently in progress.  Those extensions can handle the <tt>private-browsing-request</tt> notification.  This notification sends either <tt>enter</tt> or <tt>exit</tt> as the data parameter in order to indicate whether the user has requested entering or leaving the private browsing mode.  The subject parameter to this notification implements the <tt>nsISupportsPRBool</tt> interface.  If the extensions wants to prevent the user from entering or leaving the private browsing mode, it should set the <tt>data</tt> field of the subject parameter to <tt>true</tt>.  Extensions which handle the <tt>private-browsing-request</tt> notification are supposed to ignore this notification if the subject's <tt>data</tt> member equals <tt>true</tt> (which means that the user has already canceled the notification).
The code samples section below contains a sample of an extension which chooses to cancel leaving the private browsing mode (which is not a good practice; in the real worlds, the extension should just ask the user what she wants to do).


Another possibility for extensibility with the private browsing service is the <tt>private-browsing-enter</tt> notification.  This notification is sent by the private browsing service before entering the private mode, and is meant to give extension developers a chance to provide their own UI for handling the decision on whether to keep the current session open or save and close all the current windows and tabs, and then restore them when exiting the private browsing mode.  The subject parameter of this notification is a nsISupportsPRUint32 which is supposed to be set to a value which reflects the user's decision.  Returning 0 in the subject variable causes the browser to keep the current session, and returning 1 causes the current session to be saved and closed.  Returning any other value would trigger the default behavior, which is a dialog box shown to the user asking for his decision.  The fourth code sample below implements a custom handler for this notification.
Another possibility for extensibility with the private browsing service is the <tt>private-browsing-enter</tt> notification.  This notification is sent by the private browsing service before entering the private mode, and is meant to give extension developers a chance to provide their own UI for handling the decision on whether to keep the current session open or save and close all the current windows and tabs, and then restore them when exiting the private browsing mode.  The subject parameter of this notification is a nsISupportsPRUint32 which is supposed to be set to a value which reflects the user's decision.  Returning 0 in the subject variable causes the browser to keep the current session, and returning 1 causes the current session to be saved and closed.  Returning any other value would trigger the default behavior, which is a dialog box shown to the user asking for his decision.  The fourth code sample below implements a custom handler for this notification.
Line 143: Line 147:
       aSubject.data = 0;
       aSubject.data = 0;
   }, "private-browsing-enter", false);
   }, "private-browsing-enter", false);
===Extensions that want to prevent the user from leaving the private mode===
var os = Components.classes["@mozilla.org/observer-service;1"]
                    .getService(Components.interfaces.nsIObserverService);
os.addObserver(function (aSubject, aTopic, aData) {
    aSubject.QueryInterface(Components.interfaces.nsISupportsPRBool);
    // if another extension has not already canceled entering the private mode
    if (!aSubject.data) {
      if (aData == "exit") { // if we are leaving the private mode
        aSubject.data = true; // cancel the operation
      }
    }
  }, "private-browsing-request", false);
Confirmed users
657

edits