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