browser-plugins.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. # -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. const kPrefSessionPersistMinutes = "plugin.sessionPermissionNow.intervalInMinutes";
  6. const kPrefPersistentDays = "plugin.persistentPermissionAlways.intervalInDays";
  7. var gPluginHandler = {
  8. PLUGIN_SCRIPTED_STATE_NONE: 0,
  9. PLUGIN_SCRIPTED_STATE_FIRED: 1,
  10. PLUGIN_SCRIPTED_STATE_DONE: 2,
  11. getPluginUI: function(plugin, anonid) {
  12. return plugin.ownerDocument.
  13. getAnonymousElementByAttribute(plugin, "anonid", anonid);
  14. },
  15. _getPluginInfo: function(pluginElement) {
  16. let pluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
  17. pluginElement.QueryInterface(Ci.nsIObjectLoadingContent);
  18. let tagMimetype;
  19. let pluginName = gNavigatorBundle.getString("pluginInfo.unknownPlugin");
  20. let pluginTag = null;
  21. let permissionString = null;
  22. let fallbackType = null;
  23. let blocklistState = null;
  24. if (pluginElement instanceof HTMLAppletElement) {
  25. tagMimetype = "application/x-java-vm";
  26. } else {
  27. tagMimetype = pluginElement.actualType;
  28. if (tagMimetype == "") {
  29. tagMimetype = pluginElement.type;
  30. }
  31. }
  32. if (gPluginHandler.isKnownPlugin(pluginElement)) {
  33. pluginTag = pluginHost.getPluginTagForType(pluginElement.actualType);
  34. pluginName = gPluginHandler.makeNicePluginName(pluginTag.name);
  35. permissionString = pluginHost.getPermissionStringForType(pluginElement.actualType);
  36. fallbackType = pluginElement.defaultFallbackType;
  37. blocklistState = pluginHost.getBlocklistStateForType(pluginElement.actualType);
  38. // Make state-softblocked == state-notblocked for our purposes,
  39. // they have the same UI. STATE_OUTDATED should not exist for plugin
  40. // items, but let's alias it anyway, just in case.
  41. if (blocklistState == Ci.nsIBlocklistService.STATE_SOFTBLOCKED ||
  42. blocklistState == Ci.nsIBlocklistService.STATE_OUTDATED) {
  43. blocklistState = Ci.nsIBlocklistService.STATE_NOT_BLOCKED;
  44. }
  45. }
  46. return { mimetype: tagMimetype,
  47. pluginName: pluginName,
  48. pluginTag: pluginTag,
  49. permissionString: permissionString,
  50. fallbackType: fallbackType,
  51. blocklistState: blocklistState,
  52. };
  53. },
  54. // Map the plugin's name to a filtered version more suitable for user UI.
  55. makeNicePluginName : function(aName) {
  56. if (aName == "Shockwave Flash") {
  57. return "Adobe Flash";
  58. }
  59. // Clean up the plugin name by stripping off any trailing version numbers
  60. // or "plugin". EG, "Foo Bar Plugin 1.23_02" --> "Foo Bar"
  61. // Do this by first stripping the numbers, etc. off the end, and then
  62. // removing "Plugin" (and then trimming to get rid of any whitespace).
  63. // (Otherwise, something like "Java(TM) Plug-in 1.7.0_07" gets mangled)
  64. let newName = aName.replace(/[\s\d\.\-\_\(\)]+$/, "").replace(/\bplug-?in\b/i, "").trim();
  65. return newName;
  66. },
  67. isTooSmall : function(plugin, overlay) {
  68. // Is the <object>'s size too small to hold what we want to show?
  69. let pluginRect = plugin.getBoundingClientRect();
  70. // XXX bug 446693. The text-shadow on the submitted-report text at
  71. // the bottom causes scrollHeight to be larger than it should be.
  72. // Clamp width/height to properly show CTP overlay on different
  73. // zoom levels when embedded in iframes (rounding bug). (Bug 972237)
  74. let overflows = (overlay.scrollWidth > Math.ceil(pluginRect.width)) ||
  75. (overlay.scrollHeight - 5 > Math.ceil(pluginRect.height));
  76. return overflows;
  77. },
  78. addLinkClickCallback: function(linkNode, callbackName /*callbackArgs...*/) {
  79. // XXX just doing (callback)(arg) was giving a same-origin error. bug?
  80. let self = this;
  81. let callbackArgs = Array.prototype.slice.call(arguments).slice(2);
  82. linkNode.addEventListener("click",
  83. function(evt) {
  84. if (!evt.isTrusted) {
  85. return;
  86. }
  87. evt.preventDefault();
  88. if (callbackArgs.length == 0) {
  89. callbackArgs = [ evt ];
  90. }
  91. (self[callbackName]).apply(self, callbackArgs);
  92. }, true);
  93. linkNode.addEventListener("keydown",
  94. function(evt) {
  95. if (!evt.isTrusted) {
  96. return;
  97. }
  98. if (evt.keyCode == evt.DOM_VK_RETURN) {
  99. evt.preventDefault();
  100. if (callbackArgs.length == 0) {
  101. callbackArgs = [ evt ];
  102. }
  103. evt.preventDefault();
  104. (self[callbackName]).apply(self, callbackArgs);
  105. }
  106. }, true);
  107. },
  108. // Helper to get the binding handler type from a plugin object
  109. _getBindingType : function(plugin) {
  110. if (!(plugin instanceof Ci.nsIObjectLoadingContent)) {
  111. return null;
  112. }
  113. switch (plugin.pluginFallbackType) {
  114. case Ci.nsIObjectLoadingContent.PLUGIN_UNSUPPORTED:
  115. return "PluginNotFound";
  116. case Ci.nsIObjectLoadingContent.PLUGIN_DISABLED:
  117. return "PluginDisabled";
  118. case Ci.nsIObjectLoadingContent.PLUGIN_BLOCKLISTED:
  119. return "PluginBlocklisted";
  120. case Ci.nsIObjectLoadingContent.PLUGIN_OUTDATED:
  121. return "PluginOutdated";
  122. case Ci.nsIObjectLoadingContent.PLUGIN_CLICK_TO_PLAY:
  123. return "PluginClickToPlay";
  124. case Ci.nsIObjectLoadingContent.PLUGIN_VULNERABLE_UPDATABLE:
  125. return "PluginVulnerableUpdatable";
  126. case Ci.nsIObjectLoadingContent.PLUGIN_VULNERABLE_NO_UPDATE:
  127. return "PluginVulnerableNoUpdate";
  128. case Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW:
  129. return "PluginPlayPreview";
  130. default:
  131. // Not all states map to a handler
  132. return null;
  133. }
  134. },
  135. handleEvent : function(event) {
  136. let plugin;
  137. let doc;
  138. let eventType = event.type;
  139. if (eventType === "PluginRemoved") {
  140. doc = event.target;
  141. } else {
  142. plugin = event.target;
  143. doc = plugin.ownerDocument;
  144. if (!(plugin instanceof Ci.nsIObjectLoadingContent)) {
  145. return;
  146. }
  147. }
  148. if (eventType == "PluginBindingAttached") {
  149. // The plugin binding fires this event when it is created.
  150. // As an untrusted event, ensure that this object actually has a binding
  151. // and make sure we don't handle it twice
  152. let overlay = this.getPluginUI(plugin, "main");
  153. if (!overlay || overlay._bindingHandled) {
  154. return;
  155. }
  156. overlay._bindingHandled = true;
  157. // Lookup the handler for this binding
  158. eventType = this._getBindingType(plugin);
  159. if (!eventType) {
  160. // Not all bindings have handlers
  161. return;
  162. }
  163. }
  164. let shouldShowNotification = false;
  165. let browser = gBrowser.getBrowserForDocument(doc.defaultView.top.document);
  166. if (!browser) {
  167. return;
  168. }
  169. switch (eventType) {
  170. case "PluginCrashed":
  171. this.pluginInstanceCrashed(plugin, event);
  172. break;
  173. case "PluginNotFound":
  174. /* No action (plugin finder obsolete) */
  175. break;
  176. case "PluginBlocklisted":
  177. case "PluginOutdated":
  178. shouldShowNotification = true;
  179. break;
  180. case "PluginVulnerableUpdatable":
  181. let updateLink = this.getPluginUI(plugin, "checkForUpdatesLink");
  182. this.addLinkClickCallback(updateLink, "openPluginUpdatePage");
  183. /* FALLTHRU */
  184. case "PluginVulnerableNoUpdate":
  185. case "PluginClickToPlay":
  186. this._handleClickToPlayEvent(plugin);
  187. let overlay = this.getPluginUI(plugin, "main");
  188. let pluginName = this._getPluginInfo(plugin).pluginName;
  189. let messageString = gNavigatorBundle.getFormattedString("PluginClickToActivate", [pluginName]);
  190. let overlayText = this.getPluginUI(plugin, "clickToPlay");
  191. overlayText.textContent = messageString;
  192. if (eventType == "PluginVulnerableUpdatable" ||
  193. eventType == "PluginVulnerableNoUpdate") {
  194. let vulnerabilityString = gNavigatorBundle.getString(eventType);
  195. let vulnerabilityText = this.getPluginUI(plugin, "vulnerabilityStatus");
  196. vulnerabilityText.textContent = vulnerabilityString;
  197. }
  198. shouldShowNotification = true;
  199. break;
  200. case "PluginPlayPreview":
  201. this._handlePlayPreviewEvent(plugin);
  202. break;
  203. case "PluginDisabled":
  204. let manageLink = this.getPluginUI(plugin, "managePluginsLink");
  205. this.addLinkClickCallback(manageLink, "managePlugins");
  206. shouldShowNotification = true;
  207. break;
  208. case "PluginInstantiated":
  209. //Pale Moon: don't show the indicator when plugins are enabled/allowed
  210. if (gPrefService.getBoolPref("plugins.always_show_indicator")) {
  211. shouldShowNotification = true;
  212. }
  213. break;
  214. case "PluginRemoved":
  215. shouldShowNotification = true;
  216. break;
  217. }
  218. // Show the in-content UI if it's not too big. The crashed plugin handler already did this.
  219. if (eventType != "PluginCrashed" && eventType != "PluginRemoved") {
  220. let overlay = this.getPluginUI(plugin, "main");
  221. if (overlay != null) {
  222. if (!this.isTooSmall(plugin, overlay)) {
  223. overlay.style.visibility = "visible";
  224. }
  225. plugin.addEventListener("overflow", function(event) {
  226. overlay.style.visibility = "hidden";
  227. });
  228. plugin.addEventListener("underflow", function(event) {
  229. // this is triggered if only one dimension underflows,
  230. // the other dimension might still overflow
  231. if (!gPluginHandler.isTooSmall(plugin, overlay)) {
  232. overlay.style.visibility = "visible";
  233. }
  234. });
  235. }
  236. }
  237. // Only show the notification after we've done the isTooSmall check, so
  238. // that the notification can decide whether to show the "alert" icon
  239. if (shouldShowNotification) {
  240. this._showClickToPlayNotification(browser);
  241. }
  242. },
  243. isKnownPlugin: function(objLoadingContent) {
  244. return (objLoadingContent.getContentTypeForMIMEType(objLoadingContent.actualType) ==
  245. Ci.nsIObjectLoadingContent.TYPE_PLUGIN);
  246. },
  247. canActivatePlugin: function(objLoadingContent) {
  248. // if this isn't a known plugin, we can't activate it
  249. // (this also guards pluginHost.getPermissionStringForType against
  250. // unexpected input)
  251. if (!gPluginHandler.isKnownPlugin(objLoadingContent)) {
  252. return false;
  253. }
  254. let pluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
  255. let permissionString = pluginHost.getPermissionStringForType(objLoadingContent.actualType);
  256. let principal = objLoadingContent.ownerDocument.defaultView.top.document.nodePrincipal;
  257. let pluginPermission = Services.perms.testPermissionFromPrincipal(principal, permissionString);
  258. let isFallbackTypeValid =
  259. objLoadingContent.pluginFallbackType >= Ci.nsIObjectLoadingContent.PLUGIN_CLICK_TO_PLAY &&
  260. objLoadingContent.pluginFallbackType <= Ci.nsIObjectLoadingContent.PLUGIN_VULNERABLE_NO_UPDATE;
  261. if (objLoadingContent.pluginFallbackType == Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW) {
  262. // checking if play preview is subject to CTP rules
  263. let playPreviewInfo = pluginHost.getPlayPreviewInfo(objLoadingContent.actualType);
  264. isFallbackTypeValid = !playPreviewInfo.ignoreCTP;
  265. }
  266. return !objLoadingContent.activated &&
  267. pluginPermission != Ci.nsIPermissionManager.DENY_ACTION &&
  268. isFallbackTypeValid;
  269. },
  270. hideClickToPlayOverlay: function(aPlugin) {
  271. let overlay = this.getPluginUI(aPlugin, "main");
  272. if (overlay) {
  273. overlay.style.visibility = "hidden";
  274. }
  275. },
  276. stopPlayPreview: function(aPlugin, aPlayPlugin) {
  277. let objLoadingContent = aPlugin.QueryInterface(Ci.nsIObjectLoadingContent);
  278. if (objLoadingContent.activated) {
  279. return;
  280. }
  281. if (aPlayPlugin) {
  282. objLoadingContent.playPlugin();
  283. } else {
  284. objLoadingContent.cancelPlayPreview();
  285. }
  286. },
  287. // Callback for user clicking on a disabled plugin
  288. managePlugins: function(aEvent) {
  289. BrowserOpenAddonsMgr("addons://list/plugin");
  290. },
  291. // Callback for user clicking a "reload page" link
  292. reloadPage: function(browser) {
  293. browser.reload();
  294. },
  295. // Callback for user clicking the help icon
  296. openHelpPage: function() {
  297. openHelpLink("plugin-crashed", false);
  298. },
  299. // Event listener for click-to-play plugins.
  300. _handleClickToPlayEvent: function(aPlugin) {
  301. let doc = aPlugin.ownerDocument;
  302. let browser = gBrowser.getBrowserForDocument(doc.defaultView.top.document);
  303. let pluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
  304. let objLoadingContent = aPlugin.QueryInterface(Ci.nsIObjectLoadingContent);
  305. // guard against giving pluginHost.getPermissionStringForType a type
  306. // not associated with any known plugin
  307. if (!gPluginHandler.isKnownPlugin(objLoadingContent)) {
  308. return;
  309. }
  310. let permissionString = pluginHost.getPermissionStringForType(objLoadingContent.actualType);
  311. let principal = doc.defaultView.top.document.nodePrincipal;
  312. let pluginPermission = Services.perms.testPermissionFromPrincipal(principal, permissionString);
  313. let overlay = this.getPluginUI(aPlugin, "main");
  314. if (pluginPermission == Ci.nsIPermissionManager.DENY_ACTION) {
  315. if (overlay) {
  316. overlay.style.visibility = "hidden";
  317. }
  318. return;
  319. }
  320. if (overlay) {
  321. overlay.addEventListener("click", gPluginHandler._overlayClickListener, true);
  322. let closeIcon = gPluginHandler.getPluginUI(aPlugin, "closeIcon");
  323. closeIcon.addEventListener("click", function(aEvent) {
  324. if (aEvent.button == 0 && aEvent.isTrusted) {
  325. gPluginHandler.hideClickToPlayOverlay(aPlugin);
  326. }
  327. }, true);
  328. }
  329. },
  330. _overlayClickListener: {
  331. handleEvent: function(aEvent) {
  332. let plugin = document.getBindingParent(aEvent.target);
  333. let contentWindow = plugin.ownerDocument.defaultView.top;
  334. // gBrowser.getBrowserForDocument does not exist in the case where we
  335. // drag-and-dropped a tab from a window containing only that tab. In
  336. // that case, the window gets destroyed.
  337. let browser = gBrowser.getBrowserForDocument ?
  338. gBrowser.getBrowserForDocument(contentWindow.document) :
  339. null;
  340. // If browser is null here, we've been drag-and-dropped from another
  341. // window, and this is the wrong click handler.
  342. if (!browser) {
  343. aEvent.target.removeEventListener("click", gPluginHandler._overlayClickListener, true);
  344. return;
  345. }
  346. let objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
  347. // Have to check that the target is not the link to update the plugin
  348. if (!(aEvent.originalTarget instanceof HTMLAnchorElement) &&
  349. (aEvent.originalTarget.getAttribute('anonid') != 'closeIcon') &&
  350. aEvent.button == 0 && aEvent.isTrusted) {
  351. gPluginHandler._showClickToPlayNotification(browser, plugin);
  352. aEvent.stopPropagation();
  353. aEvent.preventDefault();
  354. }
  355. }
  356. },
  357. _handlePlayPreviewEvent: function(aPlugin) {
  358. let doc = aPlugin.ownerDocument;
  359. let browser = gBrowser.getBrowserForDocument(doc.defaultView.top.document);
  360. let pluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
  361. let pluginInfo = this._getPluginInfo(aPlugin);
  362. let playPreviewInfo = pluginHost.getPlayPreviewInfo(pluginInfo.mimetype);
  363. let previewContent = this.getPluginUI(aPlugin, "previewPluginContent");
  364. let iframe = previewContent.getElementsByClassName("previewPluginContentFrame")[0];
  365. if (!iframe) {
  366. // lazy initialization of the iframe
  367. iframe = doc.createElementNS("http://www.w3.org/1999/xhtml", "iframe");
  368. iframe.className = "previewPluginContentFrame";
  369. previewContent.appendChild(iframe);
  370. // Force a style flush, so that we ensure our binding is attached.
  371. aPlugin.clientTop;
  372. }
  373. iframe.src = playPreviewInfo.redirectURL;
  374. // MozPlayPlugin event can be dispatched from the extension chrome
  375. // code to replace the preview content with the native plugin
  376. previewContent.addEventListener("MozPlayPlugin", function playPluginHandler(aEvent) {
  377. if (!aEvent.isTrusted) {
  378. return;
  379. }
  380. previewContent.removeEventListener("MozPlayPlugin", playPluginHandler, true);
  381. let playPlugin = !aEvent.detail;
  382. gPluginHandler.stopPlayPreview(aPlugin, playPlugin);
  383. // cleaning up: removes overlay iframe from the DOM
  384. let iframe = previewContent.getElementsByClassName("previewPluginContentFrame")[0];
  385. if (iframe) {
  386. previewContent.removeChild(iframe);
  387. }
  388. }, true);
  389. if (!playPreviewInfo.ignoreCTP) {
  390. gPluginHandler._showClickToPlayNotification(browser);
  391. }
  392. },
  393. reshowClickToPlayNotification: function() {
  394. let browser = gBrowser.selectedBrowser;
  395. let contentWindow = browser.contentWindow;
  396. let cwu = contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
  397. .getInterface(Ci.nsIDOMWindowUtils);
  398. let doc = contentWindow.document;
  399. let plugins = cwu.plugins;
  400. for (let plugin of plugins) {
  401. let overlay = doc.getAnonymousElementByAttribute(plugin, "anonid", "main");
  402. if (overlay) {
  403. overlay.removeEventListener("click", gPluginHandler._overlayClickListener, true);
  404. }
  405. let objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
  406. if (gPluginHandler.canActivatePlugin(objLoadingContent)) {
  407. gPluginHandler._handleClickToPlayEvent(plugin);
  408. }
  409. }
  410. gPluginHandler._showClickToPlayNotification(browser);
  411. },
  412. _clickToPlayNotificationEventCallback: function(event) {
  413. if (event == "showing") {
  414. gPluginHandler._makeCenterActions(this);
  415. } else if (event == "dismissed") {
  416. // Once the popup is dismissed, clicking the icon should show the full
  417. // list again
  418. this.options.primaryPlugin = null;
  419. }
  420. },
  421. _makeCenterActions: function(notification) {
  422. let contentWindow = notification.browser.contentWindow;
  423. let cwu = contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
  424. .getInterface(Ci.nsIDOMWindowUtils);
  425. let principal = contentWindow.document.nodePrincipal;
  426. let centerActions = [];
  427. let pluginsFound = new Set();
  428. for (let plugin of cwu.plugins) {
  429. plugin.QueryInterface(Ci.nsIObjectLoadingContent);
  430. if (plugin.getContentTypeForMIMEType(plugin.actualType) != Ci.nsIObjectLoadingContent.TYPE_PLUGIN) {
  431. continue;
  432. }
  433. let pluginInfo = this._getPluginInfo(plugin);
  434. if (pluginInfo.permissionString === null) {
  435. Components.utils.reportError("No permission string for active plugin.");
  436. continue;
  437. }
  438. if (pluginsFound.has(pluginInfo.permissionString)) {
  439. continue;
  440. }
  441. pluginsFound.add(pluginInfo.permissionString);
  442. // Add the per-site permissions and details URLs to pluginInfo here
  443. // because they are more expensive to compute and so we avoid it in
  444. // the tighter loop above.
  445. let permissionObj = Services.perms.
  446. getPermissionObject(principal, pluginInfo.permissionString, false);
  447. if (permissionObj) {
  448. pluginInfo.pluginPermissionPrePath = permissionObj.principal.originNoSuffix;
  449. pluginInfo.pluginPermissionType = permissionObj.expireType;
  450. } else {
  451. pluginInfo.pluginPermissionPrePath = principal.originNoSuffix;
  452. pluginInfo.pluginPermissionType = undefined;
  453. }
  454. let url;
  455. if (pluginInfo.blocklistState != Ci.nsIBlocklistService.STATE_NOT_BLOCKED) {
  456. url = Services.blocklist.getPluginBlocklistURL(pluginInfo.pluginTag);
  457. }
  458. pluginInfo.detailsLink = url;
  459. centerActions.push(pluginInfo);
  460. }
  461. centerActions.sort(function(a, b) {
  462. return a.pluginName.localeCompare(b.pluginName);
  463. });
  464. notification.options.centerActions = centerActions;
  465. },
  466. /**
  467. * Called from the plugin doorhanger to set the new permissions for a plugin
  468. * and activate plugins if necessary.
  469. * aNewState should be either "allownow" "allowalways" or "block"
  470. */
  471. _updatePluginPermission: function(aNotification, aPluginInfo, aNewState) {
  472. let permission;
  473. let expireType;
  474. let expireTime;
  475. switch (aNewState) {
  476. case "allownow":
  477. permission = Ci.nsIPermissionManager.ALLOW_ACTION;
  478. expireType = Ci.nsIPermissionManager.EXPIRE_SESSION;
  479. expireTime = Date.now() + Services.prefs.getIntPref(kPrefSessionPersistMinutes) * 60 * 1000;
  480. break;
  481. case "allowalways":
  482. permission = Ci.nsIPermissionManager.ALLOW_ACTION;
  483. expireType = Ci.nsIPermissionManager.EXPIRE_TIME;
  484. expireTime = Date.now() +
  485. Services.prefs.getIntPref(kPrefPersistentDays) * 24 * 60 * 60 * 1000;
  486. break;
  487. case "block":
  488. permission = Ci.nsIPermissionManager.PROMPT_ACTION;
  489. expireType = Ci.nsIPermissionManager.EXPIRE_NEVER;
  490. expireTime = 0;
  491. break;
  492. // In case a plugin has already been allowed in another tab, the "continue allowing" button
  493. // shouldn't change any permissions but should run the plugin-enablement code below.
  494. case "continue":
  495. break;
  496. default:
  497. Cu.reportError(Error("Unexpected plugin state: " + aNewState));
  498. return;
  499. }
  500. let browser = aNotification.browser;
  501. let contentWindow = browser.contentWindow;
  502. if (aNewState != "continue") {
  503. let principal = contentWindow.document.nodePrincipal;
  504. Services.perms.addFromPrincipal(principal, aPluginInfo.permissionString,
  505. permission, expireType, expireTime);
  506. if (aNewState == "block") {
  507. return;
  508. }
  509. }
  510. // Manually activate the plugins that would have been automatically
  511. // activated.
  512. let cwu = contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
  513. .getInterface(Ci.nsIDOMWindowUtils);
  514. let plugins = cwu.plugins;
  515. let pluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
  516. for (let plugin of plugins) {
  517. plugin.QueryInterface(Ci.nsIObjectLoadingContent);
  518. // canActivatePlugin will return false if this isn't a known plugin type,
  519. // so the pluginHost.getPermissionStringForType call is protected
  520. if (gPluginHandler.canActivatePlugin(plugin) &&
  521. aPluginInfo.permissionString == pluginHost.getPermissionStringForType(plugin.actualType)) {
  522. plugin.playPlugin();
  523. }
  524. }
  525. },
  526. _showClickToPlayNotification: function(aBrowser, aPrimaryPlugin) {
  527. let notification = PopupNotifications.getNotification("click-to-play-plugins", aBrowser);
  528. let contentWindow = aBrowser.contentWindow;
  529. let contentDoc = aBrowser.contentDocument;
  530. let cwu = contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
  531. .getInterface(Ci.nsIDOMWindowUtils);
  532. // Pale Moon: cwu.plugins may contain non-plugin <object>s, filter them out
  533. let plugins = cwu.plugins.filter(function(plugin) {
  534. return (plugin.getContentTypeForMIMEType(plugin.actualType) ==
  535. Ci.nsIObjectLoadingContent.TYPE_PLUGIN);
  536. });
  537. if (plugins.length == 0) {
  538. if (notification) {
  539. PopupNotifications.remove(notification);
  540. }
  541. return;
  542. }
  543. let icon = 'plugins-notification-icon';
  544. for (let plugin of plugins) {
  545. let fallbackType = plugin.pluginFallbackType;
  546. if (fallbackType == plugin.PLUGIN_VULNERABLE_UPDATABLE ||
  547. fallbackType == plugin.PLUGIN_VULNERABLE_NO_UPDATE ||
  548. fallbackType == plugin.PLUGIN_BLOCKLISTED) {
  549. icon = 'blocked-plugins-notification-icon';
  550. break;
  551. }
  552. if (fallbackType == plugin.PLUGIN_CLICK_TO_PLAY) {
  553. let overlay = contentDoc.getAnonymousElementByAttribute(plugin, "anonid", "main");
  554. if (!overlay || overlay.style.visibility == 'hidden') {
  555. icon = 'alert-plugins-notification-icon';
  556. }
  557. }
  558. }
  559. let dismissed = notification ? notification.dismissed : true;
  560. if (aPrimaryPlugin) {
  561. dismissed = false;
  562. }
  563. let primaryPluginPermission = null;
  564. if (aPrimaryPlugin) {
  565. primaryPluginPermission = this._getPluginInfo(aPrimaryPlugin).permissionString;
  566. }
  567. let options = {
  568. dismissed: dismissed,
  569. eventCallback: this._clickToPlayNotificationEventCallback,
  570. primaryPlugin: primaryPluginPermission
  571. };
  572. PopupNotifications.show(aBrowser, "click-to-play-plugins",
  573. "", icon, null, null, options);
  574. },
  575. // Crashed-plugin observer. Notified once per plugin crash, before events
  576. // are dispatched to individual plugin instances.
  577. pluginCrashed : function(subject, topic, data) {
  578. let propertyBag = subject;
  579. if (!(propertyBag instanceof Ci.nsIPropertyBag2) ||
  580. !(propertyBag instanceof Ci.nsIWritablePropertyBag2)) {
  581. return;
  582. }
  583. },
  584. // Crashed-plugin event listener. Called for every instance of a
  585. // plugin in content.
  586. pluginInstanceCrashed: function(plugin, aEvent) {
  587. // Ensure the plugin and event are of the right type.
  588. if (!(aEvent instanceof Ci.nsIDOMDataContainerEvent)) {
  589. return;
  590. }
  591. let submittedReport = aEvent.getData("submittedCrashReport");
  592. let doPrompt = true; // XXX followup for .getData("doPrompt");
  593. let submitReports = true; // XXX followup for .getData("submitReports");
  594. let pluginName = aEvent.getData("pluginName");
  595. let pluginDumpID = aEvent.getData("pluginDumpID");
  596. let browserDumpID = aEvent.getData("browserDumpID");
  597. // Remap the plugin name to a more user-presentable form.
  598. pluginName = this.makeNicePluginName(pluginName);
  599. let messageString = gNavigatorBundle.getFormattedString("crashedpluginsMessage.title", [pluginName]);
  600. //
  601. // Configure the crashed-plugin placeholder.
  602. //
  603. // Force a layout flush so the binding is attached.
  604. plugin.clientTop;
  605. let doc = plugin.ownerDocument;
  606. let overlay = doc.getAnonymousElementByAttribute(plugin, "class", "mainBox");
  607. let statusDiv = doc.getAnonymousElementByAttribute(plugin, "class", "submitStatus");
  608. let crashText = doc.getAnonymousElementByAttribute(plugin, "class", "msgCrashedText");
  609. crashText.textContent = messageString;
  610. let browser = gBrowser.getBrowserForDocument(doc.defaultView.top.document);
  611. let link = doc.getAnonymousElementByAttribute(plugin, "class", "reloadLink");
  612. this.addLinkClickCallback(link, "reloadPage", browser);
  613. let notificationBox = gBrowser.getNotificationBox(browser);
  614. let isShowing = true;
  615. // Is the <object>'s size too small to hold what we want to show?
  616. if (this.isTooSmall(plugin, overlay)) {
  617. // First try hiding the crash report submission UI.
  618. statusDiv.removeAttribute("status");
  619. if (this.isTooSmall(plugin, overlay)) {
  620. // Hide the overlay's contents. Use visibility style, so that it doesn't
  621. // collapse down to 0x0.
  622. overlay.style.visibility = "hidden";
  623. isShowing = false;
  624. }
  625. }
  626. if (isShowing) {
  627. // If a previous plugin on the page was too small and resulted in adding a
  628. // notification bar, then remove it because this plugin instance it big
  629. // enough to serve as in-content notification.
  630. hideNotificationBar();
  631. doc.mozNoPluginCrashedNotification = true;
  632. } else {
  633. // If another plugin on the page was large enough to show our UI, we don't
  634. // want to show a notification bar.
  635. if (!doc.mozNoPluginCrashedNotification)
  636. showNotificationBar(pluginDumpID, browserDumpID);
  637. }
  638. function hideNotificationBar() {
  639. let notification = notificationBox.getNotificationWithValue("plugin-crashed");
  640. if (notification) {
  641. notificationBox.removeNotification(notification, true);
  642. }
  643. }
  644. function showNotificationBar(pluginDumpID, browserDumpID) {
  645. // If there's already an existing notification bar, don't do anything.
  646. let notification = notificationBox.getNotificationWithValue("plugin-crashed");
  647. if (notification) {
  648. return;
  649. }
  650. // Configure the notification bar
  651. let priority = notificationBox.PRIORITY_WARNING_MEDIUM;
  652. let iconURL = "chrome://mozapps/skin/plugins/notifyPluginCrashed.png";
  653. let reloadLabel = gNavigatorBundle.getString("crashedpluginsMessage.reloadButton.label");
  654. let reloadKey = gNavigatorBundle.getString("crashedpluginsMessage.reloadButton.accesskey");
  655. let submitLabel = gNavigatorBundle.getString("crashedpluginsMessage.submitButton.label");
  656. let submitKey = gNavigatorBundle.getString("crashedpluginsMessage.submitButton.accesskey");
  657. let buttons = [{
  658. label: reloadLabel,
  659. accessKey: reloadKey,
  660. popup: null,
  661. callback: function() { browser.reload(); },
  662. }];
  663. notification = notificationBox.appendNotification(messageString, "plugin-crashed",
  664. iconURL, priority, buttons);
  665. // Add the "learn more" link.
  666. let XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  667. let link = notification.ownerDocument.createElementNS(XULNS, "label");
  668. link.className = "text-link";
  669. link.setAttribute("value", gNavigatorBundle.getString("crashedpluginsMessage.learnMore"));
  670. let crashurl = formatURL("app.support.baseURL", true);
  671. crashurl += "plugin-crashed-notificationbar";
  672. link.href = crashurl;
  673. let description = notification.ownerDocument.getAnonymousElementByAttribute(notification, "anonid", "messageText");
  674. description.appendChild(link);
  675. // Remove the notfication when the page is reloaded.
  676. doc.defaultView.top.addEventListener("unload", function() {
  677. notificationBox.removeNotification(notification);
  678. }, false);
  679. }
  680. }
  681. };