| 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'); | 11 var logging = requireNative('logging'); |
| 12 | 12 |
| 13 var callbacksInfo = {}; | 13 var callbacksInfo = {}; |
| 14 | 14 |
| 15 function callbackWrapper(callback, method, message) { | 15 function callbackWrapper(callback, method, message) { |
| 16 if (callback == undefined) | 16 if (callback == undefined) |
| 17 return; | 17 return; |
| 18 | 18 |
| 19 try { | 19 try { |
| 20 if (message !== null) | 20 if (message !== null) |
| 21 lastError.set('displaySource.startSession', message, null, chrome); | 21 lastError.set(method, message, null, chrome); |
| 22 callback(); | 22 callback(); |
| 23 } finally { | 23 } finally { |
| 24 lastError.clear(chrome); | 24 lastError.clear(chrome); |
| 25 } | 25 } |
| 26 } | 26 } |
| 27 | 27 |
| 28 function callCompletionCallback(callbackId, error_message) { | 28 function callCompletionCallback(callbackId, error_message) { |
| 29 try { | 29 try { |
| 30 var callbackInfo = callbacksInfo[callbackId]; | 30 var callbackInfo = callbacksInfo[callbackId]; |
| 31 logging.DCHECK(callbackInfo != null); | 31 logging.DCHECK(callbackInfo != null); |
| 32 callbackWrapper(callbackInfo.callback, callbackInfo.method, error_message); | 32 callbackWrapper(callbackInfo.callback, callbackInfo.method, error_message); |
| 33 } finally { | 33 } finally { |
| 34 delete callbacksInfo[callbackId]; | 34 delete callbacksInfo[callbackId]; |
| 35 } | 35 } |
| 36 } | 36 } |
| 37 | 37 |
| 38 binding.registerCustomHook(function(bindingsAPI, extensionId) { | 38 binding.registerCustomHook(function(bindingsAPI, extensionId) { |
| 39 var apiFunctions = bindingsAPI.apiFunctions; | 39 var apiFunctions = bindingsAPI.apiFunctions; |
| 40 apiFunctions.setHandleRequest( | 40 apiFunctions.setHandleRequest( |
| 41 'startSession', function(sessionInfo, callback) { | 41 'startSession', function(sessionInfo, callback) { |
| 42 try { | 42 try { |
| 43 var callId = natives.StartSession(sessionInfo, callbackWrapper); | 43 var callId = natives.StartSession(sessionInfo); |
| 44 callbacksInfo[callId] = { | 44 callbacksInfo[callId] = { |
| 45 callback: callback, | 45 callback: callback, |
| 46 method: 'displaySource.startSession' | 46 method: 'displaySource.startSession' |
| 47 }; | 47 }; |
| 48 } catch (e) { | 48 } catch (e) { |
| 49 callbackWrapper(callback, 'displaySource.startSession', e.message); | 49 callbackWrapper(callback, 'displaySource.startSession', e.message); |
| 50 } | 50 } |
| 51 }); | 51 }); |
| 52 apiFunctions.setHandleRequest( | 52 apiFunctions.setHandleRequest( |
| 53 'terminateSession', function(sink_id, callback) { | 53 'terminateSession', function(sink_id, callback) { |
| 54 try { | 54 try { |
| 55 var callId = natives.TerminateSession(sink_id, callbackWrapper); | 55 var callId = natives.TerminateSession(sink_id); |
| 56 callbacksInfo[callId] = { | 56 callbacksInfo[callId] = { |
| 57 callback: callback, | 57 callback: callback, |
| 58 method: 'displaySource.terminateSession' | 58 method: 'displaySource.terminateSession' |
| 59 }; | 59 }; |
| 60 } catch (e) { | 60 } catch (e) { |
| 61 callbackWrapper( | 61 callbackWrapper( |
| 62 callback, 'displaySource.terminateSession', e.message); | 62 callback, 'displaySource.terminateSession', e.message); |
| 63 } | 63 } |
| 64 }); | 64 }); |
| 65 }); | 65 }); |
| 66 | 66 |
| 67 exports.$set('binding', binding.generate()); | 67 exports.$set('binding', binding.generate()); |
| 68 // Called by C++. | 68 // Called by C++. |
| 69 exports.$set('callCompletionCallback', callCompletionCallback); | 69 exports.$set('callCompletionCallback', callCompletionCallback); |
| OLD | NEW |