PageRenderTime 115ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 2ms

/browser/base/content/browser.js

http://github.com/zpao/v8monkey
JavaScript | 9169 lines | 6631 code | 1252 blank | 1286 comment | 1363 complexity | e72746e497ea254805b14fd1dcfe9bfe MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD
  1. # -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. #
  5. # The contents of this file are subject to the Mozilla Public License Version
  6. # 1.1 (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. # http://www.mozilla.org/MPL/
  9. #
  10. # Software distributed under the License is distributed on an "AS IS" basis,
  11. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. # for the specific language governing rights and limitations under the
  13. # License.
  14. #
  15. # The Original Code is mozilla.org code.
  16. #
  17. # The Initial Developer of the Original Code is
  18. # Netscape Communications Corporation.
  19. # Portions created by the Initial Developer are Copyright (C) 1998
  20. # the Initial Developer. All Rights Reserved.
  21. #
  22. # Contributor(s):
  23. # Blake Ross <blake@cs.stanford.edu>
  24. # David Hyatt <hyatt@mozilla.org>
  25. # Peter Annema <disttsc@bart.nl>
  26. # Dean Tessman <dean_tessman@hotmail.com>
  27. # Kevin Puetz <puetzk@iastate.edu>
  28. # Ben Goodger <ben@netscape.com>
  29. # Pierre Chanial <chanial@noos.fr>
  30. # Jason Eager <jce2@po.cwru.edu>
  31. # Joe Hewitt <hewitt@netscape.com>
  32. # Alec Flett <alecf@netscape.com>
  33. # Asaf Romano <mozilla.mano@sent.com>
  34. # Jason Barnabe <jason_barnabe@fastmail.fm>
  35. # Peter Parente <parente@cs.unc.edu>
  36. # Giorgio Maone <g.maone@informaction.com>
  37. # Tom Germeau <tom.germeau@epigoon.com>
  38. # Jesse Ruderman <jruderman@gmail.com>
  39. # Joe Hughes <joe@retrovirus.com>
  40. # Pamela Greene <pamg.bugs@gmail.com>
  41. # Michael Ventnor <m.ventnor@gmail.com>
  42. # Simon Bünzli <zeniko@gmail.com>
  43. # Johnathan Nightingale <johnath@mozilla.com>
  44. # Ehsan Akhgari <ehsan.akhgari@gmail.com>
  45. # Dão Gottwald <dao@mozilla.com>
  46. # Thomas K. Dyas <tdyas@zecador.org>
  47. # Edward Lee <edward.lee@engineering.uiuc.edu>
  48. # Paul OShannessy <paul@oshannessy.com>
  49. # Nils Maier <maierman@web.de>
  50. # Rob Arnold <robarnold@cmu.edu>
  51. # Dietrich Ayala <dietrich@mozilla.com>
  52. # Gavin Sharp <gavin@gavinsharp.com>
  53. # Justin Dolske <dolske@mozilla.com>
  54. # Rob Campbell <rcampbell@mozilla.com>
  55. # David Dahl <ddahl@mozilla.com>
  56. # Patrick Walton <pcwalton@mozilla.com>
  57. # Mihai Sucan <mihai.sucan@gmail.com>
  58. # Victor Porof <vporof@mozilla.com>
  59. #
  60. # Alternatively, the contents of this file may be used under the terms of
  61. # either the GNU General Public License Version 2 or later (the "GPL"), or
  62. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  63. # in which case the provisions of the GPL or the LGPL are applicable instead
  64. # of those above. If you wish to allow use of your version of this file only
  65. # under the terms of either the GPL or the LGPL, and not to allow others to
  66. # use your version of this file under the terms of the MPL, indicate your
  67. # decision by deleting the provisions above and replace them with the notice
  68. # and other provisions required by the GPL or the LGPL. If you do not delete
  69. # the provisions above, a recipient may use your version of this file under
  70. # the terms of any one of the MPL, the GPL or the LGPL.
  71. #
  72. # ***** END LICENSE BLOCK *****
  73. let Ci = Components.interfaces;
  74. let Cu = Components.utils;
  75. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  76. const nsIWebNavigation = Ci.nsIWebNavigation;
  77. var gCharsetMenu = null;
  78. var gLastBrowserCharset = null;
  79. var gPrevCharset = null;
  80. var gProxyFavIcon = null;
  81. var gLastValidURLStr = "";
  82. var gInPrintPreviewMode = false;
  83. var gDownloadMgr = null;
  84. var gContextMenu = null; // nsContextMenu instance
  85. var gDelayedStartupTimeoutId;
  86. var gStartupRan = false;
  87. #ifndef XP_MACOSX
  88. var gEditUIVisible = true;
  89. #endif
  90. [
  91. ["gBrowser", "content"],
  92. ["gNavToolbox", "navigator-toolbox"],
  93. ["gURLBar", "urlbar"],
  94. ["gNavigatorBundle", "bundle_browser"]
  95. ].forEach(function (elementGlobal) {
  96. var [name, id] = elementGlobal;
  97. window.__defineGetter__(name, function () {
  98. var element = document.getElementById(id);
  99. if (!element)
  100. return null;
  101. delete window[name];
  102. return window[name] = element;
  103. });
  104. window.__defineSetter__(name, function (val) {
  105. delete window[name];
  106. return window[name] = val;
  107. });
  108. });
  109. // Smart getter for the findbar. If you don't wish to force the creation of
  110. // the findbar, check gFindBarInitialized first.
  111. var gFindBarInitialized = false;
  112. XPCOMUtils.defineLazyGetter(window, "gFindBar", function() {
  113. let XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  114. let findbar = document.createElementNS(XULNS, "findbar");
  115. findbar.id = "FindToolbar";
  116. let browserBottomBox = document.getElementById("browser-bottombox");
  117. browserBottomBox.insertBefore(findbar, browserBottomBox.firstChild);
  118. // Force a style flush to ensure that our binding is attached.
  119. findbar.clientTop;
  120. findbar.browser = gBrowser;
  121. window.gFindBarInitialized = true;
  122. return findbar;
  123. });
  124. __defineGetter__("gPrefService", function() {
  125. delete this.gPrefService;
  126. return this.gPrefService = Services.prefs;
  127. });
  128. __defineGetter__("AddonManager", function() {
  129. let tmp = {};
  130. Cu.import("resource://gre/modules/AddonManager.jsm", tmp);
  131. return this.AddonManager = tmp.AddonManager;
  132. });
  133. __defineSetter__("AddonManager", function (val) {
  134. delete this.AddonManager;
  135. return this.AddonManager = val;
  136. });
  137. __defineGetter__("PluralForm", function() {
  138. Cu.import("resource://gre/modules/PluralForm.jsm");
  139. return this.PluralForm;
  140. });
  141. __defineSetter__("PluralForm", function (val) {
  142. delete this.PluralForm;
  143. return this.PluralForm = val;
  144. });
  145. #ifdef MOZ_SERVICES_SYNC
  146. XPCOMUtils.defineLazyGetter(this, "Weave", function() {
  147. let tmp = {};
  148. Cu.import("resource://services-sync/main.js", tmp);
  149. return tmp.Weave;
  150. });
  151. #endif
  152. XPCOMUtils.defineLazyGetter(this, "PopupNotifications", function () {
  153. let tmp = {};
  154. Cu.import("resource://gre/modules/PopupNotifications.jsm", tmp);
  155. try {
  156. return new tmp.PopupNotifications(gBrowser,
  157. document.getElementById("notification-popup"),
  158. document.getElementById("notification-popup-box"));
  159. } catch (ex) {
  160. Cu.reportError(ex);
  161. }
  162. });
  163. XPCOMUtils.defineLazyGetter(this, "InspectorUI", function() {
  164. let tmp = {};
  165. Cu.import("resource:///modules/inspector.jsm", tmp);
  166. return new tmp.InspectorUI(window);
  167. });
  168. XPCOMUtils.defineLazyGetter(this, "DebuggerUI", function() {
  169. let tmp = {};
  170. Cu.import("resource:///modules/devtools/DebuggerUI.jsm", tmp);
  171. return new tmp.DebuggerUI(window);
  172. });
  173. XPCOMUtils.defineLazyGetter(this, "Tilt", function() {
  174. let tmp = {};
  175. Cu.import("resource:///modules/devtools/Tilt.jsm", tmp);
  176. return new tmp.Tilt(window);
  177. });
  178. let gInitialPages = [
  179. "about:blank",
  180. "about:newtab",
  181. "about:privatebrowsing",
  182. "about:sessionrestore"
  183. ];
  184. #include browser-fullZoom.js
  185. #include browser-places.js
  186. #include browser-tabPreviews.js
  187. #include browser-tabview.js
  188. #include browser-thumbnails.js
  189. #ifdef MOZ_SERVICES_SYNC
  190. #include browser-syncui.js
  191. #endif
  192. XPCOMUtils.defineLazyGetter(this, "Win7Features", function () {
  193. #ifdef XP_WIN
  194. const WINTASKBAR_CONTRACTID = "@mozilla.org/windows-taskbar;1";
  195. if (WINTASKBAR_CONTRACTID in Cc &&
  196. Cc[WINTASKBAR_CONTRACTID].getService(Ci.nsIWinTaskbar).available) {
  197. let temp = {};
  198. Cu.import("resource:///modules/WindowsPreviewPerTab.jsm", temp);
  199. let AeroPeek = temp.AeroPeek;
  200. return {
  201. onOpenWindow: function () {
  202. AeroPeek.onOpenWindow(window);
  203. },
  204. onCloseWindow: function () {
  205. AeroPeek.onCloseWindow(window);
  206. }
  207. };
  208. }
  209. #endif
  210. return null;
  211. });
  212. #ifdef MOZ_CRASHREPORTER
  213. XPCOMUtils.defineLazyServiceGetter(this, "gCrashReporter",
  214. "@mozilla.org/xre/app-info;1",
  215. "nsICrashReporter");
  216. #endif
  217. XPCOMUtils.defineLazyGetter(this, "PageMenu", function() {
  218. let tmp = {};
  219. Cu.import("resource://gre/modules/PageMenu.jsm", tmp);
  220. return new tmp.PageMenu();
  221. });
  222. /**
  223. * We can avoid adding multiple load event listeners and save some time by adding
  224. * one listener that calls all real handlers.
  225. */
  226. function pageShowEventHandlers(event) {
  227. // Filter out events that are not about the document load we are interested in
  228. if (event.originalTarget == content.document) {
  229. charsetLoadListener(event);
  230. XULBrowserWindow.asyncUpdateUI();
  231. }
  232. }
  233. function UpdateBackForwardCommands(aWebNavigation) {
  234. var backBroadcaster = document.getElementById("Browser:Back");
  235. var forwardBroadcaster = document.getElementById("Browser:Forward");
  236. // Avoid setting attributes on broadcasters if the value hasn't changed!
  237. // Remember, guys, setting attributes on elements is expensive! They
  238. // get inherited into anonymous content, broadcast to other widgets, etc.!
  239. // Don't do it if the value hasn't changed! - dwh
  240. var backDisabled = backBroadcaster.hasAttribute("disabled");
  241. var forwardDisabled = forwardBroadcaster.hasAttribute("disabled");
  242. if (backDisabled == aWebNavigation.canGoBack) {
  243. if (backDisabled)
  244. backBroadcaster.removeAttribute("disabled");
  245. else
  246. backBroadcaster.setAttribute("disabled", true);
  247. }
  248. if (forwardDisabled == aWebNavigation.canGoForward) {
  249. if (forwardDisabled)
  250. forwardBroadcaster.removeAttribute("disabled");
  251. else
  252. forwardBroadcaster.setAttribute("disabled", true);
  253. }
  254. }
  255. /**
  256. * Click-and-Hold implementation for the Back and Forward buttons
  257. * XXXmano: should this live in toolbarbutton.xml?
  258. */
  259. function SetClickAndHoldHandlers() {
  260. var timer;
  261. function openMenu(aButton) {
  262. cancelHold(aButton);
  263. aButton.firstChild.hidden = false;
  264. aButton.open = true;
  265. }
  266. function mousedownHandler(aEvent) {
  267. if (aEvent.button != 0 ||
  268. aEvent.currentTarget.open ||
  269. aEvent.currentTarget.disabled)
  270. return;
  271. // Prevent the menupopup from opening immediately
  272. aEvent.currentTarget.firstChild.hidden = true;
  273. aEvent.currentTarget.addEventListener("mouseout", mouseoutHandler, false);
  274. aEvent.currentTarget.addEventListener("mouseup", mouseupHandler, false);
  275. timer = setTimeout(openMenu, 500, aEvent.currentTarget);
  276. }
  277. function mouseoutHandler(aEvent) {
  278. let buttonRect = aEvent.currentTarget.getBoundingClientRect();
  279. if (aEvent.clientX >= buttonRect.left &&
  280. aEvent.clientX <= buttonRect.right &&
  281. aEvent.clientY >= buttonRect.bottom)
  282. openMenu(aEvent.currentTarget);
  283. else
  284. cancelHold(aEvent.currentTarget);
  285. }
  286. function mouseupHandler(aEvent) {
  287. cancelHold(aEvent.currentTarget);
  288. }
  289. function cancelHold(aButton) {
  290. clearTimeout(timer);
  291. aButton.removeEventListener("mouseout", mouseoutHandler, false);
  292. aButton.removeEventListener("mouseup", mouseupHandler, false);
  293. }
  294. function clickHandler(aEvent) {
  295. if (aEvent.button == 0 &&
  296. aEvent.target == aEvent.currentTarget &&
  297. !aEvent.currentTarget.open &&
  298. !aEvent.currentTarget.disabled) {
  299. let cmdEvent = document.createEvent("xulcommandevent");
  300. cmdEvent.initCommandEvent("command", true, true, window, 0,
  301. aEvent.ctrlKey, aEvent.altKey, aEvent.shiftKey,
  302. aEvent.metaKey, null);
  303. aEvent.currentTarget.dispatchEvent(cmdEvent);
  304. }
  305. }
  306. function _addClickAndHoldListenersOnElement(aElm) {
  307. aElm.addEventListener("mousedown", mousedownHandler, true);
  308. aElm.addEventListener("click", clickHandler, true);
  309. }
  310. // Bug 414797: Clone unified-back-forward-button's context menu into both the
  311. // back and the forward buttons.
  312. var unifiedButton = document.getElementById("unified-back-forward-button");
  313. if (unifiedButton && !unifiedButton._clickHandlersAttached) {
  314. unifiedButton._clickHandlersAttached = true;
  315. let popup = document.getElementById("backForwardMenu").cloneNode(true);
  316. popup.removeAttribute("id");
  317. // Prevent the context attribute on unified-back-forward-button from being
  318. // inherited.
  319. popup.setAttribute("context", "");
  320. let backButton = document.getElementById("back-button");
  321. backButton.setAttribute("type", "menu");
  322. backButton.appendChild(popup);
  323. _addClickAndHoldListenersOnElement(backButton);
  324. let forwardButton = document.getElementById("forward-button");
  325. popup = popup.cloneNode(true);
  326. forwardButton.setAttribute("type", "menu");
  327. forwardButton.appendChild(popup);
  328. _addClickAndHoldListenersOnElement(forwardButton);
  329. }
  330. }
  331. const gSessionHistoryObserver = {
  332. observe: function(subject, topic, data)
  333. {
  334. if (topic != "browser:purge-session-history")
  335. return;
  336. var backCommand = document.getElementById("Browser:Back");
  337. backCommand.setAttribute("disabled", "true");
  338. var fwdCommand = document.getElementById("Browser:Forward");
  339. fwdCommand.setAttribute("disabled", "true");
  340. // Hide session restore button on about:home
  341. window.messageManager.sendAsyncMessage("Browser:HideSessionRestoreButton");
  342. if (gURLBar) {
  343. // Clear undo history of the URL bar
  344. gURLBar.editor.transactionManager.clear()
  345. }
  346. }
  347. };
  348. /**
  349. * Given a starting docshell and a URI to look up, find the docshell the URI
  350. * is loaded in.
  351. * @param aDocument
  352. * A document to find instead of using just a URI - this is more specific.
  353. * @param aDocShell
  354. * The doc shell to start at
  355. * @param aSoughtURI
  356. * The URI that we're looking for
  357. * @returns The doc shell that the sought URI is loaded in. Can be in
  358. * subframes.
  359. */
  360. function findChildShell(aDocument, aDocShell, aSoughtURI) {
  361. aDocShell.QueryInterface(Components.interfaces.nsIWebNavigation);
  362. aDocShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  363. var doc = aDocShell.getInterface(Components.interfaces.nsIDOMDocument);
  364. if ((aDocument && doc == aDocument) ||
  365. (aSoughtURI && aSoughtURI.spec == aDocShell.currentURI.spec))
  366. return aDocShell;
  367. var node = aDocShell.QueryInterface(Components.interfaces.nsIDocShellTreeNode);
  368. for (var i = 0; i < node.childCount; ++i) {
  369. var docShell = node.getChildAt(i);
  370. docShell = findChildShell(aDocument, docShell, aSoughtURI);
  371. if (docShell)
  372. return docShell;
  373. }
  374. return null;
  375. }
  376. var gPopupBlockerObserver = {
  377. _reportButton: null,
  378. onReportButtonClick: function (aEvent)
  379. {
  380. if (aEvent.button != 0 || aEvent.target != this._reportButton)
  381. return;
  382. document.getElementById("blockedPopupOptions")
  383. .openPopup(this._reportButton, "after_end", 0, 2, false, false, aEvent);
  384. },
  385. handleEvent: function (aEvent)
  386. {
  387. if (aEvent.originalTarget != gBrowser.selectedBrowser)
  388. return;
  389. if (!this._reportButton && gURLBar)
  390. this._reportButton = document.getElementById("page-report-button");
  391. if (!gBrowser.pageReport) {
  392. // Hide the icon in the location bar (if the location bar exists)
  393. if (gURLBar)
  394. this._reportButton.hidden = true;
  395. return;
  396. }
  397. if (gURLBar)
  398. this._reportButton.hidden = false;
  399. // Only show the notification again if we've not already shown it. Since
  400. // notifications are per-browser, we don't need to worry about re-adding
  401. // it.
  402. if (!gBrowser.pageReport.reported) {
  403. if (gPrefService.getBoolPref("privacy.popups.showBrowserMessage")) {
  404. var brandBundle = document.getElementById("bundle_brand");
  405. var brandShortName = brandBundle.getString("brandShortName");
  406. var message;
  407. var popupCount = gBrowser.pageReport.length;
  408. #ifdef XP_WIN
  409. var popupButtonText = gNavigatorBundle.getString("popupWarningButton");
  410. var popupButtonAccesskey = gNavigatorBundle.getString("popupWarningButton.accesskey");
  411. #else
  412. var popupButtonText = gNavigatorBundle.getString("popupWarningButtonUnix");
  413. var popupButtonAccesskey = gNavigatorBundle.getString("popupWarningButtonUnix.accesskey");
  414. #endif
  415. if (popupCount > 1)
  416. message = gNavigatorBundle.getFormattedString("popupWarningMultiple", [brandShortName, popupCount]);
  417. else
  418. message = gNavigatorBundle.getFormattedString("popupWarning", [brandShortName]);
  419. var notificationBox = gBrowser.getNotificationBox();
  420. var notification = notificationBox.getNotificationWithValue("popup-blocked");
  421. if (notification) {
  422. notification.label = message;
  423. }
  424. else {
  425. var buttons = [{
  426. label: popupButtonText,
  427. accessKey: popupButtonAccesskey,
  428. popup: "blockedPopupOptions",
  429. callback: null
  430. }];
  431. const priority = notificationBox.PRIORITY_WARNING_MEDIUM;
  432. notificationBox.appendNotification(message, "popup-blocked",
  433. "chrome://browser/skin/Info.png",
  434. priority, buttons);
  435. }
  436. }
  437. // Record the fact that we've reported this blocked popup, so we don't
  438. // show it again.
  439. gBrowser.pageReport.reported = true;
  440. }
  441. },
  442. toggleAllowPopupsForSite: function (aEvent)
  443. {
  444. var pm = Services.perms;
  445. var shouldBlock = aEvent.target.getAttribute("block") == "true";
  446. var perm = shouldBlock ? pm.DENY_ACTION : pm.ALLOW_ACTION;
  447. pm.add(gBrowser.currentURI, "popup", perm);
  448. gBrowser.getNotificationBox().removeCurrentNotification();
  449. },
  450. fillPopupList: function (aEvent)
  451. {
  452. // XXXben - rather than using |currentURI| here, which breaks down on multi-framed sites
  453. // we should really walk the pageReport and create a list of "allow for <host>"
  454. // menuitems for the common subset of hosts present in the report, this will
  455. // make us frame-safe.
  456. //
  457. // XXXjst - Note that when this is fixed to work with multi-framed sites,
  458. // also back out the fix for bug 343772 where
  459. // nsGlobalWindow::CheckOpenAllow() was changed to also
  460. // check if the top window's location is whitelisted.
  461. var uri = gBrowser.currentURI;
  462. var blockedPopupAllowSite = document.getElementById("blockedPopupAllowSite");
  463. try {
  464. blockedPopupAllowSite.removeAttribute("hidden");
  465. var pm = Services.perms;
  466. if (pm.testPermission(uri, "popup") == pm.ALLOW_ACTION) {
  467. // Offer an item to block popups for this site, if a whitelist entry exists
  468. // already for it.
  469. let blockString = gNavigatorBundle.getFormattedString("popupBlock", [uri.host]);
  470. blockedPopupAllowSite.setAttribute("label", blockString);
  471. blockedPopupAllowSite.setAttribute("block", "true");
  472. }
  473. else {
  474. // Offer an item to allow popups for this site
  475. let allowString = gNavigatorBundle.getFormattedString("popupAllow", [uri.host]);
  476. blockedPopupAllowSite.setAttribute("label", allowString);
  477. blockedPopupAllowSite.removeAttribute("block");
  478. }
  479. }
  480. catch (e) {
  481. blockedPopupAllowSite.setAttribute("hidden", "true");
  482. }
  483. if (gPrivateBrowsingUI.privateBrowsingEnabled)
  484. blockedPopupAllowSite.setAttribute("disabled", "true");
  485. else
  486. blockedPopupAllowSite.removeAttribute("disabled");
  487. var foundUsablePopupURI = false;
  488. var pageReport = gBrowser.pageReport;
  489. if (pageReport) {
  490. for (var i = 0; i < pageReport.length; ++i) {
  491. // popupWindowURI will be null if the file picker popup is blocked.
  492. // xxxdz this should make the option say "Show file picker" and do it (Bug 590306)
  493. if (!pageReport[i].popupWindowURI)
  494. continue;
  495. var popupURIspec = pageReport[i].popupWindowURI.spec;
  496. // Sometimes the popup URI that we get back from the pageReport
  497. // isn't useful (for instance, netscape.com's popup URI ends up
  498. // being "http://www.netscape.com", which isn't really the URI of
  499. // the popup they're trying to show). This isn't going to be
  500. // useful to the user, so we won't create a menu item for it.
  501. if (popupURIspec == "" || popupURIspec == "about:blank" ||
  502. popupURIspec == uri.spec)
  503. continue;
  504. // Because of the short-circuit above, we may end up in a situation
  505. // in which we don't have any usable popup addresses to show in
  506. // the menu, and therefore we shouldn't show the separator. However,
  507. // since we got past the short-circuit, we must've found at least
  508. // one usable popup URI and thus we'll turn on the separator later.
  509. foundUsablePopupURI = true;
  510. var menuitem = document.createElement("menuitem");
  511. var label = gNavigatorBundle.getFormattedString("popupShowPopupPrefix",
  512. [popupURIspec]);
  513. menuitem.setAttribute("label", label);
  514. menuitem.setAttribute("popupWindowURI", popupURIspec);
  515. menuitem.setAttribute("popupWindowFeatures", pageReport[i].popupWindowFeatures);
  516. menuitem.setAttribute("popupWindowName", pageReport[i].popupWindowName);
  517. menuitem.setAttribute("oncommand", "gPopupBlockerObserver.showBlockedPopup(event);");
  518. menuitem.requestingWindow = pageReport[i].requestingWindow;
  519. menuitem.requestingDocument = pageReport[i].requestingDocument;
  520. aEvent.target.appendChild(menuitem);
  521. }
  522. }
  523. // Show or hide the separator, depending on whether we added any
  524. // showable popup addresses to the menu.
  525. var blockedPopupsSeparator =
  526. document.getElementById("blockedPopupsSeparator");
  527. if (foundUsablePopupURI)
  528. blockedPopupsSeparator.removeAttribute("hidden");
  529. else
  530. blockedPopupsSeparator.setAttribute("hidden", true);
  531. var blockedPopupDontShowMessage = document.getElementById("blockedPopupDontShowMessage");
  532. var showMessage = gPrefService.getBoolPref("privacy.popups.showBrowserMessage");
  533. blockedPopupDontShowMessage.setAttribute("checked", !showMessage);
  534. if (aEvent.target.anchorNode.id == "page-report-button") {
  535. aEvent.target.anchorNode.setAttribute("open", "true");
  536. blockedPopupDontShowMessage.setAttribute("label", gNavigatorBundle.getString("popupWarningDontShowFromLocationbar"));
  537. } else
  538. blockedPopupDontShowMessage.setAttribute("label", gNavigatorBundle.getString("popupWarningDontShowFromMessage"));
  539. },
  540. onPopupHiding: function (aEvent) {
  541. if (aEvent.target.anchorNode.id == "page-report-button")
  542. aEvent.target.anchorNode.removeAttribute("open");
  543. let item = aEvent.target.lastChild;
  544. while (item && item.getAttribute("observes") != "blockedPopupsSeparator") {
  545. let next = item.previousSibling;
  546. item.parentNode.removeChild(item);
  547. item = next;
  548. }
  549. },
  550. showBlockedPopup: function (aEvent)
  551. {
  552. var target = aEvent.target;
  553. var popupWindowURI = target.getAttribute("popupWindowURI");
  554. var features = target.getAttribute("popupWindowFeatures");
  555. var name = target.getAttribute("popupWindowName");
  556. var dwi = target.requestingWindow;
  557. // If we have a requesting window and the requesting document is
  558. // still the current document, open the popup.
  559. if (dwi && dwi.document == target.requestingDocument) {
  560. dwi.open(popupWindowURI, name, features);
  561. }
  562. },
  563. editPopupSettings: function ()
  564. {
  565. var host = "";
  566. try {
  567. host = gBrowser.currentURI.host;
  568. }
  569. catch (e) { }
  570. var bundlePreferences = document.getElementById("bundle_preferences");
  571. var params = { blockVisible : false,
  572. sessionVisible : false,
  573. allowVisible : true,
  574. prefilledHost : host,
  575. permissionType : "popup",
  576. windowTitle : bundlePreferences.getString("popuppermissionstitle"),
  577. introText : bundlePreferences.getString("popuppermissionstext") };
  578. var existingWindow = Services.wm.getMostRecentWindow("Browser:Permissions");
  579. if (existingWindow) {
  580. existingWindow.initWithParams(params);
  581. existingWindow.focus();
  582. }
  583. else
  584. window.openDialog("chrome://browser/content/preferences/permissions.xul",
  585. "_blank", "resizable,dialog=no,centerscreen", params);
  586. },
  587. dontShowMessage: function ()
  588. {
  589. var showMessage = gPrefService.getBoolPref("privacy.popups.showBrowserMessage");
  590. gPrefService.setBoolPref("privacy.popups.showBrowserMessage", !showMessage);
  591. gBrowser.getNotificationBox().removeCurrentNotification();
  592. }
  593. };
  594. const gXPInstallObserver = {
  595. _findChildShell: function (aDocShell, aSoughtShell)
  596. {
  597. if (aDocShell == aSoughtShell)
  598. return aDocShell;
  599. var node = aDocShell.QueryInterface(Components.interfaces.nsIDocShellTreeNode);
  600. for (var i = 0; i < node.childCount; ++i) {
  601. var docShell = node.getChildAt(i);
  602. docShell = this._findChildShell(docShell, aSoughtShell);
  603. if (docShell == aSoughtShell)
  604. return docShell;
  605. }
  606. return null;
  607. },
  608. _getBrowser: function (aDocShell)
  609. {
  610. for (var i = 0; i < gBrowser.browsers.length; ++i) {
  611. var browser = gBrowser.getBrowserAtIndex(i);
  612. if (this._findChildShell(browser.docShell, aDocShell))
  613. return browser;
  614. }
  615. return null;
  616. },
  617. observe: function (aSubject, aTopic, aData)
  618. {
  619. var brandBundle = document.getElementById("bundle_brand");
  620. var installInfo = aSubject.QueryInterface(Components.interfaces.amIWebInstallInfo);
  621. var win = installInfo.originatingWindow;
  622. var shell = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  623. .getInterface(Components.interfaces.nsIWebNavigation)
  624. .QueryInterface(Components.interfaces.nsIDocShell);
  625. var browser = this._getBrowser(shell);
  626. if (!browser)
  627. return;
  628. const anchorID = "addons-notification-icon";
  629. var messageString, action;
  630. var brandShortName = brandBundle.getString("brandShortName");
  631. var notificationID = aTopic;
  632. // Make notifications persist a minimum of 30 seconds
  633. var options = {
  634. timeout: Date.now() + 30000
  635. };
  636. switch (aTopic) {
  637. case "addon-install-disabled":
  638. notificationID = "xpinstall-disabled"
  639. if (gPrefService.prefIsLocked("xpinstall.enabled")) {
  640. messageString = gNavigatorBundle.getString("xpinstallDisabledMessageLocked");
  641. buttons = [];
  642. }
  643. else {
  644. messageString = gNavigatorBundle.getString("xpinstallDisabledMessage");
  645. action = {
  646. label: gNavigatorBundle.getString("xpinstallDisabledButton"),
  647. accessKey: gNavigatorBundle.getString("xpinstallDisabledButton.accesskey"),
  648. callback: function editPrefs() {
  649. gPrefService.setBoolPref("xpinstall.enabled", true);
  650. }
  651. };
  652. }
  653. PopupNotifications.show(browser, notificationID, messageString, anchorID,
  654. action, null, options);
  655. break;
  656. case "addon-install-blocked":
  657. messageString = gNavigatorBundle.getFormattedString("xpinstallPromptWarning",
  658. [brandShortName, installInfo.originatingURI.host]);
  659. action = {
  660. label: gNavigatorBundle.getString("xpinstallPromptAllowButton"),
  661. accessKey: gNavigatorBundle.getString("xpinstallPromptAllowButton.accesskey"),
  662. callback: function() {
  663. installInfo.install();
  664. }
  665. };
  666. PopupNotifications.show(browser, notificationID, messageString, anchorID,
  667. action, null, options);
  668. break;
  669. case "addon-install-started":
  670. function needsDownload(aInstall) {
  671. return aInstall.state != AddonManager.STATE_DOWNLOADED;
  672. }
  673. // If all installs have already been downloaded then there is no need to
  674. // show the download progress
  675. if (!installInfo.installs.some(needsDownload))
  676. return;
  677. notificationID = "addon-progress";
  678. messageString = gNavigatorBundle.getString("addonDownloading");
  679. messageString = PluralForm.get(installInfo.installs.length, messageString);
  680. options.installs = installInfo.installs;
  681. options.contentWindow = browser.contentWindow;
  682. options.sourceURI = browser.currentURI;
  683. options.eventCallback = function(aEvent) {
  684. if (aEvent != "removed")
  685. return;
  686. options.contentWindow = null;
  687. options.sourceURI = null;
  688. };
  689. PopupNotifications.show(browser, notificationID, messageString, anchorID,
  690. null, null, options);
  691. break;
  692. case "addon-install-failed":
  693. // TODO This isn't terribly ideal for the multiple failure case
  694. installInfo.installs.forEach(function(aInstall) {
  695. var host = (installInfo.originatingURI instanceof Ci.nsIStandardURL) &&
  696. installInfo.originatingURI.host;
  697. if (!host)
  698. host = (aInstall.sourceURI instanceof Ci.nsIStandardURL) &&
  699. aInstall.sourceURI.host;
  700. var error = (host || aInstall.error == 0) ? "addonError" : "addonLocalError";
  701. if (aInstall.error != 0)
  702. error += aInstall.error;
  703. else if (aInstall.addon.blocklistState == Ci.nsIBlocklistService.STATE_BLOCKED)
  704. error += "Blocklisted";
  705. else
  706. error += "Incompatible";
  707. messageString = gNavigatorBundle.getString(error);
  708. messageString = messageString.replace("#1", aInstall.name);
  709. if (host)
  710. messageString = messageString.replace("#2", host);
  711. messageString = messageString.replace("#3", brandShortName);
  712. messageString = messageString.replace("#4", Services.appinfo.version);
  713. PopupNotifications.show(browser, notificationID, messageString, anchorID,
  714. action, null, options);
  715. });
  716. break;
  717. case "addon-install-complete":
  718. var needsRestart = installInfo.installs.some(function(i) {
  719. return i.addon.pendingOperations != AddonManager.PENDING_NONE;
  720. });
  721. if (needsRestart) {
  722. messageString = gNavigatorBundle.getString("addonsInstalledNeedsRestart");
  723. action = {
  724. label: gNavigatorBundle.getString("addonInstallRestartButton"),
  725. accessKey: gNavigatorBundle.getString("addonInstallRestartButton.accesskey"),
  726. callback: function() {
  727. Application.restart();
  728. }
  729. };
  730. }
  731. else {
  732. messageString = gNavigatorBundle.getString("addonsInstalled");
  733. action = {
  734. label: gNavigatorBundle.getString("addonInstallManage"),
  735. accessKey: gNavigatorBundle.getString("addonInstallManage.accesskey"),
  736. callback: function() {
  737. // Calculate the add-on type that is most popular in the list of
  738. // installs
  739. var types = {};
  740. var bestType = null;
  741. installInfo.installs.forEach(function(aInstall) {
  742. if (aInstall.type in types)
  743. types[aInstall.type]++;
  744. else
  745. types[aInstall.type] = 1;
  746. if (!bestType || types[aInstall.type] > types[bestType])
  747. bestType = aInstall.type;
  748. });
  749. BrowserOpenAddonsMgr("addons://list/" + bestType);
  750. }
  751. };
  752. }
  753. messageString = PluralForm.get(installInfo.installs.length, messageString);
  754. messageString = messageString.replace("#1", installInfo.installs[0].name);
  755. messageString = messageString.replace("#2", installInfo.installs.length);
  756. messageString = messageString.replace("#3", brandShortName);
  757. // Remove notificaion on dismissal, since it's possible to cancel the
  758. // install through the addons manager UI, making the "restart" prompt
  759. // irrelevant.
  760. options.removeOnDismissal = true;
  761. PopupNotifications.show(browser, notificationID, messageString, anchorID,
  762. action, null, options);
  763. break;
  764. }
  765. }
  766. };
  767. const gFormSubmitObserver = {
  768. QueryInterface : XPCOMUtils.generateQI([Ci.nsIFormSubmitObserver]),
  769. panel: null,
  770. init: function()
  771. {
  772. this.panel = document.getElementById('invalid-form-popup');
  773. },
  774. panelIsOpen: function()
  775. {
  776. return this.panel && this.panel.state != "hiding" &&
  777. this.panel.state != "closed";
  778. },
  779. notifyInvalidSubmit : function (aFormElement, aInvalidElements)
  780. {
  781. // We are going to handle invalid form submission attempt by focusing the
  782. // first invalid element and show the corresponding validation message in a
  783. // panel attached to the element.
  784. if (!aInvalidElements.length) {
  785. return;
  786. }
  787. // Don't show the popup if the current tab doesn't contain the invalid form.
  788. if (gBrowser.contentDocument !=
  789. aFormElement.ownerDocument.defaultView.top.document) {
  790. return;
  791. }
  792. let element = aInvalidElements.queryElementAt(0, Ci.nsISupports);
  793. if (!(element instanceof HTMLInputElement ||
  794. element instanceof HTMLTextAreaElement ||
  795. element instanceof HTMLSelectElement ||
  796. element instanceof HTMLButtonElement)) {
  797. return;
  798. }
  799. this.panel.firstChild.textContent = element.validationMessage;
  800. element.focus();
  801. // If the user interacts with the element and makes it valid or leaves it,
  802. // we want to remove the popup.
  803. // We could check for clicks but a click is already removing the popup.
  804. function blurHandler() {
  805. gFormSubmitObserver.panel.hidePopup();
  806. };
  807. function inputHandler(e) {
  808. if (e.originalTarget.validity.valid) {
  809. gFormSubmitObserver.panel.hidePopup();
  810. } else {
  811. // If the element is now invalid for a new reason, we should update the
  812. // error message.
  813. if (gFormSubmitObserver.panel.firstChild.textContent !=
  814. e.originalTarget.validationMessage) {
  815. gFormSubmitObserver.panel.firstChild.textContent =
  816. e.originalTarget.validationMessage;
  817. }
  818. }
  819. };
  820. element.addEventListener("input", inputHandler, false);
  821. element.addEventListener("blur", blurHandler, false);
  822. // One event to bring them all and in the darkness bind them.
  823. this.panel.addEventListener("popuphiding", function(aEvent) {
  824. aEvent.target.removeEventListener("popuphiding", arguments.callee, false);
  825. element.removeEventListener("input", inputHandler, false);
  826. element.removeEventListener("blur", blurHandler, false);
  827. }, false);
  828. this.panel.hidden = false;
  829. // We want to show the popup at the middle of checkbox and radio buttons
  830. // and where the content begin for the other elements.
  831. let offset = 0;
  832. let position = "";
  833. if (element.tagName == 'INPUT' &&
  834. (element.type == 'radio' || element.type == 'checkbox')) {
  835. position = "bottomcenter topleft";
  836. } else {
  837. let win = element.ownerDocument.defaultView;
  838. let style = win.getComputedStyle(element, null);
  839. let utils = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  840. .getInterface(Components.interfaces.nsIDOMWindowUtils);
  841. if (style.direction == 'rtl') {
  842. offset = parseInt(style.paddingRight) + parseInt(style.borderRightWidth);
  843. } else {
  844. offset = parseInt(style.paddingLeft) + parseInt(style.borderLeftWidth);
  845. }
  846. offset = Math.round(offset * utils.screenPixelsPerCSSPixel);
  847. position = "after_start";
  848. }
  849. this.panel.openPopup(element, position, offset, 0);
  850. }
  851. };
  852. // Simple gestures support
  853. //
  854. // As per bug #412486, web content must not be allowed to receive any
  855. // simple gesture events. Multi-touch gesture APIs are in their
  856. // infancy and we do NOT want to be forced into supporting an API that
  857. // will probably have to change in the future. (The current Mac OS X
  858. // API is undocumented and was reverse-engineered.) Until support is
  859. // implemented in the event dispatcher to keep these events as
  860. // chrome-only, we must listen for the simple gesture events during
  861. // the capturing phase and call stopPropagation on every event.
  862. let gGestureSupport = {
  863. /**
  864. * Add or remove mouse gesture event listeners
  865. *
  866. * @param aAddListener
  867. * True to add/init listeners and false to remove/uninit
  868. */
  869. init: function GS_init(aAddListener) {
  870. const gestureEvents = ["SwipeGesture",
  871. "MagnifyGestureStart", "MagnifyGestureUpdate", "MagnifyGesture",
  872. "RotateGestureStart", "RotateGestureUpdate", "RotateGesture",
  873. "TapGesture", "PressTapGesture"];
  874. let addRemove = aAddListener ? window.addEventListener :
  875. window.removeEventListener;
  876. gestureEvents.forEach(function (event) addRemove("Moz" + event, this, true),
  877. this);
  878. },
  879. /**
  880. * Dispatch events based on the type of mouse gesture event. For now, make
  881. * sure to stop propagation of every gesture event so that web content cannot
  882. * receive gesture events.
  883. *
  884. * @param aEvent
  885. * The gesture event to handle
  886. */
  887. handleEvent: function GS_handleEvent(aEvent) {
  888. aEvent.stopPropagation();
  889. // Create a preference object with some defaults
  890. let def = function(aThreshold, aLatched)
  891. ({ threshold: aThreshold, latched: !!aLatched });
  892. switch (aEvent.type) {
  893. case "MozSwipeGesture":
  894. aEvent.preventDefault();
  895. return this.onSwipe(aEvent);
  896. case "MozMagnifyGestureStart":
  897. aEvent.preventDefault();
  898. #ifdef XP_WIN
  899. return this._setupGesture(aEvent, "pinch", def(25, 0), "out", "in");
  900. #else
  901. return this._setupGesture(aEvent, "pinch", def(150, 1), "out", "in");
  902. #endif
  903. case "MozRotateGestureStart":
  904. aEvent.preventDefault();
  905. return this._setupGesture(aEvent, "twist", def(25, 0), "right", "left");
  906. case "MozMagnifyGestureUpdate":
  907. case "MozRotateGestureUpdate":
  908. aEvent.preventDefault();
  909. return this._doUpdate(aEvent);
  910. case "MozTapGesture":
  911. aEvent.preventDefault();
  912. return this._doAction(aEvent, ["tap"]);
  913. case "MozPressTapGesture":
  914. // Fall through to default behavior
  915. return;
  916. }
  917. },
  918. /**
  919. * Called at the start of "pinch" and "twist" gestures to setup all of the
  920. * information needed to process the gesture
  921. *
  922. * @param aEvent
  923. * The continual motion start event to handle
  924. * @param aGesture
  925. * Name of the gesture to handle
  926. * @param aPref
  927. * Preference object with the names of preferences and defaults
  928. * @param aInc
  929. * Command to trigger for increasing motion (without gesture name)
  930. * @param aDec
  931. * Command to trigger for decreasing motion (without gesture name)
  932. */
  933. _setupGesture: function GS__setupGesture(aEvent, aGesture, aPref, aInc, aDec) {
  934. // Try to load user-set values from preferences
  935. for (let [pref, def] in Iterator(aPref))
  936. aPref[pref] = this._getPref(aGesture + "." + pref, def);
  937. // Keep track of the total deltas and latching behavior
  938. let offset = 0;
  939. let latchDir = aEvent.delta > 0 ? 1 : -1;
  940. let isLatched = false;
  941. // Create the update function here to capture closure state
  942. this._doUpdate = function GS__doUpdate(aEvent) {
  943. // Update the offset with new event data
  944. offset += aEvent.delta;
  945. // Check if the cumulative deltas exceed the threshold
  946. if (Math.abs(offset) > aPref["threshold"]) {
  947. // Trigger the action if we don't care about latching; otherwise, make
  948. // sure either we're not latched and going the same direction of the
  949. // initial motion; or we're latched and going the opposite way
  950. let sameDir = (latchDir ^ offset) >= 0;
  951. if (!aPref["latched"] || (isLatched ^ sameDir)) {
  952. this._doAction(aEvent, [aGesture, offset > 0 ? aInc : aDec]);
  953. // We must be getting latched or leaving it, so just toggle
  954. isLatched = !isLatched;
  955. }
  956. // Reset motion counter to prepare for more of the same gesture
  957. offset = 0;
  958. }
  959. };
  960. // The start event also contains deltas, so handle an update right away
  961. this._doUpdate(aEvent);
  962. },
  963. /**
  964. * Generator producing the powerset of the input array where the first result
  965. * is the complete set and the last result (before StopIteration) is empty.
  966. *
  967. * @param aArray
  968. * Source array containing any number of elements
  969. * @yield Array that is a subset of the input array from full set to empty
  970. */
  971. _power: function GS__power(aArray) {
  972. // Create a bitmask based on the length of the array
  973. let num = 1 << aArray.length;
  974. while (--num >= 0) {
  975. // Only select array elements where the current bit is set
  976. yield aArray.reduce(function (aPrev, aCurr, aIndex) {
  977. if (num & 1 << aIndex)
  978. aPrev.push(aCurr);
  979. return aPrev;
  980. }, []);
  981. }
  982. },
  983. /**
  984. * Determine what action to do for the gesture based on which keys are
  985. * pressed and which commands are set
  986. *
  987. * @param aEvent
  988. * The original gesture event to convert into a fake click event
  989. * @param aGesture
  990. * Array of gesture name parts (to be joined by periods)
  991. * @return Name of the command found for the event's keys and gesture. If no
  992. * command is found, no value is returned (undefined).
  993. */
  994. _doAction: function GS__doAction(aEvent, aGesture) {
  995. // Create an array of pressed keys in a fixed order so that a command for
  996. // "meta" is preferred over "ctrl" when both buttons are pressed (and a
  997. // command for both don't exist)
  998. let keyCombos = [];
  999. ["shift", "alt", "ctrl", "meta"].forEach(function (key) {
  1000. if (aEvent[key + "Key"])
  1001. keyCombos.push(key);
  1002. });
  1003. // Try each combination of key presses in decreasing order for commands
  1004. for each (let subCombo in this._power(keyCombos)) {
  1005. // Convert a gesture and pressed keys into the corresponding command
  1006. // action where the preference has the gesture before "shift" before
  1007. // "alt" before "ctrl" before "meta" all separated by periods
  1008. let command;
  1009. try {
  1010. command = this._getPref(aGesture.concat(subCombo).join("."));
  1011. } catch (e) {}
  1012. if (!command)
  1013. continue;
  1014. let node = document.getElementById(command);
  1015. if (node) {
  1016. if (node.getAttribute("disabled") != "true") {
  1017. let cmdEvent = document.createEvent("xulcommandevent");
  1018. cmdEvent.initCommandEvent("command", true, true, window, 0,
  1019. aEvent.ctrlKey, aEvent.altKey, aEvent.shiftKey,
  1020. aEvent.metaKey, null);
  1021. node.dispatchEvent(cmdEvent);
  1022. }
  1023. } else {
  1024. goDoCommand(command);
  1025. }
  1026. return command;
  1027. }
  1028. return null;
  1029. },
  1030. /**
  1031. * Convert continual motion events into an action if it exceeds a threshold
  1032. * in a given direction. This function will be set by _setupGesture to
  1033. * capture state that needs to be shared across multiple gesture updates.
  1034. *
  1035. * @param aEvent
  1036. * The continual motion update event to handle
  1037. */
  1038. _doUpdate: function(aEvent) {},
  1039. /**
  1040. * Convert the swipe gesture into a browser action based on the direction
  1041. *
  1042. * @param aEvent
  1043. * The swipe event to handle
  1044. */
  1045. onSwipe: function GS_onSwipe(aEvent) {
  1046. // Figure out which one (and only one) direction was triggered
  1047. ["UP", "RIGHT", "DOWN", "LEFT"].forEach(function (dir) {
  1048. if (aEvent.direction == aEvent["DIRECTION_" + dir])
  1049. return this._doAction(aEvent, ["swipe", dir.toLowerCase()]);
  1050. }, this);
  1051. },
  1052. /**
  1053. * Get a gesture preference or use a default if it doesn't exist
  1054. *
  1055. * @param aPref
  1056. * Name of the preference to load under the gesture branch
  1057. * @param aDef
  1058. * Default value if the preference doesn't exist
  1059. */
  1060. _getPref: function GS__getPref(aPref, aDef) {
  1061. // Preferences branch under which all gestures preferences are stored
  1062. const branch = "browser.gesture.";
  1063. try {
  1064. // Determine what type of data to load based on default value's type
  1065. let type = typeof aDef;
  1066. let getFunc = "get" + (type == "boolean" ? "Bool" :
  1067. type == "number" ? "Int" : "Char") + "Pref";
  1068. return gPrefService[getFunc](branch + aPref);
  1069. }
  1070. catch (e) {
  1071. return aDef;
  1072. }
  1073. },
  1074. };
  1075. function BrowserStartup() {
  1076. var uriToLoad = null;
  1077. // window.arguments[0]: URI to load (string), or an nsISupportsArray of
  1078. // nsISupportsStrings to load, or a xul:tab of
  1079. // a tabbrowser, which will be replaced by this
  1080. // window (for this case, all other arguments are
  1081. // ignored).
  1082. // [1]: character set (string)
  1083. // [2]: referrer (nsIURI)
  1084. // [3]: postData (nsIInputStream)
  1085. // [4]: allowThirdPartyFixup (bool)
  1086. if ("arguments" in window && window.arguments[0])
  1087. uriToLoad = window.arguments[0];
  1088. var isLoadingBlank = isBlankPageURL(uriToLoad);
  1089. var mustLoadSidebar = false;
  1090. prepareForStartup();
  1091. if (uriToLoad && uriToLoad != "about:blank") {
  1092. if (uriToLoad instanceof Ci.nsISupportsArray) {
  1093. let count = uriToLoad.Count();
  1094. let specs = [];
  1095. for (let i = 0; i < count; i++) {
  1096. let urisstring = uriToLoad.GetElementAt(i).QueryInterface(Ci.nsISupportsString);
  1097. specs.push(urisstring.data);
  1098. }
  1099. // This function throws for certain malformed URIs, so use exception handling
  1100. // so that we don't disrupt startup
  1101. try {
  1102. gBrowser.loadTabs(specs, false, true);
  1103. } catch (e) {}
  1104. }
  1105. else if (uriToLoad instanceof XULElement) {
  1106. // swap the given tab with the default about:blank tab and then close
  1107. // the original tab in the other window.
  1108. // Stop the about:blank load
  1109. gBrowser.stop();
  1110. // make sure it has a docshell
  1111. gBrowser.docShell;
  1112. gBrowser.swapBrowsersAndCloseOther(gBrowser.selectedTab, uriToLoad);
  1113. }
  1114. else if (window.arguments.length >= 3) {
  1115. loadURI(uriToLoad, window.arguments[2], window.arguments[3] || null,
  1116. window.arguments[4] || false);
  1117. content.focus();
  1118. }
  1119. // Note: loadOneOrMoreURIs *must not* be called if window.arguments.length >= 3.
  1120. // Such callers expect that window.arguments[0] is handled as a single URI.
  1121. else
  1122. loadOneOrMoreURIs(uriToLoad);
  1123. }
  1124. if (window.opener && !window.opener.closed) {
  1125. let openerSidebarBox = window.opener.document.getElementById("sidebar-box");
  1126. // If the opener had a sidebar, open the same sidebar in our window.
  1127. // The opener can be the hidden window too, if we're coming from the state
  1128. // where no windows are open, and the hidden window has no sidebar box.
  1129. if (openerSidebarBox && !openerSidebarBox.hidden) {
  1130. let sidebarCmd = openerSidebarBox.getAttribute("sidebarcommand");
  1131. let sidebarCmdElem = document.getElementById(sidebarCmd);
  1132. // dynamically generated sidebars will fail this check.
  1133. if (sidebarCmdElem) {
  1134. let sidebarBox = document.getElementById("sidebar-box");
  1135. let sidebarTitle = document.getElementById("sidebar-title");
  1136. sidebarTitle.setAttribute(
  1137. "value", window.opener.document.getElementById("sidebar-title").getAttribute("value"));
  1138. sidebarBox.setAttribute("width", openerSidebarBox.boxObject.width);
  1139. sidebarBox.setAttribute("sidebarcommand", sidebarCmd);
  1140. // Note: we're setting 'src' on sidebarBox, which is a <vbox>, not on
  1141. // the <browser id="sidebar">. This lets us delay the actual load until
  1142. // delayedStartup().
  1143. sidebarBox.setAttribute(
  1144. "src", window.opener.document.getElementById("sidebar").getAttribute("src"));
  1145. mustLoadSidebar = true;
  1146. sidebarBox.hidden = false;
  1147. document.getElementById("sidebar-splitter").hidden = false;
  1148. sidebarCmdElem.setAttribute("checked", "true");
  1149. }
  1150. }
  1151. }
  1152. else {
  1153. let box = document.getElementById("sidebar-box");
  1154. if (box.hasAttribute("sidebarcommand")) {
  1155. let commandID = box.getAttribute("sidebarcommand");
  1156. if (commandID) {
  1157. let command = document.getElementById(commandID);
  1158. if (command) {
  1159. mustLoadSidebar = true;
  1160. box.hidden = false;
  1161. document.getElementById("sidebar-splitter").hidden = false;
  1162. command.setAttribute("checked", "true");
  1163. }
  1164. else {
  1165. // Remove the |sidebarcommand| attribute, because the element it
  1166. // refers to no longer exists, so we should assume this sidebar
  1167. // panel has been uninstalled. (249883)
  1168. box.removeAttribute("sidebarcommand");
  1169. }
  1170. }
  1171. }
  1172. }
  1173. // Certain kinds of automigration rely on this notification to complete their
  1174. // tasks BEFORE the browser window is shown.
  1175. Services.obs.notifyObservers(null, "browser-window-before-show", "");
  1176. // Set a sane starting width/height for all resolutions on new profiles.
  1177. if (!document.documentElement.hasAttribute("width")) {
  1178. let defaultWidth = 994;
  1179. let defaultHeight;
  1180. if (screen.availHeight <= 600) {
  1181. document.documentElement.setAttribute("sizemode", "maximized");
  1182. defaultWidth = 610;
  1183. defaultHeight = 450;
  1184. }
  1185. else {
  1186. // Create a narrower window for large or wide-aspect displays, to suggest
  1187. // side-by-side page view.
  1188. if (screen.availWidth >= 1600)
  1189. defaultWidth = (screen.availWidth / 2) - 20;
  1190. defaultHeight = screen.availHeight - 10;
  1191. #ifdef MOZ_WIDGET_GTK2
  1192. // On X, we're not currently able to account for the size of the window
  1193. // border. Use 28px as a guess (titlebar + bottom window border)
  1194. defaultHeight -= 28;
  1195. #endif
  1196. }
  1197. document.documentElement.setAttribute("width", defaultWidth);
  1198. document.documentElement.setAttribute("height", defaultHeight);
  1199. }
  1200. if (!gShowPageResizers)
  1201. document.getElementById("status-bar").setAttribute("hideresizer", "true");
  1202. if (!window.toolbar.visible) {
  1203. // adjust browser UI for popups
  1204. if (gURLBar) {
  1205. gURLBar.setAttribute("readonly", "true");
  1206. gURLBar.setAttribute("enablehistory", "false");
  1207. }
  1208. goSetCommandEnabled("cmd_newNavigatorTab", false);
  1209. }
  1210. #ifdef MENUBAR_CAN_AUTOHIDE
  1211. updateAppButtonDisplay();
  1212. #endif
  1213. CombinedStopReload.init();
  1214. allTabs.readPref();
  1215. TabsOnTop.init();
  1216. BookmarksMenuButton.init();
  1217. TabsInTitlebar.init();
  1218. gPrivateBrowsingUI.init();
  1219. retrieveToolbarIconsizesFromTheme();
  1220. gDelayedStartupTimeoutId = setTimeout(delayedStartup, 0, isLoadingBlank, mustLoadSidebar);
  1221. gStartupRan = true;
  1222. }
  1223. function HandleAppCommandEvent(evt) {
  1224. evt.stopPropagation();
  1225. switch (evt.command) {
  1226. case "Back":
  1227. BrowserBack();
  1228. break;
  1229. case "Forward":
  1230. BrowserForward();
  1231. break;
  1232. case "Reload":
  1233. BrowserReloadSkipCache();
  1234. break;
  1235. case "Stop":
  1236. BrowserStop();
  1237. break;
  1238. case "Search":
  1239. BrowserSearch.webSearch();
  1240. break;
  1241. case "Bookmarks":
  1242. toggleSidebar('viewBookmarksSidebar');
  1243. break;
  1244. case "Home":
  1245. BrowserHome();
  1246. break;
  1247. default:
  1248. break;
  1249. }
  1250. }
  1251. function prepareForStartup() {
  1252. gBrowser.addEventListener("DOMUpdatePageReport", gPopupBlockerObserver, false);
  1253. gBrowser.addEventListener("PluginNotFound", gPluginHandler, true);
  1254. gBrowser.addEventListener("PluginCrashed", gPluginHandler, true);
  1255. gBrowser.addEventListener("PluginBlocklisted", gPluginHandler, true);
  1256. gBrowser.addEventListener("PluginOutdated", gPluginHandler, true);
  1257. gBrowser.addEventListener("PluginDisabled", gPluginHandler, true);
  1258. gBrowser.addEventListener("NewPluginInstalled", gPluginHandler.newPluginInstalled, true);
  1259. #ifdef XP_MACOSX
  1260. gBrowser.addEventListener("npapi-carbon-event-model-failure", gPluginHandler, true);
  1261. #endif
  1262. Services.obs.addObserver(gPluginHandler.pluginCrashed, "plugin-crashed", false);
  1263. window.addEventListener("AppCommand", HandleAppCommandEvent, true);
  1264. var webNavigation;
  1265. try {
  1266. webNavigation = getWebNavigation();
  1267. if (!webNavigation)
  1268. throw "no XBL binding for browser";
  1269. } catch (e) {
  1270. alert("Error launching browser window:" + e);
  1271. window.close(); // Give up.
  1272. return;
  1273. }
  1274. messageManager.loadFrameScript("chrome://browser/content/content.js", true);
  1275. // initialize observers and listeners
  1276. // and give C++ access to gBrowser
  1277. gBrowser.init();
  1278. XULBrowserWindow.init();
  1279. window.QueryInterface(Ci.nsIInterfaceRequestor)
  1280. .getInterface(nsIWebNavigation)
  1281. .QueryInterface(Ci.nsIDocShellTreeItem).treeOwner
  1282. .QueryInterface(Ci.nsIInterfaceRequestor)
  1283. .getInterface(Ci.nsIXULWindow)
  1284. .XULBrowserWindow = window.XULBrowserWindow;
  1285. window.QueryInterface(Ci.nsIDOMChromeWindow).browserDOMWindow =
  1286. new nsBrowserAccess();
  1287. // set default character set if provided
  1288. if ("arguments" in window && window.arguments.length > 1 && window.arguments[1]) {
  1289. if (window.arguments[1].indexOf("charset=") != -1) {
  1290. var arrayArgComponents = window.arguments[1].split("=");
  1291. if (arrayArgComponents) {
  1292. //we should "inherit" the charset menu setting in a new window
  1293. getMarkupDocumentViewer().defaultCharacterSet = arrayArgComponents[1];
  1294. }
  1295. }
  1296. }
  1297. // Manually hook up session and global history for the first browser
  1298. // so that we don't have to load global history before bringing up a
  1299. // window.
  1300. // Wire up session and global history before any possible
  1301. // progress notifications for back/forward button updating
  1302. webNavigation.sessionHistory = Components.classes["@mozilla.org/browser/shistory;1"]
  1303. .createInstance(Components.interfaces.nsISHistory);
  1304. Services.obs.addObserver(gBrowser.browsers[0], "browser:purge-session-history", false);
  1305. // remove the disablehistory attribute so the browser cleans up, as
  1306. // though it had done this work itself
  1307. gBrowser.browsers[0].removeAttribute("disablehistory");
  1308. // enable global history
  1309. try {
  1310. gBrowser.docShell.QueryInterface(Components.interfaces.nsIDocShellHistory).useGlobalHistory = true;
  1311. } catch(ex) {
  1312. Components.utils.reportError("Places database may be locked: " + ex);
  1313. }
  1314. #ifdef MOZ_E10S_COMPAT
  1315. // Bug 666801 - WebProgress support for e10s
  1316. #else
  1317. // hook up UI through progress listener
  1318. gBrowser.addProgressListener(window.XULBrowserWindow);
  1319. gBrowser.addTabsProgressListener(window.TabsProgressListener);
  1320. #endif
  1321. // setup our common DOMLinkAdded listener
  1322. gBrowser.addEventListener("DOMLinkAdded", DOMLinkHandler, false);
  1323. // setup our MozApplicationManifest listener
  1324. gBrowser.addEventListener("MozApplicationManifest",
  1325. OfflineApps, false);
  1326. // setup simple gestures support
  1327. gGestureSupport.init(true);
  1328. }
  1329. function delayedStartup(isLoadingBlank, mustLoadSidebar) {
  1330. let tmp = {};
  1331. Cu.import("resource:///modules/TelemetryTimestamps.jsm", tmp);
  1332. let TelemetryTimestamps = tmp.TelemetryTimestamps;
  1333. TelemetryTimestamps.add("delayedStartupStarted");
  1334. gDelayedStartupTimeoutId = null;
  1335. Services.obs.addObserver(gSessionHistoryObserver, "browser:purge-session-history", false);
  1336. Services.obs.addObserver(gXPInstallObserver, "addon-install-disabled", false);
  1337. Services.obs.addObserver(gXPInstallObserver, "addon-install-started", false);
  1338. Services.obs.addObserver(gXPInstallObserver, "addon-install-blocked", false);
  1339. Services.obs.addObserver(gXPInstallObserver, "addon-install-failed", false);
  1340. Services.obs.addObserver(gXPInstallObserver, "addon-install-complete", false);
  1341. Services.obs.addObserver(gFormSubmitObserver, "invalidformsubmit", false);
  1342. BrowserOffline.init();
  1343. OfflineApps.init();
  1344. IndexedDBPromptHelper.init();
  1345. gFormSubmitObserver.init();
  1346. AddonManager.addAddonListener(AddonsMgrListener);
  1347. gBrowser.addEventListener("pageshow", function(evt) { setTimeout(pageShowEventHandlers, 0, evt); }, true);
  1348. // Ensure login manager is up and running.
  1349. Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
  1350. if (mustLoadSidebar) {
  1351. let sidebar = document.getElementById("sidebar");
  1352. let sidebarBox = document.getElementById("sidebar-box");
  1353. sidebar.setAttribute("src", sidebarBox.getAttribute("src"));
  1354. }
  1355. UpdateUrlbarSearchSplitterState();
  1356. if (isLoadingBlank && gURLBar && isElementVisible(gURLBar))
  1357. gURLBar.focus();
  1358. else
  1359. gBrowser.selectedBrowser.focus();
  1360. gNavToolbox.customizeDone = BrowserToolboxCustomizeDone;
  1361. gNavToolbox.customizeChange = BrowserToolboxCustomizeChange;
  1362. // Set up Sanitize Item
  1363. initializeSanitizer();
  1364. // Enable/Disable auto-hide tabbar
  1365. gBrowser.tabContainer.updateVisibility();
  1366. gPrefService.addObserver(gHomeButton.prefDomain, gHomeButton, false);
  1367. var homeButton = document.getElementById("home-button");
  1368. gHomeButton.updateTooltip(homeButton);
  1369. gHomeButton.updatePersonalToolbarStyle(homeButton);
  1370. #ifdef HAVE_SHELL_SERVICE
  1371. // Perform default browser checking (after window opens).
  1372. var shell = getShellService();
  1373. if (shell) {
  1374. #ifdef DEBUG
  1375. var shouldCheck = false;
  1376. #else
  1377. var shouldCheck = shell.shouldCheckDefaultBrowser;
  1378. #endif
  1379. var willRecoverSession = false;
  1380. try {
  1381. var ss = Cc["@mozilla.org/browser/sessionstartup;1"].
  1382. getService(Ci.nsISessionStartup);
  1383. willRecoverSession =
  1384. (ss.sessionType == Ci.nsISessionStartup.RECOVER_SESSION);
  1385. }
  1386. catch (ex) { /* never mind; suppose SessionStore is broken */ }
  1387. if (shouldCheck && !shell.isDefaultBrowser(true) && !willRecoverSession) {
  1388. // Delay the set-default-browser prompt so it doesn't block
  1389. // initialisation of the session store service.
  1390. setTimeout(function () {
  1391. var brandBundle = document.getElementById("bundle_brand");
  1392. var shellBundle = document.getElementById("bundle_shell");
  1393. var brandShortName = brandBundle.getString("brandShortName");
  1394. var promptTitle = shellBundle.getString("setDefaultBrowserTitle");
  1395. var promptMessage = shellBundle.getFormattedString("setDefaultBrowserMessage",
  1396. [brandShortName]);
  1397. var checkboxLabel = shellBundle.getFormattedString("setDefaultBrowserDontAsk",
  1398. [brandShortName]);
  1399. var checkEveryTime = { value: shouldCheck };
  1400. var ps = Services.prompt;
  1401. var rv = ps.confirmEx(window, promptTitle, promptMessage,
  1402. ps.STD_YES_NO_BUTTONS,
  1403. null, null, null, checkboxLabel, checkEveryTime);
  1404. if (rv == 0)
  1405. shell.setDefaultBrowser(true, false);
  1406. shell.shouldCheckDefaultBrowser = checkEveryTime.value;
  1407. }, 0);
  1408. }
  1409. }
  1410. #endif
  1411. // BiDi UI
  1412. gBidiUI = isBidiEnabled();
  1413. if (gBidiUI) {
  1414. document.getElementById("documentDirection-separator").hidden = false;
  1415. document.getElementById("documentDirection-swap").hidden = false;
  1416. document.getElementById("textfieldDirection-separator").hidden = false;
  1417. document.getElementById("textfieldDirection-swap").hidden = false;
  1418. }
  1419. // Setup click-and-hold gestures access to the session history
  1420. // menus if global click-and-hold isn't turned on
  1421. if (!getBoolPref("ui.click_hold_context_menus", false))
  1422. SetClickAndHoldHandlers();
  1423. // Initialize the full zoom setting.
  1424. // We do this before the session restore service gets initialized so we can
  1425. // apply full zoom settings to tabs restored by the session restore service.
  1426. try {
  1427. FullZoom.init();
  1428. }
  1429. catch(ex) {
  1430. Components.utils.reportError("Failed to init content pref service:\n" + ex);
  1431. }
  1432. #ifdef MOZ_E10S_COMPAT
  1433. // Bug 666804 - NetworkPrioritizer support for e10s
  1434. #else
  1435. let NP = {};
  1436. Cu.import("resource:///modules/NetworkPrioritizer.jsm", NP);
  1437. NP.trackBrowserWindow(window);
  1438. #endif
  1439. // initialize the session-restore service (in case it's not already running)
  1440. try {
  1441. Cc["@mozilla.org/browser/sessionstore;1"]
  1442. .getService(Ci.nsISessionStore)
  1443. .init(window);
  1444. } catch (ex) {
  1445. dump("nsSessionStore could not be initialized: " + ex + "\n");
  1446. }
  1447. PlacesToolbarHelper.init();
  1448. ctrlTab.readPref();
  1449. gPrefService.addObserver(ctrlTab.prefName, ctrlTab, false);
  1450. gPrefService.addObserver(allTabs.prefName, allTabs, false);
  1451. // Delayed initialization of the livemarks update timer.
  1452. // Livemark updates don't need to start until after bookmark UI
  1453. // such as the toolbar has initialized. Starting 5 seconds after
  1454. // delayedStartup in order to stagger this before the download manager starts.
  1455. setTimeout(function() PlacesUtils.livemarks.start(), 5000);
  1456. // Initialize the download manager some time after the app starts so that
  1457. // auto-resume downloads begin (such as after crashing or quitting with
  1458. // active downloads) and speeds up the first-load of the download manager UI.
  1459. // If the user manually opens the download manager before the timeout, the
  1460. // downloads will start right away, and getting the service again won't hurt.
  1461. setTimeout(function() {
  1462. gDownloadMgr = Cc["@mozilla.org/download-manager;1"].
  1463. getService(Ci.nsIDownloadManager);
  1464. if (Win7Features) {
  1465. let tempScope = {};
  1466. Cu.import("resource://gre/modules/DownloadTaskbarProgress.jsm",
  1467. tempScope);
  1468. tempScope.DownloadTaskbarProgress.onBrowserWindowLoad(window);
  1469. }
  1470. }, 10000);
  1471. #ifndef XP_MACOSX
  1472. updateEditUIVisibility();
  1473. let placesContext = document.getElementById("placesContext");
  1474. placesContext.addEventListener("popupshowing", updateEditUIVisibility, false);
  1475. placesContext.addEventListener("popuphiding", updateEditUIVisibility, false);
  1476. #endif
  1477. gBrowser.mPanelContainer.addEventListener("InstallBrowserTheme", LightWeightThemeWebInstaller, false, true);
  1478. gBrowser.mPanelContainer.addEventListener("PreviewBrowserTheme", LightWeightThemeWebInstaller, false, true);
  1479. gBrowser.mPanelContainer.addEventListener("ResetBrowserThemePreview", LightWeightThemeWebInstaller, false, true);
  1480. #ifdef MOZ_E10S_COMPAT
  1481. // Bug 666808 - AeroPeek support for e10s
  1482. #else
  1483. if (Win7Features)
  1484. Win7Features.onOpenWindow();
  1485. #endif
  1486. // called when we go into full screen, even if it is
  1487. // initiated by a web page script
  1488. window.addEventListener("fullscreen", onFullScreen, true);
  1489. // Called when we enter DOM full-screen mode. Note we can already be in browser
  1490. // full-screen mode when we enter DOM full-screen mode.
  1491. window.addEventListener("mozfullscreenchange", onMozFullScreenChange, true);
  1492. // When a restricted key is pressed in DOM full-screen mode, we should display
  1493. // the "Press ESC to exit" warning message.
  1494. window.addEventListener("MozShowFullScreenWarning", onShowFullScreenWarning, true);
  1495. if (window.fullScreen)
  1496. onFullScreen();
  1497. if (document.mozFullScreen)
  1498. onMozFullScreenChange();
  1499. #ifdef MOZ_SERVICES_SYNC
  1500. // initialize the sync UI
  1501. gSyncUI.init();
  1502. #endif
  1503. gBrowserThumbnails.init();
  1504. TabView.init();
  1505. setUrlAndSearchBarWidthForConditionalForwardButton();
  1506. window.addEventListener("resize", function resizeHandler(event) {
  1507. if (event.target == window)
  1508. setUrlAndSearchBarWidthForConditionalForwardButton();
  1509. });
  1510. // Enable Inspector?
  1511. let enabled = gPrefService.getBoolPref("devtools.inspector.enabled");
  1512. if (enabled) {
  1513. document.getElementById("menu_pageinspect").hidden = false;
  1514. document.getElementById("Tools:Inspect").removeAttribute("disabled");
  1515. #ifdef MENUBAR_CAN_AUTOHIDE
  1516. document.getElementById("appmenu_pageInspect").hidden = false;
  1517. #endif
  1518. }
  1519. // Enable Debugger?
  1520. let enabled = gPrefService.getBoolPref("devtools.debugger.enabled");
  1521. if (enabled) {
  1522. document.getElementById("menu_debugger").hidden = false;
  1523. document.getElementById("Tools:Debugger").removeAttribute("disabled");
  1524. #ifdef MENUBAR_CAN_AUTOHIDE
  1525. document.getElementById("appmenu_debugger").hidden = false;
  1526. #endif
  1527. }
  1528. // Enable Error Console?
  1529. // XXX Temporarily always-enabled, see bug 601201
  1530. let consoleEnabled = true || gPrefService.getBoolPref("devtools.errorconsole.enabled");
  1531. if (consoleEnabled) {
  1532. document.getElementById("javascriptConsole").hidden = false;
  1533. document.getElementById("key_errorConsole").removeAttribute("disabled");
  1534. #ifdef MENUBAR_CAN_AUTOHIDE
  1535. document.getElementById("appmenu_errorConsole").hidden = false;
  1536. #endif
  1537. }
  1538. // Enable Scratchpad in the UI, if the preference allows this.
  1539. let scratchpadEnabled = gPrefService.getBoolPref(Scratchpad.prefEnabledName);
  1540. if (scratchpadEnabled) {
  1541. document.getElementById("menu_scratchpad").hidden = false;
  1542. document.getElementById("Tools:Scratchpad").removeAttribute("disabled");
  1543. #ifdef MENUBAR_CAN_AUTOHIDE
  1544. document.getElementById("appmenu_scratchpad").hidden = false;
  1545. #endif
  1546. }
  1547. // Enable Style Editor?
  1548. let styleEditorEnabled = gPrefService.getBoolPref(StyleEditor.prefEnabledName);
  1549. if (styleEditorEnabled) {
  1550. document.getElementById("menu_styleeditor").hidden = false;
  1551. document.getElementById("Tools:StyleEditor").removeAttribute("disabled");
  1552. #ifdef MENUBAR_CAN_AUTOHIDE
  1553. document.getElementById("appmenu_styleeditor").hidden = false;
  1554. #endif
  1555. }
  1556. #ifdef MENUBAR_CAN_AUTOHIDE
  1557. // If the user (or the locale) hasn't enabled the top-level "Character
  1558. // Encoding" menu via the "browser.menu.showCharacterEncoding" preference,
  1559. // hide it.
  1560. if ("true" != gPrefService.getComplexValue("browser.menu.showCharacterEncoding",
  1561. Ci.nsIPrefLocalizedString).data)
  1562. document.getElementById("appmenu_charsetMenu").hidden = true;
  1563. #endif
  1564. let appMenuButton = document.getElementById("appmenu-button");
  1565. let appMenuPopup = document.getElementById("appmenu-popup");
  1566. if (appMenuButton && appMenuPopup) {
  1567. let appMenuOpening = null;
  1568. appMenuButton.addEventListener("mousedown", function(event) {
  1569. if (event.button == 0)
  1570. appMenuOpening = new Date();
  1571. }, false);
  1572. appMenuPopup.addEventListener("popupshown", function(event) {
  1573. if (event.target != appMenuPopup || !appMenuOpening)
  1574. return;
  1575. let duration = new Date() - appMenuOpening;
  1576. appMenuOpening = null;
  1577. Services.telemetry.getHistogramById("FX_APP_MENU_OPEN_MS").add(duration);
  1578. }, false);
  1579. }
  1580. window.addEventListener("mousemove", MousePosTracker, false);
  1581. window.addEventListener("dragover", MousePosTracker, false);
  1582. // End startup crash tracking after a delay to catch crashes while restoring
  1583. // tabs and to postpone saving the pref to disk.
  1584. try {
  1585. let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].
  1586. getService(Ci.nsIAppStartup);
  1587. const startupCrashEndDelay = 30 * 1000;
  1588. setTimeout(appStartup.trackStartupCrashEnd, startupCrashEndDelay);
  1589. } catch (ex) {
  1590. Cu.reportError("Could not end startup crash tracking: " + ex);
  1591. }
  1592. Services.obs.notifyObservers(window, "browser-delayed-startup-finished", "");
  1593. TelemetryTimestamps.add("delayedStartupFinished");
  1594. }
  1595. function BrowserShutdown() {
  1596. // In certain scenarios it's possible for unload to be fired before onload,
  1597. // (e.g. if the window is being closed after browser.js loads but before the
  1598. // load completes). In that case, there's nothing to do here.
  1599. if (!gStartupRan)
  1600. return;
  1601. if (!__lookupGetter__("InspectorUI"))
  1602. InspectorUI.destroy();
  1603. // First clean up services initialized in BrowserStartup (or those whose
  1604. // uninit methods don't depend on the services having been initialized).
  1605. allTabs.uninit();
  1606. CombinedStopReload.uninit();
  1607. gGestureSupport.init(false);
  1608. FullScreen.cleanup();
  1609. Services.obs.removeObserver(gPluginHandler.pluginCrashed, "plugin-crashed");
  1610. try {
  1611. gBrowser.removeProgressListener(window.XULBrowserWindow);
  1612. gBrowser.removeTabsProgressListener(window.TabsProgressListener);
  1613. } catch (ex) {
  1614. }
  1615. PlacesStarButton.uninit();
  1616. gPrivateBrowsingUI.uninit();
  1617. TabsOnTop.uninit();
  1618. TabsInTitlebar.uninit();
  1619. var enumerator = Services.wm.getEnumerator(null);
  1620. enumerator.getNext();
  1621. if (!enumerator.hasMoreElements()) {
  1622. document.persist("sidebar-box", "sidebarcommand");
  1623. document.persist("sidebar-box", "width");
  1624. document.persist("sidebar-box", "src");
  1625. document.persist("sidebar-title", "value");
  1626. }
  1627. // Now either cancel delayedStartup, or clean up the services initialized from
  1628. // it.
  1629. if (gDelayedStartupTimeoutId) {
  1630. clearTimeout(gDelayedStartupTimeoutId);
  1631. } else {
  1632. if (Win7Features)
  1633. Win7Features.onCloseWindow();
  1634. gPrefService.removeObserver(ctrlTab.prefName, ctrlTab);
  1635. gPrefService.removeObserver(allTabs.prefName, allTabs);
  1636. ctrlTab.uninit();
  1637. TabView.uninit();
  1638. gBrowserThumbnails.uninit();
  1639. try {
  1640. FullZoom.destroy();
  1641. }
  1642. catch(ex) {
  1643. Components.utils.reportError(ex);
  1644. }
  1645. Services.obs.removeObserver(gSessionHistoryObserver, "browser:purge-session-history");
  1646. Services.obs.removeObserver(gXPInstallObserver, "addon-install-disabled");
  1647. Services.obs.removeObserver(gXPInstallObserver, "addon-install-started");
  1648. Services.obs.removeObserver(gXPInstallObserver, "addon-install-blocked");
  1649. Services.obs.removeObserver(gXPInstallObserver, "addon-install-failed");
  1650. Services.obs.removeObserver(gXPInstallObserver, "addon-install-complete");
  1651. Services.obs.removeObserver(gFormSubmitObserver, "invalidformsubmit");
  1652. try {
  1653. gPrefService.removeObserver(gHomeButton.prefDomain, gHomeButton);
  1654. } catch (ex) {
  1655. Components.utils.reportError(ex);
  1656. }
  1657. BrowserOffline.uninit();
  1658. OfflineApps.uninit();
  1659. IndexedDBPromptHelper.uninit();
  1660. AddonManager.removeAddonListener(AddonsMgrListener);
  1661. }
  1662. // Final window teardown, do this last.
  1663. window.XULBrowserWindow.destroy();
  1664. window.XULBrowserWindow = null;
  1665. window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  1666. .getInterface(Components.interfaces.nsIWebNavigation)
  1667. .QueryInterface(Components.interfaces.nsIDocShellTreeItem).treeOwner
  1668. .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  1669. .getInterface(Components.interfaces.nsIXULWindow)
  1670. .XULBrowserWindow = null;
  1671. window.QueryInterface(Ci.nsIDOMChromeWindow).browserDOMWindow = null;
  1672. }
  1673. #ifdef XP_MACOSX
  1674. // nonBrowserWindowStartup(), nonBrowserWindowDelayedStartup(), and
  1675. // nonBrowserWindowShutdown() are used for non-browser windows in
  1676. // macBrowserOverlay
  1677. function nonBrowserWindowStartup() {
  1678. // Disable inappropriate commands / submenus
  1679. var disabledItems = ['Browser:SavePage',
  1680. 'Browser:SendLink', 'cmd_pageSetup', 'cmd_print', 'cmd_find', 'cmd_findAgain',
  1681. 'viewToolbarsMenu', 'viewSidebarMenuMenu', 'Browser:Reload',
  1682. 'viewFullZoomMenu', 'pageStyleMenu', 'charsetMenu', 'View:PageSource', 'View:FullScreen',
  1683. 'viewHistorySidebar', 'Browser:AddBookmarkAs', 'Browser:BookmarkAllTabs',
  1684. 'View:PageInfo', 'Tasks:InspectPage', 'Browser:ToggleTabView', 'Browser:ToggleAddonBar'];
  1685. var element;
  1686. for (var id in disabledItems) {
  1687. element = document.getElementById(disabledItems[id]);
  1688. if (element)
  1689. element.setAttribute("disabled", "true");
  1690. }
  1691. // If no windows are active (i.e. we're the hidden window), disable the close, minimize
  1692. // and zoom menu commands as well
  1693. if (window.location.href == "chrome://browser/content/hiddenWindow.xul") {
  1694. var hiddenWindowDisabledItems = ['cmd_close', 'minimizeWindow', 'zoomWindow'];
  1695. for (var id in hiddenWindowDisabledItems) {
  1696. element = document.getElementById(hiddenWindowDisabledItems[id]);
  1697. if (element)
  1698. element.setAttribute("disabled", "true");
  1699. }
  1700. // also hide the window-list separator
  1701. element = document.getElementById("sep-window-list");
  1702. element.setAttribute("hidden", "true");
  1703. // Setup the dock menu.
  1704. let dockMenuElement = document.getElementById("menu_mac_dockmenu");
  1705. if (dockMenuElement != null) {
  1706. let nativeMenu = Cc["@mozilla.org/widget/standalonenativemenu;1"]
  1707. .createInstance(Ci.nsIStandaloneNativeMenu);
  1708. try {
  1709. nativeMenu.init(dockMenuElement);
  1710. let dockSupport = Cc["@mozilla.org/widget/macdocksupport;1"]
  1711. .getService(Ci.nsIMacDockSupport);
  1712. dockSupport.dockMenu = nativeMenu;
  1713. }
  1714. catch (e) {
  1715. }
  1716. }
  1717. }
  1718. gDelayedStartupTimeoutId = setTimeout(nonBrowserWindowDelayedStartup, 0);
  1719. }
  1720. function nonBrowserWindowDelayedStartup() {
  1721. gDelayedStartupTimeoutId = null;
  1722. // initialise the offline listener
  1723. BrowserOffline.init();
  1724. // Set up Sanitize Item
  1725. initializeSanitizer();
  1726. // initialize the private browsing UI
  1727. gPrivateBrowsingUI.init();
  1728. #ifdef MOZ_SERVICES_SYNC
  1729. // initialize the sync UI
  1730. gSyncUI.init();
  1731. #endif
  1732. gStartupRan = true;
  1733. }
  1734. function nonBrowserWindowShutdown() {
  1735. // If nonBrowserWindowDelayedStartup hasn't run yet, we have no work to do -
  1736. // just cancel the pending timeout and return;
  1737. if (gDelayedStartupTimeoutId) {
  1738. clearTimeout(gDelayedStartupTimeoutId);
  1739. return;
  1740. }
  1741. BrowserOffline.uninit();
  1742. gPrivateBrowsingUI.uninit();
  1743. }
  1744. #endif
  1745. function initializeSanitizer()
  1746. {
  1747. const kDidSanitizeDomain = "privacy.sanitize.didShutdownSanitize";
  1748. if (gPrefService.prefHasUserValue(kDidSanitizeDomain)) {
  1749. gPrefService.clearUserPref(kDidSanitizeDomain);
  1750. // We need to persist this preference change, since we want to
  1751. // check it at next app start even if the browser exits abruptly
  1752. gPrefService.savePrefFile(null);
  1753. }
  1754. /**
  1755. * Migrate Firefox 3.0 privacy.item prefs under one of these conditions:
  1756. *
  1757. * a) User has customized any privacy.item prefs
  1758. * b) privacy.sanitize.sanitizeOnShutdown is set
  1759. */
  1760. if (!gPrefService.getBoolPref("privacy.sanitize.migrateFx3Prefs")) {
  1761. let itemBranch = gPrefService.getBranch("privacy.item.");
  1762. let itemArray = itemBranch.getChildList("");
  1763. // See if any privacy.item prefs are set
  1764. let doMigrate = itemArray.some(function (name) itemBranch.prefHasUserValue(name));
  1765. // Or if sanitizeOnShutdown is set
  1766. if (!doMigrate)
  1767. doMigrate = gPrefService.getBoolPref("privacy.sanitize.sanitizeOnShutdown");
  1768. if (doMigrate) {
  1769. let cpdBranch = gPrefService.getBranch("privacy.cpd.");
  1770. let clearOnShutdownBranch = gPrefService.getBranch("privacy.clearOnShutdown.");
  1771. itemArray.forEach(function (name) {
  1772. try {
  1773. // don't migrate password or offlineApps clearing in the CRH dialog since
  1774. // there's no UI for those anymore. They default to false. bug 497656
  1775. if (name != "passwords" && name != "offlineApps")
  1776. cpdBranch.setBoolPref(name, itemBranch.getBoolPref(name));
  1777. clearOnShutdownBranch.setBoolPref(name, itemBranch.getBoolPref(name));
  1778. }
  1779. catch(e) {
  1780. Cu.reportError("Exception thrown during privacy pref migration: " + e);
  1781. }
  1782. });
  1783. }
  1784. gPrefService.setBoolPref("privacy.sanitize.migrateFx3Prefs", true);
  1785. }
  1786. }
  1787. function gotoHistoryIndex(aEvent) {
  1788. let index = aEvent.target.getAttribute("index");
  1789. if (!index)
  1790. return false;
  1791. let where = whereToOpenLink(aEvent);
  1792. if (where == "current") {
  1793. // Normal click. Go there in the current tab and update session history.
  1794. try {
  1795. gBrowser.gotoIndex(index);
  1796. }
  1797. catch(ex) {
  1798. return false;
  1799. }
  1800. return true;
  1801. }
  1802. // Modified click. Go there in a new tab/window.
  1803. duplicateTabIn(gBrowser.selectedTab, where, index - gBrowser.sessionHistory.index);
  1804. return true;
  1805. }
  1806. function BrowserForward(aEvent) {
  1807. let where = whereToOpenLink(aEvent, false, true);
  1808. if (where == "current") {
  1809. try {
  1810. gBrowser.goForward();
  1811. }
  1812. catch(ex) {
  1813. }
  1814. }
  1815. else {
  1816. duplicateTabIn(gBrowser.selectedTab, where, 1);
  1817. }
  1818. }
  1819. function BrowserBack(aEvent) {
  1820. let where = whereToOpenLink(aEvent, false, true);
  1821. if (where == "current") {
  1822. try {
  1823. gBrowser.goBack();
  1824. }
  1825. catch(ex) {
  1826. }
  1827. }
  1828. else {
  1829. duplicateTabIn(gBrowser.selectedTab, where, -1);
  1830. }
  1831. }
  1832. function BrowserHandleBackspace()
  1833. {
  1834. switch (gPrefService.getIntPref("browser.backspace_action")) {
  1835. case 0:
  1836. BrowserBack();
  1837. break;
  1838. case 1:
  1839. goDoCommand("cmd_scrollPageUp");
  1840. break;
  1841. }
  1842. }
  1843. function BrowserHandleShiftBackspace()
  1844. {
  1845. switch (gPrefService.getIntPref("browser.backspace_action")) {
  1846. case 0:
  1847. BrowserForward();
  1848. break;
  1849. case 1:
  1850. goDoCommand("cmd_scrollPageDown");
  1851. break;
  1852. }
  1853. }
  1854. function BrowserStop()
  1855. {
  1856. try {
  1857. const stopFlags = nsIWebNavigation.STOP_ALL;
  1858. getWebNavigation().stop(stopFlags);
  1859. }
  1860. catch(ex) {
  1861. }
  1862. }
  1863. function BrowserReloadOrDuplicate(aEvent) {
  1864. var backgroundTabModifier = aEvent.button == 1 ||
  1865. #ifdef XP_MACOSX
  1866. aEvent.metaKey;
  1867. #else
  1868. aEvent.ctrlKey;
  1869. #endif
  1870. if (aEvent.shiftKey && !backgroundTabModifier) {
  1871. BrowserReloadSkipCache();
  1872. return;
  1873. }
  1874. let where = whereToOpenLink(aEvent, false, true);
  1875. if (where == "current")
  1876. BrowserReload();
  1877. else
  1878. duplicateTabIn(gBrowser.selectedTab, where);
  1879. }
  1880. function BrowserReload() {
  1881. const reloadFlags = nsIWebNavigation.LOAD_FLAGS_NONE;
  1882. BrowserReloadWithFlags(reloadFlags);
  1883. }
  1884. function BrowserReloadSkipCache() {
  1885. // Bypass proxy and cache.
  1886. const reloadFlags = nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY | nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE;
  1887. BrowserReloadWithFlags(reloadFlags);
  1888. }
  1889. var BrowserHome = BrowserGoHome;
  1890. function BrowserGoHome(aEvent) {
  1891. if (aEvent && "button" in aEvent &&
  1892. aEvent.button == 2) // right-click: do nothing
  1893. return;
  1894. var homePage = gHomeButton.getHomePage();
  1895. var where = whereToOpenLink(aEvent, false, true);
  1896. var urls;
  1897. // Home page should open in a new tab when current tab is an app tab
  1898. if (where == "current" &&
  1899. gBrowser &&
  1900. gBrowser.selectedTab.pinned)
  1901. where = "tab";
  1902. // openUILinkIn in utilityOverlay.js doesn't handle loading multiple pages
  1903. switch (where) {
  1904. case "current":
  1905. loadOneOrMoreURIs(homePage);
  1906. break;
  1907. case "tabshifted":
  1908. case "tab":
  1909. urls = homePage.split("|");
  1910. var loadInBackground = getBoolPref("browser.tabs.loadBookmarksInBackground", false);
  1911. gBrowser.loadTabs(urls, loadInBackground);
  1912. break;
  1913. case "window":
  1914. OpenBrowserWindow();
  1915. break;
  1916. }
  1917. }
  1918. function loadOneOrMoreURIs(aURIString)
  1919. {
  1920. #ifdef XP_MACOSX
  1921. // we're not a browser window, pass the URI string to a new browser window
  1922. if (window.location.href != getBrowserURL())
  1923. {
  1924. window.openDialog(getBrowserURL(), "_blank", "all,dialog=no", aURIString);
  1925. return;
  1926. }
  1927. #endif
  1928. // This function throws for certain malformed URIs, so use exception handling
  1929. // so that we don't disrupt startup
  1930. try {
  1931. gBrowser.loadTabs(aURIString.split("|"), false, true);
  1932. }
  1933. catch (e) {
  1934. }
  1935. }
  1936. function focusAndSelectUrlBar() {
  1937. if (gURLBar) {
  1938. if (window.fullScreen)
  1939. FullScreen.mouseoverToggle(true);
  1940. if (isElementVisible(gURLBar)) {
  1941. gURLBar.focus();
  1942. gURLBar.select();
  1943. return true;
  1944. }
  1945. }
  1946. return false;
  1947. }
  1948. function openLocation() {
  1949. if (focusAndSelectUrlBar())
  1950. return;
  1951. #ifdef XP_MACOSX
  1952. if (window.location.href != getBrowserURL()) {
  1953. var win = getTopWin();
  1954. if (win) {
  1955. // If there's an open browser window, it should handle this command
  1956. win.focus()
  1957. win.openLocation();
  1958. }
  1959. else {
  1960. // If there are no open browser windows, open a new one
  1961. win = window.openDialog("chrome://browser/content/", "_blank",
  1962. "chrome,all,dialog=no", BROWSER_NEW_TAB_URL);
  1963. win.addEventListener("load", openLocationCallback, false);
  1964. }
  1965. return;
  1966. }
  1967. #endif
  1968. openDialog("chrome://browser/content/openLocation.xul", "_blank",
  1969. "chrome,modal,titlebar", window);
  1970. }
  1971. function openLocationCallback()
  1972. {
  1973. // make sure the DOM is ready
  1974. setTimeout(function() { this.openLocation(); }, 0);
  1975. }
  1976. function BrowserOpenTab()
  1977. {
  1978. openUILinkIn(BROWSER_NEW_TAB_URL, "tab");
  1979. }
  1980. /* Called from the openLocation dialog. This allows that dialog to instruct
  1981. its opener to open a new window and then step completely out of the way.
  1982. Anything less byzantine is causing horrible crashes, rather believably,
  1983. though oddly only on Linux. */
  1984. function delayedOpenWindow(chrome, flags, href, postData)
  1985. {
  1986. // The other way to use setTimeout,
  1987. // setTimeout(openDialog, 10, chrome, "_blank", flags, url),
  1988. // doesn't work here. The extra "magic" extra argument setTimeout adds to
  1989. // the callback function would confuse prepareForStartup() by making
  1990. // window.arguments[1] be an integer instead of null.
  1991. setTimeout(function() { openDialog(chrome, "_blank", flags, href, null, null, postData); }, 10);
  1992. }
  1993. /* Required because the tab needs time to set up its content viewers and get the load of
  1994. the URI kicked off before becoming the active content area. */
  1995. function delayedOpenTab(aUrl, aReferrer, aCharset, aPostData, aAllowThirdPartyFixup)
  1996. {
  1997. gBrowser.loadOneTab(aUrl, {
  1998. referrerURI: aReferrer,
  1999. charset: aCharset,
  2000. postData: aPostData,
  2001. inBackground: false,
  2002. allowThirdPartyFixup: aAllowThirdPartyFixup});
  2003. }
  2004. var gLastOpenDirectory = {
  2005. _lastDir: null,
  2006. get path() {
  2007. if (!this._lastDir || !this._lastDir.exists()) {
  2008. try {
  2009. this._lastDir = gPrefService.getComplexValue("browser.open.lastDir",
  2010. Ci.nsILocalFile);
  2011. if (!this._lastDir.exists())
  2012. this._lastDir = null;
  2013. }
  2014. catch(e) {}
  2015. }
  2016. return this._lastDir;
  2017. },
  2018. set path(val) {
  2019. try {
  2020. if (!val || !val.isDirectory())
  2021. return;
  2022. } catch(e) {
  2023. return;
  2024. }
  2025. this._lastDir = val.clone();
  2026. // Don't save the last open directory pref inside the Private Browsing mode
  2027. if (!gPrivateBrowsingUI.privateBrowsingEnabled)
  2028. gPrefService.setComplexValue("browser.open.lastDir", Ci.nsILocalFile,
  2029. this._lastDir);
  2030. },
  2031. reset: function() {
  2032. this._lastDir = null;
  2033. }
  2034. };
  2035. function BrowserOpenFileWindow()
  2036. {
  2037. // Get filepicker component.
  2038. try {
  2039. const nsIFilePicker = Components.interfaces.nsIFilePicker;
  2040. var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  2041. fp.init(window, gNavigatorBundle.getString("openFile"), nsIFilePicker.modeOpen);
  2042. fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText | nsIFilePicker.filterImages |
  2043. nsIFilePicker.filterXML | nsIFilePicker.filterHTML);
  2044. fp.displayDirectory = gLastOpenDirectory.path;
  2045. if (fp.show() == nsIFilePicker.returnOK) {
  2046. try {
  2047. if (fp.file)
  2048. gLastOpenDirectory.path = fp.file.parent.QueryInterface(Ci.nsILocalFile);
  2049. } catch(e) {
  2050. }
  2051. openTopWin(fp.fileURL.spec);
  2052. }
  2053. } catch (ex) {
  2054. }
  2055. }
  2056. function BrowserCloseTabOrWindow() {
  2057. #ifdef XP_MACOSX
  2058. // If we're not a browser window, just close the window
  2059. if (window.location.href != getBrowserURL()) {
  2060. closeWindow(true);
  2061. return;
  2062. }
  2063. #endif
  2064. // If the current tab is the last one, this will close the window.
  2065. gBrowser.removeCurrentTab({animate: true});
  2066. }
  2067. function BrowserTryToCloseWindow()
  2068. {
  2069. if (WindowIsClosing())
  2070. window.close(); // WindowIsClosing does all the necessary checks
  2071. }
  2072. function loadURI(uri, referrer, postData, allowThirdPartyFixup) {
  2073. if (postData === undefined)
  2074. postData = null;
  2075. var flags = nsIWebNavigation.LOAD_FLAGS_NONE;
  2076. if (allowThirdPartyFixup)
  2077. flags |= nsIWebNavigation.LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP;
  2078. try {
  2079. gBrowser.loadURIWithFlags(uri, flags, referrer, null, postData);
  2080. } catch (e) {}
  2081. }
  2082. function getShortcutOrURI(aURL, aPostDataRef, aMayInheritPrincipal) {
  2083. // Initialize outparam to false
  2084. if (aMayInheritPrincipal)
  2085. aMayInheritPrincipal.value = false;
  2086. var shortcutURL = null;
  2087. var keyword = aURL;
  2088. var param = "";
  2089. var offset = aURL.indexOf(" ");
  2090. if (offset > 0) {
  2091. keyword = aURL.substr(0, offset);
  2092. param = aURL.substr(offset + 1);
  2093. }
  2094. if (!aPostDataRef)
  2095. aPostDataRef = {};
  2096. var engine = Services.search.getEngineByAlias(keyword);
  2097. if (engine) {
  2098. var submission = engine.getSubmission(param);
  2099. aPostDataRef.value = submission.postData;
  2100. return submission.uri.spec;
  2101. }
  2102. [shortcutURL, aPostDataRef.value] =
  2103. PlacesUtils.getURLAndPostDataForKeyword(keyword);
  2104. if (!shortcutURL)
  2105. return aURL;
  2106. var postData = "";
  2107. if (aPostDataRef.value)
  2108. postData = unescape(aPostDataRef.value);
  2109. if (/%s/i.test(shortcutURL) || /%s/i.test(postData)) {
  2110. var charset = "";
  2111. const re = /^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/;
  2112. var matches = shortcutURL.match(re);
  2113. if (matches)
  2114. [, shortcutURL, charset] = matches;
  2115. else {
  2116. // Try to get the saved character-set.
  2117. try {
  2118. // makeURI throws if URI is invalid.
  2119. // Will return an empty string if character-set is not found.
  2120. charset = PlacesUtils.history.getCharsetForURI(makeURI(shortcutURL));
  2121. } catch (e) {}
  2122. }
  2123. // encodeURIComponent produces UTF-8, and cannot be used for other charsets.
  2124. // escape() works in those cases, but it doesn't uri-encode +, @, and /.
  2125. // Therefore we need to manually replace these ASCII characters by their
  2126. // encodeURIComponent result, to match the behavior of nsEscape() with
  2127. // url_XPAlphas
  2128. var encodedParam = "";
  2129. if (charset && charset != "UTF-8")
  2130. encodedParam = escape(convertFromUnicode(charset, param)).
  2131. replace(/[+@\/]+/g, encodeURIComponent);
  2132. else // Default charset is UTF-8
  2133. encodedParam = encodeURIComponent(param);
  2134. shortcutURL = shortcutURL.replace(/%s/g, encodedParam).replace(/%S/g, param);
  2135. if (/%s/i.test(postData)) // POST keyword
  2136. aPostDataRef.value = getPostDataStream(postData, param, encodedParam,
  2137. "application/x-www-form-urlencoded");
  2138. }
  2139. else if (param) {
  2140. // This keyword doesn't take a parameter, but one was provided. Just return
  2141. // the original URL.
  2142. aPostDataRef.value = null;
  2143. return aURL;
  2144. }
  2145. // This URL came from a bookmark, so it's safe to let it inherit the current
  2146. // document's principal.
  2147. if (aMayInheritPrincipal)
  2148. aMayInheritPrincipal.value = true;
  2149. return shortcutURL;
  2150. }
  2151. function getPostDataStream(aStringData, aKeyword, aEncKeyword, aType) {
  2152. var dataStream = Cc["@mozilla.org/io/string-input-stream;1"].
  2153. createInstance(Ci.nsIStringInputStream);
  2154. aStringData = aStringData.replace(/%s/g, aEncKeyword).replace(/%S/g, aKeyword);
  2155. dataStream.data = aStringData;
  2156. var mimeStream = Cc["@mozilla.org/network/mime-input-stream;1"].
  2157. createInstance(Ci.nsIMIMEInputStream);
  2158. mimeStream.addHeader("Content-Type", aType);
  2159. mimeStream.addContentLength = true;
  2160. mimeStream.setData(dataStream);
  2161. return mimeStream.QueryInterface(Ci.nsIInputStream);
  2162. }
  2163. function readFromClipboard()
  2164. {
  2165. var url;
  2166. try {
  2167. // Get clipboard.
  2168. var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"]
  2169. .getService(Components.interfaces.nsIClipboard);
  2170. // Create transferable that will transfer the text.
  2171. var trans = Components.classes["@mozilla.org/widget/transferable;1"]
  2172. .createInstance(Components.interfaces.nsITransferable);
  2173. trans.addDataFlavor("text/unicode");
  2174. // If available, use selection clipboard, otherwise global one
  2175. if (clipboard.supportsSelectionClipboard())
  2176. clipboard.getData(trans, clipboard.kSelectionClipboard);
  2177. else
  2178. clipboard.getData(trans, clipboard.kGlobalClipboard);
  2179. var data = {};
  2180. var dataLen = {};
  2181. trans.getTransferData("text/unicode", data, dataLen);
  2182. if (data) {
  2183. data = data.value.QueryInterface(Components.interfaces.nsISupportsString);
  2184. url = data.data.substring(0, dataLen.value / 2);
  2185. }
  2186. } catch (ex) {
  2187. }
  2188. return url;
  2189. }
  2190. function BrowserViewSourceOfDocument(aDocument)
  2191. {
  2192. var pageCookie;
  2193. var webNav;
  2194. // Get the document charset
  2195. var docCharset = "charset=" + aDocument.characterSet;
  2196. // Get the nsIWebNavigation associated with the document
  2197. try {
  2198. var win;
  2199. var ifRequestor;
  2200. // Get the DOMWindow for the requested document. If the DOMWindow
  2201. // cannot be found, then just use the content window...
  2202. //
  2203. // XXX: This is a bit of a hack...
  2204. win = aDocument.defaultView;
  2205. if (win == window) {
  2206. win = content;
  2207. }
  2208. ifRequestor = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  2209. webNav = ifRequestor.getInterface(nsIWebNavigation);
  2210. } catch(err) {
  2211. // If nsIWebNavigation cannot be found, just get the one for the whole
  2212. // window...
  2213. webNav = getWebNavigation();
  2214. }
  2215. //
  2216. // Get the 'PageDescriptor' for the current document. This allows the
  2217. // view-source to access the cached copy of the content rather than
  2218. // refetching it from the network...
  2219. //
  2220. try{
  2221. var PageLoader = webNav.QueryInterface(Components.interfaces.nsIWebPageDescriptor);
  2222. pageCookie = PageLoader.currentDescriptor;
  2223. } catch(err) {
  2224. // If no page descriptor is available, just use the view-source URL...
  2225. }
  2226. top.gViewSourceUtils.viewSource(webNav.currentURI.spec, pageCookie, aDocument);
  2227. }
  2228. // doc - document to use for source, or null for this window's document
  2229. // initialTab - name of the initial tab to display, or null for the first tab
  2230. // imageElement - image to load in the Media Tab of the Page Info window; can be null/omitted
  2231. function BrowserPageInfo(doc, initialTab, imageElement) {
  2232. var args = {doc: doc, initialTab: initialTab, imageElement: imageElement};
  2233. var windows = Cc['@mozilla.org/appshell/window-mediator;1']
  2234. .getService(Ci.nsIWindowMediator)
  2235. .getEnumerator("Browser:page-info");
  2236. var documentURL = doc ? doc.location : window.content.document.location;
  2237. // Check for windows matching the url
  2238. while (windows.hasMoreElements()) {
  2239. var currentWindow = windows.getNext();
  2240. if (currentWindow.document.documentElement.getAttribute("relatedUrl") == documentURL) {
  2241. currentWindow.focus();
  2242. currentWindow.resetPageInfo(args);
  2243. return currentWindow;
  2244. }
  2245. }
  2246. // We didn't find a matching window, so open a new one.
  2247. return openDialog("chrome://browser/content/pageinfo/pageInfo.xul", "",
  2248. "chrome,toolbar,dialog=no,resizable", args);
  2249. }
  2250. function URLBarSetURI(aURI) {
  2251. var value = gBrowser.userTypedValue;
  2252. var valid = false;
  2253. if (value == null) {
  2254. let uri = aURI || getWebNavigation().currentURI;
  2255. // Replace initial page URIs with an empty string
  2256. // only if there's no opener (bug 370555).
  2257. if (gInitialPages.indexOf(uri.spec) != -1)
  2258. value = content.opener ? uri.spec : "";
  2259. else
  2260. value = losslessDecodeURI(uri);
  2261. valid = !isBlankPageURL(uri.spec);
  2262. }
  2263. gURLBar.value = value;
  2264. gURLBar.valueIsTyped = !valid;
  2265. SetPageProxyState(valid ? "valid" : "invalid");
  2266. }
  2267. function losslessDecodeURI(aURI) {
  2268. var value = aURI.spec;
  2269. // Try to decode as UTF-8 if there's no encoding sequence that we would break.
  2270. if (!/%25(?:3B|2F|3F|3A|40|26|3D|2B|24|2C|23)/i.test(value))
  2271. try {
  2272. value = decodeURI(value)
  2273. // 1. decodeURI decodes %25 to %, which creates unintended
  2274. // encoding sequences. Re-encode it, unless it's part of
  2275. // a sequence that survived decodeURI, i.e. one for:
  2276. // ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '#'
  2277. // (RFC 3987 section 3.2)
  2278. // 2. Re-encode whitespace so that it doesn't get eaten away
  2279. // by the location bar (bug 410726).
  2280. .replace(/%(?!3B|2F|3F|3A|40|26|3D|2B|24|2C|23)|[\r\n\t]/ig,
  2281. encodeURIComponent);
  2282. } catch (e) {}
  2283. // Encode invisible characters (line and paragraph separator,
  2284. // object replacement character) (bug 452979)
  2285. value = value.replace(/[\v\x0c\x1c\x1d\x1e\x1f\u2028\u2029\ufffc]/g,
  2286. encodeURIComponent);
  2287. // Encode default ignorable characters (bug 546013)
  2288. // except ZWNJ (U+200C) and ZWJ (U+200D) (bug 582186).
  2289. // This includes all bidirectional formatting characters.
  2290. // (RFC 3987 sections 3.2 and 4.1 paragraph 6)
  2291. value = value.replace(/[\u00ad\u034f\u115f-\u1160\u17b4-\u17b5\u180b-\u180d\u200b\u200e-\u200f\u202a-\u202e\u2060-\u206f\u3164\ufe00-\ufe0f\ufeff\uffa0\ufff0-\ufff8]|\ud834[\udd73-\udd7a]|[\udb40-\udb43][\udc00-\udfff]/g,
  2292. encodeURIComponent);
  2293. return value;
  2294. }
  2295. function UpdateUrlbarSearchSplitterState()
  2296. {
  2297. var splitter = document.getElementById("urlbar-search-splitter");
  2298. var urlbar = document.getElementById("urlbar-container");
  2299. var searchbar = document.getElementById("search-container");
  2300. var stop = document.getElementById("stop-button");
  2301. var ibefore = null;
  2302. if (urlbar && searchbar) {
  2303. if (urlbar.nextSibling == searchbar ||
  2304. urlbar.getAttribute("combined") &&
  2305. stop && stop.nextSibling == searchbar)
  2306. ibefore = searchbar;
  2307. else if (searchbar.nextSibling == urlbar)
  2308. ibefore = urlbar;
  2309. }
  2310. if (ibefore) {
  2311. if (!splitter) {
  2312. splitter = document.createElement("splitter");
  2313. splitter.id = "urlbar-search-splitter";
  2314. splitter.setAttribute("resizebefore", "flex");
  2315. splitter.setAttribute("resizeafter", "flex");
  2316. splitter.className = "chromeclass-toolbar-additional";
  2317. }
  2318. urlbar.parentNode.insertBefore(splitter, ibefore);
  2319. } else if (splitter)
  2320. splitter.parentNode.removeChild(splitter);
  2321. }
  2322. function setUrlAndSearchBarWidthForConditionalForwardButton() {
  2323. // Workaround for bug 694084: Showing/hiding the conditional forward button resizes
  2324. // the search bar when the url/search bar splitter hasn't been used.
  2325. var urlbarContainer = document.getElementById("urlbar-container");
  2326. var searchbarContainer = document.getElementById("search-container");
  2327. if (!urlbarContainer ||
  2328. !searchbarContainer ||
  2329. urlbarContainer.hasAttribute("width") ||
  2330. searchbarContainer.hasAttribute("width") ||
  2331. urlbarContainer.parentNode != searchbarContainer.parentNode)
  2332. return;
  2333. urlbarContainer.style.width = searchbarContainer.style.width = "";
  2334. var urlbarWidth = urlbarContainer.clientWidth;
  2335. var searchbarWidth = searchbarContainer.clientWidth;
  2336. urlbarContainer.style.width = urlbarWidth + "px";
  2337. searchbarContainer.style.width = searchbarWidth + "px";
  2338. }
  2339. function UpdatePageProxyState()
  2340. {
  2341. if (gURLBar && gURLBar.value != gLastValidURLStr)
  2342. SetPageProxyState("invalid");
  2343. }
  2344. function SetPageProxyState(aState)
  2345. {
  2346. if (!gURLBar)
  2347. return;
  2348. if (!gProxyFavIcon)
  2349. gProxyFavIcon = document.getElementById("page-proxy-favicon");
  2350. gURLBar.setAttribute("pageproxystate", aState);
  2351. gProxyFavIcon.setAttribute("pageproxystate", aState);
  2352. // the page proxy state is set to valid via OnLocationChange, which
  2353. // gets called when we switch tabs.
  2354. if (aState == "valid") {
  2355. gLastValidURLStr = gURLBar.value;
  2356. gURLBar.addEventListener("input", UpdatePageProxyState, false);
  2357. PageProxySetIcon(gBrowser.getIcon());
  2358. } else if (aState == "invalid") {
  2359. gURLBar.removeEventListener("input", UpdatePageProxyState, false);
  2360. PageProxyClearIcon();
  2361. }
  2362. }
  2363. function PageProxySetIcon (aURL)
  2364. {
  2365. if (!gProxyFavIcon)
  2366. return;
  2367. if (!aURL)
  2368. PageProxyClearIcon();
  2369. else if (gProxyFavIcon.getAttribute("src") != aURL)
  2370. gProxyFavIcon.setAttribute("src", aURL);
  2371. }
  2372. function PageProxyClearIcon ()
  2373. {
  2374. gProxyFavIcon.removeAttribute("src");
  2375. }
  2376. function PageProxyClickHandler(aEvent)
  2377. {
  2378. if (aEvent.button == 1 && gPrefService.getBoolPref("middlemouse.paste"))
  2379. middleMousePaste(aEvent);
  2380. }
  2381. /**
  2382. * Handle load of some pages (about:*) so that we can make modifications
  2383. * to the DOM for unprivileged pages.
  2384. */
  2385. function BrowserOnAboutPageLoad(document) {
  2386. if (/^about:home$/i.test(document.documentURI)) {
  2387. let ss = Components.classes["@mozilla.org/browser/sessionstore;1"].
  2388. getService(Components.interfaces.nsISessionStore);
  2389. if (!ss.canRestoreLastSession)
  2390. document.getElementById("sessionRestoreContainer").hidden = true;
  2391. // Sync-related links
  2392. if (Services.prefs.prefHasUserValue("services.sync.username")) {
  2393. document.getElementById("setupSyncLink").hidden = true;
  2394. }
  2395. }
  2396. }
  2397. /**
  2398. * Handle command events bubbling up from error page content
  2399. */
  2400. function BrowserOnClick(event) {
  2401. // Don't trust synthetic events
  2402. if (!event.isTrusted ||
  2403. (event.target.localName != "button" &&
  2404. event.target.className != "sync-link"))
  2405. return;
  2406. var ot = event.originalTarget;
  2407. var errorDoc = ot.ownerDocument;
  2408. // If the event came from an ssl error page, it is probably either the "Add
  2409. // Exception…" or "Get me out of here!" button
  2410. if (/^about:certerror/.test(errorDoc.documentURI)) {
  2411. if (ot == errorDoc.getElementById('exceptionDialogButton')) {
  2412. var params = { exceptionAdded : false, handlePrivateBrowsing : true };
  2413. try {
  2414. switch (gPrefService.getIntPref("browser.ssl_override_behavior")) {
  2415. case 2 : // Pre-fetch & pre-populate
  2416. params.prefetchCert = true;
  2417. case 1 : // Pre-populate
  2418. params.location = errorDoc.location.href;
  2419. }
  2420. } catch (e) {
  2421. Components.utils.reportError("Couldn't get ssl_override pref: " + e);
  2422. }
  2423. window.openDialog('chrome://pippki/content/exceptionDialog.xul',
  2424. '','chrome,centerscreen,modal', params);
  2425. // If the user added the exception cert, attempt to reload the page
  2426. if (params.exceptionAdded)
  2427. errorDoc.location.reload();
  2428. }
  2429. else if (ot == errorDoc.getElementById('getMeOutOfHereButton')) {
  2430. getMeOutOfHere();
  2431. }
  2432. }
  2433. else if (/^about:blocked/.test(errorDoc.documentURI)) {
  2434. // The event came from a button on a malware/phishing block page
  2435. // First check whether it's malware or phishing, so that we can
  2436. // use the right strings/links
  2437. var isMalware = /e=malwareBlocked/.test(errorDoc.documentURI);
  2438. if (ot == errorDoc.getElementById('getMeOutButton')) {
  2439. getMeOutOfHere();
  2440. }
  2441. else if (ot == errorDoc.getElementById('reportButton')) {
  2442. // This is the "Why is this site blocked" button. For malware,
  2443. // we can fetch a site-specific report, for phishing, we redirect
  2444. // to the generic page describing phishing protection.
  2445. if (isMalware) {
  2446. // Get the stop badware "why is this blocked" report url,
  2447. // append the current url, and go there.
  2448. try {
  2449. let reportURL = formatURL("browser.safebrowsing.malware.reportURL", true);
  2450. reportURL += errorDoc.location.href;
  2451. content.location = reportURL;
  2452. } catch (e) {
  2453. Components.utils.reportError("Couldn't get malware report URL: " + e);
  2454. }
  2455. }
  2456. else { // It's a phishing site, not malware
  2457. try {
  2458. content.location = formatURL("browser.safebrowsing.warning.infoURL", true);
  2459. } catch (e) {
  2460. Components.utils.reportError("Couldn't get phishing info URL: " + e);
  2461. }
  2462. }
  2463. }
  2464. else if (ot == errorDoc.getElementById('ignoreWarningButton')) {
  2465. // Allow users to override and continue through to the site,
  2466. // but add a notify bar as a reminder, so that they don't lose
  2467. // track after, e.g., tab switching.
  2468. gBrowser.loadURIWithFlags(content.location.href,
  2469. nsIWebNavigation.LOAD_FLAGS_BYPASS_CLASSIFIER,
  2470. null, null, null);
  2471. Services.perms.add(makeURI(content.location.href), "safe-browsing",
  2472. Ci.nsIPermissionManager.ALLOW_ACTION,
  2473. Ci.nsIPermissionManager.EXPIRE_SESSION);
  2474. let buttons = [{
  2475. label: gNavigatorBundle.getString("safebrowsing.getMeOutOfHereButton.label"),
  2476. accessKey: gNavigatorBundle.getString("safebrowsing.getMeOutOfHereButton.accessKey"),
  2477. callback: function() { getMeOutOfHere(); }
  2478. }];
  2479. let title;
  2480. if (isMalware) {
  2481. title = gNavigatorBundle.getString("safebrowsing.reportedAttackSite");
  2482. buttons[1] = {
  2483. label: gNavigatorBundle.getString("safebrowsing.notAnAttackButton.label"),
  2484. accessKey: gNavigatorBundle.getString("safebrowsing.notAnAttackButton.accessKey"),
  2485. callback: function() {
  2486. openUILinkIn(safebrowsing.getReportURL('MalwareError'), 'tab');
  2487. }
  2488. };
  2489. } else {
  2490. title = gNavigatorBundle.getString("safebrowsing.reportedWebForgery");
  2491. buttons[1] = {
  2492. label: gNavigatorBundle.getString("safebrowsing.notAForgeryButton.label"),
  2493. accessKey: gNavigatorBundle.getString("safebrowsing.notAForgeryButton.accessKey"),
  2494. callback: function() {
  2495. openUILinkIn(safebrowsing.getReportURL('Error'), 'tab');
  2496. }
  2497. };
  2498. }
  2499. let notificationBox = gBrowser.getNotificationBox();
  2500. let value = "blocked-badware-page";
  2501. let previousNotification = notificationBox.getNotificationWithValue(value);
  2502. if (previousNotification)
  2503. notificationBox.removeNotification(previousNotification);
  2504. notificationBox.appendNotification(
  2505. title,
  2506. value,
  2507. "chrome://global/skin/icons/blacklist_favicon.png",
  2508. notificationBox.PRIORITY_CRITICAL_HIGH,
  2509. buttons
  2510. );
  2511. }
  2512. }
  2513. else if (/^about:home$/i.test(errorDoc.documentURI)) {
  2514. if (ot == errorDoc.getElementById("restorePreviousSession")) {
  2515. let ss = Cc["@mozilla.org/browser/sessionstore;1"].
  2516. getService(Ci.nsISessionStore);
  2517. if (ss.canRestoreLastSession)
  2518. ss.restoreLastSession();
  2519. errorDoc.getElementById("sessionRestoreContainer").hidden = true;
  2520. }
  2521. else if (ot == errorDoc.getElementById("pairDeviceLink")) {
  2522. if (Services.prefs.prefHasUserValue("services.sync.username")) {
  2523. gSyncUI.openAddDevice();
  2524. } else {
  2525. gSyncUI.openSetup("pair");
  2526. }
  2527. }
  2528. else if (ot == errorDoc.getElementById("setupSyncLink")) {
  2529. gSyncUI.openSetup(null);
  2530. }
  2531. }
  2532. }
  2533. /**
  2534. * Re-direct the browser to a known-safe page. This function is
  2535. * used when, for example, the user browses to a known malware page
  2536. * and is presented with about:blocked. The "Get me out of here!"
  2537. * button should take the user to the default start page so that even
  2538. * when their own homepage is infected, we can get them somewhere safe.
  2539. */
  2540. function getMeOutOfHere() {
  2541. // Get the start page from the *default* pref branch, not the user's
  2542. var prefs = Cc["@mozilla.org/preferences-service;1"]
  2543. .getService(Ci.nsIPrefService).getDefaultBranch(null);
  2544. var url = BROWSER_NEW_TAB_URL;
  2545. try {
  2546. url = prefs.getComplexValue("browser.startup.homepage",
  2547. Ci.nsIPrefLocalizedString).data;
  2548. // If url is a pipe-delimited set of pages, just take the first one.
  2549. if (url.indexOf("|") != -1)
  2550. url = url.split("|")[0];
  2551. } catch(e) {
  2552. Components.utils.reportError("Couldn't get homepage pref: " + e);
  2553. }
  2554. content.location = url;
  2555. }
  2556. function BrowserFullScreen()
  2557. {
  2558. window.fullScreen = !window.fullScreen;
  2559. }
  2560. function onFullScreen(event) {
  2561. FullScreen.toggle(event);
  2562. }
  2563. function onMozFullScreenChange(event) {
  2564. FullScreen.enterDomFullScreen(event);
  2565. }
  2566. function onShowFullScreenWarning(event) {
  2567. FullScreen.showWarning(false);
  2568. }
  2569. function getWebNavigation()
  2570. {
  2571. try {
  2572. return gBrowser.webNavigation;
  2573. } catch (e) {
  2574. return null;
  2575. }
  2576. }
  2577. function BrowserReloadWithFlags(reloadFlags) {
  2578. /* First, we'll try to use the session history object to reload so
  2579. * that framesets are handled properly. If we're in a special
  2580. * window (such as view-source) that has no session history, fall
  2581. * back on using the web navigation's reload method.
  2582. */
  2583. var webNav = getWebNavigation();
  2584. try {
  2585. var sh = webNav.sessionHistory;
  2586. if (sh)
  2587. webNav = sh.QueryInterface(nsIWebNavigation);
  2588. } catch (e) {
  2589. }
  2590. try {
  2591. webNav.reload(reloadFlags);
  2592. } catch (e) {
  2593. }
  2594. }
  2595. var PrintPreviewListener = {
  2596. _printPreviewTab: null,
  2597. _tabBeforePrintPreview: null,
  2598. getPrintPreviewBrowser: function () {
  2599. if (!this._printPreviewTab) {
  2600. this._tabBeforePrintPreview = gBrowser.selectedTab;
  2601. this._printPreviewTab = gBrowser.loadOneTab("about:blank",
  2602. { inBackground: false });
  2603. gBrowser.selectedTab = this._printPreviewTab;
  2604. }
  2605. return gBrowser.getBrowserForTab(this._printPreviewTab);
  2606. },
  2607. getSourceBrowser: function () {
  2608. return this._tabBeforePrintPreview ?
  2609. this._tabBeforePrintPreview.linkedBrowser : gBrowser.selectedBrowser;
  2610. },
  2611. getNavToolbox: function () {
  2612. return gNavToolbox;
  2613. },
  2614. onEnter: function () {
  2615. gInPrintPreviewMode = true;
  2616. this._toggleAffectedChrome();
  2617. },
  2618. onExit: function () {
  2619. gBrowser.selectedTab = this._tabBeforePrintPreview;
  2620. this._tabBeforePrintPreview = null;
  2621. gInPrintPreviewMode = false;
  2622. this._toggleAffectedChrome();
  2623. gBrowser.removeTab(this._printPreviewTab);
  2624. this._printPreviewTab = null;
  2625. },
  2626. _toggleAffectedChrome: function () {
  2627. gNavToolbox.collapsed = gInPrintPreviewMode;
  2628. if (gInPrintPreviewMode)
  2629. this._hideChrome();
  2630. else
  2631. this._showChrome();
  2632. if (this._chromeState.sidebarOpen)
  2633. toggleSidebar(this._sidebarCommand);
  2634. #ifdef MENUBAR_CAN_AUTOHIDE
  2635. updateAppButtonDisplay();
  2636. #endif
  2637. },
  2638. _hideChrome: function () {
  2639. this._chromeState = {};
  2640. var sidebar = document.getElementById("sidebar-box");
  2641. this._chromeState.sidebarOpen = !sidebar.hidden;
  2642. this._sidebarCommand = sidebar.getAttribute("sidebarcommand");
  2643. var notificationBox = gBrowser.getNotificationBox();
  2644. this._chromeState.notificationsOpen = !notificationBox.notificationsHidden;
  2645. notificationBox.notificationsHidden = true;
  2646. document.getElementById("sidebar").setAttribute("src", "about:blank");
  2647. var addonBar = document.getElementById("addon-bar");
  2648. this._chromeState.addonBarOpen = !addonBar.collapsed;
  2649. addonBar.collapsed = true;
  2650. gBrowser.updateWindowResizers();
  2651. this._chromeState.findOpen = gFindBarInitialized && !gFindBar.hidden;
  2652. if (gFindBarInitialized)
  2653. gFindBar.close();
  2654. this._chromeState.syncNotificationsOpen = false;
  2655. var syncNotifications = document.getElementById("sync-notifications");
  2656. if (syncNotifications) {
  2657. this._chromeState.syncNotificationsOpen = !syncNotifications.notificationsHidden;
  2658. syncNotifications.notificationsHidden = true;
  2659. }
  2660. },
  2661. _showChrome: function () {
  2662. if (this._chromeState.notificationsOpen)
  2663. gBrowser.getNotificationBox().notificationsHidden = false;
  2664. if (this._chromeState.addonBarOpen) {
  2665. document.getElementById("addon-bar").collapsed = false;
  2666. gBrowser.updateWindowResizers();
  2667. }
  2668. if (this._chromeState.findOpen)
  2669. gFindBar.open();
  2670. if (this._chromeState.syncNotificationsOpen)
  2671. document.getElementById("sync-notifications").notificationsHidden = false;
  2672. }
  2673. }
  2674. function getMarkupDocumentViewer()
  2675. {
  2676. return gBrowser.markupDocumentViewer;
  2677. }
  2678. /**
  2679. * Content area tooltip.
  2680. * XXX - this must move into XBL binding/equiv! Do not want to pollute
  2681. * browser.js with functionality that can be encapsulated into
  2682. * browser widget. TEMPORARY!
  2683. *
  2684. * NOTE: Any changes to this routine need to be mirrored in DefaultTooltipTextProvider::GetNodeText()
  2685. * (located in mozilla/embedding/browser/webBrowser/nsDocShellTreeOwner.cpp)
  2686. * which performs the same function, but for embedded clients that
  2687. * don't use a XUL/JS layer. It is important that the logic of
  2688. * these two routines be kept more or less in sync.
  2689. * (pinkerton)
  2690. **/
  2691. function FillInHTMLTooltip(tipElement)
  2692. {
  2693. var retVal = false;
  2694. // Don't show the tooltip if the tooltip node is a XUL element, a document or is disconnected.
  2695. if (tipElement.namespaceURI == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" ||
  2696. !tipElement.ownerDocument ||
  2697. (tipElement.ownerDocument.compareDocumentPosition(tipElement) & document.DOCUMENT_POSITION_DISCONNECTED))
  2698. return retVal;
  2699. const XLinkNS = "http://www.w3.org/1999/xlink";
  2700. var titleText = null;
  2701. var XLinkTitleText = null;
  2702. var SVGTitleText = null;
  2703. var lookingForSVGTitle = true;
  2704. var direction = tipElement.ownerDocument.dir;
  2705. // If the element is invalid per HTML5 Forms specifications and has no title,
  2706. // show the constraint validation error message.
  2707. if ((tipElement instanceof HTMLInputElement ||
  2708. tipElement instanceof HTMLTextAreaElement ||
  2709. tipElement instanceof HTMLSelectElement ||
  2710. tipElement instanceof HTMLButtonElement) &&
  2711. !tipElement.hasAttribute('title') &&
  2712. (!tipElement.form || !tipElement.form.noValidate)) {
  2713. // If the element is barred from constraint validation or valid,
  2714. // the validation message will be the empty string.
  2715. titleText = tipElement.validationMessage;
  2716. }
  2717. while (!titleText && !XLinkTitleText && !SVGTitleText && tipElement) {
  2718. if (tipElement.nodeType == Node.ELEMENT_NODE) {
  2719. titleText = tipElement.getAttribute("title");
  2720. if ((tipElement instanceof HTMLAnchorElement ||
  2721. tipElement instanceof HTMLAreaElement ||
  2722. tipElement instanceof HTMLLinkElement ||
  2723. tipElement instanceof SVGAElement) && tipElement.href) {
  2724. XLinkTitleText = tipElement.getAttributeNS(XLinkNS, "title");
  2725. }
  2726. if (lookingForSVGTitle &&
  2727. (!(tipElement instanceof SVGElement) ||
  2728. tipElement.parentNode.nodeType == Node.DOCUMENT_NODE)) {
  2729. lookingForSVGTitle = false;
  2730. }
  2731. if (lookingForSVGTitle) {
  2732. let length = tipElement.childNodes.length;
  2733. for (let i = 0; i < length; i++) {
  2734. let childNode = tipElement.childNodes[i];
  2735. if (childNode instanceof SVGTitleElement) {
  2736. SVGTitleText = childNode.textContent;
  2737. break;
  2738. }
  2739. }
  2740. }
  2741. var defView = tipElement.ownerDocument.defaultView;
  2742. // XXX Work around bug 350679:
  2743. // "Tooltips can be fired in documents with no view".
  2744. if (!defView)
  2745. return retVal;
  2746. direction = defView.getComputedStyle(tipElement, "")
  2747. .getPropertyValue("direction");
  2748. }
  2749. tipElement = tipElement.parentNode;
  2750. }
  2751. var tipNode = document.getElementById("aHTMLTooltip");
  2752. tipNode.style.direction = direction;
  2753. [titleText, XLinkTitleText, SVGTitleText].forEach(function (t) {
  2754. if (t && /\S/.test(t)) {
  2755. // Make CRLF and CR render one line break each.
  2756. t = t.replace(/\r\n?/g, '\n');
  2757. tipNode.setAttribute("label", t);
  2758. retVal = true;
  2759. }
  2760. });
  2761. return retVal;
  2762. }
  2763. var browserDragAndDrop = {
  2764. canDropLink: function (aEvent) Services.droppedLinkHandler.canDropLink(aEvent, true),
  2765. dragOver: function (aEvent)
  2766. {
  2767. if (this.canDropLink(aEvent)) {
  2768. aEvent.preventDefault();
  2769. }
  2770. },
  2771. drop: function (aEvent, aName, aDisallowInherit) {
  2772. return Services.droppedLinkHandler.dropLink(aEvent, aName, aDisallowInherit);
  2773. }
  2774. };
  2775. var homeButtonObserver = {
  2776. onDrop: function (aEvent)
  2777. {
  2778. // disallow setting home pages that inherit the principal
  2779. let url = browserDragAndDrop.drop(aEvent, {}, true);
  2780. setTimeout(openHomeDialog, 0, url);
  2781. },
  2782. onDragOver: function (aEvent)
  2783. {
  2784. browserDragAndDrop.dragOver(aEvent);
  2785. aEvent.dropEffect = "link";
  2786. },
  2787. onDragExit: function (aEvent)
  2788. {
  2789. }
  2790. }
  2791. function openHomeDialog(aURL)
  2792. {
  2793. var promptTitle = gNavigatorBundle.getString("droponhometitle");
  2794. var promptMsg = gNavigatorBundle.getString("droponhomemsg");
  2795. var pressedVal = Services.prompt.confirmEx(window, promptTitle, promptMsg,
  2796. Services.prompt.STD_YES_NO_BUTTONS,
  2797. null, null, null, null, {value:0});
  2798. if (pressedVal == 0) {
  2799. try {
  2800. var str = Components.classes["@mozilla.org/supports-string;1"]
  2801. .createInstance(Components.interfaces.nsISupportsString);
  2802. str.data = aURL;
  2803. gPrefService.setComplexValue("browser.startup.homepage",
  2804. Components.interfaces.nsISupportsString, str);
  2805. } catch (ex) {
  2806. dump("Failed to set the home page.\n"+ex+"\n");
  2807. }
  2808. }
  2809. }
  2810. var bookmarksButtonObserver = {
  2811. onDrop: function (aEvent)
  2812. {
  2813. let name = { };
  2814. let url = browserDragAndDrop.drop(aEvent, name);
  2815. try {
  2816. PlacesUIUtils.showBookmarkDialog({ action: "add"
  2817. , type: "bookmark"
  2818. , uri: makeURI(url)
  2819. , title: name
  2820. , hiddenRows: [ "description"
  2821. , "location"
  2822. , "loadInSidebar"
  2823. , "keyword" ]
  2824. }, window);
  2825. } catch(ex) { }
  2826. },
  2827. onDragOver: function (aEvent)
  2828. {
  2829. browserDragAndDrop.dragOver(aEvent);
  2830. aEvent.dropEffect = "link";
  2831. },
  2832. onDragExit: function (aEvent)
  2833. {
  2834. }
  2835. }
  2836. var newTabButtonObserver = {
  2837. onDragOver: function (aEvent)
  2838. {
  2839. browserDragAndDrop.dragOver(aEvent);
  2840. },
  2841. onDragExit: function (aEvent)
  2842. {
  2843. },
  2844. onDrop: function (aEvent)
  2845. {
  2846. let url = browserDragAndDrop.drop(aEvent, { });
  2847. var postData = {};
  2848. url = getShortcutOrURI(url, postData);
  2849. if (url) {
  2850. // allow third-party services to fixup this URL
  2851. openNewTabWith(url, null, postData.value, aEvent, true);
  2852. }
  2853. }
  2854. }
  2855. var newWindowButtonObserver = {
  2856. onDragOver: function (aEvent)
  2857. {
  2858. browserDragAndDrop.dragOver(aEvent);
  2859. },
  2860. onDragExit: function (aEvent)
  2861. {
  2862. },
  2863. onDrop: function (aEvent)
  2864. {
  2865. let url = browserDragAndDrop.drop(aEvent, { });
  2866. var postData = {};
  2867. url = getShortcutOrURI(url, postData);
  2868. if (url) {
  2869. // allow third-party services to fixup this URL
  2870. openNewWindowWith(url, null, postData.value, true);
  2871. }
  2872. }
  2873. }
  2874. var DownloadsButtonDNDObserver = {
  2875. onDragOver: function (aEvent)
  2876. {
  2877. var types = aEvent.dataTransfer.types;
  2878. if (types.contains("text/x-moz-url") ||
  2879. types.contains("text/uri-list") ||
  2880. types.contains("text/plain"))
  2881. aEvent.preventDefault();
  2882. },
  2883. onDragExit: function (aEvent)
  2884. {
  2885. },
  2886. onDrop: function (aEvent)
  2887. {
  2888. let name = { };
  2889. let url = browserDragAndDrop.drop(aEvent, name);
  2890. if (url)
  2891. saveURL(url, name, null, true, true);
  2892. }
  2893. }
  2894. const DOMLinkHandler = {
  2895. handleEvent: function (event) {
  2896. switch (event.type) {
  2897. case "DOMLinkAdded":
  2898. this.onLinkAdded(event);
  2899. break;
  2900. }
  2901. },
  2902. onLinkAdded: function (event) {
  2903. var link = event.originalTarget;
  2904. var rel = link.rel && link.rel.toLowerCase();
  2905. if (!link || !link.ownerDocument || !rel || !link.href)
  2906. return;
  2907. var feedAdded = false;
  2908. var iconAdded = false;
  2909. var searchAdded = false;
  2910. var relStrings = rel.split(/\s+/);
  2911. var rels = {};
  2912. for (let i = 0; i < relStrings.length; i++)
  2913. rels[relStrings[i]] = true;
  2914. for (let relVal in rels) {
  2915. switch (relVal) {
  2916. case "feed":
  2917. case "alternate":
  2918. if (!feedAdded) {
  2919. if (!rels.feed && rels.alternate && rels.stylesheet)
  2920. break;
  2921. if (isValidFeed(link, link.ownerDocument.nodePrincipal, rels.feed)) {
  2922. FeedHandler.addFeed(link, link.ownerDocument);
  2923. feedAdded = true;
  2924. }
  2925. }
  2926. break;
  2927. case "icon":
  2928. if (!iconAdded) {
  2929. if (!gPrefService.getBoolPref("browser.chrome.site_icons"))
  2930. break;
  2931. var targetDoc = link.ownerDocument;
  2932. var uri = makeURI(link.href, targetDoc.characterSet);
  2933. if (gBrowser.isFailedIcon(uri))
  2934. break;
  2935. // Verify that the load of this icon is legal.
  2936. // Some error or special pages can load their favicon.
  2937. // To be on the safe side, only allow chrome:// favicons.
  2938. var isAllowedPage = [
  2939. /^about:neterror\?/,
  2940. /^about:blocked\?/,
  2941. /^about:certerror\?/,
  2942. /^about:home$/,
  2943. ].some(function (re) re.test(targetDoc.documentURI));
  2944. if (!isAllowedPage || !uri.schemeIs("chrome")) {
  2945. var ssm = Cc["@mozilla.org/scriptsecuritymanager;1"].
  2946. getService(Ci.nsIScriptSecurityManager);
  2947. try {
  2948. ssm.checkLoadURIWithPrincipal(targetDoc.nodePrincipal, uri,
  2949. Ci.nsIScriptSecurityManager.DISALLOW_SCRIPT);
  2950. } catch(e) {
  2951. break;
  2952. }
  2953. }
  2954. try {
  2955. var contentPolicy = Cc["@mozilla.org/layout/content-policy;1"].
  2956. getService(Ci.nsIContentPolicy);
  2957. } catch(e) {
  2958. break; // Refuse to load if we can't do a security check.
  2959. }
  2960. // Security says okay, now ask content policy
  2961. if (contentPolicy.shouldLoad(Ci.nsIContentPolicy.TYPE_IMAGE,
  2962. uri, targetDoc.documentURIObject,
  2963. link, link.type, null)
  2964. != Ci.nsIContentPolicy.ACCEPT)
  2965. break;
  2966. var browserIndex = gBrowser.getBrowserIndexForDocument(targetDoc);
  2967. // no browser? no favicon.
  2968. if (browserIndex == -1)
  2969. break;
  2970. let tab = gBrowser.tabs[browserIndex];
  2971. gBrowser.setIcon(tab, link.href);
  2972. iconAdded = true;
  2973. }
  2974. break;
  2975. case "search":
  2976. if (!searchAdded) {
  2977. var type = link.type && link.type.toLowerCase();
  2978. type = type.replace(/^\s+|\s*(?:;.*)?$/g, "");
  2979. if (type == "application/opensearchdescription+xml" && link.title &&
  2980. /^(?:https?|ftp):/i.test(link.href) &&
  2981. !gPrivateBrowsingUI.privateBrowsingEnabled) {
  2982. var engine = { title: link.title, href: link.href };
  2983. BrowserSearch.addEngine(engine, link.ownerDocument);
  2984. searchAdded = true;
  2985. }
  2986. }
  2987. break;
  2988. }
  2989. }
  2990. }
  2991. }
  2992. const BrowserSearch = {
  2993. addEngine: function(engine, targetDoc) {
  2994. if (!this.searchBar)
  2995. return;
  2996. var browser = gBrowser.getBrowserForDocument(targetDoc);
  2997. // ignore search engines from subframes (see bug 479408)
  2998. if (!browser)
  2999. return;
  3000. // Check to see whether we've already added an engine with this title
  3001. if (browser.engines) {
  3002. if (browser.engines.some(function (e) e.title == engine.title))
  3003. return;
  3004. }
  3005. // Append the URI and an appropriate title to the browser data.
  3006. // Use documentURIObject in the check for shouldLoadFavIcon so that we
  3007. // do the right thing with about:-style error pages. Bug 453442
  3008. var iconURL = null;
  3009. if (gBrowser.shouldLoadFavIcon(targetDoc.documentURIObject))
  3010. iconURL = targetDoc.documentURIObject.prePath + "/favicon.ico";
  3011. var hidden = false;
  3012. // If this engine (identified by title) is already in the list, add it
  3013. // to the list of hidden engines rather than to the main list.
  3014. // XXX This will need to be changed when engines are identified by URL;
  3015. // see bug 335102.
  3016. if (Services.search.getEngineByName(engine.title))
  3017. hidden = true;
  3018. var engines = (hidden ? browser.hiddenEngines : browser.engines) || [];
  3019. engines.push({ uri: engine.href,
  3020. title: engine.title,
  3021. icon: iconURL });
  3022. if (hidden)
  3023. browser.hiddenEngines = engines;
  3024. else
  3025. browser.engines = engines;
  3026. },
  3027. /**
  3028. * Gives focus to the search bar, if it is present on the toolbar, or loads
  3029. * the default engine's search form otherwise. For Mac, opens a new window
  3030. * or focuses an existing window, if necessary.
  3031. */
  3032. webSearch: function BrowserSearch_webSearch() {
  3033. #ifdef XP_MACOSX
  3034. if (window.location.href != getBrowserURL()) {
  3035. var win = getTopWin();
  3036. if (win) {
  3037. // If there's an open browser window, it should handle this command
  3038. win.focus();
  3039. win.BrowserSearch.webSearch();
  3040. } else {
  3041. // If there are no open browser windows, open a new one
  3042. function observer(subject, topic, data) {
  3043. if (subject == win) {
  3044. BrowserSearch.webSearch();
  3045. Services.obs.removeObserver(observer, "browser-delayed-startup-finished");
  3046. }
  3047. }
  3048. win = window.openDialog(getBrowserURL(), "_blank",
  3049. "chrome,all,dialog=no", "about:blank");
  3050. Services.obs.addObserver(observer, "browser-delayed-startup-finished", false);
  3051. }
  3052. return;
  3053. }
  3054. #endif
  3055. var searchBar = this.searchBar;
  3056. if (searchBar && window.fullScreen)
  3057. FullScreen.mouseoverToggle(true);
  3058. if (isElementVisible(searchBar)) {
  3059. searchBar.select();
  3060. searchBar.focus();
  3061. } else {
  3062. openUILinkIn(Services.search.defaultEngine.searchForm, "current");
  3063. }
  3064. },
  3065. /**
  3066. * Loads a search results page, given a set of search terms. Uses the current
  3067. * engine if the search bar is visible, or the default engine otherwise.
  3068. *
  3069. * @param searchText
  3070. * The search terms to use for the search.
  3071. *
  3072. * @param useNewTab
  3073. * Boolean indicating whether or not the search should load in a new
  3074. * tab.
  3075. *
  3076. * @param responseType [optional]
  3077. * The MIME type that we'd like to receive in response
  3078. * to this submission. If null or the the response type is not supported
  3079. * for the search engine, will fallback to "text/html".
  3080. */
  3081. loadSearch: function BrowserSearch_search(searchText, useNewTab, responseType) {
  3082. var engine;
  3083. // If the search bar is visible, use the current engine, otherwise, fall
  3084. // back to the default engine.
  3085. if (isElementVisible(this.searchBar))
  3086. engine = Services.search.currentEngine;
  3087. else
  3088. engine = Services.search.defaultEngine;
  3089. var submission = engine.getSubmission(searchText, responseType);
  3090. // If a response type was specified and getSubmission returned null,
  3091. // fallback to the default response type.
  3092. if (!submission && responseType)
  3093. submission = engine.getSubmission(searchText);
  3094. // getSubmission can return null if the engine doesn't have a URL
  3095. // with a text/html response type. This is unlikely (since
  3096. // SearchService._addEngineToStore() should fail for such an engine),
  3097. // but let's be on the safe side.
  3098. if (!submission)
  3099. return;
  3100. let inBackground = Services.prefs.getBoolPref("browser.search.context.loadInBackground");
  3101. openLinkIn(submission.uri.spec,
  3102. useNewTab ? "tab" : "current",
  3103. { postData: submission.postData,
  3104. inBackground: inBackground,
  3105. relatedToCurrent: true });
  3106. },
  3107. /**
  3108. * Returns the search bar element if it is present in the toolbar, null otherwise.
  3109. */
  3110. get searchBar() {
  3111. return document.getElementById("searchbar");
  3112. },
  3113. loadAddEngines: function BrowserSearch_loadAddEngines() {
  3114. var newWindowPref = gPrefService.getIntPref("browser.link.open_newwindow");
  3115. var where = newWindowPref == 3 ? "tab" : "window";
  3116. var regionBundle = document.getElementById("bundle_browser_region");
  3117. var searchEnginesURL = formatURL("browser.search.searchEnginesURL", true);
  3118. openUILinkIn(searchEnginesURL, where);
  3119. }
  3120. }
  3121. function FillHistoryMenu(aParent) {
  3122. // Lazily add the hover listeners on first showing and never remove them
  3123. if (!aParent.hasStatusListener) {
  3124. // Show history item's uri in the status bar when hovering, and clear on exit
  3125. aParent.addEventListener("DOMMenuItemActive", function(aEvent) {
  3126. // Only the current page should have the checked attribute, so skip it
  3127. if (!aEvent.target.hasAttribute("checked"))
  3128. XULBrowserWindow.setOverLink(aEvent.target.getAttribute("uri"));
  3129. }, false);
  3130. aParent.addEventListener("DOMMenuItemInactive", function() {
  3131. XULBrowserWindow.setOverLink("");
  3132. }, false);
  3133. aParent.hasStatusListener = true;
  3134. }
  3135. // Remove old entries if any
  3136. var children = aParent.childNodes;
  3137. for (var i = children.length - 1; i >= 0; --i) {
  3138. if (children[i].hasAttribute("index"))
  3139. aParent.removeChild(children[i]);
  3140. }
  3141. var webNav = getWebNavigation();
  3142. var sessionHistory = webNav.sessionHistory;
  3143. var count = sessionHistory.count;
  3144. if (count <= 1) // don't display the popup for a single item
  3145. return false;
  3146. const MAX_HISTORY_MENU_ITEMS = 15;
  3147. var index = sessionHistory.index;
  3148. var half_length = Math.floor(MAX_HISTORY_MENU_ITEMS / 2);
  3149. var start = Math.max(index - half_length, 0);
  3150. var end = Math.min(start == 0 ? MAX_HISTORY_MENU_ITEMS : index + half_length + 1, count);
  3151. if (end == count)
  3152. start = Math.max(count - MAX_HISTORY_MENU_ITEMS, 0);
  3153. var tooltipBack = gNavigatorBundle.getString("tabHistory.goBack");
  3154. var tooltipCurrent = gNavigatorBundle.getString("tabHistory.current");
  3155. var tooltipForward = gNavigatorBundle.getString("tabHistory.goForward");
  3156. for (var j = end - 1; j >= start; j--) {
  3157. let item = document.createElement("menuitem");
  3158. let entry = sessionHistory.getEntryAtIndex(j, false);
  3159. let uri = entry.URI.spec;
  3160. item.setAttribute("uri", uri);
  3161. item.setAttribute("label", entry.title || uri);
  3162. item.setAttribute("index", j);
  3163. if (j != index) {
  3164. try {
  3165. let iconURL = Cc["@mozilla.org/browser/favicon-service;1"]
  3166. .getService(Ci.nsIFaviconService)
  3167. .getFaviconForPage(entry.URI).spec;
  3168. item.style.listStyleImage = "url(" + iconURL + ")";
  3169. } catch (ex) {}
  3170. }
  3171. if (j < index) {
  3172. item.className = "unified-nav-back menuitem-iconic menuitem-with-favicon";
  3173. item.setAttribute("tooltiptext", tooltipBack);
  3174. } else if (j == index) {
  3175. item.setAttribute("type", "radio");
  3176. item.setAttribute("checked", "true");
  3177. item.className = "unified-nav-current";
  3178. item.setAttribute("tooltiptext", tooltipCurrent);
  3179. } else {
  3180. item.className = "unified-nav-forward menuitem-iconic menuitem-with-favicon";
  3181. item.setAttribute("tooltiptext", tooltipForward);
  3182. }
  3183. aParent.appendChild(item);
  3184. }
  3185. return true;
  3186. }
  3187. function addToUrlbarHistory(aUrlToAdd) {
  3188. if (aUrlToAdd &&
  3189. aUrlToAdd.indexOf(" ") == -1 &&
  3190. !/[\x00-\x1F]/.test(aUrlToAdd))
  3191. PlacesUIUtils.markPageAsTyped(aUrlToAdd);
  3192. }
  3193. function toJavaScriptConsole()
  3194. {
  3195. toOpenWindowByType("global:console", "chrome://global/content/console.xul");
  3196. }
  3197. function BrowserDownloadsUI()
  3198. {
  3199. Cc["@mozilla.org/download-manager-ui;1"].
  3200. getService(Ci.nsIDownloadManagerUI).show(window);
  3201. }
  3202. function toOpenWindowByType(inType, uri, features)
  3203. {
  3204. var topWindow = Services.wm.getMostRecentWindow(inType);
  3205. if (topWindow)
  3206. topWindow.focus();
  3207. else if (features)
  3208. window.open(uri, "_blank", features);
  3209. else
  3210. window.open(uri, "_blank", "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar");
  3211. }
  3212. function OpenBrowserWindow()
  3213. {
  3214. var charsetArg = new String();
  3215. var handler = Components.classes["@mozilla.org/browser/clh;1"]
  3216. .getService(Components.interfaces.nsIBrowserHandler);
  3217. var defaultArgs = handler.defaultArgs;
  3218. var wintype = document.documentElement.getAttribute('windowtype');
  3219. // if and only if the current window is a browser window and it has a document with a character
  3220. // set, then extract the current charset menu setting from the current document and use it to
  3221. // initialize the new browser window...
  3222. var win;
  3223. if (window && (wintype == "navigator:browser") && window.content && window.content.document)
  3224. {
  3225. var DocCharset = window.content.document.characterSet;
  3226. charsetArg = "charset="+DocCharset;
  3227. //we should "inherit" the charset menu setting in a new window
  3228. win = window.openDialog("chrome://browser/content/", "_blank", "chrome,all,dialog=no", defaultArgs, charsetArg);
  3229. }
  3230. else // forget about the charset information.
  3231. {
  3232. win = window.openDialog("chrome://browser/content/", "_blank", "chrome,all,dialog=no", defaultArgs);
  3233. }
  3234. return win;
  3235. }
  3236. var gCustomizeSheet = false;
  3237. // Returns a reference to the window in which the toolbar
  3238. // customization document is loaded.
  3239. function BrowserCustomizeToolbar()
  3240. {
  3241. // Disable the toolbar context menu items
  3242. var menubar = document.getElementById("main-menubar");
  3243. for (var i = 0; i < menubar.childNodes.length; ++i)
  3244. menubar.childNodes[i].setAttribute("disabled", true);
  3245. var cmd = document.getElementById("cmd_CustomizeToolbars");
  3246. cmd.setAttribute("disabled", "true");
  3247. var splitter = document.getElementById("urlbar-search-splitter");
  3248. if (splitter)
  3249. splitter.parentNode.removeChild(splitter);
  3250. CombinedStopReload.uninit();
  3251. PlacesToolbarHelper.customizeStart();
  3252. BookmarksMenuButton.customizeStart();
  3253. TabsInTitlebar.allowedBy("customizing-toolbars", false);
  3254. var customizeURL = "chrome://global/content/customizeToolbar.xul";
  3255. gCustomizeSheet = getBoolPref("toolbar.customization.usesheet", false);
  3256. if (gCustomizeSheet) {
  3257. var sheetFrame = document.getElementById("customizeToolbarSheetIFrame");
  3258. var panel = document.getElementById("customizeToolbarSheetPopup");
  3259. sheetFrame.hidden = false;
  3260. sheetFrame.toolbox = gNavToolbox;
  3261. sheetFrame.panel = panel;
  3262. // The document might not have been loaded yet, if this is the first time.
  3263. // If it is already loaded, reload it so that the onload initialization code
  3264. // re-runs.
  3265. if (sheetFrame.getAttribute("src") == customizeURL)
  3266. sheetFrame.contentWindow.location.reload()
  3267. else
  3268. sheetFrame.setAttribute("src", customizeURL);
  3269. // Open the panel, but make it invisible until the iframe has loaded so
  3270. // that the user doesn't see a white flash.
  3271. panel.style.visibility = "hidden";
  3272. gNavToolbox.addEventListener("beforecustomization", function () {
  3273. gNavToolbox.removeEventListener("beforecustomization", arguments.callee, false);
  3274. panel.style.removeProperty("visibility");
  3275. }, false);
  3276. panel.openPopup(gNavToolbox, "after_start", 0, 0);
  3277. return sheetFrame.contentWindow;
  3278. } else {
  3279. return window.openDialog(customizeURL,
  3280. "CustomizeToolbar",
  3281. "chrome,titlebar,toolbar,location,resizable,dependent",
  3282. gNavToolbox);
  3283. }
  3284. }
  3285. function BrowserToolboxCustomizeDone(aToolboxChanged) {
  3286. if (gCustomizeSheet) {
  3287. document.getElementById("customizeToolbarSheetIFrame").hidden = true;
  3288. document.getElementById("customizeToolbarSheetPopup").hidePopup();
  3289. }
  3290. // Update global UI elements that may have been added or removed
  3291. if (aToolboxChanged) {
  3292. gURLBar = document.getElementById("urlbar");
  3293. gProxyFavIcon = document.getElementById("page-proxy-favicon");
  3294. gHomeButton.updateTooltip();
  3295. gIdentityHandler._cacheElements();
  3296. window.XULBrowserWindow.init();
  3297. #ifndef XP_MACOSX
  3298. updateEditUIVisibility();
  3299. #endif
  3300. // Hacky: update the PopupNotifications' object's reference to the iconBox,
  3301. // if it already exists, since it may have changed if the URL bar was
  3302. // added/removed.
  3303. if (!__lookupGetter__("PopupNotifications"))
  3304. PopupNotifications.iconBox = document.getElementById("notification-popup-box");
  3305. }
  3306. PlacesToolbarHelper.customizeDone();
  3307. BookmarksMenuButton.customizeDone();
  3308. // The url bar splitter state is dependent on whether stop/reload
  3309. // and the location bar are combined, so we need this ordering
  3310. CombinedStopReload.init();
  3311. UpdateUrlbarSearchSplitterState();
  3312. setUrlAndSearchBarWidthForConditionalForwardButton();
  3313. // Update the urlbar
  3314. if (gURLBar) {
  3315. URLBarSetURI();
  3316. XULBrowserWindow.asyncUpdateUI();
  3317. PlacesStarButton.updateState();
  3318. }
  3319. TabsInTitlebar.allowedBy("customizing-toolbars", true);
  3320. // Re-enable parts of the UI we disabled during the dialog
  3321. var menubar = document.getElementById("main-menubar");
  3322. for (var i = 0; i < menubar.childNodes.length; ++i)
  3323. menubar.childNodes[i].setAttribute("disabled", false);
  3324. var cmd = document.getElementById("cmd_CustomizeToolbars");
  3325. cmd.removeAttribute("disabled");
  3326. // make sure to re-enable click-and-hold
  3327. if (!getBoolPref("ui.click_hold_context_menus", false))
  3328. SetClickAndHoldHandlers();
  3329. window.content.focus();
  3330. }
  3331. function BrowserToolboxCustomizeChange(aType) {
  3332. switch (aType) {
  3333. case "iconsize":
  3334. case "mode":
  3335. retrieveToolbarIconsizesFromTheme();
  3336. break;
  3337. default:
  3338. gHomeButton.updatePersonalToolbarStyle();
  3339. BookmarksMenuButton.customizeChange();
  3340. allTabs.readPref();
  3341. }
  3342. }
  3343. /**
  3344. * Allows themes to override the "iconsize" attribute on toolbars.
  3345. */
  3346. function retrieveToolbarIconsizesFromTheme() {
  3347. function retrieveToolbarIconsize(aToolbar) {
  3348. if (aToolbar.localName != "toolbar")
  3349. return;
  3350. // The theme indicates that it wants to override the "iconsize" attribute
  3351. // by specifying a special value for the "counter-reset" property on the
  3352. // toolbar. A custom property cannot be used because getComputedStyle can
  3353. // only return the values of standard CSS properties.
  3354. let counterReset = getComputedStyle(aToolbar).counterReset;
  3355. if (counterReset == "smallicons 0")
  3356. aToolbar.setAttribute("iconsize", "small");
  3357. else if (counterReset == "largeicons 0")
  3358. aToolbar.setAttribute("iconsize", "large");
  3359. }
  3360. Array.forEach(gNavToolbox.childNodes, retrieveToolbarIconsize);
  3361. gNavToolbox.externalToolbars.forEach(retrieveToolbarIconsize);
  3362. }
  3363. /**
  3364. * Update the global flag that tracks whether or not any edit UI (the Edit menu,
  3365. * edit-related items in the context menu, and edit-related toolbar buttons
  3366. * is visible, then update the edit commands' enabled state accordingly. We use
  3367. * this flag to skip updating the edit commands on focus or selection changes
  3368. * when no UI is visible to improve performance (including pageload performance,
  3369. * since focus changes when you load a new page).
  3370. *
  3371. * If UI is visible, we use goUpdateGlobalEditMenuItems to set the commands'
  3372. * enabled state so the UI will reflect it appropriately.
  3373. *
  3374. * If the UI isn't visible, we enable all edit commands so keyboard shortcuts
  3375. * still work and just lazily disable them as needed when the user presses a
  3376. * shortcut.
  3377. *
  3378. * This doesn't work on Mac, since Mac menus flash when users press their
  3379. * keyboard shortcuts, so edit UI is essentially always visible on the Mac,
  3380. * and we need to always update the edit commands. Thus on Mac this function
  3381. * is a no op.
  3382. */
  3383. function updateEditUIVisibility()
  3384. {
  3385. #ifndef XP_MACOSX
  3386. let editMenuPopupState = document.getElementById("menu_EditPopup").state;
  3387. let contextMenuPopupState = document.getElementById("contentAreaContextMenu").state;
  3388. let placesContextMenuPopupState = document.getElementById("placesContext").state;
  3389. #ifdef MENUBAR_CAN_AUTOHIDE
  3390. let appMenuPopupState = document.getElementById("appmenu-popup").state;
  3391. #endif
  3392. // The UI is visible if the Edit menu is opening or open, if the context menu
  3393. // is open, or if the toolbar has been customized to include the Cut, Copy,
  3394. // or Paste toolbar buttons.
  3395. gEditUIVisible = editMenuPopupState == "showing" ||
  3396. editMenuPopupState == "open" ||
  3397. contextMenuPopupState == "showing" ||
  3398. contextMenuPopupState == "open" ||
  3399. placesContextMenuPopupState == "showing" ||
  3400. placesContextMenuPopupState == "open" ||
  3401. #ifdef MENUBAR_CAN_AUTOHIDE
  3402. appMenuPopupState == "showing" ||
  3403. appMenuPopupState == "open" ||
  3404. #endif
  3405. document.getElementById("cut-button") ||
  3406. document.getElementById("copy-button") ||
  3407. document.getElementById("paste-button") ? true : false;
  3408. // If UI is visible, update the edit commands' enabled state to reflect
  3409. // whether or not they are actually enabled for the current focus/selection.
  3410. if (gEditUIVisible)
  3411. goUpdateGlobalEditMenuItems();
  3412. // Otherwise, enable all commands, so that keyboard shortcuts still work,
  3413. // then lazily determine their actual enabled state when the user presses
  3414. // a keyboard shortcut.
  3415. else {
  3416. goSetCommandEnabled("cmd_undo", true);
  3417. goSetCommandEnabled("cmd_redo", true);
  3418. goSetCommandEnabled("cmd_cut", true);
  3419. goSetCommandEnabled("cmd_copy", true);
  3420. goSetCommandEnabled("cmd_paste", true);
  3421. goSetCommandEnabled("cmd_selectAll", true);
  3422. goSetCommandEnabled("cmd_delete", true);
  3423. goSetCommandEnabled("cmd_switchTextDirection", true);
  3424. }
  3425. #endif
  3426. }
  3427. var FullScreen = {
  3428. _XULNS: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
  3429. toggle: function (event) {
  3430. var enterFS = window.fullScreen;
  3431. // We get the fullscreen event _before_ the window transitions into or out of FS mode.
  3432. if (event && event.type == "fullscreen")
  3433. enterFS = !enterFS;
  3434. // show/hide all menubars, toolbars (except the full screen toolbar)
  3435. this.showXULChrome("toolbar", !enterFS);
  3436. document.getElementById("View:FullScreen").setAttribute("checked", enterFS);
  3437. if (enterFS) {
  3438. // Add a tiny toolbar to receive mouseover and dragenter events, and provide affordance.
  3439. // This will help simulate the "collapse" metaphor while also requiring less code and
  3440. // events than raw listening of mouse coords. We don't add the toolbar in DOM full-screen
  3441. // mode, only browser full-screen mode.
  3442. if (!document.mozFullScreen) {
  3443. let fullScrToggler = document.getElementById("fullscr-toggler");
  3444. if (!fullScrToggler) {
  3445. fullScrToggler = document.createElement("hbox");
  3446. fullScrToggler.id = "fullscr-toggler";
  3447. fullScrToggler.collapsed = true;
  3448. gNavToolbox.parentNode.insertBefore(fullScrToggler, gNavToolbox.nextSibling);
  3449. }
  3450. fullScrToggler.addEventListener("mouseover", this._expandCallback, false);
  3451. fullScrToggler.addEventListener("dragenter", this._expandCallback, false);
  3452. }
  3453. if (gPrefService.getBoolPref("browser.fullscreen.autohide"))
  3454. gBrowser.mPanelContainer.addEventListener("mousemove",
  3455. this._collapseCallback, false);
  3456. document.addEventListener("keypress", this._keyToggleCallback, false);
  3457. document.addEventListener("popupshown", this._setPopupOpen, false);
  3458. document.addEventListener("popuphidden", this._setPopupOpen, false);
  3459. // We don't animate the toolbar collapse if in DOM full-screen mode,
  3460. // as the size of the content area would still be changing after the
  3461. // mozfullscreenchange event fired, which could confuse content script.
  3462. this._shouldAnimate = !document.mozFullScreen;
  3463. this.mouseoverToggle(false);
  3464. // Autohide prefs
  3465. gPrefService.addObserver("browser.fullscreen", this, false);
  3466. }
  3467. else {
  3468. // The user may quit fullscreen during an animation
  3469. this._cancelAnimation();
  3470. gNavToolbox.style.marginTop = "";
  3471. if (this._isChromeCollapsed)
  3472. this.mouseoverToggle(true);
  3473. // This is needed if they use the context menu to quit fullscreen
  3474. this._isPopupOpen = false;
  3475. this.cleanup();
  3476. }
  3477. },
  3478. exitDomFullScreen : function(e) {
  3479. document.mozCancelFullScreen();
  3480. },
  3481. enterDomFullScreen : function(event) {
  3482. if (!document.mozFullScreen) {
  3483. return;
  3484. }
  3485. // We receive "mozfullscreenchange" events for each subdocument which
  3486. // is an ancestor of the document containing the element which requested
  3487. // full-screen. Only add listeners and show warning etc when the event we
  3488. // receive is targeted at the chrome document, i.e. only once every time
  3489. // we enter DOM full-screen mode.
  3490. if (event.target != document) {
  3491. // However, if we receive a "mozfullscreenchange" event for a document
  3492. // which is not a subdocument of the currently selected tab, we know that
  3493. // we've switched tabs since the request to enter full-screen was made,
  3494. // so we should exit full-screen since the "full-screen document" isn't
  3495. // acutally visible.
  3496. if (event.target.defaultView.top != gBrowser.contentWindow) {
  3497. document.mozCancelFullScreen();
  3498. }
  3499. return;
  3500. }
  3501. let focusManger = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager);
  3502. if (focusManger.activeWindow != window) {
  3503. // The top-level window has lost focus since the request to enter
  3504. // full-screen was made. Cancel full-screen.
  3505. document.mozCancelFullScreen();
  3506. return;
  3507. }
  3508. // Ensure the sidebar is hidden.
  3509. if (!document.getElementById("sidebar-box").hidden)
  3510. toggleSidebar();
  3511. if (gFindBarInitialized)
  3512. gFindBar.close();
  3513. this.showWarning(true);
  3514. // Exit DOM full-screen mode upon open, close, or change tab.
  3515. gBrowser.tabContainer.addEventListener("TabOpen", this.exitDomFullScreen);
  3516. gBrowser.tabContainer.addEventListener("TabClose", this.exitDomFullScreen);
  3517. gBrowser.tabContainer.addEventListener("TabSelect", this.exitDomFullScreen);
  3518. // Exit DOM full-screen mode when the browser window loses focus (ALT+TAB, etc).
  3519. window.addEventListener("deactivate", this.exitDomFullScreen, true);
  3520. // Cancel any "hide the toolbar" animation which is in progress, and make
  3521. // the toolbar hide immediately.
  3522. this._cancelAnimation();
  3523. this.mouseoverToggle(false);
  3524. // If there's a full-screen toggler, remove its listeners, so that mouseover
  3525. // the top of the screen will not cause the toolbar to re-appear.
  3526. let fullScrToggler = document.getElementById("fullscr-toggler");
  3527. if (fullScrToggler) {
  3528. fullScrToggler.removeEventListener("mouseover", this._expandCallback, false);
  3529. fullScrToggler.removeEventListener("dragenter", this._expandCallback, false);
  3530. }
  3531. },
  3532. cleanup: function () {
  3533. if (window.fullScreen) {
  3534. gBrowser.mPanelContainer.removeEventListener("mousemove",
  3535. this._collapseCallback, false);
  3536. document.removeEventListener("keypress", this._keyToggleCallback, false);
  3537. document.removeEventListener("popupshown", this._setPopupOpen, false);
  3538. document.removeEventListener("popuphidden", this._setPopupOpen, false);
  3539. gPrefService.removeObserver("browser.fullscreen", this);
  3540. let fullScrToggler = document.getElementById("fullscr-toggler");
  3541. if (fullScrToggler) {
  3542. fullScrToggler.removeEventListener("mouseover", this._expandCallback, false);
  3543. fullScrToggler.removeEventListener("dragenter", this._expandCallback, false);
  3544. }
  3545. this.cancelWarning();
  3546. gBrowser.tabContainer.removeEventListener("TabOpen", this.exitDomFullScreen);
  3547. gBrowser.tabContainer.removeEventListener("TabClose", this.exitDomFullScreen);
  3548. gBrowser.tabContainer.removeEventListener("TabSelect", this.exitDomFullScreen);
  3549. window.removeEventListener("deactivate", this.exitDomFullScreen, true);
  3550. }
  3551. },
  3552. observe: function(aSubject, aTopic, aData)
  3553. {
  3554. if (aData == "browser.fullscreen.autohide") {
  3555. if (gPrefService.getBoolPref("browser.fullscreen.autohide")) {
  3556. gBrowser.mPanelContainer.addEventListener("mousemove",
  3557. this._collapseCallback, false);
  3558. }
  3559. else {
  3560. gBrowser.mPanelContainer.removeEventListener("mousemove",
  3561. this._collapseCallback, false);
  3562. }
  3563. }
  3564. },
  3565. // Event callbacks
  3566. _expandCallback: function()
  3567. {
  3568. FullScreen.mouseoverToggle(true);
  3569. },
  3570. _collapseCallback: function()
  3571. {
  3572. FullScreen.mouseoverToggle(false);
  3573. },
  3574. _keyToggleCallback: function(aEvent)
  3575. {
  3576. // if we can use the keyboard (eg Ctrl+L or Ctrl+E) to open the toolbars, we
  3577. // should provide a way to collapse them too.
  3578. if (aEvent.keyCode == aEvent.DOM_VK_ESCAPE) {
  3579. FullScreen._shouldAnimate = false;
  3580. FullScreen.mouseoverToggle(false, true);
  3581. }
  3582. // F6 is another shortcut to the address bar, but its not covered in OpenLocation()
  3583. else if (aEvent.keyCode == aEvent.DOM_VK_F6)
  3584. FullScreen.mouseoverToggle(true);
  3585. },
  3586. // Checks whether we are allowed to collapse the chrome
  3587. _isPopupOpen: false,
  3588. _isChromeCollapsed: false,
  3589. _safeToCollapse: function(forceHide)
  3590. {
  3591. if (!gPrefService.getBoolPref("browser.fullscreen.autohide"))
  3592. return false;
  3593. // a popup menu is open in chrome: don't collapse chrome
  3594. if (!forceHide && this._isPopupOpen)
  3595. return false;
  3596. // a textbox in chrome is focused (location bar anyone?): don't collapse chrome
  3597. if (document.commandDispatcher.focusedElement &&
  3598. document.commandDispatcher.focusedElement.ownerDocument == document &&
  3599. document.commandDispatcher.focusedElement.localName == "input") {
  3600. if (forceHide)
  3601. // hidden textboxes that still have focus are bad bad bad
  3602. document.commandDispatcher.focusedElement.blur();
  3603. else
  3604. return false;
  3605. }
  3606. return true;
  3607. },
  3608. _setPopupOpen: function(aEvent)
  3609. {
  3610. // Popups should only veto chrome collapsing if they were opened when the chrome was not collapsed.
  3611. // Otherwise, they would not affect chrome and the user would expect the chrome to go away.
  3612. // e.g. we wouldn't want the autoscroll icon firing this event, so when the user
  3613. // toggles chrome when moving mouse to the top, it doesn't go away again.
  3614. if (aEvent.type == "popupshown" && !FullScreen._isChromeCollapsed &&
  3615. aEvent.target.localName != "tooltip" && aEvent.target.localName != "window")
  3616. FullScreen._isPopupOpen = true;
  3617. else if (aEvent.type == "popuphidden" && aEvent.target.localName != "tooltip" &&
  3618. aEvent.target.localName != "window")
  3619. FullScreen._isPopupOpen = false;
  3620. },
  3621. // Autohide helpers for the context menu item
  3622. getAutohide: function(aItem)
  3623. {
  3624. aItem.setAttribute("checked", gPrefService.getBoolPref("browser.fullscreen.autohide"));
  3625. },
  3626. setAutohide: function()
  3627. {
  3628. gPrefService.setBoolPref("browser.fullscreen.autohide", !gPrefService.getBoolPref("browser.fullscreen.autohide"));
  3629. },
  3630. // Animate the toolbars disappearing
  3631. _shouldAnimate: true,
  3632. _isAnimating: false,
  3633. _animationTimeout: 0,
  3634. _animationHandle: 0,
  3635. _animateUp: function() {
  3636. // check again, the user may have done something before the animation was due to start
  3637. if (!window.fullScreen || !this._safeToCollapse(false)) {
  3638. this._isAnimating = false;
  3639. this._shouldAnimate = true;
  3640. return;
  3641. }
  3642. this._animateStartTime = window.mozAnimationStartTime;
  3643. if (!this._animationHandle)
  3644. this._animationHandle = window.mozRequestAnimationFrame(this);
  3645. },
  3646. sample: function (timeStamp) {
  3647. const duration = 1500;
  3648. const timePassed = timeStamp - this._animateStartTime;
  3649. const pos = timePassed >= duration ? 1 :
  3650. 1 - Math.pow(1 - timePassed / duration, 4);
  3651. if (pos >= 1) {
  3652. // We've animated enough
  3653. this._cancelAnimation();
  3654. gNavToolbox.style.marginTop = "";
  3655. this.mouseoverToggle(false);
  3656. return;
  3657. }
  3658. gNavToolbox.style.marginTop = (gNavToolbox.boxObject.height * pos * -1) + "px";
  3659. this._animationHandle = window.mozRequestAnimationFrame(this);
  3660. },
  3661. _cancelAnimation: function() {
  3662. window.mozCancelAnimationFrame(this._animationHandle);
  3663. this._animationHandle = 0;
  3664. clearTimeout(this._animationTimeout);
  3665. this._isAnimating = false;
  3666. this._shouldAnimate = false;
  3667. },
  3668. cancelWarning: function(event) {
  3669. if (!this.warningBox) {
  3670. return;
  3671. }
  3672. if (this.onWarningHidden) {
  3673. this.warningBox.removeEventListener("transitionend", this.onWarningHidden, false);
  3674. this.onWarningHidden = null;
  3675. }
  3676. if (this.warningFadeOutTimeout) {
  3677. clearTimeout(this.warningFadeOutTimeout);
  3678. this.warningFadeOutTimeout = null;
  3679. }
  3680. if (this.revealBrowserTimeout) {
  3681. clearTimeout(this.revealBrowserTimeout);
  3682. this.revealBrowserTimeout = null;
  3683. }
  3684. this.warningBox.removeAttribute("fade-warning-out");
  3685. this.warningBox.removeAttribute("stop-obscuring-browser");
  3686. this.warningBox.removeAttribute("obscure-browser");
  3687. this.warningBox.setAttribute("hidden", true);
  3688. this.warningBox = null;
  3689. },
  3690. warningBox: null,
  3691. warningFadeOutTimeout: null,
  3692. revealBrowserTimeout: null,
  3693. onWarningHidden: null,
  3694. // Fade in a warning that document has entered full-screen, and then fade it
  3695. // out after a few seconds.
  3696. showWarning: function(obscureBackground) {
  3697. if (!document.mozFullScreen || !gPrefService.getBoolPref("full-screen-api.warning.enabled")) {
  3698. return;
  3699. }
  3700. if (this.warningBox) {
  3701. // Warning is already showing. Reset the timer which fades out the warning message,
  3702. // and we'll restart the timer down below.
  3703. if (this.warningFadeOutTimeout) {
  3704. clearTimeout(this.warningFadeOutTimeout);
  3705. this.warningFadeOutTimeout = null;
  3706. }
  3707. } else {
  3708. this.warningBox = document.getElementById("full-screen-warning-container");
  3709. // Add a listener to clean up state after the warning is hidden.
  3710. this.onWarningHidden =
  3711. function(event) {
  3712. if (event.propertyName != "opacity")
  3713. return;
  3714. this.cancelWarning();
  3715. }.bind(this);
  3716. this.warningBox.addEventListener("transitionend", this.onWarningHidden, false);
  3717. this.warningBox.removeAttribute("hidden");
  3718. }
  3719. if (obscureBackground) {
  3720. // Partially obscure the <browser> element underneath the warning panel...
  3721. this.warningBox.setAttribute("obscure-browser", "true");
  3722. // ...But set a timeout to stop obscuring the browser after a few moments.
  3723. this.warningBox.removeAttribute("stop-obscuring-browser");
  3724. this.revealBrowserTimeout =
  3725. setTimeout(
  3726. function() {
  3727. if (this.warningBox)
  3728. this.warningBox.setAttribute("stop-obscuring-browser", "true");
  3729. }.bind(this),
  3730. 1250);
  3731. }
  3732. // Set a timeout to fade the warning out after a few moments.
  3733. this.warningFadeOutTimeout =
  3734. setTimeout(
  3735. function() {
  3736. if (this.warningBox)
  3737. this.warningBox.setAttribute("fade-warning-out", "true");
  3738. }.bind(this),
  3739. 3000);
  3740. },
  3741. mouseoverToggle: function(aShow, forceHide)
  3742. {
  3743. // Don't do anything if:
  3744. // a) we're already in the state we want,
  3745. // b) we're animating and will become collapsed soon, or
  3746. // c) we can't collapse because it would be undesirable right now
  3747. if (aShow != this._isChromeCollapsed || (!aShow && this._isAnimating) ||
  3748. (!aShow && !this._safeToCollapse(forceHide)))
  3749. return;
  3750. // browser.fullscreen.animateUp
  3751. // 0 - never animate up
  3752. // 1 - animate only for first collapse after entering fullscreen (default for perf's sake)
  3753. // 2 - animate every time it collapses
  3754. if (gPrefService.getIntPref("browser.fullscreen.animateUp") == 0)
  3755. this._shouldAnimate = false;
  3756. if (!aShow && this._shouldAnimate) {
  3757. this._isAnimating = true;
  3758. this._shouldAnimate = false;
  3759. this._animationTimeout = setTimeout(this._animateUp.bind(this), 800);
  3760. return;
  3761. }
  3762. // The chrome is collapsed so don't spam needless mousemove events
  3763. if (aShow) {
  3764. gBrowser.mPanelContainer.addEventListener("mousemove",
  3765. this._collapseCallback, false);
  3766. }
  3767. else {
  3768. gBrowser.mPanelContainer.removeEventListener("mousemove",
  3769. this._collapseCallback, false);
  3770. }
  3771. // Hiding/collapsing the toolbox interferes with the tab bar's scrollbox,
  3772. // so we just move it off-screen instead. See bug 430687.
  3773. gNavToolbox.style.marginTop =
  3774. aShow ? "" : -gNavToolbox.getBoundingClientRect().height + "px";
  3775. let toggler = document.getElementById("fullscr-toggler");
  3776. if (toggler) {
  3777. toggler.collapsed = aShow;
  3778. }
  3779. this._isChromeCollapsed = !aShow;
  3780. if (gPrefService.getIntPref("browser.fullscreen.animateUp") == 2)
  3781. this._shouldAnimate = true;
  3782. },
  3783. showXULChrome: function(aTag, aShow)
  3784. {
  3785. var els = document.getElementsByTagNameNS(this._XULNS, aTag);
  3786. for (var i = 0; i < els.length; ++i) {
  3787. // XXX don't interfere with previously collapsed toolbars
  3788. if (els[i].getAttribute("fullscreentoolbar") == "true") {
  3789. if (!aShow) {
  3790. var toolbarMode = els[i].getAttribute("mode");
  3791. if (toolbarMode != "text") {
  3792. els[i].setAttribute("saved-mode", toolbarMode);
  3793. els[i].setAttribute("saved-iconsize",
  3794. els[i].getAttribute("iconsize"));
  3795. els[i].setAttribute("mode", "icons");
  3796. els[i].setAttribute("iconsize", "small");
  3797. }
  3798. // Give the main nav bar and the tab bar the fullscreen context menu,
  3799. // otherwise remove context menu to prevent breakage
  3800. els[i].setAttribute("saved-context",
  3801. els[i].getAttribute("context"));
  3802. if (els[i].id == "nav-bar" || els[i].id == "TabsToolbar")
  3803. els[i].setAttribute("context", "autohide-context");
  3804. else
  3805. els[i].removeAttribute("context");
  3806. // Set the inFullscreen attribute to allow specific styling
  3807. // in fullscreen mode
  3808. els[i].setAttribute("inFullscreen", true);
  3809. }
  3810. else {
  3811. function restoreAttr(attrName) {
  3812. var savedAttr = "saved-" + attrName;
  3813. if (els[i].hasAttribute(savedAttr)) {
  3814. els[i].setAttribute(attrName, els[i].getAttribute(savedAttr));
  3815. els[i].removeAttribute(savedAttr);
  3816. }
  3817. }
  3818. restoreAttr("mode");
  3819. restoreAttr("iconsize");
  3820. restoreAttr("context");
  3821. els[i].removeAttribute("inFullscreen");
  3822. }
  3823. } else {
  3824. // use moz-collapsed so it doesn't persist hidden/collapsed,
  3825. // so that new windows don't have missing toolbars
  3826. if (aShow)
  3827. els[i].removeAttribute("moz-collapsed");
  3828. else
  3829. els[i].setAttribute("moz-collapsed", "true");
  3830. }
  3831. }
  3832. if (aShow) {
  3833. gNavToolbox.removeAttribute("inFullscreen");
  3834. document.documentElement.removeAttribute("inFullscreen");
  3835. } else {
  3836. gNavToolbox.setAttribute("inFullscreen", true);
  3837. document.documentElement.setAttribute("inFullscreen", true);
  3838. }
  3839. // In tabs-on-top mode, move window controls to the tab bar,
  3840. // and in tabs-on-bottom mode, move them back to the navigation toolbar.
  3841. // When there is a chance the tab bar may be collapsed, put window
  3842. // controls on nav bar.
  3843. var fullscreenflex = document.getElementById("fullscreenflex");
  3844. var fullscreenctls = document.getElementById("window-controls");
  3845. var navbar = document.getElementById("nav-bar");
  3846. var ctlsOnTabbar = window.toolbar.visible &&
  3847. (navbar.collapsed ||
  3848. (TabsOnTop.enabled &&
  3849. !gPrefService.getBoolPref("browser.tabs.autoHide")));
  3850. if (fullscreenctls.parentNode == navbar && ctlsOnTabbar) {
  3851. document.getElementById("TabsToolbar").appendChild(fullscreenctls);
  3852. // we don't need this space in tabs-on-top mode, so prevent it from
  3853. // being shown
  3854. fullscreenflex.removeAttribute("fullscreencontrol");
  3855. }
  3856. else if (fullscreenctls.parentNode.id == "TabsToolbar" && !ctlsOnTabbar) {
  3857. navbar.appendChild(fullscreenctls);
  3858. fullscreenflex.setAttribute("fullscreencontrol", "true");
  3859. }
  3860. var controls = document.getElementsByAttribute("fullscreencontrol", "true");
  3861. for (var i = 0; i < controls.length; ++i)
  3862. controls[i].hidden = aShow;
  3863. }
  3864. };
  3865. /**
  3866. * Returns true if |aMimeType| is text-based, false otherwise.
  3867. *
  3868. * @param aMimeType
  3869. * The MIME type to check.
  3870. *
  3871. * If adding types to this function, please also check the similar
  3872. * function in findbar.xml
  3873. */
  3874. function mimeTypeIsTextBased(aMimeType)
  3875. {
  3876. return /^text\/|\+xml$/.test(aMimeType) ||
  3877. aMimeType == "application/x-javascript" ||
  3878. aMimeType == "application/javascript" ||
  3879. aMimeType == "application/xml" ||
  3880. aMimeType == "mozilla.application/cached-xul";
  3881. }
  3882. var XULBrowserWindow = {
  3883. // Stored Status, Link and Loading values
  3884. status: "",
  3885. defaultStatus: "",
  3886. jsStatus: "",
  3887. jsDefaultStatus: "",
  3888. overLink: "",
  3889. startTime: 0,
  3890. statusText: "",
  3891. isBusy: false,
  3892. inContentWhitelist: ["about:addons", "about:permissions", "about:sync-progress"],
  3893. QueryInterface: function (aIID) {
  3894. if (aIID.equals(Ci.nsIWebProgressListener) ||
  3895. aIID.equals(Ci.nsIWebProgressListener2) ||
  3896. aIID.equals(Ci.nsISupportsWeakReference) ||
  3897. aIID.equals(Ci.nsIXULBrowserWindow) ||
  3898. aIID.equals(Ci.nsISupports))
  3899. return this;
  3900. throw Cr.NS_NOINTERFACE;
  3901. },
  3902. get stopCommand () {
  3903. delete this.stopCommand;
  3904. return this.stopCommand = document.getElementById("Browser:Stop");
  3905. },
  3906. get reloadCommand () {
  3907. delete this.reloadCommand;
  3908. return this.reloadCommand = document.getElementById("Browser:Reload");
  3909. },
  3910. get statusTextField () {
  3911. delete this.statusTextField;
  3912. return this.statusTextField = document.getElementById("statusbar-display");
  3913. },
  3914. get isImage () {
  3915. delete this.isImage;
  3916. return this.isImage = document.getElementById("isImage");
  3917. },
  3918. get _uriFixup () {
  3919. delete this._uriFixup;
  3920. return this._uriFixup = Cc["@mozilla.org/docshell/urifixup;1"]
  3921. .getService(Ci.nsIURIFixup);
  3922. },
  3923. init: function () {
  3924. this.throbberElement = document.getElementById("navigator-throbber");
  3925. #ifdef MOZ_E10S_COMPAT
  3926. // Bug 666809 - SecurityUI support for e10s
  3927. #else
  3928. // Initialize the security button's state and tooltip text. Remember to reset
  3929. // _hostChanged, otherwise onSecurityChange will short circuit.
  3930. var securityUI = gBrowser.securityUI;
  3931. this._hostChanged = true;
  3932. this.onSecurityChange(null, null, securityUI.state);
  3933. #endif
  3934. },
  3935. destroy: function () {
  3936. // XXXjag to avoid leaks :-/, see bug 60729
  3937. delete this.throbberElement;
  3938. delete this.stopCommand;
  3939. delete this.reloadCommand;
  3940. delete this.statusTextField;
  3941. delete this.statusText;
  3942. },
  3943. setJSStatus: function (status) {
  3944. this.jsStatus = status;
  3945. this.updateStatusField();
  3946. },
  3947. setJSDefaultStatus: function (status) {
  3948. this.jsDefaultStatus = status;
  3949. this.updateStatusField();
  3950. },
  3951. setDefaultStatus: function (status) {
  3952. this.defaultStatus = status;
  3953. this.updateStatusField();
  3954. },
  3955. setOverLink: function (url, anchorElt) {
  3956. // Encode bidirectional formatting characters.
  3957. // (RFC 3987 sections 3.2 and 4.1 paragraph 6)
  3958. this.overLink = url.replace(/[\u200e\u200f\u202a\u202b\u202c\u202d\u202e]/g,
  3959. encodeURIComponent);
  3960. LinkTargetDisplay.update();
  3961. },
  3962. updateStatusField: function () {
  3963. var text, type, types = ["overLink"];
  3964. if (this._busyUI)
  3965. types.push("status");
  3966. types.push("jsStatus", "jsDefaultStatus", "defaultStatus");
  3967. for (let i = 0; !text && i < types.length; i++) {
  3968. type = types[i];
  3969. text = this[type];
  3970. }
  3971. // check the current value so we don't trigger an attribute change
  3972. // and cause needless (slow!) UI updates
  3973. if (this.statusText != text) {
  3974. let field = this.statusTextField;
  3975. field.setAttribute("previoustype", field.getAttribute("type"));
  3976. field.setAttribute("type", type);
  3977. field.label = text;
  3978. field.setAttribute("crop", type == "overLink" ? "center" : "end");
  3979. this.statusText = text;
  3980. }
  3981. },
  3982. // Called before links are navigated to to allow us to retarget them if needed.
  3983. onBeforeLinkTraversal: function(originalTarget, linkURI, linkNode, isAppTab) {
  3984. // Don't modify non-default targets or targets that aren't in top-level app
  3985. // tab docshells (isAppTab will be false for app tab subframes).
  3986. if (originalTarget != "" || !isAppTab)
  3987. return originalTarget;
  3988. // External links from within app tabs should always open in new tabs
  3989. // instead of replacing the app tab's page (Bug 575561)
  3990. let linkHost;
  3991. let docHost;
  3992. try {
  3993. linkHost = linkURI.host;
  3994. docHost = linkNode.ownerDocument.documentURIObject.host;
  3995. } catch(e) {
  3996. // nsIURI.host can throw for non-nsStandardURL nsIURIs.
  3997. // If we fail to get either host, just return originalTarget.
  3998. return originalTarget;
  3999. }
  4000. if (docHost == linkHost)
  4001. return originalTarget;
  4002. // Special case: ignore "www" prefix if it is part of host string
  4003. let [longHost, shortHost] =
  4004. linkHost.length > docHost.length ? [linkHost, docHost] : [docHost, linkHost];
  4005. if (longHost == "www." + shortHost)
  4006. return originalTarget;
  4007. return "_blank";
  4008. },
  4009. onLinkIconAvailable: function (aIconURL) {
  4010. if (gProxyFavIcon && gBrowser.userTypedValue === null)
  4011. PageProxySetIcon(aIconURL); // update the favicon in the URL bar
  4012. },
  4013. onProgressChange: function (aWebProgress, aRequest,
  4014. aCurSelfProgress, aMaxSelfProgress,
  4015. aCurTotalProgress, aMaxTotalProgress) {
  4016. // Do nothing.
  4017. },
  4018. onProgressChange64: function (aWebProgress, aRequest,
  4019. aCurSelfProgress, aMaxSelfProgress,
  4020. aCurTotalProgress, aMaxTotalProgress) {
  4021. return this.onProgressChange(aWebProgress, aRequest,
  4022. aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress,
  4023. aMaxTotalProgress);
  4024. },
  4025. onStateChange: function (aWebProgress, aRequest, aStateFlags, aStatus) {
  4026. const nsIWebProgressListener = Ci.nsIWebProgressListener;
  4027. const nsIChannel = Ci.nsIChannel;
  4028. if (aStateFlags & nsIWebProgressListener.STATE_START &&
  4029. aStateFlags & nsIWebProgressListener.STATE_IS_NETWORK) {
  4030. if (aRequest && aWebProgress.DOMWindow == content)
  4031. this.startDocumentLoad(aRequest);
  4032. this.isBusy = true;
  4033. if (!(aStateFlags & nsIWebProgressListener.STATE_RESTORING)) {
  4034. this._busyUI = true;
  4035. // Turn the throbber on.
  4036. if (this.throbberElement)
  4037. this.throbberElement.setAttribute("busy", "true");
  4038. // XXX: This needs to be based on window activity...
  4039. this.stopCommand.removeAttribute("disabled");
  4040. CombinedStopReload.switchToStop();
  4041. }
  4042. }
  4043. else if (aStateFlags & nsIWebProgressListener.STATE_STOP) {
  4044. if (aStateFlags & nsIWebProgressListener.STATE_IS_NETWORK &&
  4045. aWebProgress.DOMWindow == content &&
  4046. aRequest)
  4047. this.endDocumentLoad(aRequest, aStatus);
  4048. // This (thanks to the filter) is a network stop or the last
  4049. // request stop outside of loading the document, stop throbbers
  4050. // and progress bars and such
  4051. if (aRequest) {
  4052. let msg = "";
  4053. let location;
  4054. // Get the URI either from a channel or a pseudo-object
  4055. if (aRequest instanceof nsIChannel || "URI" in aRequest) {
  4056. location = aRequest.URI;
  4057. // For keyword URIs clear the user typed value since they will be changed into real URIs
  4058. if (location.scheme == "keyword" && aWebProgress.DOMWindow == content)
  4059. gBrowser.userTypedValue = null;
  4060. if (location.spec != "about:blank") {
  4061. switch (aStatus) {
  4062. case Components.results.NS_ERROR_NET_TIMEOUT:
  4063. msg = gNavigatorBundle.getString("nv_timeout");
  4064. break;
  4065. }
  4066. }
  4067. }
  4068. this.status = "";
  4069. this.setDefaultStatus(msg);
  4070. // Disable menu entries for images, enable otherwise
  4071. if (content.document && mimeTypeIsTextBased(content.document.contentType))
  4072. this.isImage.removeAttribute('disabled');
  4073. else
  4074. this.isImage.setAttribute('disabled', 'true');
  4075. }
  4076. this.isBusy = false;
  4077. if (this._busyUI) {
  4078. this._busyUI = false;
  4079. // Turn the throbber off.
  4080. if (this.throbberElement)
  4081. this.throbberElement.removeAttribute("busy");
  4082. this.stopCommand.setAttribute("disabled", "true");
  4083. CombinedStopReload.switchToReload(aRequest instanceof Ci.nsIRequest);
  4084. }
  4085. }
  4086. },
  4087. onLocationChange: function (aWebProgress, aRequest, aLocationURI, aFlags) {
  4088. var location = aLocationURI ? aLocationURI.spec : "";
  4089. this._hostChanged = true;
  4090. // Hide the form invalid popup.
  4091. if (gFormSubmitObserver.panelIsOpen()) {
  4092. gFormSubmitObserver.panel.hidePopup();
  4093. }
  4094. if (document.tooltipNode) {
  4095. // Optimise for the common case
  4096. if (aWebProgress.DOMWindow == content) {
  4097. document.getElementById("aHTMLTooltip").hidePopup();
  4098. document.tooltipNode = null;
  4099. }
  4100. else {
  4101. for (let tooltipWindow =
  4102. document.tooltipNode.ownerDocument.defaultView;
  4103. tooltipWindow != tooltipWindow.parent;
  4104. tooltipWindow = tooltipWindow.parent) {
  4105. if (tooltipWindow == aWebProgress.DOMWindow) {
  4106. document.getElementById("aHTMLTooltip").hidePopup();
  4107. document.tooltipNode = null;
  4108. break;
  4109. }
  4110. }
  4111. }
  4112. }
  4113. // This code here does not compare uris exactly when determining
  4114. // whether or not the message should be hidden since the message
  4115. // may be prematurely hidden when an install is invoked by a click
  4116. // on a link that looks like this:
  4117. //
  4118. // <a href="#" onclick="return install();">Install Foo</a>
  4119. //
  4120. // - which fires a onLocationChange message to uri + '#'...
  4121. var selectedBrowser = gBrowser.selectedBrowser;
  4122. if (selectedBrowser.lastURI) {
  4123. let oldSpec = selectedBrowser.lastURI.spec;
  4124. let oldIndexOfHash = oldSpec.indexOf("#");
  4125. if (oldIndexOfHash != -1)
  4126. oldSpec = oldSpec.substr(0, oldIndexOfHash);
  4127. let newSpec = location;
  4128. let newIndexOfHash = newSpec.indexOf("#");
  4129. if (newIndexOfHash != -1)
  4130. newSpec = newSpec.substr(0, newSpec.indexOf("#"));
  4131. if (newSpec != oldSpec) {
  4132. // Remove all the notifications, except for those which want to
  4133. // persist across the first location change.
  4134. let nBox = gBrowser.getNotificationBox(selectedBrowser);
  4135. nBox.removeTransientNotifications();
  4136. // Only need to call locationChange if the PopupNotifications object
  4137. // for this window has already been initialized (i.e. its getter no
  4138. // longer exists)
  4139. if (!__lookupGetter__("PopupNotifications"))
  4140. PopupNotifications.locationChange();
  4141. }
  4142. }
  4143. // Disable menu entries for images, enable otherwise
  4144. if (content.document && mimeTypeIsTextBased(content.document.contentType))
  4145. this.isImage.removeAttribute('disabled');
  4146. else
  4147. this.isImage.setAttribute('disabled', 'true');
  4148. this.hideOverLinkImmediately = true;
  4149. this.setOverLink("", null);
  4150. this.hideOverLinkImmediately = false;
  4151. // We should probably not do this if the value has changed since the user
  4152. // searched
  4153. // Update urlbar only if a new page was loaded on the primary content area
  4154. // Do not update urlbar if there was a subframe navigation
  4155. var browser = gBrowser.selectedBrowser;
  4156. if (aWebProgress.DOMWindow == content) {
  4157. if ((location == "about:blank" && !content.opener) ||
  4158. location == "") { // Second condition is for new tabs, otherwise
  4159. // reload function is enabled until tab is refreshed.
  4160. this.reloadCommand.setAttribute("disabled", "true");
  4161. } else {
  4162. this.reloadCommand.removeAttribute("disabled");
  4163. }
  4164. if (gURLBar) {
  4165. // Strip off "wyciwyg://" and passwords for the location bar
  4166. let uri = aLocationURI;
  4167. try {
  4168. uri = this._uriFixup.createExposableURI(uri);
  4169. } catch (e) {}
  4170. URLBarSetURI(uri);
  4171. // Update starring UI
  4172. PlacesStarButton.updateState();
  4173. }
  4174. // Show or hide browser chrome based on the whitelist
  4175. if (this.hideChromeForLocation(location))
  4176. document.documentElement.setAttribute("disablechrome", "true");
  4177. else
  4178. document.documentElement.removeAttribute("disablechrome");
  4179. // Disable find commands in documents that ask for them to be disabled.
  4180. let disableFind = false;
  4181. if (aLocationURI &&
  4182. (aLocationURI.schemeIs("about") || aLocationURI.schemeIs("chrome"))) {
  4183. let docElt = content.document.documentElement;
  4184. disableFind = docElt && docElt.getAttribute("disablefastfind") == "true";
  4185. }
  4186. let findCommands = [document.getElementById("cmd_find"),
  4187. document.getElementById("cmd_findAgain"),
  4188. document.getElementById("cmd_findPrevious")];
  4189. findCommands.forEach(function (elt) {
  4190. if (disableFind)
  4191. elt.setAttribute("disabled", "true");
  4192. else
  4193. elt.removeAttribute("disabled");
  4194. });
  4195. if (gFindBarInitialized) {
  4196. if (gFindBar.findMode != gFindBar.FIND_NORMAL) {
  4197. // Close the Find toolbar if we're in old-style TAF mode
  4198. gFindBar.close();
  4199. }
  4200. // fix bug 253793 - turn off highlight when page changes
  4201. gFindBar.getElement("highlight").checked = false;
  4202. }
  4203. }
  4204. UpdateBackForwardCommands(gBrowser.webNavigation);
  4205. // See bug 358202, when tabs are switched during a drag operation,
  4206. // timers don't fire on windows (bug 203573)
  4207. if (aRequest)
  4208. setTimeout(function () { XULBrowserWindow.asyncUpdateUI(); }, 0);
  4209. else
  4210. this.asyncUpdateUI();
  4211. },
  4212. asyncUpdateUI: function () {
  4213. FeedHandler.updateFeeds();
  4214. },
  4215. hideChromeForLocation: function(aLocation) {
  4216. aLocation = aLocation.toLowerCase();
  4217. return this.inContentWhitelist.some(function(aSpec) {
  4218. return aSpec == aLocation;
  4219. });
  4220. },
  4221. onStatusChange: function (aWebProgress, aRequest, aStatus, aMessage) {
  4222. this.status = aMessage;
  4223. this.updateStatusField();
  4224. },
  4225. // Properties used to cache security state used to update the UI
  4226. _state: null,
  4227. _hostChanged: false, // onLocationChange will flip this bit
  4228. onSecurityChange: function (aWebProgress, aRequest, aState) {
  4229. // Don't need to do anything if the data we use to update the UI hasn't
  4230. // changed
  4231. if (this._state == aState &&
  4232. !this._hostChanged) {
  4233. #ifdef DEBUG
  4234. try {
  4235. var contentHost = gBrowser.contentWindow.location.host;
  4236. if (this._host !== undefined && this._host != contentHost) {
  4237. Components.utils.reportError(
  4238. "ASSERTION: browser.js host is inconsistent. Content window has " +
  4239. "<" + contentHost + "> but cached host is <" + this._host + ">.\n"
  4240. );
  4241. }
  4242. } catch (ex) {}
  4243. #endif
  4244. return;
  4245. }
  4246. this._state = aState;
  4247. #ifdef DEBUG
  4248. try {
  4249. this._host = gBrowser.contentWindow.location.host;
  4250. } catch(ex) {
  4251. this._host = null;
  4252. }
  4253. #endif
  4254. this._hostChanged = false;
  4255. // aState is defined as a bitmask that may be extended in the future.
  4256. // We filter out any unknown bits before testing for known values.
  4257. const wpl = Components.interfaces.nsIWebProgressListener;
  4258. const wpl_security_bits = wpl.STATE_IS_SECURE |
  4259. wpl.STATE_IS_BROKEN |
  4260. wpl.STATE_IS_INSECURE |
  4261. wpl.STATE_SECURE_HIGH |
  4262. wpl.STATE_SECURE_MED |
  4263. wpl.STATE_SECURE_LOW;
  4264. var level;
  4265. switch (this._state & wpl_security_bits) {
  4266. case wpl.STATE_IS_SECURE | wpl.STATE_SECURE_HIGH:
  4267. level = "high";
  4268. break;
  4269. case wpl.STATE_IS_SECURE | wpl.STATE_SECURE_MED:
  4270. case wpl.STATE_IS_SECURE | wpl.STATE_SECURE_LOW:
  4271. level = "low";
  4272. break;
  4273. case wpl.STATE_IS_BROKEN:
  4274. level = "broken";
  4275. break;
  4276. }
  4277. if (level) {
  4278. // We don't style the Location Bar based on the the 'level' attribute
  4279. // anymore, but still set it for third-party themes.
  4280. if (gURLBar)
  4281. gURLBar.setAttribute("level", level);
  4282. } else {
  4283. if (gURLBar)
  4284. gURLBar.removeAttribute("level");
  4285. }
  4286. // Don't pass in the actual location object, since it can cause us to
  4287. // hold on to the window object too long. Just pass in the fields we
  4288. // care about. (bug 424829)
  4289. var location = gBrowser.contentWindow.location;
  4290. var locationObj = {};
  4291. try {
  4292. // about:blank can be used by webpages so pretend it is http
  4293. locationObj.protocol = location == "about:blank" ? "http:" : location.protocol;
  4294. locationObj.host = location.host;
  4295. locationObj.hostname = location.hostname;
  4296. locationObj.port = location.port;
  4297. } catch (ex) {
  4298. // Can sometimes throw if the URL being visited has no host/hostname,
  4299. // e.g. about:blank. The _state for these pages means we won't need these
  4300. // properties anyways, though.
  4301. }
  4302. gIdentityHandler.checkIdentity(this._state, locationObj);
  4303. },
  4304. // simulate all change notifications after switching tabs
  4305. onUpdateCurrentBrowser: function XWB_onUpdateCurrentBrowser(aStateFlags, aStatus, aMessage, aTotalProgress) {
  4306. if (FullZoom.updateBackgroundTabs)
  4307. FullZoom.onLocationChange(gBrowser.currentURI, true);
  4308. var nsIWebProgressListener = Components.interfaces.nsIWebProgressListener;
  4309. var loadingDone = aStateFlags & nsIWebProgressListener.STATE_STOP;
  4310. // use a pseudo-object instead of a (potentially nonexistent) channel for getting
  4311. // a correct error message - and make sure that the UI is always either in
  4312. // loading (STATE_START) or done (STATE_STOP) mode
  4313. this.onStateChange(
  4314. gBrowser.webProgress,
  4315. { URI: gBrowser.currentURI },
  4316. loadingDone ? nsIWebProgressListener.STATE_STOP : nsIWebProgressListener.STATE_START,
  4317. aStatus
  4318. );
  4319. // status message and progress value are undefined if we're done with loading
  4320. if (loadingDone)
  4321. return;
  4322. this.onStatusChange(gBrowser.webProgress, null, 0, aMessage);
  4323. },
  4324. startDocumentLoad: function XWB_startDocumentLoad(aRequest) {
  4325. // clear out feed data
  4326. gBrowser.selectedBrowser.feeds = null;
  4327. // clear out search-engine data
  4328. gBrowser.selectedBrowser.engines = null;
  4329. var uri = aRequest.QueryInterface(Ci.nsIChannel).URI;
  4330. try {
  4331. Services.obs.notifyObservers(content, "StartDocumentLoad", uri.spec);
  4332. } catch (e) {
  4333. }
  4334. },
  4335. endDocumentLoad: function XWB_endDocumentLoad(aRequest, aStatus) {
  4336. var urlStr = aRequest.QueryInterface(Ci.nsIChannel).originalURI.spec;
  4337. var notification = Components.isSuccessCode(aStatus) ? "EndDocumentLoad" : "FailDocumentLoad";
  4338. try {
  4339. Services.obs.notifyObservers(content, notification, urlStr);
  4340. } catch (e) {
  4341. }
  4342. }
  4343. };
  4344. var LinkTargetDisplay = {
  4345. get DELAY_SHOW() {
  4346. delete this.DELAY_SHOW;
  4347. return this.DELAY_SHOW = Services.prefs.getIntPref("browser.overlink-delay");
  4348. },
  4349. DELAY_HIDE: 250,
  4350. _timer: 0,
  4351. get _isVisible () XULBrowserWindow.statusTextField.label != "",
  4352. update: function () {
  4353. clearTimeout(this._timer);
  4354. window.removeEventListener("mousemove", this, true);
  4355. if (!XULBrowserWindow.overLink) {
  4356. if (XULBrowserWindow.hideOverLinkImmediately)
  4357. this._hide();
  4358. else
  4359. this._timer = setTimeout(this._hide.bind(this), this.DELAY_HIDE);
  4360. return;
  4361. }
  4362. if (this._isVisible) {
  4363. XULBrowserWindow.updateStatusField();
  4364. } else {
  4365. // Let the display appear when the mouse doesn't move within the delay
  4366. this._showDelayed();
  4367. window.addEventListener("mousemove", this, true);
  4368. }
  4369. },
  4370. handleEvent: function (event) {
  4371. switch (event.type) {
  4372. case "mousemove":
  4373. // Restart the delay since the mouse was moved
  4374. clearTimeout(this._timer);
  4375. this._showDelayed();
  4376. break;
  4377. }
  4378. },
  4379. _showDelayed: function () {
  4380. this._timer = setTimeout(function (self) {
  4381. XULBrowserWindow.updateStatusField();
  4382. window.removeEventListener("mousemove", self, true);
  4383. }, this.DELAY_SHOW, this);
  4384. },
  4385. _hide: function () {
  4386. clearTimeout(this._timer);
  4387. XULBrowserWindow.updateStatusField();
  4388. }
  4389. };
  4390. var CombinedStopReload = {
  4391. init: function () {
  4392. if (this._initialized)
  4393. return;
  4394. var urlbar = document.getElementById("urlbar-container");
  4395. var reload = document.getElementById("reload-button");
  4396. var stop = document.getElementById("stop-button");
  4397. if (urlbar) {
  4398. if (urlbar.parentNode.getAttribute("mode") != "icons" ||
  4399. !reload || urlbar.nextSibling != reload ||
  4400. !stop || reload.nextSibling != stop)
  4401. urlbar.removeAttribute("combined");
  4402. else {
  4403. urlbar.setAttribute("combined", "true");
  4404. reload = document.getElementById("urlbar-reload-button");
  4405. stop = document.getElementById("urlbar-stop-button");
  4406. }
  4407. }
  4408. if (!stop || !reload || reload.nextSibling != stop)
  4409. return;
  4410. this._initialized = true;
  4411. if (XULBrowserWindow.stopCommand.getAttribute("disabled") != "true")
  4412. reload.setAttribute("displaystop", "true");
  4413. stop.addEventListener("click", this, false);
  4414. this.reload = reload;
  4415. this.stop = stop;
  4416. },
  4417. uninit: function () {
  4418. if (!this._initialized)
  4419. return;
  4420. this._cancelTransition();
  4421. this._initialized = false;
  4422. this.stop.removeEventListener("click", this, false);
  4423. this.reload = null;
  4424. this.stop = null;
  4425. },
  4426. handleEvent: function (event) {
  4427. // the only event we listen to is "click" on the stop button
  4428. if (event.button == 0 &&
  4429. !this.stop.disabled)
  4430. this._stopClicked = true;
  4431. },
  4432. switchToStop: function () {
  4433. if (!this._initialized)
  4434. return;
  4435. this._cancelTransition();
  4436. this.reload.setAttribute("displaystop", "true");
  4437. },
  4438. switchToReload: function (aDelay) {
  4439. if (!this._initialized)
  4440. return;
  4441. this.reload.removeAttribute("displaystop");
  4442. if (!aDelay || this._stopClicked) {
  4443. this._stopClicked = false;
  4444. this._cancelTransition();
  4445. this.reload.disabled = XULBrowserWindow.reloadCommand
  4446. .getAttribute("disabled") == "true";
  4447. return;
  4448. }
  4449. if (this._timer)
  4450. return;
  4451. // Temporarily disable the reload button to prevent the user from
  4452. // accidentally reloading the page when intending to click the stop button
  4453. this.reload.disabled = true;
  4454. this._timer = setTimeout(function (self) {
  4455. self._timer = 0;
  4456. self.reload.disabled = XULBrowserWindow.reloadCommand
  4457. .getAttribute("disabled") == "true";
  4458. }, 650, this);
  4459. },
  4460. _cancelTransition: function () {
  4461. if (this._timer) {
  4462. clearTimeout(this._timer);
  4463. this._timer = 0;
  4464. }
  4465. }
  4466. };
  4467. var TabsProgressListener = {
  4468. onStateChange: function (aBrowser, aWebProgress, aRequest, aStateFlags, aStatus) {
  4469. #ifdef MOZ_CRASHREPORTER
  4470. if (aRequest instanceof Ci.nsIChannel &&
  4471. aStateFlags & Ci.nsIWebProgressListener.STATE_START &&
  4472. aStateFlags & Ci.nsIWebProgressListener.STATE_IS_DOCUMENT &&
  4473. gCrashReporter.enabled) {
  4474. gCrashReporter.annotateCrashReport("URL", aRequest.URI.spec);
  4475. }
  4476. #endif
  4477. // Attach a listener to watch for "click" events bubbling up from error
  4478. // pages and other similar page. This lets us fix bugs like 401575 which
  4479. // require error page UI to do privileged things, without letting error
  4480. // pages have any privilege themselves.
  4481. // We can't look for this during onLocationChange since at that point the
  4482. // document URI is not yet the about:-uri of the error page.
  4483. if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP &&
  4484. /^about:/.test(aWebProgress.DOMWindow.document.documentURI)) {
  4485. aBrowser.addEventListener("click", BrowserOnClick, false);
  4486. aBrowser.addEventListener("pagehide", function () {
  4487. aBrowser.removeEventListener("click", BrowserOnClick, false);
  4488. aBrowser.removeEventListener("pagehide", arguments.callee, true);
  4489. }, true);
  4490. // We also want to make changes to page UI for unprivileged about pages.
  4491. BrowserOnAboutPageLoad(aWebProgress.DOMWindow.document);
  4492. }
  4493. },
  4494. onLocationChange: function (aBrowser, aWebProgress, aRequest, aLocationURI,
  4495. aFlags) {
  4496. // Filter out any sub-frame loads
  4497. if (aBrowser.contentWindow == aWebProgress.DOMWindow)
  4498. FullZoom.onLocationChange(aLocationURI, false, aBrowser);
  4499. },
  4500. onRefreshAttempted: function (aBrowser, aWebProgress, aURI, aDelay, aSameURI) {
  4501. if (gPrefService.getBoolPref("accessibility.blockautorefresh")) {
  4502. let brandBundle = document.getElementById("bundle_brand");
  4503. let brandShortName = brandBundle.getString("brandShortName");
  4504. let refreshButtonText =
  4505. gNavigatorBundle.getString("refreshBlocked.goButton");
  4506. let refreshButtonAccesskey =
  4507. gNavigatorBundle.getString("refreshBlocked.goButton.accesskey");
  4508. let message =
  4509. gNavigatorBundle.getFormattedString(aSameURI ? "refreshBlocked.refreshLabel"
  4510. : "refreshBlocked.redirectLabel",
  4511. [brandShortName]);
  4512. let docShell = aWebProgress.DOMWindow
  4513. .QueryInterface(Ci.nsIInterfaceRequestor)
  4514. .getInterface(Ci.nsIWebNavigation)
  4515. .QueryInterface(Ci.nsIDocShell);
  4516. let notificationBox = gBrowser.getNotificationBox(aBrowser);
  4517. let notification = notificationBox.getNotificationWithValue("refresh-blocked");
  4518. if (notification) {
  4519. notification.label = message;
  4520. notification.refreshURI = aURI;
  4521. notification.delay = aDelay;
  4522. notification.docShell = docShell;
  4523. } else {
  4524. let buttons = [{
  4525. label: refreshButtonText,
  4526. accessKey: refreshButtonAccesskey,
  4527. callback: function (aNotification, aButton) {
  4528. var refreshURI = aNotification.docShell
  4529. .QueryInterface(Ci.nsIRefreshURI);
  4530. refreshURI.forceRefreshURI(aNotification.refreshURI,
  4531. aNotification.delay, true);
  4532. }
  4533. }];
  4534. notification =
  4535. notificationBox.appendNotification(message, "refresh-blocked",
  4536. "chrome://browser/skin/Info.png",
  4537. notificationBox.PRIORITY_INFO_MEDIUM,
  4538. buttons);
  4539. notification.refreshURI = aURI;
  4540. notification.delay = aDelay;
  4541. notification.docShell = docShell;
  4542. }
  4543. return false;
  4544. }
  4545. return true;
  4546. }
  4547. }
  4548. function nsBrowserAccess() { }
  4549. nsBrowserAccess.prototype = {
  4550. QueryInterface: XPCOMUtils.generateQI([Ci.nsIBrowserDOMWindow, Ci.nsISupports]),
  4551. openURI: function (aURI, aOpener, aWhere, aContext) {
  4552. var newWindow = null;
  4553. var isExternal = (aContext == Ci.nsIBrowserDOMWindow.OPEN_EXTERNAL);
  4554. if (isExternal && aURI && aURI.schemeIs("chrome")) {
  4555. dump("use -chrome command-line option to load external chrome urls\n");
  4556. return null;
  4557. }
  4558. if (aWhere == Ci.nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW) {
  4559. if (isExternal &&
  4560. gPrefService.prefHasUserValue("browser.link.open_newwindow.override.external"))
  4561. aWhere = gPrefService.getIntPref("browser.link.open_newwindow.override.external");
  4562. else
  4563. aWhere = gPrefService.getIntPref("browser.link.open_newwindow");
  4564. }
  4565. switch (aWhere) {
  4566. case Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW :
  4567. // FIXME: Bug 408379. So how come this doesn't send the
  4568. // referrer like the other loads do?
  4569. var url = aURI ? aURI.spec : BROWSER_NEW_TAB_URL;
  4570. // Pass all params to openDialog to ensure that "url" isn't passed through
  4571. // loadOneOrMoreURIs, which splits based on "|"
  4572. newWindow = openDialog(getBrowserURL(), "_blank", "all,dialog=no", url, null, null, null);
  4573. break;
  4574. case Ci.nsIBrowserDOMWindow.OPEN_NEWTAB :
  4575. let win, needToFocusWin;
  4576. // try the current window. if we're in a popup, fall back on the most recent browser window
  4577. if (window.toolbar.visible)
  4578. win = window;
  4579. else {
  4580. win = Cc["@mozilla.org/browser/browserglue;1"]
  4581. .getService(Ci.nsIBrowserGlue)
  4582. .getMostRecentBrowserWindow();
  4583. needToFocusWin = true;
  4584. }
  4585. if (!win) {
  4586. // we couldn't find a suitable window, a new one needs to be opened.
  4587. return null;
  4588. }
  4589. if (isExternal && (!aURI || aURI.spec == "about:blank")) {
  4590. win.BrowserOpenTab(); // this also focuses the location bar
  4591. win.focus();
  4592. newWindow = win.content;
  4593. break;
  4594. }
  4595. let loadInBackground = gPrefService.getBoolPref("browser.tabs.loadDivertedInBackground");
  4596. let referrer = aOpener ? makeURI(aOpener.location.href) : null;
  4597. let tab = win.gBrowser.loadOneTab(aURI ? aURI.spec : BROWSER_NEW_TAB_URL, {
  4598. referrerURI: referrer,
  4599. fromExternal: isExternal,
  4600. inBackground: loadInBackground});
  4601. let browser = win.gBrowser.getBrowserForTab(tab);
  4602. newWindow = browser.contentWindow;
  4603. if (needToFocusWin || (!loadInBackground && isExternal))
  4604. newWindow.focus();
  4605. break;
  4606. default : // OPEN_CURRENTWINDOW or an illegal value
  4607. newWindow = content;
  4608. if (aURI) {
  4609. let referrer = aOpener ? makeURI(aOpener.location.href) : null;
  4610. let loadflags = isExternal ?
  4611. Ci.nsIWebNavigation.LOAD_FLAGS_FROM_EXTERNAL :
  4612. Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
  4613. gBrowser.loadURIWithFlags(aURI.spec, loadflags, referrer, null, null);
  4614. }
  4615. if (!gPrefService.getBoolPref("browser.tabs.loadDivertedInBackground"))
  4616. content.focus();
  4617. }
  4618. return newWindow;
  4619. },
  4620. isTabContentWindow: function (aWindow) {
  4621. return gBrowser.browsers.some(function (browser) browser.contentWindow == aWindow);
  4622. }
  4623. }
  4624. function onViewToolbarsPopupShowing(aEvent, aInsertPoint) {
  4625. var popup = aEvent.target;
  4626. if (popup != aEvent.currentTarget)
  4627. return;
  4628. // Empty the menu
  4629. for (var i = popup.childNodes.length-1; i >= 0; --i) {
  4630. var deadItem = popup.childNodes[i];
  4631. if (deadItem.hasAttribute("toolbarId"))
  4632. popup.removeChild(deadItem);
  4633. }
  4634. var firstMenuItem = aInsertPoint || popup.firstChild;
  4635. let toolbarNodes = Array.slice(gNavToolbox.childNodes);
  4636. toolbarNodes.push(document.getElementById("addon-bar"));
  4637. toolbarNodes.forEach(function(toolbar) {
  4638. var toolbarName = toolbar.getAttribute("toolbarname");
  4639. if (toolbarName) {
  4640. let menuItem = document.createElement("menuitem");
  4641. let hidingAttribute = toolbar.getAttribute("type") == "menubar" ?
  4642. "autohide" : "collapsed";
  4643. menuItem.setAttribute("id", "toggle_" + toolbar.id);
  4644. menuItem.setAttribute("toolbarId", toolbar.id);
  4645. menuItem.setAttribute("type", "checkbox");
  4646. menuItem.setAttribute("label", toolbarName);
  4647. menuItem.setAttribute("checked", toolbar.getAttribute(hidingAttribute) != "true");
  4648. if (popup.id != "appmenu_customizeMenu")
  4649. menuItem.setAttribute("accesskey", toolbar.getAttribute("accesskey"));
  4650. if (popup.id != "toolbar-context-menu")
  4651. menuItem.setAttribute("key", toolbar.getAttribute("key"));
  4652. popup.insertBefore(menuItem, firstMenuItem);
  4653. menuItem.addEventListener("command", onViewToolbarCommand, false);
  4654. }
  4655. }, this);
  4656. }
  4657. function onViewToolbarCommand(aEvent) {
  4658. var toolbarId = aEvent.originalTarget.getAttribute("toolbarId");
  4659. var toolbar = document.getElementById(toolbarId);
  4660. var isVisible = aEvent.originalTarget.getAttribute("checked") == "true";
  4661. setToolbarVisibility(toolbar, isVisible);
  4662. }
  4663. function setToolbarVisibility(toolbar, isVisible) {
  4664. var hidingAttribute = toolbar.getAttribute("type") == "menubar" ?
  4665. "autohide" : "collapsed";
  4666. toolbar.setAttribute(hidingAttribute, !isVisible);
  4667. document.persist(toolbar.id, hidingAttribute);
  4668. PlacesToolbarHelper.init();
  4669. BookmarksMenuButton.updatePosition();
  4670. gBrowser.updateWindowResizers();
  4671. #ifdef MENUBAR_CAN_AUTOHIDE
  4672. updateAppButtonDisplay();
  4673. #endif
  4674. }
  4675. var TabsOnTop = {
  4676. init: function TabsOnTop_init() {
  4677. this.syncUI();
  4678. Services.prefs.addObserver(this._prefName, this, false);
  4679. },
  4680. uninit: function TabsOnTop_uninit() {
  4681. Services.prefs.removeObserver(this._prefName, this);
  4682. },
  4683. toggle: function () {
  4684. this.enabled = !Services.prefs.getBoolPref(this._prefName);
  4685. },
  4686. syncUI: function () {
  4687. let userEnabled = Services.prefs.getBoolPref(this._prefName);
  4688. let enabled = userEnabled && gBrowser.tabContainer.visible;
  4689. document.getElementById("cmd_ToggleTabsOnTop")
  4690. .setAttribute("checked", userEnabled);
  4691. document.documentElement.setAttribute("tabsontop", enabled);
  4692. document.getElementById("navigator-toolbox").setAttribute("tabsontop", enabled);
  4693. document.getElementById("TabsToolbar").setAttribute("tabsontop", enabled);
  4694. gBrowser.tabContainer.setAttribute("tabsontop", enabled);
  4695. TabsInTitlebar.allowedBy("tabs-on-top", enabled);
  4696. },
  4697. get enabled () {
  4698. return gNavToolbox.getAttribute("tabsontop") == "true";
  4699. },
  4700. set enabled (val) {
  4701. Services.prefs.setBoolPref(this._prefName, !!val);
  4702. return val;
  4703. },
  4704. observe: function (subject, topic, data) {
  4705. if (topic == "nsPref:changed")
  4706. this.syncUI();
  4707. },
  4708. _prefName: "browser.tabs.onTop"
  4709. }
  4710. var TabsInTitlebar = {
  4711. init: function () {
  4712. #ifdef CAN_DRAW_IN_TITLEBAR
  4713. this._readPref();
  4714. Services.prefs.addObserver(this._prefName, this, false);
  4715. // Don't trust the initial value of the sizemode attribute; wait for the resize event.
  4716. this.allowedBy("sizemode", false);
  4717. window.addEventListener("resize", function (event) {
  4718. if (event.target != window)
  4719. return;
  4720. let sizemode = document.documentElement.getAttribute("sizemode");
  4721. TabsInTitlebar.allowedBy("sizemode",
  4722. sizemode == "maximized" || sizemode == "fullscreen");
  4723. }, false);
  4724. this._initialized = true;
  4725. #endif
  4726. },
  4727. allowedBy: function (condition, allow) {
  4728. #ifdef CAN_DRAW_IN_TITLEBAR
  4729. if (allow) {
  4730. if (condition in this._disallowed) {
  4731. delete this._disallowed[condition];
  4732. this._update();
  4733. }
  4734. } else {
  4735. if (!(condition in this._disallowed)) {
  4736. this._disallowed[condition] = null;
  4737. this._update();
  4738. }
  4739. }
  4740. #endif
  4741. },
  4742. get enabled() {
  4743. return document.documentElement.getAttribute("tabsintitlebar") == "true";
  4744. },
  4745. #ifdef CAN_DRAW_IN_TITLEBAR
  4746. observe: function (subject, topic, data) {
  4747. if (topic == "nsPref:changed")
  4748. this._readPref();
  4749. },
  4750. _initialized: false,
  4751. _disallowed: {},
  4752. _prefName: "browser.tabs.drawInTitlebar",
  4753. _readPref: function () {
  4754. this.allowedBy("pref",
  4755. Services.prefs.getBoolPref(this._prefName));
  4756. },
  4757. _update: function () {
  4758. if (!this._initialized || window.fullScreen)
  4759. return;
  4760. let allowed = true;
  4761. for (let something in this._disallowed) {
  4762. allowed = false;
  4763. break;
  4764. }
  4765. if (allowed == this.enabled)
  4766. return;
  4767. function $(id) document.getElementById(id);
  4768. let titlebar = $("titlebar");
  4769. if (allowed) {
  4770. function rect(ele) ele.getBoundingClientRect();
  4771. let tabsToolbar = $("TabsToolbar");
  4772. let appmenuButtonBox = $("appmenu-button-container");
  4773. let captionButtonsBox = $("titlebar-buttonbox");
  4774. this._sizePlaceholder("appmenu-button", rect(appmenuButtonBox).width);
  4775. this._sizePlaceholder("caption-buttons", rect(captionButtonsBox).width);
  4776. let tabsToolbarRect = rect(tabsToolbar);
  4777. let titlebarTop = rect($("titlebar-content")).top;
  4778. titlebar.style.marginBottom = - Math.min(tabsToolbarRect.top - titlebarTop,
  4779. tabsToolbarRect.height) + "px";
  4780. document.documentElement.setAttribute("tabsintitlebar", "true");
  4781. if (!this._draghandle) {
  4782. let tmp = {};
  4783. Components.utils.import("resource://gre/modules/WindowDraggingUtils.jsm", tmp);
  4784. this._draghandle = new tmp.WindowDraggingElement(tabsToolbar);
  4785. this._draghandle.mouseDownCheck = function () {
  4786. return !this._dragBindingAlive && TabsInTitlebar.enabled;
  4787. };
  4788. }
  4789. } else {
  4790. document.documentElement.removeAttribute("tabsintitlebar");
  4791. titlebar.style.marginBottom = "";
  4792. }
  4793. },
  4794. _sizePlaceholder: function (type, width) {
  4795. Array.forEach(document.querySelectorAll(".titlebar-placeholder[type='"+ type +"']"),
  4796. function (node) { node.width = width; });
  4797. },
  4798. #endif
  4799. uninit: function () {
  4800. #ifdef CAN_DRAW_IN_TITLEBAR
  4801. this._initialized = false;
  4802. Services.prefs.removeObserver(this._prefName, this);
  4803. #endif
  4804. }
  4805. };
  4806. #ifdef MENUBAR_CAN_AUTOHIDE
  4807. function updateAppButtonDisplay() {
  4808. var displayAppButton =
  4809. !gInPrintPreviewMode &&
  4810. window.menubar.visible &&
  4811. document.getElementById("toolbar-menubar").getAttribute("autohide") == "true";
  4812. #ifdef CAN_DRAW_IN_TITLEBAR
  4813. document.getElementById("titlebar").hidden = !displayAppButton;
  4814. if (displayAppButton)
  4815. document.documentElement.setAttribute("chromemargin", "0,2,2,2");
  4816. else
  4817. document.documentElement.removeAttribute("chromemargin");
  4818. TabsInTitlebar.allowedBy("drawing-in-titlebar", displayAppButton);
  4819. #else
  4820. document.getElementById("appmenu-toolbar-button").hidden =
  4821. !displayAppButton;
  4822. #endif
  4823. }
  4824. #endif
  4825. #ifdef CAN_DRAW_IN_TITLEBAR
  4826. function onTitlebarMaxClick() {
  4827. if (window.windowState == window.STATE_MAXIMIZED)
  4828. window.restore();
  4829. else
  4830. window.maximize();
  4831. }
  4832. #endif
  4833. function displaySecurityInfo()
  4834. {
  4835. BrowserPageInfo(null, "securityTab");
  4836. }
  4837. /**
  4838. * Opens or closes the sidebar identified by commandID.
  4839. *
  4840. * @param commandID a string identifying the sidebar to toggle; see the
  4841. * note below. (Optional if a sidebar is already open.)
  4842. * @param forceOpen boolean indicating whether the sidebar should be
  4843. * opened regardless of its current state (optional).
  4844. * @note
  4845. * We expect to find a xul:broadcaster element with the specified ID.
  4846. * The following attributes on that element may be used and/or modified:
  4847. * - id (required) the string to match commandID. The convention
  4848. * is to use this naming scheme: 'view<sidebar-name>Sidebar'.
  4849. * - sidebarurl (required) specifies the URL to load in this sidebar.
  4850. * - sidebartitle or label (in that order) specify the title to
  4851. * display on the sidebar.
  4852. * - checked indicates whether the sidebar is currently displayed.
  4853. * Note that toggleSidebar updates this attribute when
  4854. * it changes the sidebar's visibility.
  4855. * - group this attribute must be set to "sidebar".
  4856. */
  4857. function toggleSidebar(commandID, forceOpen) {
  4858. var sidebarBox = document.getElementById("sidebar-box");
  4859. if (!commandID)
  4860. commandID = sidebarBox.getAttribute("sidebarcommand");
  4861. var sidebarBroadcaster = document.getElementById(commandID);
  4862. var sidebar = document.getElementById("sidebar"); // xul:browser
  4863. var sidebarTitle = document.getElementById("sidebar-title");
  4864. var sidebarSplitter = document.getElementById("sidebar-splitter");
  4865. if (sidebarBroadcaster.getAttribute("checked") == "true") {
  4866. if (!forceOpen) {
  4867. sidebarBroadcaster.removeAttribute("checked");
  4868. sidebarBox.setAttribute("sidebarcommand", "");
  4869. sidebarTitle.value = "";
  4870. sidebar.setAttribute("src", "about:blank");
  4871. sidebarBox.hidden = true;
  4872. sidebarSplitter.hidden = true;
  4873. content.focus();
  4874. } else {
  4875. fireSidebarFocusedEvent();
  4876. }
  4877. return;
  4878. }
  4879. // now we need to show the specified sidebar
  4880. // ..but first update the 'checked' state of all sidebar broadcasters
  4881. var broadcasters = document.getElementsByAttribute("group", "sidebar");
  4882. for (var i = 0; i < broadcasters.length; ++i) {
  4883. // skip elements that observe sidebar broadcasters and random
  4884. // other elements
  4885. if (broadcasters[i].localName != "broadcaster")
  4886. continue;
  4887. if (broadcasters[i] != sidebarBroadcaster)
  4888. broadcasters[i].removeAttribute("checked");
  4889. else
  4890. sidebarBroadcaster.setAttribute("checked", "true");
  4891. }
  4892. sidebarBox.hidden = false;
  4893. sidebarSplitter.hidden = false;
  4894. var url = sidebarBroadcaster.getAttribute("sidebarurl");
  4895. var title = sidebarBroadcaster.getAttribute("sidebartitle");
  4896. if (!title)
  4897. title = sidebarBroadcaster.getAttribute("label");
  4898. sidebar.setAttribute("src", url); // kick off async load
  4899. sidebarBox.setAttribute("sidebarcommand", sidebarBroadcaster.id);
  4900. sidebarTitle.value = title;
  4901. // We set this attribute here in addition to setting it on the <browser>
  4902. // element itself, because the code in BrowserShutdown persists this
  4903. // attribute, not the "src" of the <browser id="sidebar">. The reason it
  4904. // does that is that we want to delay sidebar load a bit when a browser
  4905. // window opens. See delayedStartup().
  4906. sidebarBox.setAttribute("src", url);
  4907. if (sidebar.contentDocument.location.href != url)
  4908. sidebar.addEventListener("load", sidebarOnLoad, true);
  4909. else // older code handled this case, so we do it too
  4910. fireSidebarFocusedEvent();
  4911. }
  4912. function sidebarOnLoad(event) {
  4913. var sidebar = document.getElementById("sidebar");
  4914. sidebar.removeEventListener("load", sidebarOnLoad, true);
  4915. // We're handling the 'load' event before it bubbles up to the usual
  4916. // (non-capturing) event handlers. Let it bubble up before firing the
  4917. // SidebarFocused event.
  4918. setTimeout(fireSidebarFocusedEvent, 0);
  4919. }
  4920. /**
  4921. * Fire a "SidebarFocused" event on the sidebar's |window| to give the sidebar
  4922. * a chance to adjust focus as needed. An additional event is needed, because
  4923. * we don't want to focus the sidebar when it's opened on startup or in a new
  4924. * window, only when the user opens the sidebar.
  4925. */
  4926. function fireSidebarFocusedEvent() {
  4927. var sidebar = document.getElementById("sidebar");
  4928. var event = document.createEvent("Events");
  4929. event.initEvent("SidebarFocused", true, false);
  4930. sidebar.contentWindow.dispatchEvent(event);
  4931. }
  4932. var gHomeButton = {
  4933. prefDomain: "browser.startup.homepage",
  4934. observe: function (aSubject, aTopic, aPrefName)
  4935. {
  4936. if (aTopic != "nsPref:changed" || aPrefName != this.prefDomain)
  4937. return;
  4938. this.updateTooltip();
  4939. },
  4940. updateTooltip: function (homeButton)
  4941. {
  4942. if (!homeButton)
  4943. homeButton = document.getElementById("home-button");
  4944. if (homeButton) {
  4945. var homePage = this.getHomePage();
  4946. homePage = homePage.replace(/\|/g,', ');
  4947. if (homePage.toLowerCase() == "about:home")
  4948. homeButton.setAttribute("tooltiptext", homeButton.getAttribute("aboutHomeOverrideTooltip"));
  4949. else
  4950. homeButton.setAttribute("tooltiptext", homePage);
  4951. }
  4952. },
  4953. getHomePage: function ()
  4954. {
  4955. var url;
  4956. try {
  4957. url = gPrefService.getComplexValue(this.prefDomain,
  4958. Components.interfaces.nsIPrefLocalizedString).data;
  4959. } catch (e) {
  4960. }
  4961. // use this if we can't find the pref
  4962. if (!url) {
  4963. var SBS = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
  4964. var configBundle = SBS.createBundle("chrome://branding/locale/browserconfig.properties");
  4965. url = configBundle.GetStringFromName(this.prefDomain);
  4966. }
  4967. return url;
  4968. },
  4969. updatePersonalToolbarStyle: function (homeButton)
  4970. {
  4971. if (!homeButton)
  4972. homeButton = document.getElementById("home-button");
  4973. if (homeButton)
  4974. homeButton.className = homeButton.parentNode.id == "PersonalToolbar"
  4975. || homeButton.parentNode.parentNode.id == "PersonalToolbar" ?
  4976. homeButton.className.replace("toolbarbutton-1", "bookmark-item") :
  4977. homeButton.className.replace("bookmark-item", "toolbarbutton-1");
  4978. }
  4979. };
  4980. /**
  4981. * Gets the selected text in the active browser. Leading and trailing
  4982. * whitespace is removed, and consecutive whitespace is replaced by a single
  4983. * space. A maximum of 150 characters will be returned, regardless of the value
  4984. * of aCharLen.
  4985. *
  4986. * @param aCharLen
  4987. * The maximum number of characters to return.
  4988. */
  4989. function getBrowserSelection(aCharLen) {
  4990. // selections of more than 150 characters aren't useful
  4991. const kMaxSelectionLen = 150;
  4992. const charLen = Math.min(aCharLen || kMaxSelectionLen, kMaxSelectionLen);
  4993. var focusedWindow = document.commandDispatcher.focusedWindow;
  4994. var selection = focusedWindow.getSelection().toString();
  4995. if (selection) {
  4996. if (selection.length > charLen) {
  4997. // only use the first charLen important chars. see bug 221361
  4998. var pattern = new RegExp("^(?:\\s*.){0," + charLen + "}");
  4999. pattern.test(selection);
  5000. selection = RegExp.lastMatch;
  5001. }
  5002. selection = selection.replace(/^\s+/, "")
  5003. .replace(/\s+$/, "")
  5004. .replace(/\s+/g, " ");
  5005. if (selection.length > charLen)
  5006. selection = selection.substr(0, charLen);
  5007. }
  5008. return selection;
  5009. }
  5010. var gWebPanelURI;
  5011. function openWebPanel(aTitle, aURI)
  5012. {
  5013. // Ensure that the web panels sidebar is open.
  5014. toggleSidebar('viewWebPanelsSidebar', true);
  5015. // Set the title of the panel.
  5016. document.getElementById("sidebar-title").value = aTitle;
  5017. // Tell the Web Panels sidebar to load the bookmark.
  5018. var sidebar = document.getElementById("sidebar");
  5019. if (sidebar.docShell && sidebar.contentDocument && sidebar.contentDocument.getElementById('web-panels-browser')) {
  5020. sidebar.contentWindow.loadWebPanel(aURI);
  5021. if (gWebPanelURI) {
  5022. gWebPanelURI = "";
  5023. sidebar.removeEventListener("load", asyncOpenWebPanel, true);
  5024. }
  5025. }
  5026. else {
  5027. // The panel is still being constructed. Attach an onload handler.
  5028. if (!gWebPanelURI)
  5029. sidebar.addEventListener("load", asyncOpenWebPanel, true);
  5030. gWebPanelURI = aURI;
  5031. }
  5032. }
  5033. function asyncOpenWebPanel(event)
  5034. {
  5035. var sidebar = document.getElementById("sidebar");
  5036. if (gWebPanelURI && sidebar.contentDocument && sidebar.contentDocument.getElementById('web-panels-browser'))
  5037. sidebar.contentWindow.loadWebPanel(gWebPanelURI);
  5038. gWebPanelURI = "";
  5039. sidebar.removeEventListener("load", asyncOpenWebPanel, true);
  5040. }
  5041. /*
  5042. * - [ Dependencies ] ---------------------------------------------------------
  5043. * utilityOverlay.js:
  5044. * - gatherTextUnder
  5045. */
  5046. /**
  5047. * Extracts linkNode and href for the current click target.
  5048. *
  5049. * @param event
  5050. * The click event.
  5051. * @return [href, linkNode].
  5052. *
  5053. * @note linkNode will be null if the click wasn't on an anchor
  5054. * element (or XLink).
  5055. */
  5056. function hrefAndLinkNodeForClickEvent(event)
  5057. {
  5058. function isHTMLLink(aNode)
  5059. {
  5060. // Be consistent with what nsContextMenu.js does.
  5061. return ((aNode instanceof HTMLAnchorElement && aNode.href) ||
  5062. (aNode instanceof HTMLAreaElement && aNode.href) ||
  5063. aNode instanceof HTMLLinkElement);
  5064. }
  5065. let node = event.target;
  5066. while (node && !isHTMLLink(node)) {
  5067. node = node.parentNode;
  5068. }
  5069. if (node)
  5070. return [node.href, node];
  5071. // If there is no linkNode, try simple XLink.
  5072. let href, baseURI;
  5073. node = event.target;
  5074. while (node && !href) {
  5075. if (node.nodeType == Node.ELEMENT_NODE) {
  5076. href = node.getAttributeNS("http://www.w3.org/1999/xlink", "href");
  5077. if (href)
  5078. baseURI = node.baseURI;
  5079. }
  5080. node = node.parentNode;
  5081. }
  5082. // In case of XLink, we don't return the node we got href from since
  5083. // callers expect <a>-like elements.
  5084. return [href ? makeURLAbsolute(baseURI, href) : null, null];
  5085. }
  5086. /**
  5087. * Called whenever the user clicks in the content area.
  5088. *
  5089. * @param event
  5090. * The click event.
  5091. * @param isPanelClick
  5092. * Whether the event comes from a web panel.
  5093. * @note default event is prevented if the click is handled.
  5094. */
  5095. function contentAreaClick(event, isPanelClick)
  5096. {
  5097. if (!event.isTrusted || event.defaultPrevented || event.button == 2)
  5098. return;
  5099. let [href, linkNode] = hrefAndLinkNodeForClickEvent(event);
  5100. if (!href) {
  5101. // Not a link, handle middle mouse navigation.
  5102. if (event.button == 1 &&
  5103. gPrefService.getBoolPref("middlemouse.contentLoadURL") &&
  5104. !gPrefService.getBoolPref("general.autoScroll")) {
  5105. middleMousePaste(event);
  5106. event.preventDefault();
  5107. }
  5108. return;
  5109. }
  5110. // This code only applies if we have a linkNode (i.e. clicks on real anchor
  5111. // elements, as opposed to XLink).
  5112. if (linkNode && event.button == 0 &&
  5113. !event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey) {
  5114. // A Web panel's links should target the main content area. Do this
  5115. // if no modifier keys are down and if there's no target or the target
  5116. // equals _main (the IE convention) or _content (the Mozilla convention).
  5117. let target = linkNode.target;
  5118. let mainTarget = !target || target == "_content" || target == "_main";
  5119. if (isPanelClick && mainTarget) {
  5120. // javascript and data links should be executed in the current browser.
  5121. if (linkNode.getAttribute("onclick") ||
  5122. href.substr(0, 11) === "javascript:" ||
  5123. href.substr(0, 5) === "data:")
  5124. return;
  5125. try {
  5126. urlSecurityCheck(href, linkNode.ownerDocument.nodePrincipal);
  5127. }
  5128. catch(ex) {
  5129. // Prevent loading unsecure destinations.
  5130. event.preventDefault();
  5131. return;
  5132. }
  5133. let postData = {};
  5134. let url = getShortcutOrURI(href, postData);
  5135. if (!url)
  5136. return;
  5137. loadURI(url, null, postData.value, false);
  5138. event.preventDefault();
  5139. return;
  5140. }
  5141. if (linkNode.getAttribute("rel") == "sidebar") {
  5142. // This is the Opera convention for a special link that, when clicked,
  5143. // allows to add a sidebar panel. The link's title attribute contains
  5144. // the title that should be used for the sidebar panel.
  5145. PlacesUIUtils.showBookmarkDialog({ action: "add"
  5146. , type: "bookmark"
  5147. , uri: makeURI(href)
  5148. , title: linkNode.getAttribute("title")
  5149. , loadBookmarkInSidebar: true
  5150. , hiddenRows: [ "description"
  5151. , "location"
  5152. , "keyword" ]
  5153. }, window);
  5154. event.preventDefault();
  5155. return;
  5156. }
  5157. }
  5158. handleLinkClick(event, href, linkNode);
  5159. // Mark the page as a user followed link. This is done so that history can
  5160. // distinguish automatic embed visits from user activated ones. For example
  5161. // pages loaded in frames are embed visits and lost with the session, while
  5162. // visits across frames should be preserved.
  5163. try {
  5164. PlacesUIUtils.markPageAsFollowedLink(href);
  5165. } catch (ex) { /* Skip invalid URIs. */ }
  5166. }
  5167. /**
  5168. * Handles clicks on links.
  5169. *
  5170. * @return true if the click event was handled, false otherwise.
  5171. */
  5172. function handleLinkClick(event, href, linkNode) {
  5173. if (event.button == 2) // right click
  5174. return false;
  5175. var where = whereToOpenLink(event);
  5176. if (where == "current")
  5177. return false;
  5178. var doc = event.target.ownerDocument;
  5179. if (where == "save") {
  5180. saveURL(href, linkNode ? gatherTextUnder(linkNode) : "", null, true,
  5181. true, doc.documentURIObject);
  5182. event.preventDefault();
  5183. return true;
  5184. }
  5185. urlSecurityCheck(href, doc.nodePrincipal);
  5186. openLinkIn(href, where, { referrerURI: doc.documentURIObject,
  5187. charset: doc.characterSet });
  5188. event.preventDefault();
  5189. return true;
  5190. }
  5191. function middleMousePaste(event) {
  5192. let clipboard = readFromClipboard();
  5193. if (!clipboard)
  5194. return;
  5195. // Strip embedded newlines and surrounding whitespace, to match the URL
  5196. // bar's behavior (stripsurroundingwhitespace)
  5197. clipboard = clipboard.replace(/\s*\n\s*/g, "");
  5198. let mayInheritPrincipal = { value: false };
  5199. let url = getShortcutOrURI(clipboard, mayInheritPrincipal);
  5200. try {
  5201. makeURI(url);
  5202. } catch (ex) {
  5203. // Not a valid URI.
  5204. return;
  5205. }
  5206. try {
  5207. addToUrlbarHistory(url);
  5208. } catch (ex) {
  5209. // Things may go wrong when adding url to session history,
  5210. // but don't let that interfere with the loading of the url.
  5211. Cu.reportError(ex);
  5212. }
  5213. // FIXME: Bug 631500, use openUILink directly
  5214. let where = whereToOpenLink(event, true);
  5215. openUILinkIn(url, where,
  5216. { disallowInheritPrincipal: !mayInheritPrincipal.value });
  5217. event.stopPropagation();
  5218. }
  5219. function handleDroppedLink(event, url, name)
  5220. {
  5221. let postData = { };
  5222. let uri = getShortcutOrURI(url, postData);
  5223. if (uri)
  5224. loadURI(uri, null, postData.value, false);
  5225. // Keep the event from being handled by the dragDrop listeners
  5226. // built-in to gecko if they happen to be above us.
  5227. event.preventDefault();
  5228. };
  5229. function MultiplexHandler(event)
  5230. { try {
  5231. var node = event.target;
  5232. var name = node.getAttribute('name');
  5233. if (name == 'detectorGroup') {
  5234. BrowserCharsetReload();
  5235. SelectDetector(event, false);
  5236. } else if (name == 'charsetGroup') {
  5237. var charset = node.getAttribute('id');
  5238. charset = charset.substring('charset.'.length, charset.length)
  5239. BrowserSetForcedCharacterSet(charset);
  5240. } else if (name == 'charsetCustomize') {
  5241. //do nothing - please remove this else statement, once the charset prefs moves to the pref window
  5242. } else {
  5243. BrowserSetForcedCharacterSet(node.getAttribute('id'));
  5244. }
  5245. } catch(ex) { alert(ex); }
  5246. }
  5247. function SelectDetector(event, doReload)
  5248. {
  5249. var uri = event.target.getAttribute("id");
  5250. var prefvalue = uri.substring('chardet.'.length, uri.length);
  5251. if ("off" == prefvalue) { // "off" is special value to turn off the detectors
  5252. prefvalue = "";
  5253. }
  5254. try {
  5255. var str = Cc["@mozilla.org/supports-string;1"].
  5256. createInstance(Ci.nsISupportsString);
  5257. str.data = prefvalue;
  5258. gPrefService.setComplexValue("intl.charset.detector", Ci.nsISupportsString, str);
  5259. if (doReload)
  5260. window.content.location.reload();
  5261. }
  5262. catch (ex) {
  5263. dump("Failed to set the intl.charset.detector preference.\n");
  5264. }
  5265. }
  5266. function BrowserSetForcedCharacterSet(aCharset)
  5267. {
  5268. gBrowser.docShell.charset = aCharset;
  5269. // Save the forced character-set
  5270. PlacesUtils.history.setCharsetForURI(getWebNavigation().currentURI, aCharset);
  5271. BrowserCharsetReload();
  5272. }
  5273. function BrowserCharsetReload()
  5274. {
  5275. BrowserReloadWithFlags(nsIWebNavigation.LOAD_FLAGS_CHARSET_CHANGE);
  5276. }
  5277. function charsetMenuGetElement(parent, id) {
  5278. return parent.getElementsByAttribute("id", id)[0];
  5279. }
  5280. function UpdateCurrentCharset(target) {
  5281. // extract the charset from DOM
  5282. var wnd = document.commandDispatcher.focusedWindow;
  5283. if ((window == wnd) || (wnd == null)) wnd = window.content;
  5284. // Uncheck previous item
  5285. if (gPrevCharset) {
  5286. var pref_item = charsetMenuGetElement(target, "charset." + gPrevCharset);
  5287. if (pref_item)
  5288. pref_item.setAttribute('checked', 'false');
  5289. }
  5290. var menuitem = charsetMenuGetElement(target, "charset." + wnd.document.characterSet);
  5291. if (menuitem) {
  5292. menuitem.setAttribute('checked', 'true');
  5293. }
  5294. }
  5295. function UpdateCharsetDetector(target) {
  5296. var prefvalue;
  5297. try {
  5298. prefvalue = gPrefService.getComplexValue("intl.charset.detector", Ci.nsIPrefLocalizedString).data;
  5299. }
  5300. catch (ex) {}
  5301. if (!prefvalue)
  5302. prefvalue = "off";
  5303. var menuitem = charsetMenuGetElement(target, "chardet." + prefvalue);
  5304. if (menuitem)
  5305. menuitem.setAttribute("checked", "true");
  5306. }
  5307. function UpdateMenus(event) {
  5308. UpdateCurrentCharset(event.target);
  5309. UpdateCharsetDetector(event.target);
  5310. }
  5311. function CreateMenu(node) {
  5312. Services.obs.notifyObservers(null, "charsetmenu-selected", node);
  5313. }
  5314. function charsetLoadListener(event) {
  5315. var charset = window.content.document.characterSet;
  5316. if (charset.length > 0 && (charset != gLastBrowserCharset)) {
  5317. if (!gCharsetMenu)
  5318. gCharsetMenu = Cc['@mozilla.org/rdf/datasource;1?name=charset-menu'].getService(Ci.nsICurrentCharsetListener);
  5319. gCharsetMenu.SetCurrentCharset(charset);
  5320. gPrevCharset = gLastBrowserCharset;
  5321. gLastBrowserCharset = charset;
  5322. }
  5323. }
  5324. /* Begin Page Style Functions */
  5325. function getAllStyleSheets(frameset) {
  5326. var styleSheetsArray = Array.slice(frameset.document.styleSheets);
  5327. for (let i = 0; i < frameset.frames.length; i++) {
  5328. let frameSheets = getAllStyleSheets(frameset.frames[i]);
  5329. styleSheetsArray = styleSheetsArray.concat(frameSheets);
  5330. }
  5331. return styleSheetsArray;
  5332. }
  5333. function stylesheetFillPopup(menuPopup) {
  5334. var noStyle = menuPopup.firstChild;
  5335. var persistentOnly = noStyle.nextSibling;
  5336. var sep = persistentOnly.nextSibling;
  5337. while (sep.nextSibling)
  5338. menuPopup.removeChild(sep.nextSibling);
  5339. var styleSheets = getAllStyleSheets(window.content);
  5340. var currentStyleSheets = {};
  5341. var styleDisabled = getMarkupDocumentViewer().authorStyleDisabled;
  5342. var haveAltSheets = false;
  5343. var altStyleSelected = false;
  5344. for (let i = 0; i < styleSheets.length; ++i) {
  5345. let currentStyleSheet = styleSheets[i];
  5346. if (!currentStyleSheet.title)
  5347. continue;
  5348. // Skip any stylesheets whose media attribute doesn't match.
  5349. if (currentStyleSheet.media.length > 0) {
  5350. let mediaQueryList = currentStyleSheet.media.mediaText;
  5351. if (!window.content.matchMedia(mediaQueryList).matches)
  5352. continue;
  5353. }
  5354. if (!currentStyleSheet.disabled)
  5355. altStyleSelected = true;
  5356. haveAltSheets = true;
  5357. let lastWithSameTitle = null;
  5358. if (currentStyleSheet.title in currentStyleSheets)
  5359. lastWithSameTitle = currentStyleSheets[currentStyleSheet.title];
  5360. if (!lastWithSameTitle) {
  5361. let menuItem = document.createElement("menuitem");
  5362. menuItem.setAttribute("type", "radio");
  5363. menuItem.setAttribute("label", currentStyleSheet.title);
  5364. menuItem.setAttribute("data", currentStyleSheet.title);
  5365. menuItem.setAttribute("checked", !currentStyleSheet.disabled && !styleDisabled);
  5366. menuPopup.appendChild(menuItem);
  5367. currentStyleSheets[currentStyleSheet.title] = menuItem;
  5368. } else if (currentStyleSheet.disabled) {
  5369. lastWithSameTitle.removeAttribute("checked");
  5370. }
  5371. }
  5372. noStyle.setAttribute("checked", styleDisabled);
  5373. persistentOnly.setAttribute("checked", !altStyleSelected && !styleDisabled);
  5374. persistentOnly.hidden = (window.content.document.preferredStyleSheetSet) ? haveAltSheets : false;
  5375. sep.hidden = (noStyle.hidden && persistentOnly.hidden) || !haveAltSheets;
  5376. return true;
  5377. }
  5378. function stylesheetInFrame(frame, title) {
  5379. return Array.some(frame.document.styleSheets,
  5380. function (stylesheet) stylesheet.title == title);
  5381. }
  5382. function stylesheetSwitchFrame(frame, title) {
  5383. var docStyleSheets = frame.document.styleSheets;
  5384. for (let i = 0; i < docStyleSheets.length; ++i) {
  5385. let docStyleSheet = docStyleSheets[i];
  5386. if (title == "_nostyle")
  5387. docStyleSheet.disabled = true;
  5388. else if (docStyleSheet.title)
  5389. docStyleSheet.disabled = (docStyleSheet.title != title);
  5390. else if (docStyleSheet.disabled)
  5391. docStyleSheet.disabled = false;
  5392. }
  5393. }
  5394. function stylesheetSwitchAll(frameset, title) {
  5395. if (!title || title == "_nostyle" || stylesheetInFrame(frameset, title))
  5396. stylesheetSwitchFrame(frameset, title);
  5397. for (let i = 0; i < frameset.frames.length; i++)
  5398. stylesheetSwitchAll(frameset.frames[i], title);
  5399. }
  5400. function setStyleDisabled(disabled) {
  5401. getMarkupDocumentViewer().authorStyleDisabled = disabled;
  5402. }
  5403. /* End of the Page Style functions */
  5404. var BrowserOffline = {
  5405. _inited: false,
  5406. /////////////////////////////////////////////////////////////////////////////
  5407. // BrowserOffline Public Methods
  5408. init: function ()
  5409. {
  5410. if (!this._uiElement)
  5411. this._uiElement = document.getElementById("workOfflineMenuitemState");
  5412. Services.obs.addObserver(this, "network:offline-status-changed", false);
  5413. this._updateOfflineUI(Services.io.offline);
  5414. this._inited = true;
  5415. },
  5416. uninit: function ()
  5417. {
  5418. if (this._inited) {
  5419. Services.obs.removeObserver(this, "network:offline-status-changed");
  5420. }
  5421. },
  5422. toggleOfflineStatus: function ()
  5423. {
  5424. var ioService = Services.io;
  5425. // Stop automatic management of the offline status
  5426. try {
  5427. ioService.manageOfflineStatus = false;
  5428. } catch (ex) {
  5429. }
  5430. if (!ioService.offline && !this._canGoOffline()) {
  5431. this._updateOfflineUI(false);
  5432. return;
  5433. }
  5434. ioService.offline = !ioService.offline;
  5435. },
  5436. /////////////////////////////////////////////////////////////////////////////
  5437. // nsIObserver
  5438. observe: function (aSubject, aTopic, aState)
  5439. {
  5440. if (aTopic != "network:offline-status-changed")
  5441. return;
  5442. this._updateOfflineUI(aState == "offline");
  5443. },
  5444. /////////////////////////////////////////////////////////////////////////////
  5445. // BrowserOffline Implementation Methods
  5446. _canGoOffline: function ()
  5447. {
  5448. try {
  5449. var cancelGoOffline = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
  5450. Services.obs.notifyObservers(cancelGoOffline, "offline-requested", null);
  5451. // Something aborted the quit process.
  5452. if (cancelGoOffline.data)
  5453. return false;
  5454. }
  5455. catch (ex) {
  5456. }
  5457. return true;
  5458. },
  5459. _uiElement: null,
  5460. _updateOfflineUI: function (aOffline)
  5461. {
  5462. var offlineLocked = gPrefService.prefIsLocked("network.online");
  5463. if (offlineLocked)
  5464. this._uiElement.setAttribute("disabled", "true");
  5465. this._uiElement.setAttribute("checked", aOffline);
  5466. }
  5467. };
  5468. var OfflineApps = {
  5469. /////////////////////////////////////////////////////////////////////////////
  5470. // OfflineApps Public Methods
  5471. init: function ()
  5472. {
  5473. Services.obs.addObserver(this, "dom-storage-warn-quota-exceeded", false);
  5474. Services.obs.addObserver(this, "offline-cache-update-completed", false);
  5475. },
  5476. uninit: function ()
  5477. {
  5478. Services.obs.removeObserver(this, "dom-storage-warn-quota-exceeded");
  5479. Services.obs.removeObserver(this, "offline-cache-update-completed");
  5480. },
  5481. handleEvent: function(event) {
  5482. if (event.type == "MozApplicationManifest") {
  5483. this.offlineAppRequested(event.originalTarget.defaultView);
  5484. }
  5485. },
  5486. /////////////////////////////////////////////////////////////////////////////
  5487. // OfflineApps Implementation Methods
  5488. // XXX: _getBrowserWindowForContentWindow and _getBrowserForContentWindow
  5489. // were taken from browser/components/feeds/src/WebContentConverter.
  5490. _getBrowserWindowForContentWindow: function(aContentWindow) {
  5491. return aContentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
  5492. .getInterface(Ci.nsIWebNavigation)
  5493. .QueryInterface(Ci.nsIDocShellTreeItem)
  5494. .rootTreeItem
  5495. .QueryInterface(Ci.nsIInterfaceRequestor)
  5496. .getInterface(Ci.nsIDOMWindow)
  5497. .wrappedJSObject;
  5498. },
  5499. _getBrowserForContentWindow: function(aBrowserWindow, aContentWindow) {
  5500. // This depends on pseudo APIs of browser.js and tabbrowser.xml
  5501. aContentWindow = aContentWindow.top;
  5502. var browsers = aBrowserWindow.gBrowser.browsers;
  5503. for (var i = 0; i < browsers.length; ++i) {
  5504. if (browsers[i].contentWindow == aContentWindow)
  5505. return browsers[i];
  5506. }
  5507. return null;
  5508. },
  5509. _getManifestURI: function(aWindow) {
  5510. if (!aWindow.document.documentElement)
  5511. return null;
  5512. var attr = aWindow.document.documentElement.getAttribute("manifest");
  5513. if (!attr)
  5514. return null;
  5515. try {
  5516. var contentURI = makeURI(aWindow.location.href, null, null);
  5517. return makeURI(attr, aWindow.document.characterSet, contentURI);
  5518. } catch (e) {
  5519. return null;
  5520. }
  5521. },
  5522. // A cache update isn't tied to a specific window. Try to find
  5523. // the best browser in which to warn the user about space usage
  5524. _getBrowserForCacheUpdate: function(aCacheUpdate) {
  5525. // Prefer the current browser
  5526. var uri = this._getManifestURI(content);
  5527. if (uri && uri.equals(aCacheUpdate.manifestURI)) {
  5528. return gBrowser.selectedBrowser;
  5529. }
  5530. var browsers = gBrowser.browsers;
  5531. for (var i = 0; i < browsers.length; ++i) {
  5532. uri = this._getManifestURI(browsers[i].contentWindow);
  5533. if (uri && uri.equals(aCacheUpdate.manifestURI)) {
  5534. return browsers[i];
  5535. }
  5536. }
  5537. return null;
  5538. },
  5539. _warnUsage: function(aBrowser, aURI) {
  5540. if (!aBrowser)
  5541. return;
  5542. var notificationBox = gBrowser.getNotificationBox(aBrowser);
  5543. var notification = notificationBox.getNotificationWithValue("offline-app-usage");
  5544. if (!notification) {
  5545. var buttons = [{
  5546. label: gNavigatorBundle.getString("offlineApps.manageUsage"),
  5547. accessKey: gNavigatorBundle.getString("offlineApps.manageUsageAccessKey"),
  5548. callback: OfflineApps.manage
  5549. }];
  5550. var warnQuota = gPrefService.getIntPref("offline-apps.quota.warn");
  5551. const priority = notificationBox.PRIORITY_WARNING_MEDIUM;
  5552. var message = gNavigatorBundle.getFormattedString("offlineApps.usage",
  5553. [ aURI.host,
  5554. warnQuota / 1024 ]);
  5555. notificationBox.appendNotification(message, "offline-app-usage",
  5556. "chrome://browser/skin/Info.png",
  5557. priority, buttons);
  5558. }
  5559. // Now that we've warned once, prevent the warning from showing up
  5560. // again.
  5561. Services.perms.add(aURI, "offline-app",
  5562. Ci.nsIOfflineCacheUpdateService.ALLOW_NO_WARN);
  5563. },
  5564. // XXX: duplicated in preferences/advanced.js
  5565. _getOfflineAppUsage: function (host, groups)
  5566. {
  5567. var cacheService = Cc["@mozilla.org/network/application-cache-service;1"].
  5568. getService(Ci.nsIApplicationCacheService);
  5569. if (!groups)
  5570. groups = cacheService.getGroups();
  5571. var usage = 0;
  5572. for (var i = 0; i < groups.length; i++) {
  5573. var uri = Services.io.newURI(groups[i], null, null);
  5574. if (uri.asciiHost == host) {
  5575. var cache = cacheService.getActiveCache(groups[i]);
  5576. usage += cache.usage;
  5577. }
  5578. }
  5579. var storageManager = Cc["@mozilla.org/dom/storagemanager;1"].
  5580. getService(Ci.nsIDOMStorageManager);
  5581. usage += storageManager.getUsage(host);
  5582. return usage;
  5583. },
  5584. _checkUsage: function(aURI) {
  5585. // if the user has already allowed excessive usage, don't bother checking
  5586. if (Services.perms.testExactPermission(aURI, "offline-app") !=
  5587. Ci.nsIOfflineCacheUpdateService.ALLOW_NO_WARN) {
  5588. var usage = this._getOfflineAppUsage(aURI.asciiHost);
  5589. var warnQuota = gPrefService.getIntPref("offline-apps.quota.warn");
  5590. if (usage >= warnQuota * 1024) {
  5591. return true;
  5592. }
  5593. }
  5594. return false;
  5595. },
  5596. offlineAppRequested: function(aContentWindow) {
  5597. if (!gPrefService.getBoolPref("browser.offline-apps.notify")) {
  5598. return;
  5599. }
  5600. var browserWindow = this._getBrowserWindowForContentWindow(aContentWindow);
  5601. var browser = this._getBrowserForContentWindow(browserWindow,
  5602. aContentWindow);
  5603. var currentURI = aContentWindow.document.documentURIObject;
  5604. // don't bother showing UI if the user has already made a decision
  5605. if (Services.perms.testExactPermission(currentURI, "offline-app") != Services.perms.UNKNOWN_ACTION)
  5606. return;
  5607. try {
  5608. if (gPrefService.getBoolPref("offline-apps.allow_by_default")) {
  5609. // all pages can use offline capabilities, no need to ask the user
  5610. return;
  5611. }
  5612. } catch(e) {
  5613. // this pref isn't set by default, ignore failures
  5614. }
  5615. var host = currentURI.asciiHost;
  5616. var notificationBox = gBrowser.getNotificationBox(browser);
  5617. var notificationID = "offline-app-requested-" + host;
  5618. var notification = notificationBox.getNotificationWithValue(notificationID);
  5619. if (notification) {
  5620. notification.documents.push(aContentWindow.document);
  5621. } else {
  5622. var buttons = [{
  5623. label: gNavigatorBundle.getString("offlineApps.allow"),
  5624. accessKey: gNavigatorBundle.getString("offlineApps.allowAccessKey"),
  5625. callback: function() {
  5626. for (var i = 0; i < notification.documents.length; i++) {
  5627. OfflineApps.allowSite(notification.documents[i]);
  5628. }
  5629. }
  5630. },{
  5631. label: gNavigatorBundle.getString("offlineApps.never"),
  5632. accessKey: gNavigatorBundle.getString("offlineApps.neverAccessKey"),
  5633. callback: function() {
  5634. for (var i = 0; i < notification.documents.length; i++) {
  5635. OfflineApps.disallowSite(notification.documents[i]);
  5636. }
  5637. }
  5638. },{
  5639. label: gNavigatorBundle.getString("offlineApps.notNow"),
  5640. accessKey: gNavigatorBundle.getString("offlineApps.notNowAccessKey"),
  5641. callback: function() { /* noop */ }
  5642. }];
  5643. const priority = notificationBox.PRIORITY_INFO_LOW;
  5644. var message = gNavigatorBundle.getFormattedString("offlineApps.available",
  5645. [ host ]);
  5646. notification =
  5647. notificationBox.appendNotification(message, notificationID,
  5648. "chrome://browser/skin/Info.png",
  5649. priority, buttons);
  5650. notification.documents = [ aContentWindow.document ];
  5651. }
  5652. },
  5653. allowSite: function(aDocument) {
  5654. Services.perms.add(aDocument.documentURIObject, "offline-app", Services.perms.ALLOW_ACTION);
  5655. // When a site is enabled while loading, manifest resources will
  5656. // start fetching immediately. This one time we need to do it
  5657. // ourselves.
  5658. this._startFetching(aDocument);
  5659. },
  5660. disallowSite: function(aDocument) {
  5661. Services.perms.add(aDocument.documentURIObject, "offline-app", Services.perms.DENY_ACTION);
  5662. },
  5663. manage: function() {
  5664. openAdvancedPreferences("networkTab");
  5665. },
  5666. _startFetching: function(aDocument) {
  5667. if (!aDocument.documentElement)
  5668. return;
  5669. var manifest = aDocument.documentElement.getAttribute("manifest");
  5670. if (!manifest)
  5671. return;
  5672. var manifestURI = makeURI(manifest, aDocument.characterSet,
  5673. aDocument.documentURIObject);
  5674. var updateService = Cc["@mozilla.org/offlinecacheupdate-service;1"].
  5675. getService(Ci.nsIOfflineCacheUpdateService);
  5676. updateService.scheduleUpdate(manifestURI, aDocument.documentURIObject, window);
  5677. },
  5678. /////////////////////////////////////////////////////////////////////////////
  5679. // nsIObserver
  5680. observe: function (aSubject, aTopic, aState)
  5681. {
  5682. if (aTopic == "dom-storage-warn-quota-exceeded") {
  5683. if (aSubject) {
  5684. var uri = makeURI(aSubject.location.href);
  5685. if (OfflineApps._checkUsage(uri)) {
  5686. var browserWindow =
  5687. this._getBrowserWindowForContentWindow(aSubject);
  5688. var browser = this._getBrowserForContentWindow(browserWindow,
  5689. aSubject);
  5690. OfflineApps._warnUsage(browser, uri);
  5691. }
  5692. }
  5693. } else if (aTopic == "offline-cache-update-completed") {
  5694. var cacheUpdate = aSubject.QueryInterface(Ci.nsIOfflineCacheUpdate);
  5695. var uri = cacheUpdate.manifestURI;
  5696. if (OfflineApps._checkUsage(uri)) {
  5697. var browser = this._getBrowserForCacheUpdate(cacheUpdate);
  5698. if (browser) {
  5699. OfflineApps._warnUsage(browser, cacheUpdate.manifestURI);
  5700. }
  5701. }
  5702. }
  5703. }
  5704. };
  5705. var IndexedDBPromptHelper = {
  5706. _permissionsPrompt: "indexedDB-permissions-prompt",
  5707. _permissionsResponse: "indexedDB-permissions-response",
  5708. _quotaPrompt: "indexedDB-quota-prompt",
  5709. _quotaResponse: "indexedDB-quota-response",
  5710. _quotaCancel: "indexedDB-quota-cancel",
  5711. _notificationIcon: "indexedDB-notification-icon",
  5712. init:
  5713. function IndexedDBPromptHelper_init() {
  5714. Services.obs.addObserver(this, this._permissionsPrompt, false);
  5715. Services.obs.addObserver(this, this._quotaPrompt, false);
  5716. Services.obs.addObserver(this, this._quotaCancel, false);
  5717. },
  5718. uninit:
  5719. function IndexedDBPromptHelper_uninit() {
  5720. Services.obs.removeObserver(this, this._permissionsPrompt, false);
  5721. Services.obs.removeObserver(this, this._quotaPrompt, false);
  5722. Services.obs.removeObserver(this, this._quotaCancel, false);
  5723. },
  5724. observe:
  5725. function IndexedDBPromptHelper_observe(subject, topic, data) {
  5726. if (topic != this._permissionsPrompt &&
  5727. topic != this._quotaPrompt &&
  5728. topic != this._quotaCancel) {
  5729. throw new Error("Unexpected topic!");
  5730. }
  5731. var requestor = subject.QueryInterface(Ci.nsIInterfaceRequestor);
  5732. var contentWindow = requestor.getInterface(Ci.nsIDOMWindow);
  5733. var contentDocument = contentWindow.document;
  5734. var browserWindow =
  5735. OfflineApps._getBrowserWindowForContentWindow(contentWindow);
  5736. if (browserWindow != window) {
  5737. // Must belong to some other window.
  5738. return;
  5739. }
  5740. var browser =
  5741. OfflineApps._getBrowserForContentWindow(browserWindow, contentWindow);
  5742. var host = contentDocument.documentURIObject.asciiHost;
  5743. var message;
  5744. var responseTopic;
  5745. if (topic == this._permissionsPrompt) {
  5746. message = gNavigatorBundle.getFormattedString("offlineApps.available",
  5747. [ host ]);
  5748. responseTopic = this._permissionsResponse;
  5749. }
  5750. else if (topic == this._quotaPrompt) {
  5751. message = gNavigatorBundle.getFormattedString("indexedDB.usage",
  5752. [ host, data ]);
  5753. responseTopic = this._quotaResponse;
  5754. }
  5755. else if (topic == this._quotaCancel) {
  5756. responseTopic = this._quotaResponse;
  5757. }
  5758. const hiddenTimeoutDuration = 30000; // 30 seconds
  5759. const firstTimeoutDuration = 360000; // 5 minutes
  5760. var timeoutId;
  5761. var observer = requestor.getInterface(Ci.nsIObserver);
  5762. var mainAction = {
  5763. label: gNavigatorBundle.getString("offlineApps.allow"),
  5764. accessKey: gNavigatorBundle.getString("offlineApps.allowAccessKey"),
  5765. callback: function() {
  5766. clearTimeout(timeoutId);
  5767. observer.observe(null, responseTopic,
  5768. Ci.nsIPermissionManager.ALLOW_ACTION);
  5769. }
  5770. };
  5771. var secondaryActions = [
  5772. {
  5773. label: gNavigatorBundle.getString("offlineApps.never"),
  5774. accessKey: gNavigatorBundle.getString("offlineApps.neverAccessKey"),
  5775. callback: function() {
  5776. clearTimeout(timeoutId);
  5777. observer.observe(null, responseTopic,
  5778. Ci.nsIPermissionManager.DENY_ACTION);
  5779. }
  5780. }
  5781. ];
  5782. // This will be set to the result of PopupNotifications.show() below, or to
  5783. // the result of PopupNotifications.getNotification() if this is a
  5784. // quotaCancel notification.
  5785. var notification;
  5786. function timeoutNotification() {
  5787. // Remove the notification.
  5788. if (notification) {
  5789. notification.remove();
  5790. }
  5791. // Clear all of our timeout stuff. We may be called directly, not just
  5792. // when the timeout actually elapses.
  5793. clearTimeout(timeoutId);
  5794. // And tell the page that the popup timed out.
  5795. observer.observe(null, responseTopic,
  5796. Ci.nsIPermissionManager.UNKNOWN_ACTION);
  5797. }
  5798. var options = {
  5799. eventCallback: function(state) {
  5800. // Don't do anything if the timeout has not been set yet.
  5801. if (!timeoutId) {
  5802. return;
  5803. }
  5804. // If the popup is being dismissed start the short timeout.
  5805. if (state == "dismissed") {
  5806. clearTimeout(timeoutId);
  5807. timeoutId = setTimeout(timeoutNotification, hiddenTimeoutDuration);
  5808. return;
  5809. }
  5810. // If the popup is being re-shown then clear the timeout allowing
  5811. // unlimited waiting.
  5812. if (state == "shown") {
  5813. clearTimeout(timeoutId);
  5814. }
  5815. }
  5816. };
  5817. if (topic == this._quotaCancel) {
  5818. notification = PopupNotifications.getNotification(this._quotaPrompt,
  5819. browser);
  5820. timeoutNotification();
  5821. return;
  5822. }
  5823. notification = PopupNotifications.show(browser, topic, message,
  5824. this._notificationIcon, mainAction,
  5825. secondaryActions, options);
  5826. // Set the timeoutId after the popup has been created, and use the long
  5827. // timeout value. If the user doesn't notice the popup after this amount of
  5828. // time then it is most likely not visible and we want to alert the page.
  5829. timeoutId = setTimeout(timeoutNotification, firstTimeoutDuration);
  5830. }
  5831. };
  5832. function WindowIsClosing()
  5833. {
  5834. if (TabView.isVisible()) {
  5835. TabView.hide();
  5836. return false;
  5837. }
  5838. var reallyClose = closeWindow(false, warnAboutClosingWindow);
  5839. if (!reallyClose)
  5840. return false;
  5841. var numBrowsers = gBrowser.browsers.length;
  5842. for (let i = 0; reallyClose && i < numBrowsers; ++i) {
  5843. let ds = gBrowser.browsers[i].docShell;
  5844. if (ds.contentViewer && !ds.contentViewer.permitUnload())
  5845. reallyClose = false;
  5846. }
  5847. return reallyClose;
  5848. }
  5849. /**
  5850. * Checks if this is the last full *browser* window around. If it is, this will
  5851. * be communicated like quitting. Otherwise, we warn about closing multiple tabs.
  5852. * @returns true if closing can proceed, false if it got cancelled.
  5853. */
  5854. function warnAboutClosingWindow() {
  5855. // Popups aren't considered full browser windows.
  5856. if (!toolbar.visible)
  5857. return gBrowser.warnAboutClosingTabs(true);
  5858. // Figure out if there's at least one other browser window around.
  5859. let e = Services.wm.getEnumerator("navigator:browser");
  5860. while (e.hasMoreElements()) {
  5861. let win = e.getNext();
  5862. if (win != window && win.toolbar.visible)
  5863. return gBrowser.warnAboutClosingTabs(true);
  5864. }
  5865. let os = Services.obs;
  5866. let closingCanceled = Cc["@mozilla.org/supports-PRBool;1"].
  5867. createInstance(Ci.nsISupportsPRBool);
  5868. os.notifyObservers(closingCanceled,
  5869. "browser-lastwindow-close-requested", null);
  5870. if (closingCanceled.data)
  5871. return false;
  5872. os.notifyObservers(null, "browser-lastwindow-close-granted", null);
  5873. #ifdef XP_MACOSX
  5874. // OS X doesn't quit the application when the last window is closed, but keeps
  5875. // the session alive. Hence don't prompt users to save tabs, but warn about
  5876. // closing multiple tabs.
  5877. return gBrowser.warnAboutClosingTabs(true);
  5878. #else
  5879. return true;
  5880. #endif
  5881. }
  5882. var MailIntegration = {
  5883. sendLinkForWindow: function (aWindow) {
  5884. this.sendMessage(aWindow.location.href,
  5885. aWindow.document.title);
  5886. },
  5887. sendMessage: function (aBody, aSubject) {
  5888. // generate a mailto url based on the url and the url's title
  5889. var mailtoUrl = "mailto:";
  5890. if (aBody) {
  5891. mailtoUrl += "?body=" + encodeURIComponent(aBody);
  5892. mailtoUrl += "&subject=" + encodeURIComponent(aSubject);
  5893. }
  5894. var uri = makeURI(mailtoUrl);
  5895. // now pass this uri to the operating system
  5896. this._launchExternalUrl(uri);
  5897. },
  5898. // a generic method which can be used to pass arbitrary urls to the operating
  5899. // system.
  5900. // aURL --> a nsIURI which represents the url to launch
  5901. _launchExternalUrl: function (aURL) {
  5902. var extProtocolSvc =
  5903. Cc["@mozilla.org/uriloader/external-protocol-service;1"]
  5904. .getService(Ci.nsIExternalProtocolService);
  5905. if (extProtocolSvc)
  5906. extProtocolSvc.loadUrl(aURL);
  5907. }
  5908. };
  5909. function BrowserOpenAddonsMgr(aView) {
  5910. if (aView) {
  5911. let emWindow;
  5912. let browserWindow;
  5913. function receivePong(aSubject, aTopic, aData) {
  5914. let browserWin = aSubject.QueryInterface(Ci.nsIInterfaceRequestor)
  5915. .getInterface(Ci.nsIWebNavigation)
  5916. .QueryInterface(Ci.nsIDocShellTreeItem)
  5917. .rootTreeItem
  5918. .QueryInterface(Ci.nsIInterfaceRequestor)
  5919. .getInterface(Ci.nsIDOMWindow);
  5920. if (!emWindow || browserWin == window /* favor the current window */) {
  5921. emWindow = aSubject;
  5922. browserWindow = browserWin;
  5923. }
  5924. }
  5925. Services.obs.addObserver(receivePong, "EM-pong", false);
  5926. Services.obs.notifyObservers(null, "EM-ping", "");
  5927. Services.obs.removeObserver(receivePong, "EM-pong");
  5928. if (emWindow) {
  5929. emWindow.loadView(aView);
  5930. browserWindow.gBrowser.selectedTab =
  5931. browserWindow.gBrowser._getTabForContentWindow(emWindow);
  5932. emWindow.focus();
  5933. return;
  5934. }
  5935. }
  5936. var newLoad = !switchToTabHavingURI("about:addons", true);
  5937. if (aView) {
  5938. // This must be a new load, else the ping/pong would have
  5939. // found the window above.
  5940. Services.obs.addObserver(function (aSubject, aTopic, aData) {
  5941. Services.obs.removeObserver(arguments.callee, aTopic);
  5942. aSubject.loadView(aView);
  5943. }, "EM-loaded", false);
  5944. }
  5945. }
  5946. function AddKeywordForSearchField() {
  5947. var node = document.popupNode;
  5948. var charset = node.ownerDocument.characterSet;
  5949. var docURI = makeURI(node.ownerDocument.URL,
  5950. charset);
  5951. var formURI = makeURI(node.form.getAttribute("action"),
  5952. charset,
  5953. docURI);
  5954. var spec = formURI.spec;
  5955. var isURLEncoded =
  5956. (node.form.method.toUpperCase() == "POST"
  5957. && (node.form.enctype == "application/x-www-form-urlencoded" ||
  5958. node.form.enctype == ""));
  5959. var title = gNavigatorBundle.getFormattedString("addKeywordTitleAutoFill",
  5960. [node.ownerDocument.title]);
  5961. var description = PlacesUIUtils.getDescriptionFromDocument(node.ownerDocument);
  5962. var el, type;
  5963. var formData = [];
  5964. function escapeNameValuePair(aName, aValue, aIsFormUrlEncoded) {
  5965. if (aIsFormUrlEncoded)
  5966. return escape(aName + "=" + aValue);
  5967. else
  5968. return escape(aName) + "=" + escape(aValue);
  5969. }
  5970. for (var i=0; i < node.form.elements.length; i++) {
  5971. el = node.form.elements[i];
  5972. if (!el.type) // happens with fieldsets
  5973. continue;
  5974. if (el == node) {
  5975. formData.push((isURLEncoded) ? escapeNameValuePair(el.name, "%s", true) :
  5976. // Don't escape "%s", just append
  5977. escapeNameValuePair(el.name, "", false) + "%s");
  5978. continue;
  5979. }
  5980. type = el.type.toLowerCase();
  5981. if (((el instanceof HTMLInputElement && el.mozIsTextField(true)) ||
  5982. type == "hidden" || type == "textarea") ||
  5983. ((type == "checkbox" || type == "radio") && el.checked)) {
  5984. formData.push(escapeNameValuePair(el.name, el.value, isURLEncoded));
  5985. } else if (el instanceof HTMLSelectElement && el.selectedIndex >= 0) {
  5986. for (var j=0; j < el.options.length; j++) {
  5987. if (el.options[j].selected)
  5988. formData.push(escapeNameValuePair(el.name, el.options[j].value,
  5989. isURLEncoded));
  5990. }
  5991. }
  5992. }
  5993. var postData;
  5994. if (isURLEncoded)
  5995. postData = formData.join("&");
  5996. else
  5997. spec += "?" + formData.join("&");
  5998. PlacesUIUtils.showBookmarkDialog({ action: "add"
  5999. , type: "bookmark"
  6000. , uri: makeURI(spec)
  6001. , title: title
  6002. , description: description
  6003. , keyword: ""
  6004. , postData: postData
  6005. , charSet: charset
  6006. , hiddenRows: [ "location"
  6007. , "description"
  6008. , "tags"
  6009. , "loadInSidebar" ]
  6010. }, window);
  6011. }
  6012. function SwitchDocumentDirection(aWindow) {
  6013. aWindow.document.dir = (aWindow.document.dir == "ltr" ? "rtl" : "ltr");
  6014. for (var run = 0; run < aWindow.frames.length; run++)
  6015. SwitchDocumentDirection(aWindow.frames[run]);
  6016. }
  6017. function getPluginInfo(pluginElement)
  6018. {
  6019. var tagMimetype;
  6020. var pluginsPage;
  6021. if (pluginElement instanceof HTMLAppletElement) {
  6022. tagMimetype = "application/x-java-vm";
  6023. } else {
  6024. if (pluginElement instanceof HTMLObjectElement) {
  6025. pluginsPage = pluginElement.getAttribute("codebase");
  6026. } else {
  6027. pluginsPage = pluginElement.getAttribute("pluginspage");
  6028. }
  6029. // only attempt if a pluginsPage is defined.
  6030. if (pluginsPage) {
  6031. var doc = pluginElement.ownerDocument;
  6032. var docShell = findChildShell(doc, gBrowser.docShell, null);
  6033. try {
  6034. pluginsPage = makeURI(pluginsPage, doc.characterSet, docShell.currentURI).spec;
  6035. } catch (ex) {
  6036. pluginsPage = "";
  6037. }
  6038. }
  6039. tagMimetype = pluginElement.QueryInterface(Components.interfaces.nsIObjectLoadingContent)
  6040. .actualType;
  6041. if (tagMimetype == "") {
  6042. tagMimetype = pluginElement.type;
  6043. }
  6044. }
  6045. return {mimetype: tagMimetype, pluginsPage: pluginsPage};
  6046. }
  6047. var gPluginHandler = {
  6048. get CrashSubmit() {
  6049. delete this.CrashSubmit;
  6050. Cu.import("resource://gre/modules/CrashSubmit.jsm", this);
  6051. return this.CrashSubmit;
  6052. },
  6053. // Map the plugin's name to a filtered version more suitable for user UI.
  6054. makeNicePluginName : function (aName, aFilename) {
  6055. if (aName == "Shockwave Flash")
  6056. return "Adobe Flash";
  6057. // Clean up the plugin name by stripping off any trailing version numbers
  6058. // or "plugin". EG, "Foo Bar Plugin 1.23_02" --> "Foo Bar"
  6059. let newName = aName.replace(/\bplug-?in\b/i, "").replace(/[\s\d\.\-\_\(\)]+$/, "");
  6060. return newName;
  6061. },
  6062. isTooSmall : function (plugin, overlay) {
  6063. // Is the <object>'s size too small to hold what we want to show?
  6064. let pluginRect = plugin.getBoundingClientRect();
  6065. // XXX bug 446693. The text-shadow on the submitted-report text at
  6066. // the bottom causes scrollHeight to be larger than it should be.
  6067. let overflows = (overlay.scrollWidth > pluginRect.width) ||
  6068. (overlay.scrollHeight - 5 > pluginRect.height);
  6069. return overflows;
  6070. },
  6071. addLinkClickCallback: function (linkNode, callbackName /*callbackArgs...*/) {
  6072. // XXX just doing (callback)(arg) was giving a same-origin error. bug?
  6073. let self = this;
  6074. let callbackArgs = Array.prototype.slice.call(arguments).slice(2);
  6075. linkNode.addEventListener("click",
  6076. function(evt) {
  6077. if (!evt.isTrusted)
  6078. return;
  6079. evt.preventDefault();
  6080. if (callbackArgs.length == 0)
  6081. callbackArgs = [ evt ];
  6082. (self[callbackName]).apply(self, callbackArgs);
  6083. },
  6084. true);
  6085. linkNode.addEventListener("keydown",
  6086. function(evt) {
  6087. if (!evt.isTrusted)
  6088. return;
  6089. if (evt.keyCode == evt.DOM_VK_RETURN) {
  6090. evt.preventDefault();
  6091. if (callbackArgs.length == 0)
  6092. callbackArgs = [ evt ];
  6093. evt.preventDefault();
  6094. (self[callbackName]).apply(self, callbackArgs);
  6095. }
  6096. },
  6097. true);
  6098. },
  6099. handleEvent : function(event) {
  6100. let self = gPluginHandler;
  6101. let plugin = event.target;
  6102. let doc = plugin.ownerDocument;
  6103. // We're expecting the target to be a plugin.
  6104. if (!(plugin instanceof Ci.nsIObjectLoadingContent))
  6105. return;
  6106. // Force a style flush, so that we ensure our binding is attached.
  6107. plugin.clientTop;
  6108. switch (event.type) {
  6109. case "PluginCrashed":
  6110. self.pluginInstanceCrashed(plugin, event);
  6111. break;
  6112. case "PluginNotFound":
  6113. // For non-object plugin tags, register a click handler to install the
  6114. // plugin. Object tags can, and often do, deal with that themselves,
  6115. // so don't stomp on the page developers toes.
  6116. if (!(plugin instanceof HTMLObjectElement)) {
  6117. // We don't yet check to see if there's actually an installer available.
  6118. let installStatus = doc.getAnonymousElementByAttribute(plugin, "class", "installStatus");
  6119. installStatus.setAttribute("status", "ready");
  6120. let iconStatus = doc.getAnonymousElementByAttribute(plugin, "class", "icon");
  6121. iconStatus.setAttribute("status", "ready");
  6122. let installLink = doc.getAnonymousElementByAttribute(plugin, "class", "installPluginLink");
  6123. self.addLinkClickCallback(installLink, "installSinglePlugin", plugin);
  6124. }
  6125. /* FALLTHRU */
  6126. case "PluginBlocklisted":
  6127. case "PluginOutdated":
  6128. #ifdef XP_MACOSX
  6129. case "npapi-carbon-event-model-failure":
  6130. #endif
  6131. self.pluginUnavailable(plugin, event.type);
  6132. break;
  6133. case "PluginDisabled":
  6134. let manageLink = doc.getAnonymousElementByAttribute(plugin, "class", "managePluginsLink");
  6135. self.addLinkClickCallback(manageLink, "managePlugins");
  6136. break;
  6137. }
  6138. // Hide the in-content UI if it's too big. The crashed plugin handler already did this.
  6139. if (event.type != "PluginCrashed") {
  6140. let overlay = doc.getAnonymousElementByAttribute(plugin, "class", "mainBox");
  6141. if (self.isTooSmall(plugin, overlay))
  6142. overlay.style.visibility = "hidden";
  6143. }
  6144. },
  6145. newPluginInstalled : function(event) {
  6146. // browser elements are anonymous so we can't just use target.
  6147. var browser = event.originalTarget;
  6148. // clear the plugin list, now that at least one plugin has been installed
  6149. browser.missingPlugins = null;
  6150. var notificationBox = gBrowser.getNotificationBox(browser);
  6151. var notification = notificationBox.getNotificationWithValue("missing-plugins");
  6152. if (notification)
  6153. notificationBox.removeNotification(notification);
  6154. // reload the browser to make the new plugin show.
  6155. browser.reload();
  6156. },
  6157. // Callback for user clicking on a missing (unsupported) plugin.
  6158. installSinglePlugin: function (plugin) {
  6159. var missingPluginsArray = {};
  6160. var pluginInfo = getPluginInfo(plugin);
  6161. missingPluginsArray[pluginInfo.mimetype] = pluginInfo;
  6162. openDialog("chrome://mozapps/content/plugins/pluginInstallerWizard.xul",
  6163. "PFSWindow", "chrome,centerscreen,resizable=yes",
  6164. {plugins: missingPluginsArray, browser: gBrowser.selectedBrowser});
  6165. },
  6166. // Callback for user clicking on a disabled plugin
  6167. managePlugins: function (aEvent) {
  6168. BrowserOpenAddonsMgr("addons://list/plugin");
  6169. },
  6170. // Callback for user clicking "submit a report" link
  6171. submitReport : function(pluginDumpID, browserDumpID) {
  6172. // The crash reporter wants a DOM element it can append an IFRAME to,
  6173. // which it uses to submit a form. Let's just give it gBrowser.
  6174. this.CrashSubmit.submit(pluginDumpID);
  6175. if (browserDumpID)
  6176. this.CrashSubmit.submit(browserDumpID);
  6177. },
  6178. // Callback for user clicking a "reload page" link
  6179. reloadPage: function (browser) {
  6180. browser.reload();
  6181. },
  6182. // Callback for user clicking the help icon
  6183. openHelpPage: function () {
  6184. openHelpLink("plugin-crashed", false);
  6185. },
  6186. // event listener for missing/blocklisted/outdated/carbonFailure plugins.
  6187. pluginUnavailable: function (plugin, eventType) {
  6188. let browser = gBrowser.getBrowserForDocument(plugin.ownerDocument
  6189. .defaultView.top.document);
  6190. if (!browser.missingPlugins)
  6191. browser.missingPlugins = {};
  6192. var pluginInfo = getPluginInfo(plugin);
  6193. browser.missingPlugins[pluginInfo.mimetype] = pluginInfo;
  6194. var notificationBox = gBrowser.getNotificationBox(browser);
  6195. // Should only display one of these warnings per page.
  6196. // In order of priority, they are: outdated > missing > blocklisted
  6197. let outdatedNotification = notificationBox.getNotificationWithValue("outdated-plugins");
  6198. let blockedNotification = notificationBox.getNotificationWithValue("blocked-plugins");
  6199. let missingNotification = notificationBox.getNotificationWithValue("missing-plugins");
  6200. function showBlocklistInfo() {
  6201. var url = formatURL("extensions.blocklist.detailsURL", true);
  6202. gBrowser.loadOneTab(url, {inBackground: false});
  6203. return true;
  6204. }
  6205. function showOutdatedPluginsInfo() {
  6206. gPrefService.setBoolPref("plugins.update.notifyUser", false);
  6207. var url = formatURL("plugins.update.url", true);
  6208. gBrowser.loadOneTab(url, {inBackground: false});
  6209. return true;
  6210. }
  6211. function showPluginsMissing() {
  6212. // get the urls of missing plugins
  6213. var missingPluginsArray = gBrowser.selectedBrowser.missingPlugins;
  6214. if (missingPluginsArray) {
  6215. openDialog("chrome://mozapps/content/plugins/pluginInstallerWizard.xul",
  6216. "PFSWindow", "chrome,centerscreen,resizable=yes",
  6217. {plugins: missingPluginsArray, browser: gBrowser.selectedBrowser});
  6218. }
  6219. }
  6220. #ifdef XP_MACOSX
  6221. function carbonFailurePluginsRestartBrowser()
  6222. {
  6223. // Notify all windows that an application quit has been requested.
  6224. let cancelQuit = Cc["@mozilla.org/supports-PRBool;1"].
  6225. createInstance(Ci.nsISupportsPRBool);
  6226. Services.obs.notifyObservers(cancelQuit, "quit-application-requested", null);
  6227. // Something aborted the quit process.
  6228. if (cancelQuit.data)
  6229. return;
  6230. let as = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup);
  6231. as.quit(Ci.nsIAppStartup.eRestarti386 | Ci.nsIAppStartup.eRestart | Ci.nsIAppStartup.eAttemptQuit);
  6232. }
  6233. #endif
  6234. let notifications = {
  6235. PluginBlocklisted : {
  6236. barID : "blocked-plugins",
  6237. iconURL : "chrome://mozapps/skin/plugins/notifyPluginBlocked.png",
  6238. message : gNavigatorBundle.getString("blockedpluginsMessage.title"),
  6239. buttons : [{
  6240. label : gNavigatorBundle.getString("blockedpluginsMessage.infoButton.label"),
  6241. accessKey : gNavigatorBundle.getString("blockedpluginsMessage.infoButton.accesskey"),
  6242. popup : null,
  6243. callback : showBlocklistInfo
  6244. },
  6245. {
  6246. label : gNavigatorBundle.getString("blockedpluginsMessage.searchButton.label"),
  6247. accessKey : gNavigatorBundle.getString("blockedpluginsMessage.searchButton.accesskey"),
  6248. popup : null,
  6249. callback : showOutdatedPluginsInfo
  6250. }],
  6251. },
  6252. PluginOutdated : {
  6253. barID : "outdated-plugins",
  6254. iconURL : "chrome://mozapps/skin/plugins/notifyPluginOutdated.png",
  6255. message : gNavigatorBundle.getString("outdatedpluginsMessage.title"),
  6256. buttons : [{
  6257. label : gNavigatorBundle.getString("outdatedpluginsMessage.updateButton.label"),
  6258. accessKey : gNavigatorBundle.getString("outdatedpluginsMessage.updateButton.accesskey"),
  6259. popup : null,
  6260. callback : showOutdatedPluginsInfo
  6261. }],
  6262. },
  6263. PluginNotFound : {
  6264. barID : "missing-plugins",
  6265. iconURL : "chrome://mozapps/skin/plugins/notifyPluginGeneric.png",
  6266. message : gNavigatorBundle.getString("missingpluginsMessage.title"),
  6267. buttons : [{
  6268. label : gNavigatorBundle.getString("missingpluginsMessage.button.label"),
  6269. accessKey : gNavigatorBundle.getString("missingpluginsMessage.button.accesskey"),
  6270. popup : null,
  6271. callback : showPluginsMissing
  6272. }],
  6273. },
  6274. #ifdef XP_MACOSX
  6275. "npapi-carbon-event-model-failure" : {
  6276. barID : "carbon-failure-plugins",
  6277. iconURL : "chrome://mozapps/skin/plugins/notifyPluginGeneric.png",
  6278. message : gNavigatorBundle.getString("carbonFailurePluginsMessage.message"),
  6279. buttons: [{
  6280. label : gNavigatorBundle.getString("carbonFailurePluginsMessage.restartButton.label"),
  6281. accessKey : gNavigatorBundle.getString("carbonFailurePluginsMessage.restartButton.accesskey"),
  6282. popup : null,
  6283. callback : carbonFailurePluginsRestartBrowser
  6284. }],
  6285. }
  6286. #endif
  6287. };
  6288. // If there is already an outdated plugin notification then do nothing
  6289. if (outdatedNotification)
  6290. return;
  6291. #ifdef XP_MACOSX
  6292. if (eventType == "npapi-carbon-event-model-failure") {
  6293. if (gPrefService.getBoolPref("plugins.hide_infobar_for_carbon_failure_plugin"))
  6294. return;
  6295. let carbonFailureNotification =
  6296. notificationBox.getNotificationWithValue("carbon-failure-plugins");
  6297. if (carbonFailureNotification)
  6298. carbonFailureNotification.close();
  6299. let macutils = Cc["@mozilla.org/xpcom/mac-utils;1"].getService(Ci.nsIMacUtils);
  6300. // if this is not a Universal build, just follow PluginNotFound path
  6301. if (!macutils.isUniversalBinary)
  6302. eventType = "PluginNotFound";
  6303. }
  6304. #endif
  6305. if (eventType == "PluginBlocklisted") {
  6306. if (gPrefService.getBoolPref("plugins.hide_infobar_for_missing_plugin")) // XXX add a new pref?
  6307. return;
  6308. if (blockedNotification || missingNotification)
  6309. return;
  6310. }
  6311. else if (eventType == "PluginOutdated") {
  6312. if (gPrefService.getBoolPref("plugins.hide_infobar_for_outdated_plugin"))
  6313. return;
  6314. // Cancel any notification about blocklisting/missing plugins
  6315. if (blockedNotification)
  6316. blockedNotification.close();
  6317. if (missingNotification)
  6318. missingNotification.close();
  6319. }
  6320. else if (eventType == "PluginNotFound") {
  6321. if (gPrefService.getBoolPref("plugins.hide_infobar_for_missing_plugin"))
  6322. return;
  6323. if (missingNotification)
  6324. return;
  6325. // Cancel any notification about blocklisting plugins
  6326. if (blockedNotification)
  6327. blockedNotification.close();
  6328. }
  6329. let notify = notifications[eventType];
  6330. notificationBox.appendNotification(notify.message, notify.barID, notify.iconURL,
  6331. notificationBox.PRIORITY_WARNING_MEDIUM,
  6332. notify.buttons);
  6333. },
  6334. // Crashed-plugin observer. Notified once per plugin crash, before events
  6335. // are dispatched to individual plugin instances.
  6336. pluginCrashed : function(subject, topic, data) {
  6337. let propertyBag = subject;
  6338. if (!(propertyBag instanceof Ci.nsIPropertyBag2) ||
  6339. !(propertyBag instanceof Ci.nsIWritablePropertyBag2))
  6340. return;
  6341. #ifdef MOZ_CRASHREPORTER
  6342. let pluginDumpID = propertyBag.getPropertyAsAString("pluginDumpID");
  6343. let browserDumpID= propertyBag.getPropertyAsAString("browserDumpID");
  6344. let shouldSubmit = gCrashReporter.submitReports;
  6345. let doPrompt = true; // XXX followup to get via gCrashReporter
  6346. // Submit automatically when appropriate.
  6347. if (pluginDumpID && shouldSubmit && !doPrompt) {
  6348. this.submitReport(pluginDumpID, browserDumpID);
  6349. // Submission is async, so we can't easily show failure UI.
  6350. propertyBag.setPropertyAsBool("submittedCrashReport", true);
  6351. }
  6352. #endif
  6353. },
  6354. // Crashed-plugin event listener. Called for every instance of a
  6355. // plugin in content.
  6356. pluginInstanceCrashed: function (plugin, aEvent) {
  6357. // Ensure the plugin and event are of the right type.
  6358. if (!(aEvent instanceof Ci.nsIDOMDataContainerEvent))
  6359. return;
  6360. let submittedReport = aEvent.getData("submittedCrashReport");
  6361. let doPrompt = true; // XXX followup for .getData("doPrompt");
  6362. let submitReports = true; // XXX followup for .getData("submitReports");
  6363. let pluginName = aEvent.getData("pluginName");
  6364. let pluginFilename = aEvent.getData("pluginFilename");
  6365. let pluginDumpID = aEvent.getData("pluginDumpID");
  6366. let browserDumpID = aEvent.getData("browserDumpID");
  6367. // Remap the plugin name to a more user-presentable form.
  6368. pluginName = this.makeNicePluginName(pluginName, pluginFilename);
  6369. let messageString = gNavigatorBundle.getFormattedString("crashedpluginsMessage.title", [pluginName]);
  6370. //
  6371. // Configure the crashed-plugin placeholder.
  6372. //
  6373. let doc = plugin.ownerDocument;
  6374. let overlay = doc.getAnonymousElementByAttribute(plugin, "class", "mainBox");
  6375. let statusDiv = doc.getAnonymousElementByAttribute(plugin, "class", "submitStatus");
  6376. #ifdef MOZ_CRASHREPORTER
  6377. let status;
  6378. // Determine which message to show regarding crash reports.
  6379. if (submittedReport) { // submitReports && !doPrompt, handled in observer
  6380. status = "submitted";
  6381. }
  6382. else if (!submitReports && !doPrompt) {
  6383. status = "noSubmit";
  6384. }
  6385. else { // doPrompt
  6386. status = "please";
  6387. // XXX can we make the link target actually be blank?
  6388. let pleaseLink = doc.getAnonymousElementByAttribute(
  6389. plugin, "class", "pleaseSubmitLink");
  6390. this.addLinkClickCallback(pleaseLink, "submitReport",
  6391. pluginDumpID, browserDumpID);
  6392. }
  6393. // If we don't have a minidumpID, we can't (or didn't) submit anything.
  6394. // This can happen if the plugin is killed from the task manager.
  6395. if (!pluginDumpID) {
  6396. status = "noReport";
  6397. }
  6398. statusDiv.setAttribute("status", status);
  6399. let bottomLinks = doc.getAnonymousElementByAttribute(plugin, "class", "msg msgBottomLinks");
  6400. bottomLinks.style.display = "block";
  6401. let helpIcon = doc.getAnonymousElementByAttribute(plugin, "class", "helpIcon");
  6402. this.addLinkClickCallback(helpIcon, "openHelpPage");
  6403. // If we're showing the link to manually trigger report submission, we'll
  6404. // want to be able to update all the instances of the UI for this crash to
  6405. // show an updated message when a report is submitted.
  6406. if (doPrompt) {
  6407. let observer = {
  6408. QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
  6409. Ci.nsISupportsWeakReference]),
  6410. observe : function(subject, topic, data) {
  6411. let propertyBag = subject;
  6412. if (!(propertyBag instanceof Ci.nsIPropertyBag2))
  6413. return;
  6414. // Ignore notifications for other crashes.
  6415. if (propertyBag.get("minidumpID") != pluginDumpID)
  6416. return;
  6417. statusDiv.setAttribute("status", data);
  6418. },
  6419. handleEvent : function(event) {
  6420. // Not expected to be called, just here for the closure.
  6421. }
  6422. }
  6423. // Use a weak reference, so we don't have to remove it...
  6424. Services.obs.addObserver(observer, "crash-report-status", true);
  6425. // ...alas, now we need something to hold a strong reference to prevent
  6426. // it from being GC. But I don't want to manually manage the reference's
  6427. // lifetime (which should be no greater than the page).
  6428. // Clever solution? Use a closue with an event listener on the document.
  6429. // When the doc goes away, so do the listener references and the closure.
  6430. doc.addEventListener("mozCleverClosureHack", observer, false);
  6431. }
  6432. #endif
  6433. let crashText = doc.getAnonymousElementByAttribute(plugin, "class", "msg msgCrashed");
  6434. crashText.textContent = messageString;
  6435. let browser = gBrowser.getBrowserForDocument(doc.defaultView.top.document);
  6436. let link = doc.getAnonymousElementByAttribute(plugin, "class", "reloadLink");
  6437. this.addLinkClickCallback(link, "reloadPage", browser);
  6438. let notificationBox = gBrowser.getNotificationBox(browser);
  6439. // Is the <object>'s size too small to hold what we want to show?
  6440. if (this.isTooSmall(plugin, overlay)) {
  6441. // Hide the overlay's contents. Use visibility style, so that it
  6442. // doesn't collapse down to 0x0.
  6443. overlay.style.visibility = "hidden";
  6444. // If another plugin on the page was large enough to show our UI, we
  6445. // don't want to show a notification bar.
  6446. if (!doc.mozNoPluginCrashedNotification)
  6447. showNotificationBar(pluginDumpID, browserDumpID);
  6448. } else {
  6449. // If a previous plugin on the page was too small and resulted in
  6450. // adding a notification bar, then remove it because this plugin
  6451. // instance it big enough to serve as in-content notification.
  6452. hideNotificationBar();
  6453. doc.mozNoPluginCrashedNotification = true;
  6454. }
  6455. function hideNotificationBar() {
  6456. let notification = notificationBox.getNotificationWithValue("plugin-crashed");
  6457. if (notification)
  6458. notificationBox.removeNotification(notification, true);
  6459. }
  6460. function showNotificationBar(pluginDumpID, browserDumpID) {
  6461. // If there's already an existing notification bar, don't do anything.
  6462. let notification = notificationBox.getNotificationWithValue("plugin-crashed");
  6463. if (notification)
  6464. return;
  6465. // Configure the notification bar
  6466. let priority = notificationBox.PRIORITY_WARNING_MEDIUM;
  6467. let iconURL = "chrome://mozapps/skin/plugins/notifyPluginCrashed.png";
  6468. let reloadLabel = gNavigatorBundle.getString("crashedpluginsMessage.reloadButton.label");
  6469. let reloadKey = gNavigatorBundle.getString("crashedpluginsMessage.reloadButton.accesskey");
  6470. let submitLabel = gNavigatorBundle.getString("crashedpluginsMessage.submitButton.label");
  6471. let submitKey = gNavigatorBundle.getString("crashedpluginsMessage.submitButton.accesskey");
  6472. let buttons = [{
  6473. label: reloadLabel,
  6474. accessKey: reloadKey,
  6475. popup: null,
  6476. callback: function() { browser.reload(); },
  6477. }];
  6478. #ifdef MOZ_CRASHREPORTER
  6479. let submitButton = {
  6480. label: submitLabel,
  6481. accessKey: submitKey,
  6482. popup: null,
  6483. callback: function() { gPluginHandler.submitReport(pluginDumpID, browserDumpID); },
  6484. };
  6485. if (pluginDumpID)
  6486. buttons.push(submitButton);
  6487. #endif
  6488. let notification = notificationBox.appendNotification(messageString, "plugin-crashed",
  6489. iconURL, priority, buttons);
  6490. // Add the "learn more" link.
  6491. let XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  6492. let link = notification.ownerDocument.createElementNS(XULNS, "label");
  6493. link.className = "text-link";
  6494. link.setAttribute("value", gNavigatorBundle.getString("crashedpluginsMessage.learnMore"));
  6495. let crashurl = formatURL("app.support.baseURL", true);
  6496. crashurl += "plugin-crashed-notificationbar";
  6497. link.href = crashurl;
  6498. let description = notification.ownerDocument.getAnonymousElementByAttribute(notification, "anonid", "messageText");
  6499. description.appendChild(link);
  6500. // Remove the notfication when the page is reloaded.
  6501. doc.defaultView.top.addEventListener("unload", function() {
  6502. notificationBox.removeNotification(notification);
  6503. }, false);
  6504. }
  6505. }
  6506. };
  6507. function convertFromUnicode(charset, str)
  6508. {
  6509. try {
  6510. var unicodeConverter = Components
  6511. .classes["@mozilla.org/intl/scriptableunicodeconverter"]
  6512. .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
  6513. unicodeConverter.charset = charset;
  6514. str = unicodeConverter.ConvertFromUnicode(str);
  6515. return str + unicodeConverter.Finish();
  6516. } catch(ex) {
  6517. return null;
  6518. }
  6519. }
  6520. /**
  6521. * The Feed Handler object manages discovery of RSS/ATOM feeds in web pages
  6522. * and shows UI when they are discovered.
  6523. */
  6524. var FeedHandler = {
  6525. /**
  6526. * The click handler for the Feed icon in the toolbar. Opens the
  6527. * subscription page if user is not given a choice of feeds.
  6528. * (Otherwise the list of available feeds will be presented to the
  6529. * user in a popup menu.)
  6530. */
  6531. onFeedButtonClick: function(event) {
  6532. event.stopPropagation();
  6533. let feeds = gBrowser.selectedBrowser.feeds || [];
  6534. // If there are multiple feeds, the menu will open, so no need to do
  6535. // anything. If there are no feeds, nothing to do either.
  6536. if (feeds.length != 1)
  6537. return;
  6538. if (event.eventPhase == Event.AT_TARGET &&
  6539. (event.button == 0 || event.button == 1)) {
  6540. this.subscribeToFeed(feeds[0].href, event);
  6541. }
  6542. },
  6543. /** Called when the user clicks on the Subscribe to This Page... menu item.
  6544. * Builds a menu of unique feeds associated with the page, and if there
  6545. * is only one, shows the feed inline in the browser window.
  6546. * @param menuPopup
  6547. * The feed list menupopup to be populated.
  6548. * @returns true if the menu should be shown, false if there was only
  6549. * one feed and the feed should be shown inline in the browser
  6550. * window (do not show the menupopup).
  6551. */
  6552. buildFeedList: function(menuPopup) {
  6553. var feeds = gBrowser.selectedBrowser.feeds;
  6554. if (feeds == null) {
  6555. // XXX hack -- menu opening depends on setting of an "open"
  6556. // attribute, and the menu refuses to open if that attribute is
  6557. // set (because it thinks it's already open). onpopupshowing gets
  6558. // called after the attribute is unset, and it doesn't get unset
  6559. // if we return false. so we unset it here; otherwise, the menu
  6560. // refuses to work past this point.
  6561. menuPopup.parentNode.removeAttribute("open");
  6562. return false;
  6563. }
  6564. while (menuPopup.firstChild)
  6565. menuPopup.removeChild(menuPopup.firstChild);
  6566. if (feeds.length <= 1)
  6567. return false;
  6568. // Build the menu showing the available feed choices for viewing.
  6569. for (var i = 0; i < feeds.length; ++i) {
  6570. var feedInfo = feeds[i];
  6571. var menuItem = document.createElement("menuitem");
  6572. var baseTitle = feedInfo.title || feedInfo.href;
  6573. var labelStr = gNavigatorBundle.getFormattedString("feedShowFeedNew", [baseTitle]);
  6574. menuItem.setAttribute("class", "feed-menuitem");
  6575. menuItem.setAttribute("label", labelStr);
  6576. menuItem.setAttribute("feed", feedInfo.href);
  6577. menuItem.setAttribute("tooltiptext", feedInfo.href);
  6578. menuItem.setAttribute("crop", "center");
  6579. menuPopup.appendChild(menuItem);
  6580. }
  6581. return true;
  6582. },
  6583. /**
  6584. * Subscribe to a given feed. Called when
  6585. * 1. Page has a single feed and user clicks feed icon in location bar
  6586. * 2. Page has a single feed and user selects Subscribe menu item
  6587. * 3. Page has multiple feeds and user selects from feed icon popup
  6588. * 4. Page has multiple feeds and user selects from Subscribe submenu
  6589. * @param href
  6590. * The feed to subscribe to. May be null, in which case the
  6591. * event target's feed attribute is examined.
  6592. * @param event
  6593. * The event this method is handling. Used to decide where
  6594. * to open the preview UI. (Optional, unless href is null)
  6595. */
  6596. subscribeToFeed: function(href, event) {
  6597. // Just load the feed in the content area to either subscribe or show the
  6598. // preview UI
  6599. if (!href)
  6600. href = event.target.getAttribute("feed");
  6601. urlSecurityCheck(href, gBrowser.contentPrincipal,
  6602. Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL);
  6603. var feedURI = makeURI(href, document.characterSet);
  6604. // Use the feed scheme so X-Moz-Is-Feed will be set
  6605. // The value doesn't matter
  6606. if (/^https?/.test(feedURI.scheme))
  6607. href = "feed:" + href;
  6608. this.loadFeed(href, event);
  6609. },
  6610. loadFeed: function(href, event) {
  6611. var feeds = gBrowser.selectedBrowser.feeds;
  6612. try {
  6613. openUILink(href, event, false, true, false, null);
  6614. }
  6615. finally {
  6616. // We might default to a livebookmarks modal dialog,
  6617. // so reset that if the user happens to click it again
  6618. gBrowser.selectedBrowser.feeds = feeds;
  6619. }
  6620. },
  6621. get _feedMenuitem() {
  6622. delete this._feedMenuitem;
  6623. return this._feedMenuitem = document.getElementById("singleFeedMenuitemState");
  6624. },
  6625. get _feedMenupopup() {
  6626. delete this._feedMenupopup;
  6627. return this._feedMenupopup = document.getElementById("multipleFeedsMenuState");
  6628. },
  6629. /**
  6630. * Update the browser UI to show whether or not feeds are available when
  6631. * a page is loaded or the user switches tabs to a page that has feeds.
  6632. */
  6633. updateFeeds: function() {
  6634. if (this._updateFeedTimeout)
  6635. clearTimeout(this._updateFeedTimeout);
  6636. var feeds = gBrowser.selectedBrowser.feeds;
  6637. var haveFeeds = feeds && feeds.length > 0;
  6638. var feedButton = document.getElementById("feed-button");
  6639. if (feedButton)
  6640. feedButton.disabled = !haveFeeds;
  6641. if (!haveFeeds) {
  6642. this._feedMenuitem.setAttribute("disabled", "true");
  6643. this._feedMenuitem.removeAttribute("hidden");
  6644. this._feedMenupopup.setAttribute("hidden", "true");
  6645. return;
  6646. }
  6647. if (feeds.length > 1) {
  6648. this._feedMenuitem.setAttribute("hidden", "true");
  6649. this._feedMenupopup.removeAttribute("hidden");
  6650. } else {
  6651. this._feedMenuitem.setAttribute("feed", feeds[0].href);
  6652. this._feedMenuitem.removeAttribute("disabled");
  6653. this._feedMenuitem.removeAttribute("hidden");
  6654. this._feedMenupopup.setAttribute("hidden", "true");
  6655. }
  6656. },
  6657. addFeed: function(link, targetDoc) {
  6658. // find which tab this is for, and set the attribute on the browser
  6659. var browserForLink = gBrowser.getBrowserForDocument(targetDoc);
  6660. if (!browserForLink) {
  6661. // ignore feeds loaded in subframes (see bug 305472)
  6662. return;
  6663. }
  6664. if (!browserForLink.feeds)
  6665. browserForLink.feeds = [];
  6666. browserForLink.feeds.push({ href: link.href, title: link.title });
  6667. // If this addition was for the current browser, update the UI. For
  6668. // background browsers, we'll update on tab switch.
  6669. if (browserForLink == gBrowser.selectedBrowser) {
  6670. // Batch updates to avoid updating the UI for multiple onLinkAdded events
  6671. // fired within 100ms of each other.
  6672. if (this._updateFeedTimeout)
  6673. clearTimeout(this._updateFeedTimeout);
  6674. this._updateFeedTimeout = setTimeout(this.updateFeeds.bind(this), 100);
  6675. }
  6676. }
  6677. };
  6678. /**
  6679. * Re-open a closed tab.
  6680. * @param aIndex
  6681. * The index of the tab (via nsSessionStore.getClosedTabData)
  6682. * @returns a reference to the reopened tab.
  6683. */
  6684. function undoCloseTab(aIndex) {
  6685. // wallpaper patch to prevent an unnecessary blank tab (bug 343895)
  6686. var blankTabToRemove = null;
  6687. if (gBrowser.tabs.length == 1 &&
  6688. !gPrefService.getBoolPref("browser.tabs.autoHide") &&
  6689. isTabEmpty(gBrowser.selectedTab))
  6690. blankTabToRemove = gBrowser.selectedTab;
  6691. var tab = null;
  6692. var ss = Cc["@mozilla.org/browser/sessionstore;1"].
  6693. getService(Ci.nsISessionStore);
  6694. if (ss.getClosedTabCount(window) > (aIndex || 0)) {
  6695. TabView.prepareUndoCloseTab(blankTabToRemove);
  6696. tab = ss.undoCloseTab(window, aIndex || 0);
  6697. TabView.afterUndoCloseTab();
  6698. if (blankTabToRemove)
  6699. gBrowser.removeTab(blankTabToRemove);
  6700. }
  6701. return tab;
  6702. }
  6703. /**
  6704. * Re-open a closed window.
  6705. * @param aIndex
  6706. * The index of the window (via nsSessionStore.getClosedWindowData)
  6707. * @returns a reference to the reopened window.
  6708. */
  6709. function undoCloseWindow(aIndex) {
  6710. let ss = Cc["@mozilla.org/browser/sessionstore;1"].
  6711. getService(Ci.nsISessionStore);
  6712. let window = null;
  6713. if (ss.getClosedWindowCount() > (aIndex || 0))
  6714. window = ss.undoCloseWindow(aIndex || 0);
  6715. return window;
  6716. }
  6717. /*
  6718. * Determines if a tab is "empty", usually used in the context of determining
  6719. * if it's ok to close the tab.
  6720. */
  6721. function isTabEmpty(aTab) {
  6722. let browser = aTab.linkedBrowser;
  6723. let uri = browser.currentURI.spec;
  6724. let body = browser.contentDocument.body;
  6725. return browser.sessionHistory.count < 2 &&
  6726. isBlankPageURL(uri) &&
  6727. (!body || !body.hasChildNodes()) &&
  6728. !aTab.hasAttribute("busy");
  6729. }
  6730. #ifdef MOZ_SERVICES_SYNC
  6731. function BrowserOpenSyncTabs() {
  6732. switchToTabHavingURI("about:sync-tabs", true);
  6733. }
  6734. #endif
  6735. /**
  6736. * Format a URL
  6737. * eg:
  6738. * echo formatURL("https://addons.mozilla.org/%LOCALE%/%APP%/%VERSION%/");
  6739. * > https://addons.mozilla.org/en-US/firefox/3.0a1/
  6740. *
  6741. * Currently supported built-ins are LOCALE, APP, and any value from nsIXULAppInfo, uppercased.
  6742. */
  6743. function formatURL(aFormat, aIsPref) {
  6744. var formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].getService(Ci.nsIURLFormatter);
  6745. return aIsPref ? formatter.formatURLPref(aFormat) : formatter.formatURL(aFormat);
  6746. }
  6747. /**
  6748. * Utility object to handle manipulations of the identity indicators in the UI
  6749. */
  6750. var gIdentityHandler = {
  6751. // Mode strings used to control CSS display
  6752. IDENTITY_MODE_IDENTIFIED : "verifiedIdentity", // High-quality identity information
  6753. IDENTITY_MODE_DOMAIN_VERIFIED : "verifiedDomain", // Minimal SSL CA-signed domain verification
  6754. IDENTITY_MODE_UNKNOWN : "unknownIdentity", // No trusted identity information
  6755. IDENTITY_MODE_MIXED_CONTENT : "unknownIdentity mixedContent", // SSL with unauthenticated content
  6756. IDENTITY_MODE_CHROMEUI : "chromeUI", // Part of the product's UI
  6757. // Cache the most recent SSLStatus and Location seen in checkIdentity
  6758. _lastStatus : null,
  6759. _lastLocation : null,
  6760. _mode : "unknownIdentity",
  6761. // smart getters
  6762. get _encryptionLabel () {
  6763. delete this._encryptionLabel;
  6764. this._encryptionLabel = {};
  6765. this._encryptionLabel[this.IDENTITY_MODE_DOMAIN_VERIFIED] =
  6766. gNavigatorBundle.getString("identity.encrypted");
  6767. this._encryptionLabel[this.IDENTITY_MODE_IDENTIFIED] =
  6768. gNavigatorBundle.getString("identity.encrypted");
  6769. this._encryptionLabel[this.IDENTITY_MODE_UNKNOWN] =
  6770. gNavigatorBundle.getString("identity.unencrypted");
  6771. this._encryptionLabel[this.IDENTITY_MODE_MIXED_CONTENT] =
  6772. gNavigatorBundle.getString("identity.mixed_content");
  6773. return this._encryptionLabel;
  6774. },
  6775. get _identityPopup () {
  6776. delete this._identityPopup;
  6777. return this._identityPopup = document.getElementById("identity-popup");
  6778. },
  6779. get _identityBox () {
  6780. delete this._identityBox;
  6781. return this._identityBox = document.getElementById("identity-box");
  6782. },
  6783. get _identityPopupContentBox () {
  6784. delete this._identityPopupContentBox;
  6785. return this._identityPopupContentBox =
  6786. document.getElementById("identity-popup-content-box");
  6787. },
  6788. get _identityPopupContentHost () {
  6789. delete this._identityPopupContentHost;
  6790. return this._identityPopupContentHost =
  6791. document.getElementById("identity-popup-content-host");
  6792. },
  6793. get _identityPopupContentOwner () {
  6794. delete this._identityPopupContentOwner;
  6795. return this._identityPopupContentOwner =
  6796. document.getElementById("identity-popup-content-owner");
  6797. },
  6798. get _identityPopupContentSupp () {
  6799. delete this._identityPopupContentSupp;
  6800. return this._identityPopupContentSupp =
  6801. document.getElementById("identity-popup-content-supplemental");
  6802. },
  6803. get _identityPopupContentVerif () {
  6804. delete this._identityPopupContentVerif;
  6805. return this._identityPopupContentVerif =
  6806. document.getElementById("identity-popup-content-verifier");
  6807. },
  6808. get _identityPopupEncLabel () {
  6809. delete this._identityPopupEncLabel;
  6810. return this._identityPopupEncLabel =
  6811. document.getElementById("identity-popup-encryption-label");
  6812. },
  6813. get _identityIconLabel () {
  6814. delete this._identityIconLabel;
  6815. return this._identityIconLabel = document.getElementById("identity-icon-label");
  6816. },
  6817. get _overrideService () {
  6818. delete this._overrideService;
  6819. return this._overrideService = Cc["@mozilla.org/security/certoverride;1"]
  6820. .getService(Ci.nsICertOverrideService);
  6821. },
  6822. get _identityIconCountryLabel () {
  6823. delete this._identityIconCountryLabel;
  6824. return this._identityIconCountryLabel = document.getElementById("identity-icon-country-label");
  6825. },
  6826. /**
  6827. * Rebuild cache of the elements that may or may not exist depending
  6828. * on whether there's a location bar.
  6829. */
  6830. _cacheElements : function() {
  6831. delete this._identityBox;
  6832. delete this._identityIconLabel;
  6833. delete this._identityIconCountryLabel;
  6834. this._identityBox = document.getElementById("identity-box");
  6835. this._identityIconLabel = document.getElementById("identity-icon-label");
  6836. this._identityIconCountryLabel = document.getElementById("identity-icon-country-label");
  6837. },
  6838. /**
  6839. * Handler for mouseclicks on the "More Information" button in the
  6840. * "identity-popup" panel.
  6841. */
  6842. handleMoreInfoClick : function(event) {
  6843. displaySecurityInfo();
  6844. event.stopPropagation();
  6845. },
  6846. /**
  6847. * Helper to parse out the important parts of _lastStatus (of the SSL cert in
  6848. * particular) for use in constructing identity UI strings
  6849. */
  6850. getIdentityData : function() {
  6851. var result = {};
  6852. var status = this._lastStatus.QueryInterface(Components.interfaces.nsISSLStatus);
  6853. var cert = status.serverCert;
  6854. // Human readable name of Subject
  6855. result.subjectOrg = cert.organization;
  6856. // SubjectName fields, broken up for individual access
  6857. if (cert.subjectName) {
  6858. result.subjectNameFields = {};
  6859. cert.subjectName.split(",").forEach(function(v) {
  6860. var field = v.split("=");
  6861. this[field[0]] = field[1];
  6862. }, result.subjectNameFields);
  6863. // Call out city, state, and country specifically
  6864. result.city = result.subjectNameFields.L;
  6865. result.state = result.subjectNameFields.ST;
  6866. result.country = result.subjectNameFields.C;
  6867. }
  6868. // Human readable name of Certificate Authority
  6869. result.caOrg = cert.issuerOrganization || cert.issuerCommonName;
  6870. result.cert = cert;
  6871. return result;
  6872. },
  6873. /**
  6874. * Determine the identity of the page being displayed by examining its SSL cert
  6875. * (if available) and, if necessary, update the UI to reflect this. Intended to
  6876. * be called by onSecurityChange
  6877. *
  6878. * @param PRUint32 state
  6879. * @param JS Object location that mirrors an nsLocation (i.e. has .host and
  6880. * .hostname and .port)
  6881. */
  6882. checkIdentity : function(state, location) {
  6883. var currentStatus = gBrowser.securityUI
  6884. .QueryInterface(Components.interfaces.nsISSLStatusProvider)
  6885. .SSLStatus;
  6886. this._lastStatus = currentStatus;
  6887. this._lastLocation = location;
  6888. let nsIWebProgressListener = Ci.nsIWebProgressListener;
  6889. if (location.protocol == "chrome:" || location.protocol == "about:")
  6890. this.setMode(this.IDENTITY_MODE_CHROMEUI);
  6891. else if (state & nsIWebProgressListener.STATE_IDENTITY_EV_TOPLEVEL)
  6892. this.setMode(this.IDENTITY_MODE_IDENTIFIED);
  6893. else if (state & nsIWebProgressListener.STATE_SECURE_HIGH)
  6894. this.setMode(this.IDENTITY_MODE_DOMAIN_VERIFIED);
  6895. else if (state & nsIWebProgressListener.STATE_IS_BROKEN)
  6896. this.setMode(this.IDENTITY_MODE_MIXED_CONTENT);
  6897. else
  6898. this.setMode(this.IDENTITY_MODE_UNKNOWN);
  6899. },
  6900. /**
  6901. * Return the eTLD+1 version of the current hostname
  6902. */
  6903. getEffectiveHost : function() {
  6904. // Cache the eTLDService if this is our first time through
  6905. if (!this._eTLDService)
  6906. this._eTLDService = Cc["@mozilla.org/network/effective-tld-service;1"]
  6907. .getService(Ci.nsIEffectiveTLDService);
  6908. if (!this._IDNService)
  6909. this._IDNService = Cc["@mozilla.org/network/idn-service;1"]
  6910. .getService(Ci.nsIIDNService);
  6911. try {
  6912. let baseDomain =
  6913. this._eTLDService.getBaseDomainFromHost(this._lastLocation.hostname);
  6914. return this._IDNService.convertToDisplayIDN(baseDomain, {});
  6915. } catch (e) {
  6916. // If something goes wrong (e.g. hostname is an IP address) just fail back
  6917. // to the full domain.
  6918. return this._lastLocation.hostname;
  6919. }
  6920. },
  6921. /**
  6922. * Update the UI to reflect the specified mode, which should be one of the
  6923. * IDENTITY_MODE_* constants.
  6924. */
  6925. setMode : function(newMode) {
  6926. if (!this._identityBox) {
  6927. // No identity box means the identity box is not visible, in which
  6928. // case there's nothing to do.
  6929. return;
  6930. }
  6931. this._identityBox.className = newMode;
  6932. this.setIdentityMessages(newMode);
  6933. // Update the popup too, if it's open
  6934. if (this._identityPopup.state == "open")
  6935. this.setPopupMessages(newMode);
  6936. this._mode = newMode;
  6937. },
  6938. /**
  6939. * Set up the messages for the primary identity UI based on the specified mode,
  6940. * and the details of the SSL cert, where applicable
  6941. *
  6942. * @param newMode The newly set identity mode. Should be one of the IDENTITY_MODE_* constants.
  6943. */
  6944. setIdentityMessages : function(newMode) {
  6945. if (newMode == this.IDENTITY_MODE_DOMAIN_VERIFIED) {
  6946. var iData = this.getIdentityData();
  6947. // It would be sort of nice to use the CN= field in the cert, since that's
  6948. // typically what we want here, but thanks to x509 certs being extensible,
  6949. // it's not the only place you have to check, there can be more than one domain,
  6950. // et cetera, ad nauseum. We know the cert is valid for location.host, so
  6951. // let's just use that. Check the pref to determine how much of the verified
  6952. // hostname to show
  6953. var icon_label = "";
  6954. var icon_country_label = "";
  6955. var icon_labels_dir = "ltr";
  6956. switch (gPrefService.getIntPref("browser.identity.ssl_domain_display")) {
  6957. case 2 : // Show full domain
  6958. icon_label = this._lastLocation.hostname;
  6959. break;
  6960. case 1 : // Show eTLD.
  6961. icon_label = this.getEffectiveHost();
  6962. }
  6963. // Verifier is either the CA Org, for a normal cert, or a special string
  6964. // for certs that are trusted because of a security exception.
  6965. var tooltip = gNavigatorBundle.getFormattedString("identity.identified.verifier",
  6966. [iData.caOrg]);
  6967. // Check whether this site is a security exception. XPConnect does the right
  6968. // thing here in terms of converting _lastLocation.port from string to int, but
  6969. // the overrideService doesn't like undefined ports, so make sure we have
  6970. // something in the default case (bug 432241).
  6971. // .hostname can return an empty string in some exceptional cases -
  6972. // hasMatchingOverride does not handle that, so avoid calling it.
  6973. // Updating the tooltip value in those cases isn't critical.
  6974. // FIXME: Fixing bug 646690 would probably makes this check unnecessary
  6975. if (this._lastLocation.hostname &&
  6976. this._overrideService.hasMatchingOverride(this._lastLocation.hostname,
  6977. (this._lastLocation.port || 443),
  6978. iData.cert, {}, {}))
  6979. tooltip = gNavigatorBundle.getString("identity.identified.verified_by_you");
  6980. }
  6981. else if (newMode == this.IDENTITY_MODE_IDENTIFIED) {
  6982. // If it's identified, then we can populate the dialog with credentials
  6983. iData = this.getIdentityData();
  6984. tooltip = gNavigatorBundle.getFormattedString("identity.identified.verifier",
  6985. [iData.caOrg]);
  6986. icon_label = iData.subjectOrg;
  6987. if (iData.country)
  6988. icon_country_label = "(" + iData.country + ")";
  6989. // If the organization name starts with an RTL character, then
  6990. // swap the positions of the organization and country code labels.
  6991. // The Unicode ranges reflect the definition of the UCS2_CHAR_IS_BIDI
  6992. // macro in intl/unicharutil/util/nsBidiUtils.h. When bug 218823 gets
  6993. // fixed, this test should be replaced by one adhering to the
  6994. // Unicode Bidirectional Algorithm proper (at the paragraph level).
  6995. icon_labels_dir = /^[\u0590-\u08ff\ufb1d-\ufdff\ufe70-\ufefc]/.test(icon_label) ?
  6996. "rtl" : "ltr";
  6997. }
  6998. else if (newMode == this.IDENTITY_MODE_CHROMEUI) {
  6999. icon_label = "";
  7000. tooltip = "";
  7001. icon_country_label = "";
  7002. icon_labels_dir = "ltr";
  7003. }
  7004. else {
  7005. tooltip = gNavigatorBundle.getString("identity.unknown.tooltip");
  7006. icon_label = "";
  7007. icon_country_label = "";
  7008. icon_labels_dir = "ltr";
  7009. }
  7010. // Push the appropriate strings out to the UI
  7011. this._identityBox.tooltipText = tooltip;
  7012. this._identityIconLabel.value = icon_label;
  7013. this._identityIconCountryLabel.value = icon_country_label;
  7014. // Set cropping and direction
  7015. this._identityIconLabel.crop = icon_country_label ? "end" : "center";
  7016. this._identityIconLabel.parentNode.style.direction = icon_labels_dir;
  7017. // Hide completely if the organization label is empty
  7018. this._identityIconLabel.parentNode.collapsed = icon_label ? false : true;
  7019. },
  7020. /**
  7021. * Set up the title and content messages for the identity message popup,
  7022. * based on the specified mode, and the details of the SSL cert, where
  7023. * applicable
  7024. *
  7025. * @param newMode The newly set identity mode. Should be one of the IDENTITY_MODE_* constants.
  7026. */
  7027. setPopupMessages : function(newMode) {
  7028. this._identityPopup.className = newMode;
  7029. this._identityPopupContentBox.className = newMode;
  7030. // Set the static strings up front
  7031. this._identityPopupEncLabel.textContent = this._encryptionLabel[newMode];
  7032. // Initialize the optional strings to empty values
  7033. var supplemental = "";
  7034. var verifier = "";
  7035. if (newMode == this.IDENTITY_MODE_DOMAIN_VERIFIED) {
  7036. var iData = this.getIdentityData();
  7037. var host = this.getEffectiveHost();
  7038. var owner = gNavigatorBundle.getString("identity.ownerUnknown2");
  7039. verifier = this._identityBox.tooltipText;
  7040. supplemental = "";
  7041. }
  7042. else if (newMode == this.IDENTITY_MODE_IDENTIFIED) {
  7043. // If it's identified, then we can populate the dialog with credentials
  7044. iData = this.getIdentityData();
  7045. host = this.getEffectiveHost();
  7046. owner = iData.subjectOrg;
  7047. verifier = this._identityBox.tooltipText;
  7048. // Build an appropriate supplemental block out of whatever location data we have
  7049. if (iData.city)
  7050. supplemental += iData.city + "\n";
  7051. if (iData.state && iData.country)
  7052. supplemental += gNavigatorBundle.getFormattedString("identity.identified.state_and_country",
  7053. [iData.state, iData.country]);
  7054. else if (iData.state) // State only
  7055. supplemental += iData.state;
  7056. else if (iData.country) // Country only
  7057. supplemental += iData.country;
  7058. }
  7059. else {
  7060. // These strings will be hidden in CSS anyhow
  7061. host = "";
  7062. owner = "";
  7063. }
  7064. // Push the appropriate strings out to the UI
  7065. this._identityPopupContentHost.textContent = host;
  7066. this._identityPopupContentOwner.textContent = owner;
  7067. this._identityPopupContentSupp.textContent = supplemental;
  7068. this._identityPopupContentVerif.textContent = verifier;
  7069. },
  7070. hideIdentityPopup : function() {
  7071. this._identityPopup.hidePopup();
  7072. },
  7073. _popupOpenTime : null,
  7074. /**
  7075. * Click handler for the identity-box element in primary chrome.
  7076. */
  7077. handleIdentityButtonEvent : function(event) {
  7078. this._popupOpenTime = new Date();
  7079. event.stopPropagation();
  7080. if ((event.type == "click" && event.button != 0) ||
  7081. (event.type == "keypress" && event.charCode != KeyEvent.DOM_VK_SPACE &&
  7082. event.keyCode != KeyEvent.DOM_VK_RETURN))
  7083. return; // Left click, space or enter only
  7084. // Revert the contents of the location bar, see bug 406779
  7085. gURLBar.handleRevert();
  7086. if (this._mode == this.IDENTITY_MODE_CHROMEUI)
  7087. return;
  7088. // Make sure that the display:none style we set in xul is removed now that
  7089. // the popup is actually needed
  7090. this._identityPopup.hidden = false;
  7091. // Tell the popup to consume dismiss clicks, to avoid bug 395314
  7092. this._identityPopup.popupBoxObject
  7093. .setConsumeRollupEvent(Ci.nsIPopupBoxObject.ROLLUP_CONSUME);
  7094. // Update the popup strings
  7095. this.setPopupMessages(this._identityBox.className);
  7096. // Add the "open" attribute to the identity box for styling
  7097. this._identityBox.setAttribute("open", "true");
  7098. var self = this;
  7099. this._identityPopup.addEventListener("popuphidden", function (e) {
  7100. e.currentTarget.removeEventListener("popuphidden", arguments.callee, false);
  7101. self._identityBox.removeAttribute("open");
  7102. }, false);
  7103. // Now open the popup, anchored off the primary chrome element
  7104. this._identityPopup.openPopup(this._identityBox, "bottomcenter topleft");
  7105. },
  7106. onPopupShown : function(event) {
  7107. let openingDuration = new Date() - this._popupOpenTime;
  7108. this._popupOpenTime = null;
  7109. try {
  7110. Services.telemetry.getHistogramById("FX_IDENTITY_POPUP_OPEN_MS").add(openingDuration);
  7111. } catch (ex) {
  7112. Components.utils.reportError("Unable to report telemetry for FX_IDENTITY_POPUP_OPEN_MS.");
  7113. }
  7114. document.getElementById('identity-popup-more-info-button').focus();
  7115. },
  7116. onDragStart: function (event) {
  7117. if (gURLBar.getAttribute("pageproxystate") != "valid")
  7118. return;
  7119. var value = content.location.href;
  7120. var urlString = value + "\n" + content.document.title;
  7121. var htmlString = "<a href=\"" + value + "\">" + value + "</a>";
  7122. var dt = event.dataTransfer;
  7123. dt.setData("text/x-moz-url", urlString);
  7124. dt.setData("text/uri-list", value);
  7125. dt.setData("text/plain", value);
  7126. dt.setData("text/html", htmlString);
  7127. dt.setDragImage(gProxyFavIcon, 16, 16);
  7128. }
  7129. };
  7130. let DownloadMonitorPanel = {
  7131. //////////////////////////////////////////////////////////////////////////////
  7132. //// DownloadMonitorPanel Member Variables
  7133. _panel: null,
  7134. _activeStr: null,
  7135. _pausedStr: null,
  7136. _lastTime: Infinity,
  7137. _listening: false,
  7138. get DownloadUtils() {
  7139. delete this.DownloadUtils;
  7140. Cu.import("resource://gre/modules/DownloadUtils.jsm", this);
  7141. return this.DownloadUtils;
  7142. },
  7143. //////////////////////////////////////////////////////////////////////////////
  7144. //// DownloadMonitorPanel Public Methods
  7145. /**
  7146. * Initialize the status panel and member variables
  7147. */
  7148. init: function DMP_init() {
  7149. // Initialize "private" member variables
  7150. this._panel = document.getElementById("download-monitor");
  7151. // Cache the status strings
  7152. this._activeStr = gNavigatorBundle.getString("activeDownloads1");
  7153. this._pausedStr = gNavigatorBundle.getString("pausedDownloads1");
  7154. gDownloadMgr.addListener(this);
  7155. this._listening = true;
  7156. this.updateStatus();
  7157. },
  7158. uninit: function DMP_uninit() {
  7159. if (this._listening)
  7160. gDownloadMgr.removeListener(this);
  7161. },
  7162. inited: function DMP_inited() {
  7163. return this._panel != null;
  7164. },
  7165. /**
  7166. * Update status based on the number of active and paused downloads
  7167. */
  7168. updateStatus: function DMP_updateStatus() {
  7169. if (!this.inited())
  7170. return;
  7171. let numActive = gDownloadMgr.activeDownloadCount;
  7172. // Hide the panel and reset the "last time" if there's no downloads
  7173. if (numActive == 0) {
  7174. this._panel.hidden = true;
  7175. this._lastTime = Infinity;
  7176. return;
  7177. }
  7178. // Find the download with the longest remaining time
  7179. let numPaused = 0;
  7180. let maxTime = -Infinity;
  7181. let dls = gDownloadMgr.activeDownloads;
  7182. while (dls.hasMoreElements()) {
  7183. let dl = dls.getNext();
  7184. if (dl.state == gDownloadMgr.DOWNLOAD_DOWNLOADING) {
  7185. // Figure out if this download takes longer
  7186. if (dl.speed > 0 && dl.size > 0)
  7187. maxTime = Math.max(maxTime, (dl.size - dl.amountTransferred) / dl.speed);
  7188. else
  7189. maxTime = -1;
  7190. }
  7191. else if (dl.state == gDownloadMgr.DOWNLOAD_PAUSED)
  7192. numPaused++;
  7193. }
  7194. // Get the remaining time string and last sec for time estimation
  7195. let timeLeft;
  7196. [timeLeft, this._lastTime] =
  7197. this.DownloadUtils.getTimeLeft(maxTime, this._lastTime);
  7198. // Figure out how many downloads are currently downloading
  7199. let numDls = numActive - numPaused;
  7200. let status = this._activeStr;
  7201. // If all downloads are paused, show the paused message instead
  7202. if (numDls == 0) {
  7203. numDls = numPaused;
  7204. status = this._pausedStr;
  7205. }
  7206. // Get the correct plural form and insert the number of downloads and time
  7207. // left message if necessary
  7208. status = PluralForm.get(numDls, status);
  7209. status = status.replace("#1", numDls);
  7210. status = status.replace("#2", timeLeft);
  7211. // Update the panel and show it
  7212. this._panel.label = status;
  7213. this._panel.hidden = false;
  7214. },
  7215. //////////////////////////////////////////////////////////////////////////////
  7216. //// nsIDownloadProgressListener
  7217. /**
  7218. * Update status for download progress changes
  7219. */
  7220. onProgressChange: function() {
  7221. this.updateStatus();
  7222. },
  7223. /**
  7224. * Update status for download state changes
  7225. */
  7226. onDownloadStateChange: function() {
  7227. this.updateStatus();
  7228. },
  7229. onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus, aDownload) {
  7230. },
  7231. onSecurityChange: function(aWebProgress, aRequest, aState, aDownload) {
  7232. },
  7233. //////////////////////////////////////////////////////////////////////////////
  7234. //// nsISupports
  7235. QueryInterface: XPCOMUtils.generateQI([Ci.nsIDownloadProgressListener]),
  7236. };
  7237. function getNotificationBox(aWindow) {
  7238. var foundBrowser = gBrowser.getBrowserForDocument(aWindow.document);
  7239. if (foundBrowser)
  7240. return gBrowser.getNotificationBox(foundBrowser)
  7241. return null;
  7242. };
  7243. function getTabModalPromptBox(aWindow) {
  7244. var foundBrowser = gBrowser.getBrowserForDocument(aWindow.document);
  7245. if (foundBrowser)
  7246. return gBrowser.getTabModalPromptBox(foundBrowser);
  7247. return null;
  7248. };
  7249. /* DEPRECATED */
  7250. function getBrowser() gBrowser;
  7251. function getNavToolbox() gNavToolbox;
  7252. let gPrivateBrowsingUI = {
  7253. _privateBrowsingService: null,
  7254. _searchBarValue: null,
  7255. _findBarValue: null,
  7256. _inited: false,
  7257. init: function PBUI_init() {
  7258. Services.obs.addObserver(this, "private-browsing", false);
  7259. Services.obs.addObserver(this, "private-browsing-transition-complete", false);
  7260. this._privateBrowsingService = Cc["@mozilla.org/privatebrowsing;1"].
  7261. getService(Ci.nsIPrivateBrowsingService);
  7262. if (this.privateBrowsingEnabled)
  7263. this.onEnterPrivateBrowsing(true);
  7264. this._inited = true;
  7265. },
  7266. uninit: function PBUI_unint() {
  7267. if (!this._inited)
  7268. return;
  7269. Services.obs.removeObserver(this, "private-browsing");
  7270. Services.obs.removeObserver(this, "private-browsing-transition-complete");
  7271. },
  7272. get _disableUIOnToggle() {
  7273. if (this._privateBrowsingService.autoStarted)
  7274. return false;
  7275. try {
  7276. return !gPrefService.getBoolPref("browser.privatebrowsing.keep_current_session");
  7277. }
  7278. catch (e) {
  7279. return true;
  7280. }
  7281. },
  7282. observe: function PBUI_observe(aSubject, aTopic, aData) {
  7283. if (aTopic == "private-browsing") {
  7284. if (aData == "enter")
  7285. this.onEnterPrivateBrowsing();
  7286. else if (aData == "exit")
  7287. this.onExitPrivateBrowsing();
  7288. }
  7289. else if (aTopic == "private-browsing-transition-complete") {
  7290. if (this._disableUIOnToggle) {
  7291. document.getElementById("Tools:PrivateBrowsing")
  7292. .removeAttribute("disabled");
  7293. }
  7294. }
  7295. },
  7296. _shouldEnter: function PBUI__shouldEnter() {
  7297. try {
  7298. // Never prompt if the session is not going to be closed, or if user has
  7299. // already requested not to be prompted.
  7300. if (gPrefService.getBoolPref("browser.privatebrowsing.dont_prompt_on_enter") ||
  7301. gPrefService.getBoolPref("browser.privatebrowsing.keep_current_session"))
  7302. return true;
  7303. }
  7304. catch (ex) { }
  7305. var bundleService = Cc["@mozilla.org/intl/stringbundle;1"].
  7306. getService(Ci.nsIStringBundleService);
  7307. var pbBundle = bundleService.createBundle("chrome://browser/locale/browser.properties");
  7308. var brandBundle = bundleService.createBundle("chrome://branding/locale/brand.properties");
  7309. var appName = brandBundle.GetStringFromName("brandShortName");
  7310. # On Mac, use the header as the title.
  7311. #ifdef XP_MACOSX
  7312. var dialogTitle = pbBundle.GetStringFromName("privateBrowsingMessageHeader");
  7313. var header = "";
  7314. #else
  7315. var dialogTitle = pbBundle.GetStringFromName("privateBrowsingDialogTitle");
  7316. var header = pbBundle.GetStringFromName("privateBrowsingMessageHeader") + "\n\n";
  7317. #endif
  7318. var message = pbBundle.formatStringFromName("privateBrowsingMessage", [appName], 1);
  7319. var ps = Services.prompt;
  7320. var flags = ps.BUTTON_TITLE_IS_STRING * ps.BUTTON_POS_0 +
  7321. ps.BUTTON_TITLE_IS_STRING * ps.BUTTON_POS_1 +
  7322. ps.BUTTON_POS_0_DEFAULT;
  7323. var neverAsk = {value:false};
  7324. var button0Title = pbBundle.GetStringFromName("privateBrowsingYesTitle");
  7325. var button1Title = pbBundle.GetStringFromName("privateBrowsingNoTitle");
  7326. var neverAskText = pbBundle.GetStringFromName("privateBrowsingNeverAsk");
  7327. var result;
  7328. var choice = ps.confirmEx(null, dialogTitle, header + message,
  7329. flags, button0Title, button1Title, null,
  7330. neverAskText, neverAsk);
  7331. switch (choice) {
  7332. case 0: // Start Private Browsing
  7333. result = true;
  7334. if (neverAsk.value)
  7335. gPrefService.setBoolPref("browser.privatebrowsing.dont_prompt_on_enter", true);
  7336. break;
  7337. case 1: // Keep
  7338. result = false;
  7339. break;
  7340. }
  7341. return result;
  7342. },
  7343. onEnterPrivateBrowsing: function PBUI_onEnterPrivateBrowsing(aOnWindowOpen) {
  7344. if (BrowserSearch.searchBar)
  7345. this._searchBarValue = BrowserSearch.searchBar.textbox.value;
  7346. if (gFindBarInitialized)
  7347. this._findBarValue = gFindBar.getElement("findbar-textbox").value;
  7348. this._setPBMenuTitle("stop");
  7349. // Disable the Clear Recent History... menu item when in PB mode
  7350. // temporary fix until bug 463607 is fixed
  7351. document.getElementById("Tools:Sanitize").setAttribute("disabled", "true");
  7352. let docElement = document.documentElement;
  7353. if (this._privateBrowsingService.autoStarted) {
  7354. // Disable the menu item in auto-start mode
  7355. document.getElementById("privateBrowsingItem")
  7356. .setAttribute("disabled", "true");
  7357. #ifdef MENUBAR_CAN_AUTOHIDE
  7358. document.getElementById("appmenu_privateBrowsing")
  7359. .setAttribute("disabled", "true");
  7360. #endif
  7361. document.getElementById("Tools:PrivateBrowsing")
  7362. .setAttribute("disabled", "true");
  7363. if (window.location.href == getBrowserURL())
  7364. docElement.setAttribute("privatebrowsingmode", "permanent");
  7365. }
  7366. else if (window.location.href == getBrowserURL()) {
  7367. // Adjust the window's title
  7368. docElement.setAttribute("title",
  7369. docElement.getAttribute("title_privatebrowsing"));
  7370. docElement.setAttribute("titlemodifier",
  7371. docElement.getAttribute("titlemodifier_privatebrowsing"));
  7372. docElement.setAttribute("privatebrowsingmode", "temporary");
  7373. gBrowser.updateTitlebar();
  7374. }
  7375. if (!aOnWindowOpen && this._disableUIOnToggle)
  7376. document.getElementById("Tools:PrivateBrowsing")
  7377. .setAttribute("disabled", "true");
  7378. },
  7379. onExitPrivateBrowsing: function PBUI_onExitPrivateBrowsing() {
  7380. if (BrowserSearch.searchBar) {
  7381. let searchBox = BrowserSearch.searchBar.textbox;
  7382. searchBox.reset();
  7383. if (this._searchBarValue) {
  7384. searchBox.value = this._searchBarValue;
  7385. this._searchBarValue = null;
  7386. }
  7387. }
  7388. if (gURLBar) {
  7389. gURLBar.editor.transactionManager.clear();
  7390. }
  7391. // Re-enable the Clear Recent History... menu item on exit of PB mode
  7392. // temporary fix until bug 463607 is fixed
  7393. document.getElementById("Tools:Sanitize").removeAttribute("disabled");
  7394. if (gFindBarInitialized) {
  7395. let findbox = gFindBar.getElement("findbar-textbox");
  7396. findbox.reset();
  7397. if (this._findBarValue) {
  7398. findbox.value = this._findBarValue;
  7399. this._findBarValue = null;
  7400. }
  7401. }
  7402. this._setPBMenuTitle("start");
  7403. if (window.location.href == getBrowserURL()) {
  7404. // Adjust the window's title
  7405. let docElement = document.documentElement;
  7406. docElement.setAttribute("title",
  7407. docElement.getAttribute("title_normal"));
  7408. docElement.setAttribute("titlemodifier",
  7409. docElement.getAttribute("titlemodifier_normal"));
  7410. docElement.removeAttribute("privatebrowsingmode");
  7411. }
  7412. // Enable the menu item in after exiting the auto-start mode
  7413. document.getElementById("privateBrowsingItem")
  7414. .removeAttribute("disabled");
  7415. #ifdef MENUBAR_CAN_AUTOHIDE
  7416. document.getElementById("appmenu_privateBrowsing")
  7417. .removeAttribute("disabled");
  7418. #endif
  7419. document.getElementById("Tools:PrivateBrowsing")
  7420. .removeAttribute("disabled");
  7421. gLastOpenDirectory.reset();
  7422. if (this._disableUIOnToggle)
  7423. document.getElementById("Tools:PrivateBrowsing")
  7424. .setAttribute("disabled", "true");
  7425. },
  7426. _setPBMenuTitle: function PBUI__setPBMenuTitle(aMode) {
  7427. let pbMenuItem = document.getElementById("privateBrowsingItem");
  7428. pbMenuItem.setAttribute("label", pbMenuItem.getAttribute(aMode + "label"));
  7429. pbMenuItem.setAttribute("accesskey", pbMenuItem.getAttribute(aMode + "accesskey"));
  7430. #ifdef MENUBAR_CAN_AUTOHIDE
  7431. let appmenupbMenuItem = document.getElementById("appmenu_privateBrowsing");
  7432. appmenupbMenuItem.setAttribute("label", appmenupbMenuItem.getAttribute(aMode + "label"));
  7433. appmenupbMenuItem.setAttribute("accesskey", appmenupbMenuItem.getAttribute(aMode + "accesskey"));
  7434. #endif
  7435. },
  7436. toggleMode: function PBUI_toggleMode() {
  7437. // prompt the users on entering the private mode, if needed
  7438. if (!this.privateBrowsingEnabled)
  7439. if (!this._shouldEnter())
  7440. return;
  7441. this._privateBrowsingService.privateBrowsingEnabled =
  7442. !this.privateBrowsingEnabled;
  7443. },
  7444. get privateBrowsingEnabled() {
  7445. return this._privateBrowsingService.privateBrowsingEnabled;
  7446. }
  7447. };
  7448. var LightWeightThemeWebInstaller = {
  7449. handleEvent: function (event) {
  7450. switch (event.type) {
  7451. case "InstallBrowserTheme":
  7452. case "PreviewBrowserTheme":
  7453. case "ResetBrowserThemePreview":
  7454. // ignore requests from background tabs
  7455. if (event.target.ownerDocument.defaultView.top != content)
  7456. return;
  7457. }
  7458. switch (event.type) {
  7459. case "InstallBrowserTheme":
  7460. this._installRequest(event);
  7461. break;
  7462. case "PreviewBrowserTheme":
  7463. this._preview(event);
  7464. break;
  7465. case "ResetBrowserThemePreview":
  7466. this._resetPreview(event);
  7467. break;
  7468. case "pagehide":
  7469. case "TabSelect":
  7470. this._resetPreview();
  7471. break;
  7472. }
  7473. },
  7474. get _manager () {
  7475. var temp = {};
  7476. Cu.import("resource://gre/modules/LightweightThemeManager.jsm", temp);
  7477. delete this._manager;
  7478. return this._manager = temp.LightweightThemeManager;
  7479. },
  7480. _installRequest: function (event) {
  7481. var node = event.target;
  7482. var data = this._getThemeFromNode(node);
  7483. if (!data)
  7484. return;
  7485. if (this._isAllowed(node)) {
  7486. this._install(data);
  7487. return;
  7488. }
  7489. var allowButtonText =
  7490. gNavigatorBundle.getString("lwthemeInstallRequest.allowButton");
  7491. var allowButtonAccesskey =
  7492. gNavigatorBundle.getString("lwthemeInstallRequest.allowButton.accesskey");
  7493. var message =
  7494. gNavigatorBundle.getFormattedString("lwthemeInstallRequest.message",
  7495. [node.ownerDocument.location.host]);
  7496. var buttons = [{
  7497. label: allowButtonText,
  7498. accessKey: allowButtonAccesskey,
  7499. callback: function () {
  7500. LightWeightThemeWebInstaller._install(data);
  7501. }
  7502. }];
  7503. this._removePreviousNotifications();
  7504. var notificationBox = gBrowser.getNotificationBox();
  7505. var notificationBar =
  7506. notificationBox.appendNotification(message, "lwtheme-install-request", "",
  7507. notificationBox.PRIORITY_INFO_MEDIUM,
  7508. buttons);
  7509. notificationBar.persistence = 1;
  7510. },
  7511. _install: function (newLWTheme) {
  7512. var previousLWTheme = this._manager.currentTheme;
  7513. var listener = {
  7514. onEnabling: function(aAddon, aRequiresRestart) {
  7515. if (!aRequiresRestart)
  7516. return;
  7517. let messageString = gNavigatorBundle.getFormattedString("lwthemeNeedsRestart.message",
  7518. [aAddon.name], 1);
  7519. let action = {
  7520. label: gNavigatorBundle.getString("lwthemeNeedsRestart.button"),
  7521. accessKey: gNavigatorBundle.getString("lwthemeNeedsRestart.accesskey"),
  7522. callback: function () {
  7523. Application.restart();
  7524. }
  7525. };
  7526. let options = {
  7527. timeout: Date.now() + 30000
  7528. };
  7529. PopupNotifications.show(gBrowser.selectedBrowser, "addon-theme-change",
  7530. messageString, "addons-notification-icon",
  7531. action, null, options);
  7532. },
  7533. onEnabled: function(aAddon) {
  7534. LightWeightThemeWebInstaller._postInstallNotification(newLWTheme, previousLWTheme);
  7535. }
  7536. };
  7537. AddonManager.addAddonListener(listener);
  7538. this._manager.currentTheme = newLWTheme;
  7539. AddonManager.removeAddonListener(listener);
  7540. },
  7541. _postInstallNotification: function (newTheme, previousTheme) {
  7542. function text(id) {
  7543. return gNavigatorBundle.getString("lwthemePostInstallNotification." + id);
  7544. }
  7545. var buttons = [{
  7546. label: text("undoButton"),
  7547. accessKey: text("undoButton.accesskey"),
  7548. callback: function () {
  7549. LightWeightThemeWebInstaller._manager.forgetUsedTheme(newTheme.id);
  7550. LightWeightThemeWebInstaller._manager.currentTheme = previousTheme;
  7551. }
  7552. }, {
  7553. label: text("manageButton"),
  7554. accessKey: text("manageButton.accesskey"),
  7555. callback: function () {
  7556. BrowserOpenAddonsMgr("addons://list/theme");
  7557. }
  7558. }];
  7559. this._removePreviousNotifications();
  7560. var notificationBox = gBrowser.getNotificationBox();
  7561. var notificationBar =
  7562. notificationBox.appendNotification(text("message"),
  7563. "lwtheme-install-notification", "",
  7564. notificationBox.PRIORITY_INFO_MEDIUM,
  7565. buttons);
  7566. notificationBar.persistence = 1;
  7567. notificationBar.timeout = Date.now() + 20000; // 20 seconds
  7568. },
  7569. _removePreviousNotifications: function () {
  7570. var box = gBrowser.getNotificationBox();
  7571. ["lwtheme-install-request",
  7572. "lwtheme-install-notification"].forEach(function (value) {
  7573. var notification = box.getNotificationWithValue(value);
  7574. if (notification)
  7575. box.removeNotification(notification);
  7576. });
  7577. },
  7578. _previewWindow: null,
  7579. _preview: function (event) {
  7580. if (!this._isAllowed(event.target))
  7581. return;
  7582. var data = this._getThemeFromNode(event.target);
  7583. if (!data)
  7584. return;
  7585. this._resetPreview();
  7586. this._previewWindow = event.target.ownerDocument.defaultView;
  7587. this._previewWindow.addEventListener("pagehide", this, true);
  7588. gBrowser.tabContainer.addEventListener("TabSelect", this, false);
  7589. this._manager.previewTheme(data);
  7590. },
  7591. _resetPreview: function (event) {
  7592. if (!this._previewWindow ||
  7593. event && !this._isAllowed(event.target))
  7594. return;
  7595. this._previewWindow.removeEventListener("pagehide", this, true);
  7596. this._previewWindow = null;
  7597. gBrowser.tabContainer.removeEventListener("TabSelect", this, false);
  7598. this._manager.resetPreview();
  7599. },
  7600. _isAllowed: function (node) {
  7601. var pm = Services.perms;
  7602. var uri = node.ownerDocument.documentURIObject;
  7603. return pm.testPermission(uri, "install") == pm.ALLOW_ACTION;
  7604. },
  7605. _getThemeFromNode: function (node) {
  7606. return this._manager.parseTheme(node.getAttribute("data-browsertheme"),
  7607. node.baseURI);
  7608. }
  7609. }
  7610. /**
  7611. * Switch to a tab that has a given URI, and focusses its browser window.
  7612. * If a matching tab is in this window, it will be switched to. Otherwise, other
  7613. * windows will be searched.
  7614. *
  7615. * @param aURI
  7616. * URI to search for
  7617. * @param aOpenNew
  7618. * True to open a new tab and switch to it, if no existing tab is found.
  7619. * If no suitable window is found, a new one will be opened.
  7620. * @return True if an existing tab was found, false otherwise
  7621. */
  7622. function switchToTabHavingURI(aURI, aOpenNew) {
  7623. // This will switch to the tab in aWindow having aURI, if present.
  7624. function switchIfURIInWindow(aWindow) {
  7625. let browsers = aWindow.gBrowser.browsers;
  7626. for (let i = 0; i < browsers.length; i++) {
  7627. let browser = browsers[i];
  7628. if (browser.currentURI.equals(aURI)) {
  7629. // Focus the matching window & tab
  7630. aWindow.focus();
  7631. aWindow.gBrowser.tabContainer.selectedIndex = i;
  7632. return true;
  7633. }
  7634. }
  7635. return false;
  7636. }
  7637. // This can be passed either nsIURI or a string.
  7638. if (!(aURI instanceof Ci.nsIURI))
  7639. aURI = Services.io.newURI(aURI, null, null);
  7640. let isBrowserWindow = !!window.gBrowser;
  7641. // Prioritise this window.
  7642. if (isBrowserWindow && switchIfURIInWindow(window))
  7643. return true;
  7644. let winEnum = Services.wm.getEnumerator("navigator:browser");
  7645. while (winEnum.hasMoreElements()) {
  7646. let browserWin = winEnum.getNext();
  7647. // Skip closed (but not yet destroyed) windows,
  7648. // and the current window (which was checked earlier).
  7649. if (browserWin.closed || browserWin == window)
  7650. continue;
  7651. if (switchIfURIInWindow(browserWin))
  7652. return true;
  7653. }
  7654. // No opened tab has that url.
  7655. if (aOpenNew) {
  7656. if (isBrowserWindow && isTabEmpty(gBrowser.selectedTab))
  7657. gBrowser.selectedBrowser.loadURI(aURI.spec);
  7658. else
  7659. openUILinkIn(aURI.spec, "tab");
  7660. }
  7661. return false;
  7662. }
  7663. function restoreLastSession() {
  7664. let ss = Cc["@mozilla.org/browser/sessionstore;1"].
  7665. getService(Ci.nsISessionStore);
  7666. ss.restoreLastSession();
  7667. }
  7668. var TabContextMenu = {
  7669. contextTab: null,
  7670. updateContextMenu: function updateContextMenu(aPopupMenu) {
  7671. this.contextTab = aPopupMenu.triggerNode.localName == "tab" ?
  7672. aPopupMenu.triggerNode : gBrowser.selectedTab;
  7673. let disabled = gBrowser.tabs.length == 1;
  7674. // Enable the "Close Tab" menuitem when the window doesn't close with the last tab.
  7675. document.getElementById("context_closeTab").disabled =
  7676. disabled && gBrowser.tabContainer._closeWindowWithLastTab;
  7677. var menuItems = aPopupMenu.getElementsByAttribute("tbattr", "tabbrowser-multiple");
  7678. for (var i = 0; i < menuItems.length; i++)
  7679. menuItems[i].disabled = disabled;
  7680. disabled = gBrowser.visibleTabs.length == 1;
  7681. menuItems = aPopupMenu.getElementsByAttribute("tbattr", "tabbrowser-multiple-visible");
  7682. for (var i = 0; i < menuItems.length; i++)
  7683. menuItems[i].disabled = disabled;
  7684. // Session store
  7685. document.getElementById("context_undoCloseTab").disabled =
  7686. Cc["@mozilla.org/browser/sessionstore;1"].
  7687. getService(Ci.nsISessionStore).
  7688. getClosedTabCount(window) == 0;
  7689. // Only one of pin/unpin should be visible
  7690. document.getElementById("context_pinTab").hidden = this.contextTab.pinned;
  7691. document.getElementById("context_unpinTab").hidden = !this.contextTab.pinned;
  7692. // Disable "Close other Tabs" if there is only one unpinned tab and
  7693. // hide it when the user rightclicked on a pinned tab.
  7694. let unpinnedTabs = gBrowser.visibleTabs.length - gBrowser._numPinnedTabs;
  7695. document.getElementById("context_closeOtherTabs").disabled = unpinnedTabs <= 1;
  7696. document.getElementById("context_closeOtherTabs").hidden = this.contextTab.pinned;
  7697. // Hide "Bookmark All Tabs" for a pinned tab. Update its state if visible.
  7698. let bookmarkAllTabs = document.getElementById("context_bookmarkAllTabs");
  7699. bookmarkAllTabs.hidden = this.contextTab.pinned;
  7700. if (!bookmarkAllTabs.hidden)
  7701. PlacesCommandHook.updateBookmarkAllTabsCommand();
  7702. // Hide "Move to Group" if it's a pinned tab.
  7703. document.getElementById("context_tabViewMenu").hidden =
  7704. (this.contextTab.pinned || !TabView.firstUseExperienced);
  7705. }
  7706. };
  7707. XPCOMUtils.defineLazyGetter(this, "HUDConsoleUI", function () {
  7708. let tempScope = {};
  7709. Cu.import("resource:///modules/HUDService.jsm", tempScope);
  7710. try {
  7711. return tempScope.HUDService.consoleUI;
  7712. }
  7713. catch (ex) {
  7714. Components.utils.reportError(ex);
  7715. }
  7716. });
  7717. // Prompt user to restart the browser in safe mode
  7718. function safeModeRestart()
  7719. {
  7720. // prompt the user to confirm
  7721. let promptTitle = gNavigatorBundle.getString("safeModeRestartPromptTitle");
  7722. let promptMessage =
  7723. gNavigatorBundle.getString("safeModeRestartPromptMessage");
  7724. let restartText = gNavigatorBundle.getString("safeModeRestartButton");
  7725. let buttonFlags = (Services.prompt.BUTTON_POS_0 *
  7726. Services.prompt.BUTTON_TITLE_IS_STRING) +
  7727. (Services.prompt.BUTTON_POS_1 *
  7728. Services.prompt.BUTTON_TITLE_CANCEL) +
  7729. Services.prompt.BUTTON_POS_0_DEFAULT;
  7730. let rv = Services.prompt.confirmEx(window, promptTitle, promptMessage,
  7731. buttonFlags, restartText, null, null,
  7732. null, {});
  7733. if (rv == 0) {
  7734. let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].
  7735. getService(Ci.nsIAppStartup);
  7736. appStartup.restartInSafeMode(Ci.nsIAppStartup.eAttemptQuit);
  7737. }
  7738. }
  7739. /* duplicateTabIn duplicates tab in a place specified by the parameter |where|.
  7740. *
  7741. * |where| can be:
  7742. * "tab" new tab
  7743. * "tabshifted" same as "tab" but in background if default is to select new
  7744. * tabs, and vice versa
  7745. * "window" new window
  7746. *
  7747. * delta is the offset to the history entry that you want to load.
  7748. */
  7749. function duplicateTabIn(aTab, where, delta) {
  7750. let newTab = Cc['@mozilla.org/browser/sessionstore;1']
  7751. .getService(Ci.nsISessionStore)
  7752. .duplicateTab(window, aTab, delta);
  7753. switch (where) {
  7754. case "window":
  7755. gBrowser.hideTab(newTab);
  7756. gBrowser.replaceTabWithWindow(newTab);
  7757. break;
  7758. case "tabshifted":
  7759. // A background tab has been opened, nothing else to do here.
  7760. break;
  7761. case "tab":
  7762. gBrowser.selectedTab = newTab;
  7763. break;
  7764. }
  7765. }
  7766. /*
  7767. * When addons are installed/uninstalled, check and see if the number of items
  7768. * on the add-on bar changed:
  7769. * - If an add-on was installed, incrementing the count, show the bar.
  7770. * - If an add-on was uninstalled, and no more items are left, hide the bar.
  7771. */
  7772. let AddonsMgrListener = {
  7773. get addonBar() document.getElementById("addon-bar"),
  7774. get statusBar() document.getElementById("status-bar"),
  7775. getAddonBarItemCount: function() {
  7776. // Take into account the contents of the status bar shim for the count.
  7777. var itemCount = this.statusBar.childNodes.length;
  7778. var defaultOrNoninteractive = this.addonBar.getAttribute("defaultset")
  7779. .split(",")
  7780. .concat(["separator", "spacer", "spring"]);
  7781. this.addonBar.currentSet.split(",").forEach(function (item) {
  7782. if (defaultOrNoninteractive.indexOf(item) == -1)
  7783. itemCount++;
  7784. });
  7785. return itemCount;
  7786. },
  7787. onInstalling: function(aAddon) {
  7788. this.lastAddonBarCount = this.getAddonBarItemCount();
  7789. },
  7790. onInstalled: function(aAddon) {
  7791. if (this.getAddonBarItemCount() > this.lastAddonBarCount)
  7792. setToolbarVisibility(this.addonBar, true);
  7793. },
  7794. onUninstalling: function(aAddon) {
  7795. this.lastAddonBarCount = this.getAddonBarItemCount();
  7796. },
  7797. onUninstalled: function(aAddon) {
  7798. if (this.getAddonBarItemCount() == 0)
  7799. setToolbarVisibility(this.addonBar, false);
  7800. },
  7801. onEnabling: function(aAddon) this.onInstalling(),
  7802. onEnabled: function(aAddon) this.onInstalled(),
  7803. onDisabling: function(aAddon) this.onUninstalling(),
  7804. onDisabled: function(aAddon) this.onUninstalled(),
  7805. };
  7806. function toggleAddonBar() {
  7807. let addonBar = document.getElementById("addon-bar");
  7808. setToolbarVisibility(addonBar, addonBar.collapsed);
  7809. }
  7810. var Scratchpad = {
  7811. prefEnabledName: "devtools.scratchpad.enabled",
  7812. openScratchpad: function SP_openScratchpad() {
  7813. return this.ScratchpadManager.openScratchpad();
  7814. }
  7815. };
  7816. XPCOMUtils.defineLazyGetter(Scratchpad, "ScratchpadManager", function() {
  7817. let tmp = {};
  7818. Cu.import("resource:///modules/devtools/scratchpad-manager.jsm", tmp);
  7819. return tmp.ScratchpadManager;
  7820. });
  7821. var StyleEditor = {
  7822. prefEnabledName: "devtools.styleeditor.enabled",
  7823. openChrome: function SE_openChrome()
  7824. {
  7825. const CHROME_URL = "chrome://browser/content/styleeditor.xul";
  7826. const CHROME_WINDOW_TYPE = "Tools:StyleEditor";
  7827. const CHROME_WINDOW_FLAGS = "chrome,centerscreen,resizable,dialog=no";
  7828. // focus currently open Style Editor window for this document, if any
  7829. let contentWindow = gBrowser.selectedBrowser.contentWindow;
  7830. let contentWindowID = contentWindow.QueryInterface(Ci.nsIInterfaceRequestor).
  7831. getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
  7832. let enumerator = Services.wm.getEnumerator(CHROME_WINDOW_TYPE);
  7833. while (enumerator.hasMoreElements()) {
  7834. var win = enumerator.getNext();
  7835. if (win.styleEditorChrome.contentWindowID == contentWindowID) {
  7836. win.focus();
  7837. return win;
  7838. }
  7839. }
  7840. let chromeWindow = Services.ww.openWindow(null, CHROME_URL, "_blank",
  7841. CHROME_WINDOW_FLAGS,
  7842. contentWindow);
  7843. chromeWindow.focus();
  7844. return chromeWindow;
  7845. }
  7846. };
  7847. function onWebDeveloperMenuShowing() {
  7848. document.getElementById("Tools:WebConsole").setAttribute("checked", HUDConsoleUI.getOpenHUD() != null);
  7849. }
  7850. XPCOMUtils.defineLazyGetter(window, "gShowPageResizers", function () {
  7851. #ifdef XP_WIN
  7852. // Only show resizers on Windows 2000 and XP
  7853. let sysInfo = Components.classes["@mozilla.org/system-info;1"]
  7854. .getService(Components.interfaces.nsIPropertyBag2);
  7855. return parseFloat(sysInfo.getProperty("version")) < 6;
  7856. #else
  7857. return false;
  7858. #endif
  7859. });
  7860. var MousePosTracker = {
  7861. _listeners: [],
  7862. _x: 0,
  7863. _y: 0,
  7864. get _windowUtils() {
  7865. delete this._windowUtils;
  7866. return this._windowUtils = window.getInterface(Ci.nsIDOMWindowUtils);
  7867. },
  7868. addListener: function (listener) {
  7869. if (this._listeners.indexOf(listener) >= 0)
  7870. return;
  7871. listener._hover = false;
  7872. this._listeners.push(listener);
  7873. this._callListener(listener);
  7874. },
  7875. removeListener: function (listener) {
  7876. var index = this._listeners.indexOf(listener);
  7877. if (index < 0)
  7878. return;
  7879. this._listeners.splice(index, 1);
  7880. },
  7881. handleEvent: function (event) {
  7882. var screenPixelsPerCSSPixel = this._windowUtils.screenPixelsPerCSSPixel;
  7883. this._x = event.screenX / screenPixelsPerCSSPixel - window.mozInnerScreenX;
  7884. this._y = event.screenY / screenPixelsPerCSSPixel - window.mozInnerScreenY;
  7885. this._listeners.forEach(function (listener) {
  7886. try {
  7887. this._callListener(listener);
  7888. } catch (e) {
  7889. Cu.reportError(e);
  7890. }
  7891. }, this);
  7892. },
  7893. _callListener: function (listener) {
  7894. let rect = listener.getMouseTargetRect();
  7895. let hover = this._x >= rect.left &&
  7896. this._x <= rect.right &&
  7897. this._y >= rect.top &&
  7898. this._y <= rect.bottom;
  7899. if (hover == listener._hover)
  7900. return;
  7901. listener._hover = hover;
  7902. if (hover) {
  7903. if (listener.onMouseEnter)
  7904. listener.onMouseEnter();
  7905. } else {
  7906. if (listener.onMouseLeave)
  7907. listener.onMouseLeave();
  7908. }
  7909. }
  7910. };
  7911. function focusNextFrame(event) {
  7912. let fm = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager);
  7913. let dir = event.shiftKey ? fm.MOVEFOCUS_BACKWARDDOC : fm.MOVEFOCUS_FORWARDDOC;
  7914. let element = fm.moveFocus(window, null, dir, fm.FLAG_BYKEY);
  7915. if (element.ownerDocument == document)
  7916. focusAndSelectUrlBar();
  7917. }