toolkit/components/filepicker/nsFilePicker.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * No magic constructor behaviour, as is de rigeur for XPCOM.
michael@0 8 * If you must perform some initialization, and it could possibly fail (even
michael@0 9 * due to an out-of-memory condition), you should use an Init method, which
michael@0 10 * can convey failure appropriately (thrown exception in JS,
michael@0 11 * NS_FAILED(nsresult) return in C++).
michael@0 12 *
michael@0 13 * In JS, you can actually cheat, because a thrown exception will cause the
michael@0 14 * CreateInstance call to fail in turn, but not all languages are so lucky.
michael@0 15 * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code
michael@0 16 * for portability reasons -- and even when you're building completely
michael@0 17 * platform-specific code, you can't throw across an XPCOM method boundary.)
michael@0 18 */
michael@0 19
michael@0 20 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 21
michael@0 22 const DEBUG = false; /* set to true to enable debug messages */
michael@0 23
michael@0 24 const LOCAL_FILE_CONTRACTID = "@mozilla.org/file/local;1";
michael@0 25 const APPSHELL_SERV_CONTRACTID = "@mozilla.org/appshell/appShellService;1";
michael@0 26 const STRBUNDLE_SERV_CONTRACTID = "@mozilla.org/intl/stringbundle;1";
michael@0 27
michael@0 28 const nsIAppShellService = Components.interfaces.nsIAppShellService;
michael@0 29 const nsILocalFile = Components.interfaces.nsILocalFile;
michael@0 30 const nsIFileURL = Components.interfaces.nsIFileURL;
michael@0 31 const nsISupports = Components.interfaces.nsISupports;
michael@0 32 const nsIFactory = Components.interfaces.nsIFactory;
michael@0 33 const nsIFilePicker = Components.interfaces.nsIFilePicker;
michael@0 34 const nsIInterfaceRequestor = Components.interfaces.nsIInterfaceRequestor;
michael@0 35 const nsIDOMWindow = Components.interfaces.nsIDOMWindow;
michael@0 36 const nsIStringBundleService = Components.interfaces.nsIStringBundleService;
michael@0 37 const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
michael@0 38 const nsIDocShellTreeItem = Components.interfaces.nsIDocShellTreeItem;
michael@0 39 const nsIBaseWindow = Components.interfaces.nsIBaseWindow;
michael@0 40
michael@0 41 var titleBundle = null;
michael@0 42 var filterBundle = null;
michael@0 43 var lastDirectory = null;
michael@0 44
michael@0 45 function nsFilePicker()
michael@0 46 {
michael@0 47 if (!titleBundle)
michael@0 48 titleBundle = srGetStrBundle("chrome://global/locale/filepicker.properties");
michael@0 49 if (!filterBundle)
michael@0 50 filterBundle = srGetStrBundle("chrome://global/content/filepicker.properties");
michael@0 51
michael@0 52 /* attributes */
michael@0 53 this.mDefaultString = "";
michael@0 54 this.mFilterIndex = 0;
michael@0 55 this.mFilterTitles = new Array();
michael@0 56 this.mFilters = new Array();
michael@0 57 this.mDisplayDirectory = null;
michael@0 58 if (lastDirectory) {
michael@0 59 try {
michael@0 60 var dir = Components.classes[LOCAL_FILE_CONTRACTID].createInstance(nsILocalFile);
michael@0 61 dir.initWithPath(lastDirectory);
michael@0 62 this.mDisplayDirectory = dir;
michael@0 63 } catch (e) {}
michael@0 64 }
michael@0 65 }
michael@0 66
michael@0 67 nsFilePicker.prototype = {
michael@0 68 classID: Components.ID("{54ae32f8-1dd2-11b2-a209-df7c505370f8}"),
michael@0 69
michael@0 70 QueryInterface: function(iid) {
michael@0 71 if (iid.equals(nsIFilePicker) ||
michael@0 72 iid.equals(nsISupports))
michael@0 73 return this;
michael@0 74
michael@0 75 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 76 },
michael@0 77
michael@0 78
michael@0 79 /* attribute nsILocalFile displayDirectory; */
michael@0 80 set displayDirectory(a) {
michael@0 81 this.mDisplayDirectory = a &&
michael@0 82 a.clone().QueryInterface(nsILocalFile);
michael@0 83 },
michael@0 84 get displayDirectory() {
michael@0 85 return this.mDisplayDirectory &&
michael@0 86 this.mDisplayDirectory.clone()
michael@0 87 .QueryInterface(nsILocalFile);
michael@0 88 },
michael@0 89
michael@0 90 /* readonly attribute nsILocalFile file; */
michael@0 91 get file() { return this.mFilesEnumerator.mFiles[0]; },
michael@0 92
michael@0 93 /* readonly attribute nsISimpleEnumerator files; */
michael@0 94 get files() { return this.mFilesEnumerator; },
michael@0 95
michael@0 96 /* readonly attribute nsIDOMFile domfile; */
michael@0 97 get domfile() {
michael@0 98 let enumerator = this.domfiles;
michael@0 99 return enumerator ? enumerator.mFiles[0] : null;
michael@0 100 },
michael@0 101
michael@0 102 /* readonly attribute nsISimpleEnumerator domfiles; */
michael@0 103 get domfiles() {
michael@0 104 if (!this.mFilesEnumerator) {
michael@0 105 return null;
michael@0 106 }
michael@0 107
michael@0 108 if (!this.mDOMFilesEnumerator) {
michael@0 109 this.mDOMFilesEnumerator = {
michael@0 110 QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsISimpleEnumerator]),
michael@0 111
michael@0 112 mFiles: [],
michael@0 113 mIndex: 0,
michael@0 114
michael@0 115 hasMoreElements: function() {
michael@0 116 return (this.mIndex < this.mFiles.length);
michael@0 117 },
michael@0 118
michael@0 119 getNext: function() {
michael@0 120 if (this.mIndex >= this.mFiles.length) {
michael@0 121 throw Components.results.NS_ERROR_FAILURE;
michael@0 122 }
michael@0 123 return this.mFiles[this.mIndex++];
michael@0 124 }
michael@0 125 };
michael@0 126
michael@0 127 var utils = this.mParentWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
michael@0 128 .getInterface(Components.interfaces.nsIDOMWindowUtils);
michael@0 129
michael@0 130 for (var i = 0; i < this.mFilesEnumerator.mFiles.length; ++i) {
michael@0 131 var file = utils.wrapDOMFile(this.mFilesEnumerator.mFiles[i]);
michael@0 132 this.mDOMFilesEnumerator.mFiles.push(file);
michael@0 133 }
michael@0 134 }
michael@0 135
michael@0 136 return this.mDOMFilesEnumerator;
michael@0 137 },
michael@0 138
michael@0 139 /* readonly attribute nsIURI fileURL; */
michael@0 140 get fileURL() {
michael@0 141 if (this.mFileURL)
michael@0 142 return this.mFileURL;
michael@0 143
michael@0 144 if (!this.mFilesEnumerator)
michael@0 145 return null;
michael@0 146
michael@0 147 var ioService = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 148 .getService(Components.interfaces.nsIIOService);
michael@0 149
michael@0 150 return this.mFileURL = ioService.newFileURI(this.file);
michael@0 151 },
michael@0 152
michael@0 153 /* attribute wstring defaultString; */
michael@0 154 set defaultString(a) { this.mDefaultString = a; },
michael@0 155 get defaultString() { return this.mDefaultString; },
michael@0 156
michael@0 157 /* attribute wstring defaultExtension */
michael@0 158 set defaultExtension(ext) { },
michael@0 159 get defaultExtension() { return ""; },
michael@0 160
michael@0 161 /* attribute long filterIndex; */
michael@0 162 set filterIndex(a) { this.mFilterIndex = a; },
michael@0 163 get filterIndex() { return this.mFilterIndex; },
michael@0 164
michael@0 165 /* attribute boolean addToRecentDocs; */
michael@0 166 set addToRecentDocs(a) {},
michael@0 167 get addToRecentDocs() { return false; },
michael@0 168
michael@0 169 /* readonly attribute short mode; */
michael@0 170 get mode() { return this.mMode; },
michael@0 171
michael@0 172 /* members */
michael@0 173 mFilesEnumerator: undefined,
michael@0 174 mDOMFilesEnumerator: undefined,
michael@0 175 mParentWindow: null,
michael@0 176
michael@0 177 /* methods */
michael@0 178 init: function(parent, title, mode) {
michael@0 179 this.mParentWindow = parent;
michael@0 180 this.mTitle = title;
michael@0 181 this.mMode = mode;
michael@0 182 },
michael@0 183
michael@0 184 appendFilters: function(filterMask) {
michael@0 185 if (filterMask & nsIFilePicker.filterHTML) {
michael@0 186 this.appendFilter(titleBundle.GetStringFromName("htmlTitle"),
michael@0 187 filterBundle.GetStringFromName("htmlFilter"));
michael@0 188 }
michael@0 189 if (filterMask & nsIFilePicker.filterText) {
michael@0 190 this.appendFilter(titleBundle.GetStringFromName("textTitle"),
michael@0 191 filterBundle.GetStringFromName("textFilter"));
michael@0 192 }
michael@0 193 if (filterMask & nsIFilePicker.filterImages) {
michael@0 194 this.appendFilter(titleBundle.GetStringFromName("imageTitle"),
michael@0 195 filterBundle.GetStringFromName("imageFilter"));
michael@0 196 }
michael@0 197 if (filterMask & nsIFilePicker.filterXML) {
michael@0 198 this.appendFilter(titleBundle.GetStringFromName("xmlTitle"),
michael@0 199 filterBundle.GetStringFromName("xmlFilter"));
michael@0 200 }
michael@0 201 if (filterMask & nsIFilePicker.filterXUL) {
michael@0 202 this.appendFilter(titleBundle.GetStringFromName("xulTitle"),
michael@0 203 filterBundle.GetStringFromName("xulFilter"));
michael@0 204 }
michael@0 205 this.mAllowURLs = !!(filterMask & nsIFilePicker.filterAllowURLs);
michael@0 206 if (filterMask & nsIFilePicker.filterApps) {
michael@0 207 // We use "..apps" as a special filter for executable files
michael@0 208 this.appendFilter(titleBundle.GetStringFromName("appsTitle"),
michael@0 209 "..apps");
michael@0 210 }
michael@0 211 if (filterMask & nsIFilePicker.filterAudio) {
michael@0 212 this.appendFilter(titleBundle.GetStringFromName("audioTitle"),
michael@0 213 filterBundle.GetStringFromName("audioFilter"));
michael@0 214 }
michael@0 215 if (filterMask & nsIFilePicker.filterVideo) {
michael@0 216 this.appendFilter(titleBundle.GetStringFromName("videoTitle"),
michael@0 217 filterBundle.GetStringFromName("videoFilter"));
michael@0 218 }
michael@0 219 if (filterMask & nsIFilePicker.filterAll) {
michael@0 220 this.appendFilter(titleBundle.GetStringFromName("allTitle"),
michael@0 221 filterBundle.GetStringFromName("allFilter"));
michael@0 222 }
michael@0 223 },
michael@0 224
michael@0 225 appendFilter: function(title, extensions) {
michael@0 226 this.mFilterTitles.push(title);
michael@0 227 this.mFilters.push(extensions);
michael@0 228 },
michael@0 229
michael@0 230 open: function(aFilePickerShownCallback) {
michael@0 231 var tm = Components.classes["@mozilla.org/thread-manager;1"]
michael@0 232 .getService(Components.interfaces.nsIThreadManager);
michael@0 233 tm.mainThread.dispatch(function() {
michael@0 234 let result = Components.interfaces.nsIFilePicker.returnCancel;
michael@0 235 try {
michael@0 236 result = this.show();
michael@0 237 } catch(ex) {
michael@0 238 }
michael@0 239 if (aFilePickerShownCallback) {
michael@0 240 aFilePickerShownCallback.done(result);
michael@0 241 }
michael@0 242 }.bind(this), Components.interfaces.nsIThread.DISPATCH_NORMAL);
michael@0 243 },
michael@0 244
michael@0 245 show: function() {
michael@0 246 var o = new Object();
michael@0 247 o.title = this.mTitle;
michael@0 248 o.mode = this.mMode;
michael@0 249 o.displayDirectory = this.mDisplayDirectory;
michael@0 250 o.defaultString = this.mDefaultString;
michael@0 251 o.filterIndex = this.mFilterIndex;
michael@0 252 o.filters = new Object();
michael@0 253 o.filters.titles = this.mFilterTitles;
michael@0 254 o.filters.types = this.mFilters;
michael@0 255 o.allowURLs = this.mAllowURLs;
michael@0 256 o.retvals = new Object();
michael@0 257
michael@0 258 var parent;
michael@0 259 if (this.mParentWindow) {
michael@0 260 parent = this.mParentWindow;
michael@0 261 } else if (typeof(window) == "object" && window != null) {
michael@0 262 parent = window;
michael@0 263 } else {
michael@0 264 try {
michael@0 265 var appShellService = Components.classes[APPSHELL_SERV_CONTRACTID].getService(nsIAppShellService);
michael@0 266 parent = appShellService.hiddenDOMWindow;
michael@0 267 } catch(ex) {
michael@0 268 debug("Can't get parent. xpconnect hates me so we can't get one from the appShellService.\n");
michael@0 269 debug(ex + "\n");
michael@0 270 }
michael@0 271 }
michael@0 272
michael@0 273 var parentWin = null;
michael@0 274 try {
michael@0 275 parentWin = parent.QueryInterface(nsIInterfaceRequestor)
michael@0 276 .getInterface(nsIWebNavigation)
michael@0 277 .QueryInterface(nsIDocShellTreeItem)
michael@0 278 .treeOwner
michael@0 279 .QueryInterface(nsIInterfaceRequestor)
michael@0 280 .getInterface(nsIBaseWindow);
michael@0 281 } catch(ex) {
michael@0 282 dump("file picker couldn't get base window\n"+ex+"\n");
michael@0 283 }
michael@0 284 try {
michael@0 285 parent.openDialog("chrome://global/content/filepicker.xul",
michael@0 286 "",
michael@0 287 "chrome,modal,titlebar,resizable=yes,dependent=yes",
michael@0 288 o);
michael@0 289
michael@0 290 this.mFilterIndex = o.retvals.filterIndex;
michael@0 291 this.mFilesEnumerator = o.retvals.files;
michael@0 292 this.mFileURL = o.retvals.fileURL;
michael@0 293 lastDirectory = o.retvals.directory;
michael@0 294 return o.retvals.buttonStatus;
michael@0 295 } catch(ex) { dump("unable to open file picker\n" + ex + "\n"); }
michael@0 296
michael@0 297 return null;
michael@0 298 }
michael@0 299 }
michael@0 300
michael@0 301 if (DEBUG)
michael@0 302 debug = function (s) { dump("-*- filepicker: " + s + "\n"); };
michael@0 303 else
michael@0 304 debug = function (s) {};
michael@0 305
michael@0 306 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsFilePicker]);
michael@0 307
michael@0 308 /* crap from strres.js that I want to use for string bundles since I can't include another .js file.... */
michael@0 309
michael@0 310 var strBundleService = null;
michael@0 311
michael@0 312 function srGetStrBundle(path)
michael@0 313 {
michael@0 314 var strBundle = null;
michael@0 315
michael@0 316 if (!strBundleService) {
michael@0 317 try {
michael@0 318 strBundleService = Components.classes[STRBUNDLE_SERV_CONTRACTID].getService(nsIStringBundleService);
michael@0 319 } catch (ex) {
michael@0 320 dump("\n--** strBundleService createInstance failed **--\n");
michael@0 321 return null;
michael@0 322 }
michael@0 323 }
michael@0 324
michael@0 325 strBundle = strBundleService.createBundle(path);
michael@0 326 if (!strBundle) {
michael@0 327 dump("\n--** strBundle createInstance failed **--\n");
michael@0 328 }
michael@0 329 return strBundle;
michael@0 330 }
michael@0 331

mercurial