popupBlocker.js
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25  */
26 
27 //
28 // Popup Blocker Hat and PageReport Status Panel
29 //
30 
31 try
32 {
33 
34  // Module specific global for auto-init/deinit support
35  var popupBlocker = {};
36  popupBlocker.init_once = 0;
37  popupBlocker.deinit_once = 0;
38  popupBlocker.onLoad = function()
39  {
40  if (popupBlocker.init_once++) {
41  dump("WARNING: popupBlocker double init!!\n");
42  return;
43  }
44  if (typeof gBrowser == 'undefined') {
45  return;
46  }
47  gPopupBlockerObserver.init();
48  }
49  popupBlocker.onUnload = function()
50  {
51  if (popupBlocker.deinit_once++) {
52  dump("WARNING: popupBlocker double deinit!!\n");
53  return;
54  }
55  window.removeEventListener("load", popupBlocker.onLoad, false);
56  window.removeEventListener("unload", popupBlocker.onUnload, false);
57  if (typeof gBrowser == 'undefined') {
58  return;
59  }
60  gPopupBlockerObserver.shutdown();
61  }
62 
63  // Auto-init/deinit registration
64  window.addEventListener("load", popupBlocker.onLoad, false);
65  window.addEventListener("unload", popupBlocker.onUnload, false);
66 
67  var gPopupBlockerObserver = {
68  _reportButton: null,
69  _kIPM: Components.interfaces.nsIPermissionManager,
70  _prefService : null,
71 
72  _blockedPopupAllowSite: null,
73  _blockedPopupEditSettings: null,
74  _blockedPopupDontShowMessage: null,
75  _blockedPopupSeparator: null,
76  _blockedPopupMenu: null,
77 
78  _sbs: null,
79  _brandingStrings: null,
80  _browserStrings: null,
81  _prefStrings: null,
82 
83  init: function() {
84  this._sbs = Components.classes["@mozilla.org/intl/stringbundle;1"]
85  .getService(Components.interfaces.nsIStringBundleService);
86  this._brandingStrings = this._sbs.createBundle(
87  "chrome://branding/locale/brand.properties");
88  this._browserStrings = this._sbs.createBundle(
89  "chrome://browser/locale/browser.properties");
90  this._prefStrings = this._sbs.createBundle(
91  "chrome://browser/locale/preferences/preferences.properties");
92 
93  this._prefService = Components.classes["@mozilla.org/preferences-service;1"]
94  .getService(Components.interfaces.nsIPrefBranch);
95  gBrowser.addEventListener("DOMUpdatePageReport", gPopupBlockerObserver.onUpdatePageReport, false);
96  },
97 
98  shutdown: function() {
99  if (gBrowser) {
100  gBrowser.removeEventListener("DOMUpdatePageReport", gPopupBlockerObserver.onUpdatePageReport, false);
101  }
102  },
103 
104  onUpdatePageReport: function() {
105  if (!gPopupBlockerObserver._reportButton) {
106  gPopupBlockerObserver._reportButton = document.getElementById("page-report-button");
107  }
108  if (gBrowser.selectedBrowser.pageReport) {
109  gPopupBlockerObserver._reportButton.setAttribute("blocked", "true");
110  gPopupBlockerObserver.showNotification();
111  } else {
112  gPopupBlockerObserver._reportButton.removeAttribute("blocked");
113  }
114  },
115 
116  getElements: function(menu) {
117  this._blockedPopupAllowSite = menu.getElementsByAttribute("sbid", "blockedPopupAllowSite")[0];
118  this._blockedPopupEditSettings = menu.getElementsByAttribute("sbid", "blockedPopupEditSettings")[0];
119  this._blockedPopupDontShowMessage = menu.getElementsByAttribute("sbid", "blockedPopupDontShowMessage")[0];
120  this._blockedPopupSeparator = menu.getElementsByAttribute("sbid", "blockedPopupSeparator")[0];
121  },
122 
123  showNotification: function() {
124  if (this._prefService.getBoolPref("privacy.popups.showBrowserMessage")) {
125  var pageReport = gBrowser.selectedBrowser.pageReport;
126  var popupCount = pageReport.length;
127 
128  var brandShortName = "Songbird";
129  try {
130  brandShortName = this._brandingStrings.GetStringFromName("brandShortName");
131  } catch (e) {}
132 
133  var message;
134  if (popupCount > 1)
135  message = "%S prevented this site from opening %S popup windows.";
136  else
137  message = "%S prevented this site from opening a popup window.";
138  try {
139  if (popupCount > 1)
140  message = this._browserStrings.GetStringFromName("popupWarningMultiple");
141  else
142  message = this._browserStrings.GetStringFromName("popupWarning");
143  } catch(e) { }
144  message = message.replace("%S", brandShortName);
145  if (popupCount > 1) message = message.replace("%S", popupCount);
146 
147  var notificationName = "popup-blocked";
148  var iconURL = "chrome://songbird/skin/notification-bar/icon-blocked-popup.png";
149  var optionsLabel, optionsAccessKey;
150  optionsLabel = "Options";
151  optionsAccessKey = "O";
152  try {
153  optionsLabel = this._browserStrings.getString("popupWarningButton");
154  } catch (e) {}
155  try {
156  optionsAccessKey =
157  this._browserStrings.getString("popupWarningButton.accesskey");
158  } catch (e) {}
159 
160  var button = [
161  {
162  label: optionsLabel,
163  accessKey: optionsAccessKey,
164  popup: "blockedPopupOptions",
165  callback: null
166  }
167  ];
168 
169  var browser = gBrowser.selectedBrowser;
170  var notificationBox = browser.parentNode;
171  const priority = notificationBox.PRIORITY_WARNING_MEDIUM;
172  gBrowser.showNotification(browser,
173  "popup-blocked",
174  message,
175  iconURL,
176  priority,
177  button);
178  }
179  },
180 
181  fillPopupList: function(aEvent) {
182  this.getElements(aEvent.target);
183 
184  var uri = gBrowser.selectedBrowser.webNavigation.currentURI;
185  try {
186  this._blockedPopupAllowSite.hidden = false;
187  var pm = Components.classes["@mozilla.org/permissionmanager;1"]
188  .getService(this._kIPM);
189  if (pm.testPermission(uri, "popup") == this._kIPM.ALLOW_ACTION) {
190  // Offer an item to block popups for this site, if a whitelist entry exists
191  // already for it.
192  var blockString = this._browserStrings.GetStringFromName("popupBlock").replace("%S", uri.host);
193  this._blockedPopupAllowSite.setAttribute("label", blockString);
194  this._blockedPopupAllowSite.setAttribute("block", "true");
195  }
196  else {
197  // Offer an item to allow popups for this site
198  var allowString = this._browserStrings.GetStringFromName("popupAllow").replace("%S", uri.host);
199  this._blockedPopupAllowSite.setAttribute("label", allowString);
200  this._blockedPopupAllowSite.removeAttribute("block");
201  }
202  }
203  catch (e) {
204  this._blockedPopupAllowSite.hidden = true;
205  }
206 
207  while (aEvent.target.lastChild != this._blockedPopupSeparator)
208  aEvent.target.removeChild(aEvent.target.lastChild);
209 
210  var pageReport = gBrowser.selectedBrowser.pageReport;
211  if (pageReport && pageReport.length > 0) {
212  this._blockedPopupSeparator.hidden = false;
213  for (var i = 0; i < pageReport.length; ++i) {
214  var popupURIspec = pageReport[i].popupWindowURI.spec;
215  if (popupURIspec == "" || popupURIspec == "about:blank" ||
216  popupURIspec == uri.spec)
217  continue;
218 
219  var menuitem = document.createElement("menuitem");
220  menuitem.setAttribute("sbtype", "popupBlockerItem");
221  var label = this._browserStrings.GetStringFromName("popupShowPopupPrefix")
222  .replace("%S", popupURIspec);
223  menuitem.setAttribute("label", label);
224  menuitem.requestingWindow = pageReport[i].requestingWindow;
225  menuitem.requestingDocument = pageReport[i].requestingDocument;
226  menuitem.setAttribute("popupWindowURI", popupURIspec);
227  menuitem.setAttribute("popupWindowFeatures", pageReport[i].popupWindowFeatures);
228  menuitem.setAttribute("oncommand", "gPopupBlockerObserver.showBlockedPopup(event);");
229  aEvent.target.appendChild(menuitem);
230  }
231  }
232  else {
233  this._blockedPopupSeparator.hidden = true;
234  }
235 
236  var showMessage = true;
237  try {
238  showMessage = this._prefService.getBoolPref("privacy.popups.showBrowserMessage");
239  } catch (e) { }
240 
241  this._blockedPopupDontShowMessage.setAttribute("checked", !showMessage);
242  if (aEvent.target.localName == "popup") {
243  this._blockedPopupDontShowMessage.setAttribute("label",
244  this._browserStrings.GetStringFromName("popupWarningDontShowFromMessage"));
245  } else {
246  this._blockedPopupDontShowMessage.setAttribute("label",
247  this._browserStrings.GetStringFromName("popupWarningDontShowFromStatusbar"));
248  }
249  },
250 
251  showBlockedPopup: function(event) {
252  var target = event.target;
253  var popupWindowURI = target.getAttribute("popupWindowURI");
254  var features = target.getAttribute("popupWindowFeatures");
255  var name = target.getAttribute("popupWindowName");
256 
257  var dwi = target.requestingWindow;
258 
259  // If we have a requesting window and the requesting document is
260  // still the current document, open the popup.
261  if (dwi && dwi.document == target.requestingDocument) {
262  dwi.open(popupWindowURI, name, features);
263  }
264  },
265 
266  editPopupSettings: function () {
267  var host = "";
268  try {
269  var uri = gBrowser.selectedBrowser.webNavigation.currentURI;
270  host = uri.host;
271  }
272  catch (e) { }
273 
274  var params = { blockVisible : false,
275  sessionVisible : false,
276  allowVisible : true,
277  prefilledHost : host,
278  permissionType : "popup",
279  windowTitle : this._prefStrings.GetStringFromName("popuppermissionstitle"),
280  introText : this._prefStrings.GetStringFromName("popuppermissionstext") };
281  var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
282  .getService(Components.interfaces.nsIWindowMediator);
283  var existingWindow = wm.getMostRecentWindow("Browser:Permissions");
284  if (existingWindow) {
285  existingWindow.initWithParams(params);
286  existingWindow.focus();
287  }
288  else
289  window.openDialog("chrome://browser/content/preferences/permissions.xul",
290  "_blank", "resizable,dialog=no,centerscreen", params);
291  },
292 
293  dontShowMessage: function () {
294  var showMessage = true;
295  try {
296  showMessage = this._prefService.
297  getBoolPref("privacy.popups.showBrowserMessage");
298  } catch (e) {}
299  var firstTime = false;
300  try {
301  firstTime = this._prefService.
302  getBoolPref("privacy.popups.firstTime");
303  } catch (e) {}
304 
305  // If the info message is showing at the top of the window, and the user has never
306  // hidden the message before, show an info box telling the user where the info
307  // will be displayed.
308  if (showMessage && firstTime) {
309  window.openDialog("chrome://browser/content/pageReportFirstTime.xul", "_blank",
310  "dependent");
311  }
312 
313  this._prefService.setBoolPref("privacy.popups.showBrowserMessage", !showMessage);
314 
315  var browser = gBrowser.selectedBrowser;
316  var notificationBox = browser.parentNode;
317  var notification = notificationBox.getNotificationWithValue("popup-blocked");
318  if (notification)
319  notification.close();
320  },
321 
322  toggleAllowPopupsForSite: function(event) {
323  var currentURI = gBrowser.selectedBrowser.webNavigation.currentURI;
324  var pm = Components.classes["@mozilla.org/permissionmanager;1"]
325  .getService(this._kIPM);
326  var shouldBlock = event.target.getAttribute("block") == "true";
327  var perm = shouldBlock ? this._kIPM.DENY_ACTION : this._kIPM.ALLOW_ACTION;
328  pm.add(currentURI, "popup", perm);
329 
330  var browser = gBrowser.selectedBrowser;
331  var notificationBox = browser.parentNode;
332  var notification = notificationBox.getNotificationWithValue("popup-blocked");
333  if (notification)
334  notification.close();
335  }
336  }
337 }
338 catch (err)
339 {
340  dump("popupBlocker.js - " + err);
341 }
342 
343 
var event
var menu
function getBoolPref(prefname, def)
sbDownloadDeviceServicePaneModule prototype shutdown
let window
var firstTime
_window init
Definition: FeedWriter.js:1144
this _contentSandbox label
Definition: FeedWriter.js:814
grep callback
const gPopupBlockerObserver
Definition: browser.js:324
GstMessage * message
return null
Definition: FeedWriter.js:1143
var uri
Definition: FeedWriter.js:1135
var browser
Definition: openLocation.js:42
_getSelectedPageStyle s i