mozilla

Revision 103686 of Supporting private browsing mode

  • Revision slug: Supporting_private_browsing_mode
  • Revision title: Supporting private browsing mode
  • Revision id: 103686
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment 172 words added

Revision Content

{{ fx_minversion_header("3") }}

{{ draft() }}

Firefox 3.1 introduced private browsing mode, in which potentially private information is not recorded.  This includes cookies, history information, download information, and so forth.

Extensions that may record potentially private information may wish to hook into the private browsing service so that they can avoid saving personal information when private browsing mode is enabled.  Doing this is quite easy, using the {{ interface("nsIPrivateBrowsingService") }} interface.

Detecting private browsing mode

Determining whether or not the user is currently in private browsing mode is simple.  Just check the value of the privateBrowsingEnabled attribute on the {{ interface("nsIPrivateBrowsingService") }} service.

var pbs = Components.classes["@mozilla.org/privatebrowsing;1"]
                    .getService(Components.interfaces.nsIPrivateBrowsingService);
var inPrivateBrowsingMode = pbs.privateBrowsingEnabled;

if (!inPrivateBrowsingMode) {
  /* save private information */
}

In the above example, we only save the user's private information if not in private browsing mode.

Turning private browsing on and off

Extensions can turn private browsing mode on and off by manipulating the value of the privateBrowsingEnabled attribute.

var pbs = Components.classes["@mozilla.org/privatebrowsing;1"]
                    .getService(Components.interfaces.nsIPrivateBrowsingService);
var oldPrivateMode = pbs.privateBrowsingMode;

pbs.privateBrowsingEnabled = true;

/* this is all private */

pbs.privateBrowsingEnabled = oldPrivateMode;

In this example, we save the current state of the private browsing mode setting in the oldPrivateMode variable, then turn on private browsing by setting its value to true.  From that point until we restore the original value of the private browsing mode setting, things are done privately.  This allows us to, for example, work with the Places database without affecting its contents.

See also

  • {{ interface("nsIPrivateBrowsingService") }}

 

Revision Source

<p>{{ fx_minversion_header("3") }}</p>
<p>{{ draft() }}</p>
<p>Firefox 3.1 introduced private browsing mode, in which potentially private information is not recorded.  This includes cookies, history information, download information, and so forth.</p>
<p>Extensions that may record potentially private information may wish to hook into the private browsing service so that they can avoid saving personal information when private browsing mode is enabled.  Doing this is quite easy, using the {{ interface("nsIPrivateBrowsingService") }} interface.</p>
<h2>Detecting private browsing mode</h2>
<p>Determining whether or not the user is currently in private browsing mode is simple.  Just check the value of the <code>privateBrowsingEnabled</code> attribute on the {{ interface("nsIPrivateBrowsingService") }} service.</p>
<pre class="brush: js">var pbs = Components.classes["@mozilla.org/privatebrowsing;1"]
                    .getService(Components.interfaces.nsIPrivateBrowsingService);
var inPrivateBrowsingMode = pbs.privateBrowsingEnabled;

if (!inPrivateBrowsingMode) {
  /* save private information */
}</pre>
<p>In the above example, we only save the user's private information if not in private browsing mode.</p>
<h2>Turning private browsing on and off</h2>
<p>Extensions can turn private browsing mode on and off by manipulating the value of the <code>privateBrowsingEnabled</code> attribute.</p>
<pre class="brush: js">var pbs = Components.classes["@mozilla.org/privatebrowsing;1"]
                    .getService(Components.interfaces.nsIPrivateBrowsingService);
var oldPrivateMode = pbs.privateBrowsingMode;

pbs.privateBrowsingEnabled = true;

/* this is all private */

pbs.privateBrowsingEnabled = oldPrivateMode;
</pre>
<p>In this example, we save the current state of the private browsing mode setting in the <code>oldPrivateMode</code> variable, then turn on private browsing by setting its value to <code>true</code>.  From that point until we restore the original value of the private browsing mode setting, things are done privately.  This allows us to, for example, work with the <a class="internal" href="/En/Places" title="En/Places">Places</a> database without affecting its contents.</p>
<h2>See also</h2>
<ul> <li>{{ interface("nsIPrivateBrowsingService") }}</li>
</ul>
<p> </p>
Revert to this revision