1# Copyright 2013 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. 4import json 5 6from telemetry.core import exceptions 7 8 9class InspectorMemoryException(exceptions.Error): 10 pass 11 12 13class InspectorMemory(object): 14 """Communicates with the remote inspector's Memory domain.""" 15 16 def __init__(self, inspector_websocket): 17 self._inspector_websocket = inspector_websocket 18 self._inspector_websocket.RegisterDomain('Memory', self._OnNotification) 19 20 def _OnNotification(self, msg): 21 pass 22 23 def GetDOMCounters(self, timeout): 24 """Retrieves DOM element counts. 25 26 Args: 27 timeout: The number of seconds to wait for the inspector backend to 28 service the request before timing out. 29 30 Returns: 31 A dictionary containing the counts associated with "nodes", "documents", 32 and "jsEventListeners". 33 Raises: 34 InspectorMemoryException 35 websocket.WebSocketException 36 socket.error 37 exceptions.WebSocketDisconnected 38 """ 39 res = self._inspector_websocket.SyncRequest({ 40 'method': 'Memory.getDOMCounters' 41 }, timeout) 42 if ('result' not in res or 43 'nodes' not in res['result'] or 44 'documents' not in res['result'] or 45 'jsEventListeners' not in res['result']): 46 raise InspectorMemoryException( 47 'Inspector returned unexpected result for Memory.getDOMCounters:\n' + 48 json.dumps(res, indent=2)) 49 return { 50 'nodes': res['result']['nodes'], 51 'documents': res['result']['documents'], 52 'jsEventListeners': res['result']['jsEventListeners'] 53 } 54