| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 automation API. | 5 // Custom bindings for the automation API. |
| 6 var automation = require('binding').Binding.create('automation'); | 6 var automation = require('binding').Binding.create('automation'); |
| 7 var automationInternal = | 7 var automationInternal = |
| 8 require('binding').Binding.create('automationInternal').generate(); | 8 require('binding').Binding.create('automationInternal').generate(); |
| 9 var eventBindings = require('event_bindings'); | 9 var eventBindings = require('event_bindings'); |
| 10 var Event = eventBindings.Event; | 10 var Event = eventBindings.Event; |
| 11 var AutomationNode = require('automationNode').AutomationNode; | 11 var AutomationNode = require('automationNode').AutomationNode; |
| 12 var AutomationTree = require('automationTree').AutomationTree; | 12 var AutomationTree = require('automationTree').AutomationTree; |
| 13 | 13 |
| 14 // TODO(aboxhall): Look into using WeakMap | 14 // TODO(aboxhall): Look into using WeakMap |
| 15 var routingIdToAutomationTree = {}; | 15 var idToAutomationTree = {}; |
| 16 var routingIdToCallback = {}; | 16 var idToCallback = {}; |
| 17 |
| 18 // TODO(dtseng): Move out to automation/automation_util.js or as a static member |
| 19 // of AutomationTree to keep this file clean. |
| 20 /* |
| 21 * Creates an id associated with a particular AutomationTree based upon a |
| 22 * renderer/renderer host pair's process and routing id. |
| 23 */ |
| 24 var createAutomationTreeID = function(pid, rid) { |
| 25 return pid + '_' + rid; |
| 26 }; |
| 17 | 27 |
| 18 automation.registerCustomHook(function(bindingsAPI) { | 28 automation.registerCustomHook(function(bindingsAPI) { |
| 19 var apiFunctions = bindingsAPI.apiFunctions; | 29 var apiFunctions = bindingsAPI.apiFunctions; |
| 20 | 30 |
| 21 apiFunctions.setHandleRequest('getTree', function(callback) { | 31 apiFunctions.setHandleRequest('getTree', function(callback) { |
| 22 // enableCurrentTab() ensures the renderer for the current tab has | 32 // enableCurrentTab() ensures the renderer for the current tab has |
| 23 // accessibility enabled, and fetches its routing id to use as a key in the | 33 // accessibility enabled, and fetches its process and routing ids to use as |
| 24 // routingIdToAutomationTree map. The callback to enableCurrentTab is bound | 34 // a key in the idToAutomationTree map. The callback to enableCurrentTab is |
| 25 // to the callback passed in to getTree(), so that once the tree is | 35 // bound to the callback passed in to getTree(), so that once the tree is |
| 26 // available (either due to having been cached earlier, or after an | 36 // available (either due to having been cached earlier, or after an |
| 27 // accessibility event occurs which causes the tree to be populated), the | 37 // accessibility event occurs which causes the tree to be populated), the |
| 28 // callback can be called. | 38 // callback can be called. |
| 29 automationInternal.enableCurrentTab(function(rid) { | 39 automationInternal.enableCurrentTab(function(pid, rid) { |
| 30 var targetTree = routingIdToAutomationTree[rid]; | 40 var id = createAutomationTreeID(pid, rid); |
| 41 var targetTree = idToAutomationTree[id]; |
| 31 if (!targetTree) { | 42 if (!targetTree) { |
| 32 // If we haven't cached the tree, hold the callback until the tree is | 43 // If we haven't cached the tree, hold the callback until the tree is |
| 33 // populated by the initial onAccessibilityEvent call. | 44 // populated by the initial onAccessibilityEvent call. |
| 34 if (rid in routingIdToCallback) | 45 if (id in idToCallback) |
| 35 routingIdToCallback[rid].push(callback); | 46 idToCallback[id].push(callback); |
| 36 else | 47 else |
| 37 routingIdToCallback[rid] = [callback]; | 48 idToCallback[id] = [callback]; |
| 38 } else { | 49 } else { |
| 39 callback(targetTree); | 50 callback(targetTree); |
| 40 } | 51 } |
| 41 }); | 52 }); |
| 42 }); | 53 }); |
| 43 }); | 54 }); |
| 44 | 55 |
| 45 // Listen to the automationInternal.onaccessibilityEvent event, which is | 56 // Listen to the automationInternal.onaccessibilityEvent event, which is |
| 46 // essentially a proxy for the AccessibilityHostMsg_Events IPC from the | 57 // essentially a proxy for the AccessibilityHostMsg_Events IPC from the |
| 47 // renderer. | 58 // renderer. |
| 48 automationInternal.onAccessibilityEvent.addListener(function(data) { | 59 automationInternal.onAccessibilityEvent.addListener(function(data) { |
| 49 var rid = data.routing_id; | 60 var pid = data.processID; |
| 50 var targetTree = routingIdToAutomationTree[rid]; | 61 var rid = data.routingID; |
| 62 var id = createAutomationTreeID(pid, rid); |
| 63 var targetTree = idToAutomationTree[id]; |
| 51 if (!targetTree) { | 64 if (!targetTree) { |
| 52 // If this is the first time we've gotten data for this tree, it will | 65 // If this is the first time we've gotten data for this tree, it will |
| 53 // contain all of the tree's data, so create a new tree which will be | 66 // contain all of the tree's data, so create a new tree which will be |
| 54 // bootstrapped from |data|. | 67 // bootstrapped from |data|. |
| 55 targetTree = new AutomationTree(rid); | 68 targetTree = new AutomationTree(pid, rid); |
| 56 routingIdToAutomationTree[rid] = targetTree; | 69 idToAutomationTree[id] = targetTree; |
| 57 } | 70 } |
| 58 if (privates(targetTree).impl.update(data)) { | 71 privates(targetTree).impl.update(data); |
| 59 // TODO(aboxhall/dtseng): remove and replace with EventListener style API | |
| 60 targetTree.onUpdate.dispatch(); | |
| 61 } | |
| 62 | |
| 63 // TODO(aboxhall/dtseng): call appropriate event listeners based on | |
| 64 // data.event_type. | |
| 65 | 72 |
| 66 // If the tree wasn't available when getTree() was called, the callback will | 73 // If the tree wasn't available when getTree() was called, the callback will |
| 67 // have been cached in routingIdToCallback, so call and delete it now that we | 74 // have been cached in idToCallback, so call and delete it now that we |
| 68 // have the tree. | 75 // have the tree. |
| 69 if (rid in routingIdToCallback) { | 76 if (id in idToCallback) { |
| 70 for (var i = 0; i < routingIdToCallback[rid].length; i++) { | 77 for (var i = 0; i < idToCallback[id].length; i++) { |
| 71 var callback = routingIdToCallback[rid][i]; | 78 var callback = idToCallback[id][i]; |
| 72 callback(targetTree); | 79 callback(targetTree); |
| 73 } | 80 } |
| 74 delete routingIdToCallback[rid]; | 81 delete idToCallback[id]; |
| 75 } | 82 } |
| 76 }); | 83 }); |
| 77 | 84 |
| 78 exports.binding = automation.generate(); | 85 exports.binding = automation.generate(); |
| OLD | NEW |