- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / renderer / resources / extensions / desktop_capture_custom_bindings.js
1 // Copyright 2013 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 // Custom binding for the desktopCapture API.
6
7 var binding = require('binding').Binding.create('desktopCapture');
8 var sendRequest = require('sendRequest').sendRequest;
9 var idGenerator = requireNative('id_generator');
10
11 binding.registerCustomHook(function(bindingsAPI) {
12   var apiFunctions = bindingsAPI.apiFunctions;
13
14   var pendingRequests = {};
15
16   function onRequestResult(id, result) {
17     if (id in pendingRequests) {
18       var callback = pendingRequests[id];
19       delete pendingRequests[id];
20       callback(result);
21     }
22   }
23
24   apiFunctions.setHandleRequest('chooseDesktopMedia',
25                                 function(sources, target_tab, callback) {
26     // |target_tab| is an optional parameter.
27     if (callback === undefined) {
28       callback = target_tab;
29       target_tab = undefined;
30     }
31     var id = idGenerator.GetNextId();
32     pendingRequests[id] = callback;
33     sendRequest(this.name,
34                 [id, sources, target_tab, onRequestResult.bind(null, id)],
35                 this.definition.parameters, {});
36     return id;
37   });
38
39   apiFunctions.setHandleRequest('cancelChooseDesktopMedia', function(id) {
40     if (id in pendingRequests) {
41       delete pendingRequests[id];
42       sendRequest(this.name, [id], this.definition.parameters, {});
43     }
44   });
45 });
46
47 exports.binding = binding.generate();