1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2024 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17Description: Python Runtime Domain Interfaces 18""" 19 20import json 21 22from aw import communicate_with_debugger_server 23from aw.cdp import runtime 24from aw.types import ProtocolType 25from aw.api.protocol_api import ProtocolImpl 26 27 28class RuntimeImpl(ProtocolImpl): 29 30 def __init__(self, id_generator, websocket): 31 super().__init__(id_generator, websocket) 32 self.dispatch_table = {"enable": (self.enable, ProtocolType.send), 33 "runIfWaitingForDebugger": (self.run_if_waiting_for_debugger, ProtocolType.send), 34 "getProperties": (self.get_properties, ProtocolType.send), 35 "getHeapUsage": (self.get_heap_usage, ProtocolType.send)} 36 37 async def enable(self, message_id, connection, params): 38 response = await communicate_with_debugger_server(connection.instance_id, 39 connection.send_msg_queue, 40 connection.received_msg_queue, 41 runtime.enable(), message_id) 42 response = json.loads(response) 43 assert response == {"id": message_id, "result": {"protocols": []}} 44 return response 45 46 async def run_if_waiting_for_debugger(self, message_id, connection, params): 47 response = await communicate_with_debugger_server(connection.instance_id, 48 connection.send_msg_queue, 49 connection.received_msg_queue, 50 runtime.run_if_waiting_for_debugger(), message_id) 51 response = json.loads(response) 52 assert response == {"id": message_id, "result": {}} 53 return response 54 55 async def get_properties(self, message_id, connection, params): 56 response = await communicate_with_debugger_server(connection.instance_id, 57 connection.send_msg_queue, 58 connection.received_msg_queue, 59 runtime.get_properties(params), message_id) 60 response = json.loads(response) 61 assert response["id"] == message_id 62 return response 63 64 async def get_heap_usage(self, message_id, connection, params): 65 response = await communicate_with_debugger_server(connection.instance_id, 66 connection.send_msg_queue, 67 connection.received_msg_queue, 68 runtime.get_heap_usage(), message_id) 69 while response.startswith('{"method":"HeapProfiler.lastSeenObjectId"') or \ 70 response.startswith('{"method":"HeapProfiler.heapStatsUpdate"'): 71 response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, 72 connection.received_msg_queue) 73 response = json.loads(response) 74 assert response["id"] == message_id 75 assert response['result']['usedSize'] > 0 76 assert response['result']['totalSize'] > 0 77 return response