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