Chromium Code Reviews| Index: chrome/renderer/resources/extensions/automation_custom_bindings.js |
| diff --git a/chrome/renderer/resources/extensions/automation_custom_bindings.js b/chrome/renderer/resources/extensions/automation_custom_bindings.js |
| index 7a7878dcd1de51510d7e756abb7627c28ff3ec52..8115d8bf91c8c1371b519e415dc122743fa20876 100644 |
| --- a/chrome/renderer/resources/extensions/automation_custom_bindings.js |
| +++ b/chrome/renderer/resources/extensions/automation_custom_bindings.js |
| @@ -15,21 +15,40 @@ var lastError = require('lastError'); |
| var logging = requireNative('logging'); |
| var schema = requireNative('automationInternal').GetSchemaAdditions(); |
| +/** |
| + * A namespace to export utility functions to other files in automation. |
| + */ |
| +window.automationUtil = function() {}; |
|
aboxhall
2014/10/29 19:29:11
Perhaps this should be in another file which is in
David Tseng
2014/10/29 20:35:30
Yup that was it :).
|
| + |
| // TODO(aboxhall): Look into using WeakMap |
| -var idToAutomationRootNode = {}; |
| +automationUtil.idToAutomationRootNode = {}; |
| var idToCallback = {}; |
| -// TODO(dtseng): Move out to automation/automation_util.js or as a static member |
| -// of AutomationRootNode to keep this file clean. |
| -/* |
| +/** |
| * Creates an id associated with a particular AutomationRootNode based upon a |
| * renderer/renderer host pair's process and routing id. |
| */ |
| -var createAutomationRootNodeID = function(pid, rid) { |
| +automationUtil.createAutomationRootNodeID = function(pid, rid) { |
| return pid + '_' + rid; |
| }; |
| -var DESKTOP_TREE_ID = createAutomationRootNodeID(0, 0); |
| +automationUtil.DESKTOP_TREE_ID = |
| + automationUtil.createAutomationRootNodeID(0, 0); |
| + |
| +automationUtil.onTreeLoaded = function(pid, rid, callback) { |
|
aboxhall
2014/10/29 19:29:11
I think this is a misnomer: the tree hasn't loaded
David Tseng
2014/10/29 20:35:30
Done.
|
| + var id = automationUtil.createAutomationRootNodeID(pid, rid); |
| + var targetTree = automationUtil.idToAutomationRootNode[id]; |
| + if (!targetTree) { |
| + // If we haven't cached the tree, hold the callback until the tree is |
| + // populated by the initial onAccessibilityEvent call. |
| + if (id in idToCallback) |
| + idToCallback[id].push(callback); |
| + else |
| + idToCallback[id] = [callback]; |
| + } else { |
| + callback(targetTree); |
| + } |
| +}; |
| automation.registerCustomHook(function(bindingsAPI) { |
| var apiFunctions = bindingsAPI.apiFunctions; |
| @@ -38,45 +57,36 @@ automation.registerCustomHook(function(bindingsAPI) { |
| apiFunctions.setHandleRequest('getTree', function getTree(tabId, callback) { |
| // enableTab() ensures the renderer for the active or specified tab has |
| // accessibility enabled, and fetches its process and routing ids to use as |
| - // a key in the idToAutomationRootNode map. The callback to enableTab is is |
| - // bound to the callback passed in to getTree(), so that once the tree is |
| - // available (either due to having been cached earlier, or after an |
| - // accessibility event occurs which causes the tree to be populated), the |
| + // a key in the automationUtil.idToAutomationRootNode map. The callback to |
| + // enableTab is bound to the callback passed in to getTree(), so that once |
| + // the tree is available (either due to having been cached earlier, or after |
| + // an accessibility event occurs which causes the tree to be populated), the |
| // callback can be called. |
| automationInternal.enableTab(tabId, function onEnable(pid, rid) { |
| if (lastError.hasError(chrome)) { |
| callback(); |
| return; |
| } |
| - var id = createAutomationRootNodeID(pid, rid); |
| - var targetTree = idToAutomationRootNode[id]; |
| - if (!targetTree) { |
| - // If we haven't cached the tree, hold the callback until the tree is |
| - // populated by the initial onAccessibilityEvent call. |
| - if (id in idToCallback) |
| - idToCallback[id].push(callback); |
| - else |
| - idToCallback[id] = [callback]; |
| - } else { |
| - callback(targetTree); |
| - } |
| + automationUtil.onTreeLoaded(pid, rid, callback); |
| }); |
| }); |
| var desktopTree = null; |
| apiFunctions.setHandleRequest('getDesktop', function(callback) { |
| - desktopTree = idToAutomationRootNode[DESKTOP_TREE_ID]; |
| + desktopTree = |
| + automationUtil.idToAutomationRootNode[automationUtil.DESKTOP_TREE_ID]; |
| if (!desktopTree) { |
| - if (DESKTOP_TREE_ID in idToCallback) |
| - idToCallback[DESKTOP_TREE_ID].push(callback); |
| + if (automationUtil.DESKTOP_TREE_ID in idToCallback) |
| + idToCallback[automationUtil.DESKTOP_TREE_ID].push(callback); |
| else |
| - idToCallback[DESKTOP_TREE_ID] = [callback]; |
| + idToCallback[automationUtil.DESKTOP_TREE_ID] = [callback]; |
| // TODO(dtseng): Disable desktop tree once desktop object goes out of |
| // scope. |
| automationInternal.enableDesktop(function() { |
| if (lastError.hasError(chrome)) { |
| - delete idToAutomationRootNode[DESKTOP_TREE_ID]; |
| + delete automationUtil.idToAutomationRootNode[ |
| + automationUtil.DESKTOP_TREE_ID]; |
| callback(); |
| return; |
| } |
| @@ -93,14 +103,14 @@ automation.registerCustomHook(function(bindingsAPI) { |
| automationInternal.onAccessibilityEvent.addListener(function(data) { |
| var pid = data.processID; |
| var rid = data.routingID; |
| - var id = createAutomationRootNodeID(pid, rid); |
| - var targetTree = idToAutomationRootNode[id]; |
| + var id = automationUtil.createAutomationRootNodeID(pid, rid); |
| + var targetTree = automationUtil.idToAutomationRootNode[id]; |
| if (!targetTree) { |
| // If this is the first time we've gotten data for this tree, it will |
| // contain all of the tree's data, so create a new tree which will be |
| // bootstrapped from |data|. |
| targetTree = new AutomationRootNode(pid, rid); |
| - idToAutomationRootNode[id] = targetTree; |
| + automationUtil.idToAutomationRootNode[id] = targetTree; |
| } |
| if (!privates(targetTree).impl.onAccessibilityEvent(data)) |
| return; |
| @@ -113,7 +123,7 @@ automationInternal.onAccessibilityEvent.addListener(function(data) { |
| // attribute or child nodes. If we've got that, wait for the full tree before |
| // calling the callback. |
| // TODO(dmazzoni): Don't send down placeholder (crbug.com/397553) |
| - if (id != DESKTOP_TREE_ID && !targetTree.attributes.url && |
| + if (id != automationUtil.DESKTOP_TREE_ID && !targetTree.attributes.url && |
| targetTree.children.length == 0) { |
| return; |
| } |
| @@ -130,15 +140,15 @@ automationInternal.onAccessibilityEvent.addListener(function(data) { |
| }); |
| automationInternal.onAccessibilityTreeDestroyed.addListener(function(pid, rid) { |
| - var id = createAutomationRootNodeID(pid, rid); |
| - var targetTree = idToAutomationRootNode[id]; |
| + var id = automationUtil.createAutomationRootNodeID(pid, rid); |
| + var targetTree = automationUtil.idToAutomationRootNode[id]; |
| if (targetTree) { |
| privates(targetTree).impl.destroy(); |
| - delete idToAutomationRootNode[id]; |
| + delete automationUtil.idToAutomationRootNode[id]; |
| } else { |
| logging.WARNING('no targetTree to destroy'); |
| } |
| - delete idToAutomationRootNode[id]; |
| + delete automationUtil.idToAutomationRootNode[id]; |
| }); |
| exports.binding = automation.generate(); |