User:Ehsan/PrivateBrowsingForExtensions: Difference between revisions

Jump to navigation Jump to search
Describe the private-browsing-enter notification
(→‎Code Samples: Add a sample for private-browsing-query)
(Describe the private-browsing-enter 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.
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.


If for some reason, you don't want to make your extension dependent on nsIPrivateBrowsingService (for example, when developing an extension to support both Firefox and Thunderbird, where the private browsing service is not available), you can use the <tt>private-browsing-query</tt> notification to query the current status of the private browsing mode.  In order to use this notification, a nsISupportsPRBool object must be passed as the subject parameter.  The private browsing service, if available, will handle this notification and return the status as a boolean value in the subject parameter.  If the private browsing service is not available, the boolean value remains <tt>false</tt>.  A sample of using this notification appears in the code samples below.
If for some reason, you don't want to make your extension dependent on nsIPrivateBrowsingService (for example, when developing an extension to support both Firefox and Thunderbird, where the private browsing service is not available), you can use the <tt>private-browsing-query</tt> notification to query the current status of the private browsing mode.  In order to use this notification, a nsISupportsPRBool object must be passed as the subject parameter.  The private browsing service, if available, will handle this notification and return the status as a boolean value in the subject parameter.  If the private browsing service is not available, the boolean value remains <tt>false</tt>.  A sample of using this notification appears in the code samples below.
Line 128: Line 130:
  // exit the Private Browsing mode
  // exit the Private Browsing mode
  pbs.privateBrowsing = false;
  pbs.privateBrowsing = false;
===Extensions that want to provide a custom UI to ask the user whether to keep the current session===
var os = Components.classes["@mozilla.org/observer-service;1"]
                    .getService(Components.interfaces.nsIObserverService);
os.addObserver(function (aSubject, aTopic, aData) {
    // somehow ask the user for his decision,
    // e.g., show a dialog box...
    if (/* user wants to close the current session */)
      aSubject.data = 1;
    else /* user wants to keep the current session */
      aSubject.data = 0;
  }, "private-browsing-enter", false);


===Extensions that want get the Private Browsing mode without depending on nsIPrivateBrowsingService===
===Extensions that want get the Private Browsing mode without depending on nsIPrivateBrowsingService===
Confirmed users
657

edits

Navigation menu