OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Custom binding for the Display Source API. | 5 // Custom binding for the Display Source API. |
6 | 6 |
7 var binding = require('binding').Binding.create('displaySource'); | 7 var binding = require('binding').Binding.create('displaySource'); |
8 var chrome = requireNative('chrome').GetChrome(); | 8 var chrome = requireNative('chrome').GetChrome(); |
9 var lastError = require('lastError'); | 9 var lastError = require('lastError'); |
10 var natives = requireNative('display_source'); | 10 var natives = requireNative('display_source'); |
| 11 var logging = requireNative('logging'); |
| 12 |
| 13 var callbacksInfo = {}; |
| 14 |
| 15 function callbackWrapper(callback, method, message) { |
| 16 if (callback == undefined) |
| 17 return; |
| 18 |
| 19 try { |
| 20 if (message !== null) |
| 21 lastError.set('displaySource.startSession', message, null, chrome); |
| 22 callback(); |
| 23 } finally { |
| 24 lastError.clear(chrome); |
| 25 } |
| 26 } |
| 27 |
| 28 function callCompletionCallback(callbackId, error_message) { |
| 29 try { |
| 30 var callbackInfo = callbacksInfo[callbackId]; |
| 31 logging.DCHECK(callbackInfo != null); |
| 32 callbackWrapper(callbackInfo.callback, callbackInfo.method, error_message); |
| 33 } finally { |
| 34 delete callbacksInfo[callbackId]; |
| 35 } |
| 36 } |
11 | 37 |
12 binding.registerCustomHook(function(bindingsAPI, extensionId) { | 38 binding.registerCustomHook(function(bindingsAPI, extensionId) { |
13 var apiFunctions = bindingsAPI.apiFunctions; | 39 var apiFunctions = bindingsAPI.apiFunctions; |
14 apiFunctions.setHandleRequest('startSession', | 40 apiFunctions.setHandleRequest( |
15 function(sessionInfo, callback) { | 41 'startSession', function(sessionInfo, callback) { |
16 try { | 42 try { |
17 natives.StartSession(sessionInfo); | 43 var callId = natives.StartSession(sessionInfo, callbackWrapper); |
| 44 callbacksInfo[callId] = { |
| 45 callback: callback, |
| 46 method: 'displaySource.startSession' |
| 47 }; |
18 } catch (e) { | 48 } catch (e) { |
19 lastError.set('displaySource.startSession', e.message, null, chrome); | 49 callbackWrapper(callback, 'displaySource.startSession', e.message); |
20 } finally { | |
21 if (callback !== undefined) | |
22 callback(); | |
23 lastError.clear(chrome); | |
24 } | 50 } |
25 }); | 51 }); |
26 apiFunctions.setHandleRequest('terminateSession', | 52 apiFunctions.setHandleRequest( |
27 function(sink_id, callback) { | 53 'terminateSession', function(sink_id, callback) { |
28 try { | 54 try { |
29 natives.TerminateSession(sink_id); | 55 var callId = natives.TerminateSession(sink_id, callbackWrapper); |
| 56 callbacksInfo[callId] = { |
| 57 callback: callback, |
| 58 method: 'displaySource.terminateSession' |
| 59 }; |
30 } catch (e) { | 60 } catch (e) { |
31 lastError.set( | 61 callbackWrapper( |
32 'displaySource.terminateSession', e.message, null, chrome); | 62 callback, 'displaySource.terminateSession', e.message); |
33 } finally { | |
34 if (callback !== undefined) | |
35 callback(); | |
36 lastError.clear(chrome); | |
37 } | 63 } |
38 }); | 64 }); |
39 }); | 65 }); |
40 | 66 |
41 exports.$set('binding', binding.generate()); | 67 exports.$set('binding', binding.generate()); |
| 68 // Called by C++. |
| 69 exports.$set('callCompletionCallback', callCompletionCallback); |
OLD | NEW |