Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | const Cc = Components.classes; |
michael@0 | 6 | const Ci = Components.interfaces; |
michael@0 | 7 | const Cu = Components.utils; |
michael@0 | 8 | const Cr = Components.results; |
michael@0 | 9 | |
michael@0 | 10 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 11 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | function MetroUIUtils() { |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | const URLElements = { |
michael@0 | 17 | "a": "href", |
michael@0 | 18 | "applet": ["archive", "code", "codebase"], |
michael@0 | 19 | "area": "href", |
michael@0 | 20 | "audio": "src", |
michael@0 | 21 | "base": "href", |
michael@0 | 22 | "blockquote": ["cite"], |
michael@0 | 23 | "body": "background", |
michael@0 | 24 | "button": "formaction", |
michael@0 | 25 | "command": "icon", |
michael@0 | 26 | "del": ["cite"], |
michael@0 | 27 | "embed": "src", |
michael@0 | 28 | "form": "action", |
michael@0 | 29 | "frame": ["longdesc", "src"], |
michael@0 | 30 | "iframe": ["longdesc", "src"], |
michael@0 | 31 | "img": ["longdesc", "src"], |
michael@0 | 32 | "input": ["formaction", "src"], |
michael@0 | 33 | "ins": ["cite"], |
michael@0 | 34 | "link": "href", |
michael@0 | 35 | "object": ["archive", "codebase", "data"], |
michael@0 | 36 | "q": ["cite"], |
michael@0 | 37 | "script": "src", |
michael@0 | 38 | "source": "src", |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | MetroUIUtils.prototype = { |
michael@0 | 42 | classID : Components.ID("e4626085-17f7-4068-a225-66c1acc0485c"), |
michael@0 | 43 | QueryInterface : XPCOMUtils.generateQI([Ci.nsIMetroUIUtils]), |
michael@0 | 44 | /** |
michael@0 | 45 | * Loads the specified panel in the browser. |
michael@0 | 46 | * @ param aPanelId The identifier of the pane to load |
michael@0 | 47 | */ |
michael@0 | 48 | showPanel: function(aPanelId) { |
michael@0 | 49 | let browserWin = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 50 | browserWin.PanelUI.show(aPanelId); |
michael@0 | 51 | }, |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * Determines if the browser has selected content |
michael@0 | 55 | */ |
michael@0 | 56 | get hasSelectedContent() { |
michael@0 | 57 | try { |
michael@0 | 58 | let browserWin = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 59 | let tabBrowser = browserWin.getBrowser(); |
michael@0 | 60 | if (!browserWin || !tabBrowser || !tabBrowser.contentWindow) { |
michael@0 | 61 | return false; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | let sel = tabBrowser.contentWindow.getSelection(); |
michael@0 | 65 | return sel && sel.toString(); |
michael@0 | 66 | } catch(e) { |
michael@0 | 67 | return false; |
michael@0 | 68 | } |
michael@0 | 69 | }, |
michael@0 | 70 | |
michael@0 | 71 | /** |
michael@0 | 72 | * Obtains the current page title |
michael@0 | 73 | */ |
michael@0 | 74 | get currentPageTitle() { |
michael@0 | 75 | let browserWin = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 76 | if (!browserWin || !browserWin.content || !browserWin.content.document) { |
michael@0 | 77 | throw Cr.NS_ERROR_FAILURE; |
michael@0 | 78 | } |
michael@0 | 79 | return browserWin.content.document.title || ""; |
michael@0 | 80 | }, |
michael@0 | 81 | |
michael@0 | 82 | /** |
michael@0 | 83 | * Obtains the current page URI |
michael@0 | 84 | */ |
michael@0 | 85 | get currentPageURI() { |
michael@0 | 86 | let browserWin = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 87 | if (!browserWin || !browserWin.content || !browserWin.content.document) { |
michael@0 | 88 | throw Cr.NS_ERROR_FAILURE; |
michael@0 | 89 | } |
michael@0 | 90 | return browserWin.content.document.URL || ""; |
michael@0 | 91 | }, |
michael@0 | 92 | |
michael@0 | 93 | /** |
michael@0 | 94 | * Determines the text that should be shared |
michael@0 | 95 | */ |
michael@0 | 96 | get shareText() { |
michael@0 | 97 | let browserWin = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 98 | let tabBrowser = browserWin.getBrowser(); |
michael@0 | 99 | if (browserWin && tabBrowser && tabBrowser.contentWindow) { |
michael@0 | 100 | let sel = tabBrowser.contentWindow.getSelection(); |
michael@0 | 101 | if (sel && sel.rangeCount) |
michael@0 | 102 | return sel; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | throw Cr.NS_ERROR_FAILURE; |
michael@0 | 106 | }, |
michael@0 | 107 | |
michael@0 | 108 | /** |
michael@0 | 109 | * Replaces the node's attribute value to be a fully qualified URL |
michael@0 | 110 | */ |
michael@0 | 111 | _expandAttribute : function(ioService, doc, node, attrName) { |
michael@0 | 112 | let attrValue = node.getAttribute(attrName); |
michael@0 | 113 | if (!attrValue) |
michael@0 | 114 | return; |
michael@0 | 115 | |
michael@0 | 116 | try { |
michael@0 | 117 | let uri = ioService.newURI(attrValue, null, doc.baseURIObject); |
michael@0 | 118 | node.setAttribute(attrName, uri.spec); |
michael@0 | 119 | } catch (e) { |
michael@0 | 120 | } |
michael@0 | 121 | }, |
michael@0 | 122 | |
michael@0 | 123 | /* |
michael@0 | 124 | * Replaces all attribute values in 'n' which contain URLs recursiely |
michael@0 | 125 | * to fully qualified URLs. |
michael@0 | 126 | */ |
michael@0 | 127 | _expandURLs: function(doc, n) { |
michael@0 | 128 | let ioService = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 129 | getService(Ci.nsIIOService); |
michael@0 | 130 | for (let i = 0; i < n.children.length; i++) { |
michael@0 | 131 | let child = n.children[i]; |
michael@0 | 132 | let childTagName = child.tagName.toLowerCase(); |
michael@0 | 133 | |
michael@0 | 134 | // Iterate through all known tags which can contain URLs. A tag either |
michael@0 | 135 | // contains a single attribute name or an array of attribute names. |
michael@0 | 136 | for (let tagName in URLElements) { |
michael@0 | 137 | if (tagName === childTagName) { |
michael@0 | 138 | if (URLElements[tagName] instanceof Array) { |
michael@0 | 139 | URLElements[tagName].forEach(function(attrName) { |
michael@0 | 140 | this._expandAttribute(ioService ,doc, child, attrName); |
michael@0 | 141 | }, this); |
michael@0 | 142 | } else { |
michael@0 | 143 | this._expandAttribute(ioService ,doc, child, URLElements[tagName]); |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | this._expandURLs(doc, child); |
michael@0 | 149 | } |
michael@0 | 150 | }, |
michael@0 | 151 | |
michael@0 | 152 | /** |
michael@0 | 153 | * Determines the HTML that should be shared |
michael@0 | 154 | */ |
michael@0 | 155 | get shareHTML() { |
michael@0 | 156 | let browserWin = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 157 | let tabBrowser = browserWin.getBrowser(); |
michael@0 | 158 | let sel; |
michael@0 | 159 | if (browserWin && tabBrowser && tabBrowser.contentWindow && |
michael@0 | 160 | (sel = tabBrowser.contentWindow.getSelection()) && sel.rangeCount) { |
michael@0 | 161 | let div = tabBrowser.contentWindow.document.createElement("DIV"); |
michael@0 | 162 | for (let i = 0; i < sel.rangeCount; i++) { |
michael@0 | 163 | let contents = sel.getRangeAt(i).cloneContents(true); |
michael@0 | 164 | div.appendChild(contents); |
michael@0 | 165 | } |
michael@0 | 166 | this._expandURLs(tabBrowser.contentWindow.document, div); |
michael@0 | 167 | return div.outerHTML; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | throw Cr.NS_ERROR_FAILURE; |
michael@0 | 171 | } |
michael@0 | 172 | }; |
michael@0 | 173 | |
michael@0 | 174 | var component = [MetroUIUtils]; |
michael@0 | 175 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component); |