OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // TODO(gdk): This all looks very similar to the socket custom bindings, and for |
| 6 // a good reason, because they essentially do the same work. Refactor onEvent |
| 7 // bindings out of a per-extension mechanism into a generalized mechanism. |
| 8 |
| 9 var experimentalUsbNatives = requireNative('experimental_usb'); |
| 10 var GetNextUsbEventId = experimentalUsbNatives.GetNextUsbEventId; |
| 11 |
| 12 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 13 var sendRequest = require('sendRequest').sendRequest; |
| 14 var lazyBG = requireNative('lazy_background_page'); |
| 15 |
| 16 chromeHidden.registerCustomHook('experimental.usb', function(api) { |
| 17 var apiFunctions = api.apiFunctions; |
| 18 |
| 19 apiFunctions.setHandleRequest('findDevice', function() { |
| 20 var args = arguments; |
| 21 if (args[2] && args[2].onEvent) { |
| 22 var id = GetNextUsbEventId(); |
| 23 args[2].srcId = id; |
| 24 chromeHidden.usb.handlers[id] = args[2].onEvent; |
| 25 lazyBG.IncrementKeepaliveCount(); |
| 26 } |
| 27 sendRequest(this.name, args, this.definition.parameters); |
| 28 return id; |
| 29 }); |
| 30 |
| 31 chromeHidden.usb = {}; |
| 32 chromeHidden.usb.handlers = {}; |
| 33 chrome.experimental.usb.onEvent.addListener(function(event) { |
| 34 var eventHandler = chromeHidden.usb.handlers[event.srcId]; |
| 35 if (eventHandler) { |
| 36 switch (event.type) { |
| 37 case 'transferComplete': |
| 38 eventHandler({resultCode: event.resultCode, data: event.data}); |
| 39 break; |
| 40 default: |
| 41 console.error('Unexpected UsbEvent, type ' + event.type); |
| 42 break; |
| 43 } |
| 44 } |
| 45 }); |
| 46 }); |
OLD | NEW |