summaryrefslogtreecommitdiffstats
path: root/chrome/test/functional/tracing/timeline_model.py
blob: 68573093a0760b696d249cdc3d626da17e122d3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import json
import os


class TimelineModel(object):
  """A proxy for about:tracing's TimelineModel class.

  Test authors should never need to know that this class is a proxy.
  """
  @staticmethod
  def _EscapeForQuotedJavascriptExecution(js):
      # Poor man's string escape.
      return js.replace('\'', '\\\'');

  def __init__(self, js_executor, shim_id):
    self._js_executor = js_executor
    self._shim_id = shim_id

  # Warning: The JSON serialization process removes cyclic references.
  # TODO(eatnumber): regenerate these cyclic references on deserialization.
  def _CallModelMethod(self, method_name, *args):
    result = self._js_executor(
        """window.timelineModelShims['%s'].invokeMethod('%s', '%s')""" % (
            self._shim_id,
            self._EscapeForQuotedJavascriptExecution(method_name),
            self._EscapeForQuotedJavascriptExecution(json.dumps(args))
        )
    )
    if result['success']:
      return result['data']
    # TODO(eatnumber): Make these exceptions more reader friendly.
    raise RuntimeError(result)

  def __del__(self):
    self._js_executor("""
        window.timelineModelShims['%s'] = undefined;
        window.domAutomationController.send('');
    """ % self._shim_id)

  def GetAllThreads(self):
    return self._CallModelMethod('getAllThreads')

  def GetAllCpus(self):
    return self._CallModelMethod('getAllCpus')

  def GetAllProcesses(self):
    return self._CallModelMethod('getAllProcesses')

  def GetAllCounters(self):
    return self._CallModelMethod('getAllCounters')

  def FindAllThreadsNamed(self, name):
    return self._CallModelMethod('findAllThreadsNamed', name);