blob: 758af527709705aa86349caf55c90d90d45854cb (
plain)
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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// TODO(gdk): This all looks very similar to the socket custom bindings, and for
// a good reason, because they essentially do the same work. Refactor onEvent
// bindings out of a per-extension mechanism into a generalized mechanism.
var experimentalUsbNatives = requireNative('experimental_usb');
var GetNextUsbEventId = experimentalUsbNatives.GetNextUsbEventId;
var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
var sendRequest = require('sendRequest').sendRequest;
var lazyBG = requireNative('lazy_background_page');
chromeHidden.registerCustomHook('experimental.usb', function(api) {
var apiFunctions = api.apiFunctions;
apiFunctions.setHandleRequest('findDevice', function() {
var args = arguments;
if (args[2] && args[2].onEvent) {
var id = GetNextUsbEventId();
args[2].srcId = id;
chromeHidden.usb.handlers[id] = args[2].onEvent;
lazyBG.IncrementKeepaliveCount();
}
sendRequest(this.name, args, this.definition.parameters);
return id;
});
chromeHidden.usb = {};
chromeHidden.usb.handlers = {};
chrome.experimental.usb.onEvent.addListener(function(event) {
var eventHandler = chromeHidden.usb.handlers[event.srcId];
if (eventHandler) {
switch (event.type) {
case 'transferComplete':
eventHandler({resultCode: event.resultCode, data: event.data});
break;
default:
console.error('Unexpected UsbEvent, type ' + event.type);
break;
}
}
});
});
|