Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | this.EXPORTED_SYMBOLS = ["MockFilePicker"]; |
michael@0 | 6 | |
michael@0 | 7 | const Cc = Components.classes; |
michael@0 | 8 | const Ci = Components.interfaces; |
michael@0 | 9 | const Cm = Components.manager; |
michael@0 | 10 | const Cu = Components.utils; |
michael@0 | 11 | |
michael@0 | 12 | const CONTRACT_ID = "@mozilla.org/filepicker;1"; |
michael@0 | 13 | |
michael@0 | 14 | Cu.import("resource://gre/modules/FileUtils.jsm"); |
michael@0 | 15 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 16 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 17 | |
michael@0 | 18 | var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); |
michael@0 | 19 | var oldClassID, oldFactory; |
michael@0 | 20 | var newClassID = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID(); |
michael@0 | 21 | var newFactory = function (window) { |
michael@0 | 22 | return { |
michael@0 | 23 | createInstance: function(aOuter, aIID) { |
michael@0 | 24 | if (aOuter) |
michael@0 | 25 | throw Components.results.NS_ERROR_NO_AGGREGATION; |
michael@0 | 26 | return new MockFilePickerInstance(window).QueryInterface(aIID); |
michael@0 | 27 | }, |
michael@0 | 28 | lockFactory: function(aLock) { |
michael@0 | 29 | throw Components.results.NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 30 | }, |
michael@0 | 31 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]) |
michael@0 | 32 | }; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | this.MockFilePicker = { |
michael@0 | 36 | returnOK: Ci.nsIFilePicker.returnOK, |
michael@0 | 37 | returnCancel: Ci.nsIFilePicker.returnCancel, |
michael@0 | 38 | returnReplace: Ci.nsIFilePicker.returnReplace, |
michael@0 | 39 | |
michael@0 | 40 | filterAll: Ci.nsIFilePicker.filterAll, |
michael@0 | 41 | filterHTML: Ci.nsIFilePicker.filterHTML, |
michael@0 | 42 | filterText: Ci.nsIFilePicker.filterText, |
michael@0 | 43 | filterImages: Ci.nsIFilePicker.filterImages, |
michael@0 | 44 | filterXML: Ci.nsIFilePicker.filterXML, |
michael@0 | 45 | filterXUL: Ci.nsIFilePicker.filterXUL, |
michael@0 | 46 | filterApps: Ci.nsIFilePicker.filterApps, |
michael@0 | 47 | filterAllowURLs: Ci.nsIFilePicker.filterAllowURLs, |
michael@0 | 48 | filterAudio: Ci.nsIFilePicker.filterAudio, |
michael@0 | 49 | filterVideo: Ci.nsIFilePicker.filterVideo, |
michael@0 | 50 | |
michael@0 | 51 | window: null, |
michael@0 | 52 | |
michael@0 | 53 | init: function(window) { |
michael@0 | 54 | this.window = window; |
michael@0 | 55 | |
michael@0 | 56 | this.reset(); |
michael@0 | 57 | this.factory = newFactory(window); |
michael@0 | 58 | if (!registrar.isCIDRegistered(newClassID)) { |
michael@0 | 59 | oldClassID = registrar.contractIDToCID(CONTRACT_ID); |
michael@0 | 60 | oldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory); |
michael@0 | 61 | registrar.unregisterFactory(oldClassID, oldFactory); |
michael@0 | 62 | registrar.registerFactory(newClassID, "", CONTRACT_ID, this.factory); |
michael@0 | 63 | } |
michael@0 | 64 | }, |
michael@0 | 65 | |
michael@0 | 66 | reset: function() { |
michael@0 | 67 | this.appendFilterCallback = null; |
michael@0 | 68 | this.appendFiltersCallback = null; |
michael@0 | 69 | this.displayDirectory = null; |
michael@0 | 70 | this.filterIndex = 0; |
michael@0 | 71 | this.mode = null; |
michael@0 | 72 | this.returnFiles = []; |
michael@0 | 73 | this.returnValue = null; |
michael@0 | 74 | this.showCallback = null; |
michael@0 | 75 | this.shown = false; |
michael@0 | 76 | this.showing = false; |
michael@0 | 77 | }, |
michael@0 | 78 | |
michael@0 | 79 | cleanup: function() { |
michael@0 | 80 | var previousFactory = this.factory; |
michael@0 | 81 | this.reset(); |
michael@0 | 82 | this.factory = null; |
michael@0 | 83 | if (oldFactory) { |
michael@0 | 84 | registrar.unregisterFactory(newClassID, previousFactory); |
michael@0 | 85 | registrar.registerFactory(oldClassID, "", CONTRACT_ID, oldFactory); |
michael@0 | 86 | } |
michael@0 | 87 | }, |
michael@0 | 88 | |
michael@0 | 89 | useAnyFile: function() { |
michael@0 | 90 | var file = FileUtils.getDir("TmpD", [], false); |
michael@0 | 91 | file.append("testfile"); |
michael@0 | 92 | file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0644); |
michael@0 | 93 | this.returnFiles = [file]; |
michael@0 | 94 | }, |
michael@0 | 95 | |
michael@0 | 96 | useBlobFile: function() { |
michael@0 | 97 | var blob = new this.window.Blob([]); |
michael@0 | 98 | var file = new this.window.File(blob, { name: 'helloworld.txt', type: 'plain/text' }); |
michael@0 | 99 | this.returnFiles = [file]; |
michael@0 | 100 | }, |
michael@0 | 101 | |
michael@0 | 102 | isNsIFile: function(aFile) { |
michael@0 | 103 | let ret = false; |
michael@0 | 104 | try { |
michael@0 | 105 | if (aFile.QueryInterface(Ci.nsIFile)) |
michael@0 | 106 | ret = true; |
michael@0 | 107 | } catch(e) {} |
michael@0 | 108 | |
michael@0 | 109 | return ret; |
michael@0 | 110 | } |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | function MockFilePickerInstance(window) { |
michael@0 | 114 | this.window = window; |
michael@0 | 115 | }; |
michael@0 | 116 | MockFilePickerInstance.prototype = { |
michael@0 | 117 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIFilePicker]), |
michael@0 | 118 | init: function(aParent, aTitle, aMode) { |
michael@0 | 119 | MockFilePicker.mode = aMode; |
michael@0 | 120 | this.filterIndex = MockFilePicker.filterIndex; |
michael@0 | 121 | this.parent = aParent; |
michael@0 | 122 | }, |
michael@0 | 123 | appendFilter: function(aTitle, aFilter) { |
michael@0 | 124 | if (typeof MockFilePicker.appendFilterCallback == "function") |
michael@0 | 125 | MockFilePicker.appendFilterCallback(this, aTitle, aFilter); |
michael@0 | 126 | }, |
michael@0 | 127 | appendFilters: function(aFilterMask) { |
michael@0 | 128 | if (typeof MockFilePicker.appendFiltersCallback == "function") |
michael@0 | 129 | MockFilePicker.appendFiltersCallback(this, aFilterMask); |
michael@0 | 130 | }, |
michael@0 | 131 | defaultString: "", |
michael@0 | 132 | defaultExtension: "", |
michael@0 | 133 | parent: null, |
michael@0 | 134 | filterIndex: 0, |
michael@0 | 135 | displayDirectory: null, |
michael@0 | 136 | get file() { |
michael@0 | 137 | if (MockFilePicker.returnFiles.length >= 1 && |
michael@0 | 138 | // window.File does not implement nsIFile |
michael@0 | 139 | MockFilePicker.isNsIFile(MockFilePicker.returnFiles[0])) { |
michael@0 | 140 | return MockFilePicker.returnFiles[0]; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | return null; |
michael@0 | 144 | }, |
michael@0 | 145 | get domfile() { |
michael@0 | 146 | if (MockFilePicker.returnFiles.length >= 1) { |
michael@0 | 147 | // window.File does not implement nsIFile |
michael@0 | 148 | if (!MockFilePicker.isNsIFile(MockFilePicker.returnFiles[0])) { |
michael@0 | 149 | return MockFilePicker.returnFiles[0]; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | let utils = this.parent.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 153 | .getInterface(Ci.nsIDOMWindowUtils); |
michael@0 | 154 | return utils.wrapDOMFile(MockFilePicker.returnFiles[0]); |
michael@0 | 155 | } |
michael@0 | 156 | return null; |
michael@0 | 157 | }, |
michael@0 | 158 | get fileURL() { |
michael@0 | 159 | if (MockFilePicker.returnFiles.length >= 1 && |
michael@0 | 160 | // window.File does not implement nsIFile |
michael@0 | 161 | MockFilePicker.isNsIFile(MockFilePicker.returnFiles[0])) { |
michael@0 | 162 | return Services.io.newFileURI(MockFilePicker.returnFiles[0]); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | return null; |
michael@0 | 166 | }, |
michael@0 | 167 | get files() { |
michael@0 | 168 | return { |
michael@0 | 169 | index: 0, |
michael@0 | 170 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISimpleEnumerator]), |
michael@0 | 171 | hasMoreElements: function() { |
michael@0 | 172 | return this.index < MockFilePicker.returnFiles.length; |
michael@0 | 173 | }, |
michael@0 | 174 | getNext: function() { |
michael@0 | 175 | // window.File does not implement nsIFile |
michael@0 | 176 | if (!MockFilePicker.isNsIFile(MockFilePicker.returnFiles[this.index])) { |
michael@0 | 177 | return null; |
michael@0 | 178 | } |
michael@0 | 179 | return MockFilePicker.returnFiles[this.index++]; |
michael@0 | 180 | } |
michael@0 | 181 | }; |
michael@0 | 182 | }, |
michael@0 | 183 | get domfiles() { |
michael@0 | 184 | let utils = this.parent.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 185 | .getInterface(Ci.nsIDOMWindowUtils); |
michael@0 | 186 | return { |
michael@0 | 187 | index: 0, |
michael@0 | 188 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISimpleEnumerator]), |
michael@0 | 189 | hasMoreElements: function() { |
michael@0 | 190 | return this.index < MockFilePicker.returnFiles.length; |
michael@0 | 191 | }, |
michael@0 | 192 | getNext: function() { |
michael@0 | 193 | // window.File does not implement nsIFile |
michael@0 | 194 | if (!MockFilePicker.isNsIFile(MockFilePicker.returnFiles[this.index])) { |
michael@0 | 195 | return MockFilePicker.returnFiles[this.index++]; |
michael@0 | 196 | } |
michael@0 | 197 | return utils.wrapDOMFile(MockFilePicker.returnFiles[this.index++]); |
michael@0 | 198 | } |
michael@0 | 199 | }; |
michael@0 | 200 | }, |
michael@0 | 201 | show: function() { |
michael@0 | 202 | MockFilePicker.displayDirectory = this.displayDirectory; |
michael@0 | 203 | MockFilePicker.shown = true; |
michael@0 | 204 | if (typeof MockFilePicker.showCallback == "function") { |
michael@0 | 205 | var returnValue = MockFilePicker.showCallback(this); |
michael@0 | 206 | if (typeof returnValue != "undefined") |
michael@0 | 207 | return returnValue; |
michael@0 | 208 | } |
michael@0 | 209 | return MockFilePicker.returnValue; |
michael@0 | 210 | }, |
michael@0 | 211 | open: function(aFilePickerShownCallback) { |
michael@0 | 212 | MockFilePicker.showing = true; |
michael@0 | 213 | this.window.setTimeout(function() { |
michael@0 | 214 | let result = Components.interfaces.nsIFilePicker.returnCancel; |
michael@0 | 215 | try { |
michael@0 | 216 | result = this.show(); |
michael@0 | 217 | } catch(ex) { |
michael@0 | 218 | } |
michael@0 | 219 | if (aFilePickerShownCallback) { |
michael@0 | 220 | aFilePickerShownCallback.done(result); |
michael@0 | 221 | } |
michael@0 | 222 | }.bind(this), 0); |
michael@0 | 223 | } |
michael@0 | 224 | }; |
michael@0 | 225 | |
michael@0 | 226 | // Expose everything to content. We call reset() here so that all of the relevant |
michael@0 | 227 | // lazy expandos get added. |
michael@0 | 228 | MockFilePicker.reset(); |
michael@0 | 229 | function exposeAll(obj) { |
michael@0 | 230 | var props = {}; |
michael@0 | 231 | for (var prop in obj) |
michael@0 | 232 | props[prop] = 'rw'; |
michael@0 | 233 | obj.__exposedProps__ = props; |
michael@0 | 234 | } |
michael@0 | 235 | exposeAll(MockFilePicker); |
michael@0 | 236 | exposeAll(MockFilePickerInstance.prototype); |