1/*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {Operation} from 'trace/tree_node/operations/operation';
18import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
19import {DEFAULT_PROPERTY_TREE_NODE_FACTORY} from 'trace/tree_node/property_tree_node_factory';
20
21export class UpdateAbortTimeNodes implements Operation<PropertyTreeNode> {
22  apply(value: PropertyTreeNode) {
23    const wmDataNode = value.getChildByName('wmData');
24    const shellDataNode = value.getChildByName('shellData');
25
26    const existingWmAbortTime = wmDataNode?.getChildByName('wmAbortTimeNs');
27    if (wmDataNode && existingWmAbortTime) {
28      const newAbortTimeNode = this.makeNewAbortTimeNode(
29        wmDataNode.id,
30        existingWmAbortTime,
31      );
32      wmDataNode.addOrReplaceChild(newAbortTimeNode);
33      wmDataNode.removeChild(existingWmAbortTime.id);
34    }
35
36    const existingShellAbortTime =
37      shellDataNode?.getChildByName('shellAbortTimeNs');
38    if (shellDataNode && existingShellAbortTime) {
39      const newAbortTimeNode = this.makeNewAbortTimeNode(
40        shellDataNode.id,
41        existingShellAbortTime,
42      );
43      shellDataNode.addOrReplaceChild(newAbortTimeNode);
44      shellDataNode.removeChild(existingShellAbortTime.id);
45    }
46  }
47
48  makeNewAbortTimeNode(rootId: string, existingNode: PropertyTreeNode) {
49    return DEFAULT_PROPERTY_TREE_NODE_FACTORY.makeProtoProperty(
50      rootId,
51      'abortTimeNs',
52      existingNode.getValue(),
53    );
54  }
55}
56