This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
_system_uri.js
102 lines (92 loc) · 3.08 KB
/
_system_uri.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under
// the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT> or
// the Modified BSD license <LICENSE-BSD or https://opensource.org/licenses/BSD-3-Clause>,
// at your option.
//
// This file may not be copied, modified, or distributed except according to those terms.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.
const path = require('path');
const FFI = require('ffi');
const ref = require('ref');
const { getSystemUriLibFilename } = require('../helpers');
const makeError = require('./_error.js');
const h = require('./helpers');
const t = require('./types');
const ArrayType = require('ref-array');
const StringArray = ArrayType(ref.types.CString);
const currentDir = path.dirname(__filename);
let ffi = null;
let isSysUriLibLoadErr = null;
const init = (options) => {
ffi = FFI.Library(getSystemUriLibFilename(currentDir, options), {
open_uri: ["void", ['string', 'pointer', 'pointer'] ],
install: ["void", ['string', //bundle
'string', //vendor
'string', //name
StringArray, //exec args
t.usize, //exec args length
'string', //icon
'string', //schemes,
'pointer', // userdata
'pointer'
] ],
});
};
const _handleError = (resolve, reject) => {
return FFI.Callback("void", [t.VoidPtr, t.FfiResultPtr],
(userData, resultPtr) => {
const result = h.makeFfiResult(resultPtr);
if (result.error_code !== 0) {
return reject(makeError(result.error_code, result.error_description));
}
return resolve();
}
);
}
const openUri = (uri) => {
if (!ffi) {
return;
}
return new Promise((resolve, reject) => {
try {
const cb = _handleError(resolve, reject);
ffi.open_uri(uri.uri || uri, ref.NULL, cb);
} catch (err) {
return reject(err);
}
});
}
const registerUriScheme = (appInfo, schemes) => {
if (!ffi) {
return;
}
if (appInfo.exec && !Array.isArray(appInfo.exec)) {
throw new Error("Exec command must be an array of string arguments");
}
const bundle = appInfo.bundle || appInfo.id;
const exec = appInfo.exec ? new StringArray(appInfo.exec) : new StringArray([process.execPath]);
const vendor = appInfo.vendor.replace(/\s/g, '-');
const name = appInfo.name.replace(/\s/g, '-');
const icon = appInfo.icon;
const joinedSchemes = schemes.join ? schemes.join(',') : schemes;
return new Promise((resolve, reject) => {
try {
const cb = _handleError(resolve, reject);
ffi.install(bundle, vendor, name, exec, exec.length, icon, joinedSchemes, ref.NULL, cb);
} catch (err) {
return reject(err);
}
});
}
// FIXME: As long as `safe-app` doesn't expose system uri itself, we'll
// patch it directly on it. This should later move into its own sub-module
// and take care of mobile support for other platforms, too.
module.exports = (other, options) => {
other.openUri = openUri;
other.registerUriScheme = registerUriScheme;
init(options);
}