| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 tts API. | 5 // Custom bindings for the tts API. |
| 6 | |
| 7 var binding = require('binding').Binding.create('tts'); | |
| 8 | 6 |
| 9 var ttsNatives = requireNative('tts'); | 7 var ttsNatives = requireNative('tts'); |
| 10 var GetNextTTSEventId = ttsNatives.GetNextTTSEventId; | 8 var GetNextTTSEventId = ttsNatives.GetNextTTSEventId; |
| 11 | 9 |
| 12 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 10 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 13 var sendRequest = require('sendRequest').sendRequest; | 11 var sendRequest = require('sendRequest').sendRequest; |
| 14 var lazyBG = requireNative('lazy_background_page'); | 12 var lazyBG = requireNative('lazy_background_page'); |
| 15 | 13 |
| 16 binding.registerCustomHook(function(api) { | 14 chromeHidden.registerCustomHook('tts', function(api) { |
| 17 var apiFunctions = api.apiFunctions; | 15 var apiFunctions = api.apiFunctions; |
| 18 var tts = api.compiledApi; | |
| 19 | 16 |
| 20 chromeHidden.tts = { | 17 chromeHidden.tts = { |
| 21 handlers: {} | 18 handlers: {} |
| 22 }; | 19 }; |
| 23 | 20 |
| 24 function ttsEventListener(event) { | 21 function ttsEventListener(event) { |
| 25 var eventHandler = chromeHidden.tts.handlers[event.srcId]; | 22 var eventHandler = chromeHidden.tts.handlers[event.srcId]; |
| 26 if (eventHandler) { | 23 if (eventHandler) { |
| 27 eventHandler({ | 24 eventHandler({ |
| 28 type: event.type, | 25 type: event.type, |
| 29 charIndex: event.charIndex, | 26 charIndex: event.charIndex, |
| 30 errorMessage: event.errorMessage | 27 errorMessage: event.errorMessage |
| 31 }); | 28 }); |
| 32 if (event.isFinalEvent) { | 29 if (event.isFinalEvent) { |
| 33 delete chromeHidden.tts.handlers[event.srcId]; | 30 delete chromeHidden.tts.handlers[event.srcId]; |
| 34 // Balanced in 'speak' handler. | 31 // Balanced in 'speak' handler. |
| 35 lazyBG.DecrementKeepaliveCount(); | 32 lazyBG.DecrementKeepaliveCount(); |
| 36 } | 33 } |
| 37 } | 34 } |
| 38 } | 35 } |
| 39 | 36 |
| 40 // This file will get run if an extension needs the ttsEngine permission, but | 37 // This file will get run if an extension needs the ttsEngine permission, but |
| 41 // it doesn't necessarily have the tts permission. If it doesn't, trying to | 38 // it doesn't necessarily have the tts permission. If it doesn't, trying to |
| 42 // add a listener to chrome.tts.onEvent will fail. | 39 // add a listener to chrome.tts.onEvent will fail. |
| 43 // See http://crbug.com/122474. | 40 // See http://crbug.com/122474. |
| 44 try { | 41 try { |
| 45 tts.onEvent.addListener(ttsEventListener); | 42 chrome.tts.onEvent.addListener(ttsEventListener); |
| 46 } catch (e) {} | 43 } catch (e) {} |
| 47 | 44 |
| 48 apiFunctions.setHandleRequest('speak', function() { | 45 apiFunctions.setHandleRequest('speak', function() { |
| 49 var args = arguments; | 46 var args = arguments; |
| 50 if (args.length > 1 && args[1] && args[1].onEvent) { | 47 if (args.length > 1 && args[1] && args[1].onEvent) { |
| 51 var id = GetNextTTSEventId(); | 48 var id = GetNextTTSEventId(); |
| 52 args[1].srcId = id; | 49 args[1].srcId = id; |
| 53 chromeHidden.tts.handlers[id] = args[1].onEvent; | 50 chromeHidden.tts.handlers[id] = args[1].onEvent; |
| 54 // Keep the page alive until the event finishes. | 51 // Keep the page alive until the event finishes. |
| 55 // Balanced in eventHandler. | 52 // Balanced in eventHandler. |
| 56 lazyBG.IncrementKeepaliveCount(); | 53 lazyBG.IncrementKeepaliveCount(); |
| 57 } | 54 } |
| 58 sendRequest(this.name, args, this.definition.parameters); | 55 sendRequest(this.name, args, this.definition.parameters); |
| 59 return id; | 56 return id; |
| 60 }); | 57 }); |
| 61 }); | 58 }); |
| 62 | |
| 63 exports.binding = binding.generate(); | |
| OLD | NEW |