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