OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 /** |
| 6 * @fileoverview Defines various strategies for recovering automation nodes. |
| 7 */ |
| 8 |
| 9 goog.provide('AncestryRecoveryStrategy'); |
| 10 goog.provide('RecoveryStrategy'); |
| 11 goog.provide('TreePathRecoveryStrategy'); |
| 12 |
| 13 goog.scope(function() { |
| 14 var AutomationNode = chrome.automation.AutomationNode; |
| 15 var RoleType = chrome.automation.RoleType; |
| 16 |
| 17 /** |
| 18 * @param {!AutomationNode} node |
| 19 * @constructor |
| 20 */ |
| 21 RecoveryStrategy = function(node) { |
| 22 /** @private {!AutomationNode} */ |
| 23 this.node_ = node; |
| 24 }; |
| 25 |
| 26 RecoveryStrategy.prototype = { |
| 27 /** @return {!AutomationNode} */ |
| 28 get node() { |
| 29 if (this.requiresRecovery()) |
| 30 this.node_ = this.recover() || this.node_; |
| 31 |
| 32 return this.node_; |
| 33 }, |
| 34 |
| 35 /** @return {boolean} */ |
| 36 requiresRecovery: function() { |
| 37 return !this.node_ || !this.node_.role; |
| 38 }, |
| 39 |
| 40 /** |
| 41 * @return {AutomationNode} |
| 42 * @protected |
| 43 */ |
| 44 recover: function() { |
| 45 return null; |
| 46 } |
| 47 }; |
| 48 |
| 49 /** |
| 50 * A recovery strategy that uses the node's ancestors. |
| 51 * @constructor |
| 52 * @extends {RecoveryStrategy} |
| 53 */ |
| 54 AncestryRecoveryStrategy = function(node) { |
| 55 RecoveryStrategy.call(this, node); |
| 56 |
| 57 /** @type {!Array<AutomationNode>} @private */ |
| 58 this.ancestry_ = []; |
| 59 var nodeWalker = node; |
| 60 while (nodeWalker) { |
| 61 this.ancestry_.push(nodeWalker); |
| 62 nodeWalker = nodeWalker.parent; |
| 63 if (nodeWalker && nodeWalker.role == RoleType.WINDOW) |
| 64 break; |
| 65 } |
| 66 }; |
| 67 |
| 68 AncestryRecoveryStrategy.prototype = { |
| 69 __proto__: RecoveryStrategy.prototype, |
| 70 |
| 71 /** @override */ |
| 72 recover: function() { |
| 73 return this.ancestry_[this.getFirstValidNodeIndex_()]; |
| 74 }, |
| 75 |
| 76 /** |
| 77 * @return {number} |
| 78 * @protected |
| 79 */ |
| 80 getFirstValidNodeIndex_: function() { |
| 81 for (var i = 0; i < this.ancestry_.length; i++) { |
| 82 var firstValidNode = this.ancestry_[i]; |
| 83 if (firstValidNode != null && firstValidNode.role !== undefined && |
| 84 firstValidNode.root != undefined) { |
| 85 return i; |
| 86 } |
| 87 } |
| 88 return 0; |
| 89 } |
| 90 }; |
| 91 |
| 92 /** |
| 93 * A recovery strategy that uses the node's tree path. |
| 94 * @constructor |
| 95 * @extends {AncestryRecoveryStrategy} |
| 96 */ |
| 97 TreePathRecoveryStrategy = function(node) { |
| 98 AncestryRecoveryStrategy.call(this, node); |
| 99 |
| 100 /** @type {!Array<number>} @private */ |
| 101 this.recoveryChildIndex_ = []; |
| 102 var nodeWalker = node; |
| 103 while (nodeWalker) { |
| 104 this.recoveryChildIndex_.push(nodeWalker.indexInParent); |
| 105 nodeWalker = nodeWalker.parent; |
| 106 if (nodeWalker && nodeWalker.role == RoleType.WINDOW) |
| 107 break; |
| 108 } |
| 109 }; |
| 110 |
| 111 TreePathRecoveryStrategy.prototype = { |
| 112 __proto__: AncestryRecoveryStrategy.prototype, |
| 113 |
| 114 /** @override */ |
| 115 recover: function() { |
| 116 var index = this.getFirstValidNodeIndex_(); |
| 117 if (index == 0) |
| 118 return this.ancestry_[index]; |
| 119 |
| 120 // Otherwise, attempt to recover. |
| 121 var node = this.ancestry_[index]; |
| 122 for (var j = index - 1; j >= 0; j--) { |
| 123 var childIndex = this.recoveryChildIndex_[j]; |
| 124 var children = node.children; |
| 125 if (!children[childIndex]) |
| 126 return node; |
| 127 node = children[childIndex]; |
| 128 } |
| 129 return node; |
| 130 } |
| 131 }; |
| 132 |
| 133 }); // goog.scope |
OLD | NEW |