michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Native (xpcom) implementation of key OS.File functions michael@0: */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["read"]; michael@0: michael@0: let {results: Cr, utils: Cu, interfaces: Ci} = Components; michael@0: michael@0: let SharedAll = Cu.import("resource://gre/modules/osfile/osfile_shared_allthreads.jsm", {}); michael@0: michael@0: let SysAll = {}; michael@0: if (SharedAll.Constants.Win) { michael@0: Cu.import("resource://gre/modules/osfile/osfile_win_allthreads.jsm", SysAll); michael@0: } else if (SharedAll.Constants.libc) { michael@0: Cu.import("resource://gre/modules/osfile/osfile_unix_allthreads.jsm", SysAll); michael@0: } else { michael@0: throw new Error("I am neither under Windows nor under a Posix system"); michael@0: } michael@0: let {Promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); michael@0: let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); michael@0: michael@0: /** michael@0: * The native service holding the implementation of the functions. michael@0: */ michael@0: XPCOMUtils.defineLazyServiceGetter(this, michael@0: "Internals", michael@0: "@mozilla.org/toolkit/osfile/native-internals;1", michael@0: "nsINativeOSFileInternalsService"); michael@0: michael@0: /** michael@0: * Native implementation of OS.File.read michael@0: * michael@0: * This implementation does not handle option |compression|. michael@0: */ michael@0: this.read = function(path, options = {}) { michael@0: // Sanity check on types of options michael@0: if ("encoding" in options && typeof options.encoding != "string") { michael@0: return Promise.reject(new TypeError("Invalid type for option encoding")); michael@0: } michael@0: if ("compression" in options && typeof options.compression != "string") { michael@0: return Promise.reject(new TypeError("Invalid type for option compression")); michael@0: } michael@0: if ("bytes" in options && typeof options.bytes != "number") { michael@0: return Promise.reject(new TypeError("Invalid type for option bytes")); michael@0: } michael@0: michael@0: let deferred = Promise.defer(); michael@0: Internals.read(path, michael@0: options, michael@0: function onSuccess(success) { michael@0: success.QueryInterface(Ci.nsINativeOSFileResult); michael@0: if ("outExecutionDuration" in options) { michael@0: options.outExecutionDuration = michael@0: success.executionDurationMS + michael@0: (options.outExecutionDuration || 0); michael@0: } michael@0: deferred.resolve(success.result); michael@0: }, michael@0: function onError(operation, oserror) { michael@0: deferred.reject(new SysAll.Error(operation, oserror, path)); michael@0: } michael@0: ); michael@0: return deferred.promise; michael@0: };