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
3 // found in the LICENSE file.
5 // Custom bindings for the automation API.
6 var AutomationNode
= require('automationNode').AutomationNode
;
7 var AutomationRootNode
= require('automationNode').AutomationRootNode
;
8 var automation
= require('binding').Binding
.create('automation');
9 var automationInternal
=
10 require('binding').Binding
.create('automationInternal').generate();
11 var eventBindings
= require('event_bindings');
12 var Event
= eventBindings
.Event
;
13 var forEach
= require('utils').forEach
;
14 var lastError
= require('lastError');
15 var logging
= requireNative('logging');
16 var schema
= requireNative('automationInternal').GetSchemaAdditions();
19 * A namespace to export utility functions to other files in automation.
21 window
.automationUtil = function() {};
23 // TODO(aboxhall): Look into using WeakMap
24 var idToAutomationRootNode
= {};
25 var idToCallback
= {};
27 var DESKTOP_TREE_ID
= 0;
29 automationUtil
.storeTreeCallback = function(id
, callback
) {
33 var targetTree
= idToAutomationRootNode
[id
];
35 // If we haven't cached the tree, hold the callback until the tree is
36 // populated by the initial onAccessibilityEvent call.
37 if (id
in idToCallback
)
38 idToCallback
[id
].push(callback
);
40 idToCallback
[id
] = [callback
];
46 automation
.registerCustomHook(function(bindingsAPI
) {
47 var apiFunctions
= bindingsAPI
.apiFunctions
;
49 // TODO(aboxhall, dtseng): Make this return the speced AutomationRootNode obj.
50 apiFunctions
.setHandleRequest('getTree', function getTree(tabId
, callback
) {
51 // enableTab() ensures the renderer for the active or specified tab has
52 // accessibility enabled, and fetches its ax tree id to use as
53 // a key in the idToAutomationRootNode map. The callback to
54 // enableTab is bound to the callback passed in to getTree(), so that once
55 // the tree is available (either due to having been cached earlier, or after
56 // an accessibility event occurs which causes the tree to be populated), the
57 // callback can be called.
58 automationInternal
.enableTab(tabId
, function onEnable(id
) {
59 if (lastError
.hasError(chrome
)) {
63 automationUtil
.storeTreeCallback(id
, callback
);
67 var desktopTree
= null;
68 apiFunctions
.setHandleRequest('getDesktop', function(callback
) {
70 idToAutomationRootNode
[DESKTOP_TREE_ID
];
72 if (DESKTOP_TREE_ID
in idToCallback
)
73 idToCallback
[DESKTOP_TREE_ID
].push(callback
);
75 idToCallback
[DESKTOP_TREE_ID
] = [callback
];
77 // TODO(dtseng): Disable desktop tree once desktop object goes out of
79 automationInternal
.enableDesktop(function() {
80 if (lastError
.hasError(chrome
)) {
81 delete idToAutomationRootNode
[
88 callback(desktopTree
);
93 // Listen to the automationInternal.onAccessibilityEvent event, which is
94 // essentially a proxy for the AccessibilityHostMsg_Events IPC from the
96 automationInternal
.onAccessibilityEvent
.addListener(function(data
) {
98 var targetTree
= idToAutomationRootNode
[id
];
100 // If this is the first time we've gotten data for this tree, it will
101 // contain all of the tree's data, so create a new tree which will be
102 // bootstrapped from |data|.
103 targetTree
= new AutomationRootNode(id
);
104 idToAutomationRootNode
[id
] = targetTree
;
106 if (!privates(targetTree
).impl
.onAccessibilityEvent(data
))
109 // If we're not waiting on a callback to getTree(), we can early out here.
110 if (!(id
in idToCallback
))
113 // We usually get a 'placeholder' tree first, which doesn't have any url
114 // attribute or child nodes. If we've got that, wait for the full tree before
115 // calling the callback.
116 // TODO(dmazzoni): Don't send down placeholder (crbug.com/397553)
117 if (id
!= DESKTOP_TREE_ID
&& !targetTree
.attributes
.url
&&
118 targetTree
.children
.length
== 0) {
122 // If the tree wasn't available when getTree() was called, the callback will
123 // have been cached in idToCallback, so call and delete it now that we
124 // have the complete tree.
125 for (var i
= 0; i
< idToCallback
[id
].length
; i
++) {
126 console
.log('calling getTree() callback');
127 var callback
= idToCallback
[id
][i
];
128 callback(targetTree
);
130 delete idToCallback
[id
];
133 automationInternal
.onAccessibilityTreeDestroyed
.addListener(function(id
) {
134 var targetTree
= idToAutomationRootNode
[id
];
136 privates(targetTree
).impl
.destroy();
137 delete idToAutomationRootNode
[id
];
139 logging
.WARNING('no targetTree to destroy');
141 delete idToAutomationRootNode
[id
];
144 exports
.binding
= automation
.generate();
146 // Add additional accessibility bindings not specified in the automation IDL.
147 // Accessibility and automation share some APIs (see
148 // ui/accessibility/ax_enums.idl).
149 forEach(schema
, function(k
, v
) {
150 exports
.binding
[k
] = v
;