1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/osfile/modules/osfile_native.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,70 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/** 1.9 + * Native (xpcom) implementation of key OS.File functions 1.10 + */ 1.11 + 1.12 +"use strict"; 1.13 + 1.14 +this.EXPORTED_SYMBOLS = ["read"]; 1.15 + 1.16 +let {results: Cr, utils: Cu, interfaces: Ci} = Components; 1.17 + 1.18 +let SharedAll = Cu.import("resource://gre/modules/osfile/osfile_shared_allthreads.jsm", {}); 1.19 + 1.20 +let SysAll = {}; 1.21 +if (SharedAll.Constants.Win) { 1.22 + Cu.import("resource://gre/modules/osfile/osfile_win_allthreads.jsm", SysAll); 1.23 +} else if (SharedAll.Constants.libc) { 1.24 + Cu.import("resource://gre/modules/osfile/osfile_unix_allthreads.jsm", SysAll); 1.25 +} else { 1.26 + throw new Error("I am neither under Windows nor under a Posix system"); 1.27 +} 1.28 +let {Promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); 1.29 +let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); 1.30 + 1.31 +/** 1.32 + * The native service holding the implementation of the functions. 1.33 + */ 1.34 +XPCOMUtils.defineLazyServiceGetter(this, 1.35 + "Internals", 1.36 + "@mozilla.org/toolkit/osfile/native-internals;1", 1.37 + "nsINativeOSFileInternalsService"); 1.38 + 1.39 +/** 1.40 + * Native implementation of OS.File.read 1.41 + * 1.42 + * This implementation does not handle option |compression|. 1.43 + */ 1.44 +this.read = function(path, options = {}) { 1.45 + // Sanity check on types of options 1.46 + if ("encoding" in options && typeof options.encoding != "string") { 1.47 + return Promise.reject(new TypeError("Invalid type for option encoding")); 1.48 + } 1.49 + if ("compression" in options && typeof options.compression != "string") { 1.50 + return Promise.reject(new TypeError("Invalid type for option compression")); 1.51 + } 1.52 + if ("bytes" in options && typeof options.bytes != "number") { 1.53 + return Promise.reject(new TypeError("Invalid type for option bytes")); 1.54 + } 1.55 + 1.56 + let deferred = Promise.defer(); 1.57 + Internals.read(path, 1.58 + options, 1.59 + function onSuccess(success) { 1.60 + success.QueryInterface(Ci.nsINativeOSFileResult); 1.61 + if ("outExecutionDuration" in options) { 1.62 + options.outExecutionDuration = 1.63 + success.executionDurationMS + 1.64 + (options.outExecutionDuration || 0); 1.65 + } 1.66 + deferred.resolve(success.result); 1.67 + }, 1.68 + function onError(operation, oserror) { 1.69 + deferred.reject(new SysAll.Error(operation, oserror, path)); 1.70 + } 1.71 + ); 1.72 + return deferred.promise; 1.73 +};