forked from GPII/universal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapture.js
More file actions
executable file
·229 lines (221 loc) · 8.61 KB
/
Capture.js
File metadata and controls
executable file
·229 lines (221 loc) · 8.61 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* GPII snapshot Handler
*
* Copyright 2014 Raising the Floor - International
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* You may obtain a copy of the License at
* https://github.com/gpii/universal/LICENSE.txt
*/
"use strict";
var fluid = require("infusion"),
gpii = fluid.registerNamespace("gpii");
fluid.defaults("gpii.flowManager.capture", {
gradeNames: ["fluid.component"],
events: {
// Pseudoevents for transforming promise chains to fetch the solutions on the current device
// and capture the settings for the device.
onSolutionsForCurrentDevice: null,
onCaptureSettingsForCurrentDevice: null
},
listeners: {
// Begin declaration of Promise Chain for onSolutionsForCurrentDevice
"onSolutionsForCurrentDevice.getDeviceContextPromise": {
funcName: "gpii.lifecycleManager.getDeviceContextPromise",
args: ["{flowManager}.deviceReporter"]
},
"onSolutionsForCurrentDevice.getSolutionsPromise": {
funcName: "gpii.flowManager.getSolutionsPromise",
args: [ "{flowManager}.solutionsRegistryDataSource", "{arguments}.0"],
priority: "after:getDeviceContextPromise"
},
"onSolutionsForCurrentDevice.solutionsRegistryEntriesToPromise": {
funcName: "fluid.toPromise",
args: ["{arguments}.0.solutionsRegistryEntries"],
priority: "after:getSolutionsPromise"
},
// Begin declaration of Promise Chain for onCaptureSettingsForCurrentDevice
"onCaptureSettingsForCurrentDevice.getInstalledSolutionsForCurrentDevice": {
func: "{that}.getInstalledSolutionsForCurrentDevice"
},
"onCaptureSettingsForCurrentDevice.captureSystemSettings": {
func: "{that}.captureSystemSettings",
args: ["{arguments}.0", "{arguments}.1"],
priority: "after:getInstalledSolutionsForCurrentDevice"
},
"onCaptureSettingsForCurrentDevice.formatRawCapturedSettings": {
func: "gpii.flowManager.formatRawCapturedSettings",
args: ["{arguments}.0"],
priority: "after:captureSystemSettings"
}
},
invokers: {
getInstalledSolutionsForCurrentDevice: {
funcName: "gpii.flowManager.getInstalledSolutionsForCurrentDevice",
args: ["{that}"]
},
getSystemSettingsCapture: {
funcName: "gpii.flowManager.getSystemSettingsCapture",
args: ["{that}.events.onCaptureSettingsForCurrentDevice", "{arguments}.0"] // options
},
captureSystemSettings: {
funcName: "gpii.flowManager.captureSystemSettings",
args: ["{lifecycleManager}.read", "{arguments}.0", "{arguments}.1"] // solutionsRegistryEntries, options
}
}
});
gpii.flowManager.getInstalledSolutionsForCurrentDevice = function (that) {
return fluid.promise.fireTransformEvent(that.events.onSolutionsForCurrentDevice);
};
/**
* This main API entry point for capturing settings from a system or computer. This captures
* the actual settings on the device, so it assumed to be running in a local untrusted flow
* manager.
*
* @param {Event} onCaptureSettingsForCurrentDevice - The transforming promise chain
* @param {Object} options - Options for this chain.
* @param {Array} options.solutionsList - An array of solution IDs to filter by when
* retreiving settings. If this option is not included, all available settings will be
* returned. ex: `["com.microsoft.windows.mouseSettings", "com.freedomscientific.jaws"]`.
* @return {Promise} A promise resolved with the payload of captured system settings.
*/
gpii.flowManager.getSystemSettingsCapture = function (onCaptureSettingsForCurrentDevice, options) {
return fluid.promise.fireTransformEvent(onCaptureSettingsForCurrentDevice,
{}, options);
};
/**
* This function invokers a settings handler to fetch settings, but wraps the processing and reports
* back any failures in an `isError: true` json block.
*
* @param {Function} invokeSettingsHandlerGet - The get function from the appropriate settings handler.
* @param {String} solutionID - The dotted solution id, ex `com.freedomscientific.jaws`
* @param {Object} handlerSpec - TODO, exactly how much of the solutions entry block is this?
* @return {Object} The returned settings, or an error block with debugging information.
*/
gpii.flowManager.capture.safeHandlerGet = function (invokeSettingsHandlerGet, solutionID, handlerSpec) {
var promiseTogo = fluid.promise();
try {
invokeSettingsHandlerGet(solutionID, handlerSpec).then(function (data) {
promiseTogo.resolve(data);
}, function (err) {
promiseTogo.resolve({
solutionId: solutionID,
handlerSpec: handlerSpec,
isError: true,
message: "Capture Workflow Error: Settings Handler Promise Rejection",
err: err
});
});
}
catch (err) {
promiseTogo.resolve({
solutionId: solutionID,
handlerSpec: handlerSpec,
isError: true,
message: "Capture Workflow Error: Unexpected Error in operating settings handler.",
err: err
});
}
return promiseTogo;
};
/**
* Runs through all the solutions currently available on the system, pulls the current
* setting for each supportedSetting and returns them in an object. Primary use case
* is for backing Capture tools that would allow a user to set up their GPII profile
* starting with the current settings for their applications on the local machine.
*
* @param {Function|gpii.lifecycleManager.read} readSettingsFunc - lifecycleManager.read (or suitable implementation),
* that takes solution registry entries, reads their current values on the device, and returns a promise resolved to
* them.
* @param {Object} solutions - Solutions registry entries for solutions available on the current machine.
* @param {Object} options - Extra options for processing.
* @param {Array} options.solutionsList - If provided, only solutions in this list of `solutionsID`s will
* be captured. Example:
*
* '''json
* ["com.microsoft.windows.cursors", "com.freedomscientific.jaws"]
* '''
* @return {fluid.promise} Returns a promise resolving with the entire system settings capture.
*/
gpii.flowManager.captureSystemSettings = function (readSettingsFunc, solutions, options) {
var solutionsToFetch = fluid.copy(solutions);
if (options.solutionsList) {
fluid.remove_if(solutionsToFetch, function (solution, solutionID) {
return !options.solutionsList.includes(solutionID);
});
}
return readSettingsFunc(solutionsToFetch);
};
/**
* The raw return payload from the capture promise sequence looks like:
* '''json
* [
* {
* "fakemag1": [
* {
* "settings": {
* "magnification": 2
* }
* }
* ]
* },
* {
* "fakemag1": [
* {
* "settings": {
* "invert": true
* }
* }
* ]
* },
* {
* "fakemag2": [
* {
* "settings": {
* "magnification": 2,
* "invert": true
* }
* }
* ]
* }
* ]
* '''
*
* and we want:
* '''json
* {
* "fakemag1": {
* "magnification": 2,
* "invert": true
* },
* "fakemag2": {
* "magnification": 2,
* "invert": true
* }
* }
* '''
*
* @param {Object} data - The raw captured data.
* @return {Object} Returns a new payload with collapsed data, and multiple settings handler
* results for the same solution merged together.
*/
gpii.flowManager.formatRawCapturedSettings = function (data) {
var togo = {};
fluid.each(data, function (sequenceItem) {
if (sequenceItem.isError) {
fluid.log("Error capturing settings for: ", sequenceItem);
return;
}
fluid.each(sequenceItem, function (item, key) {
if (!togo[key]) {
togo[key] = {};
}
fluid.each(item[0].settings, function (value, settingId) {
togo[key][settingId] = value;
});
});
});
return togo;
};