|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 /** |
|
6 * Native (xpcom) implementation of key OS.File functions |
|
7 */ |
|
8 |
|
9 "use strict"; |
|
10 |
|
11 this.EXPORTED_SYMBOLS = ["read"]; |
|
12 |
|
13 let {results: Cr, utils: Cu, interfaces: Ci} = Components; |
|
14 |
|
15 let SharedAll = Cu.import("resource://gre/modules/osfile/osfile_shared_allthreads.jsm", {}); |
|
16 |
|
17 let SysAll = {}; |
|
18 if (SharedAll.Constants.Win) { |
|
19 Cu.import("resource://gre/modules/osfile/osfile_win_allthreads.jsm", SysAll); |
|
20 } else if (SharedAll.Constants.libc) { |
|
21 Cu.import("resource://gre/modules/osfile/osfile_unix_allthreads.jsm", SysAll); |
|
22 } else { |
|
23 throw new Error("I am neither under Windows nor under a Posix system"); |
|
24 } |
|
25 let {Promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); |
|
26 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
|
27 |
|
28 /** |
|
29 * The native service holding the implementation of the functions. |
|
30 */ |
|
31 XPCOMUtils.defineLazyServiceGetter(this, |
|
32 "Internals", |
|
33 "@mozilla.org/toolkit/osfile/native-internals;1", |
|
34 "nsINativeOSFileInternalsService"); |
|
35 |
|
36 /** |
|
37 * Native implementation of OS.File.read |
|
38 * |
|
39 * This implementation does not handle option |compression|. |
|
40 */ |
|
41 this.read = function(path, options = {}) { |
|
42 // Sanity check on types of options |
|
43 if ("encoding" in options && typeof options.encoding != "string") { |
|
44 return Promise.reject(new TypeError("Invalid type for option encoding")); |
|
45 } |
|
46 if ("compression" in options && typeof options.compression != "string") { |
|
47 return Promise.reject(new TypeError("Invalid type for option compression")); |
|
48 } |
|
49 if ("bytes" in options && typeof options.bytes != "number") { |
|
50 return Promise.reject(new TypeError("Invalid type for option bytes")); |
|
51 } |
|
52 |
|
53 let deferred = Promise.defer(); |
|
54 Internals.read(path, |
|
55 options, |
|
56 function onSuccess(success) { |
|
57 success.QueryInterface(Ci.nsINativeOSFileResult); |
|
58 if ("outExecutionDuration" in options) { |
|
59 options.outExecutionDuration = |
|
60 success.executionDurationMS + |
|
61 (options.outExecutionDuration || 0); |
|
62 } |
|
63 deferred.resolve(success.result); |
|
64 }, |
|
65 function onError(operation, oserror) { |
|
66 deferred.reject(new SysAll.Error(operation, oserror, path)); |
|
67 } |
|
68 ); |
|
69 return deferred.promise; |
|
70 }; |