1 # Copyright (c) 2012 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. 4 5 import json 6 import os 7 8 9 class TimelineModel(object): 10 """A proxy for about:tracing's TimelineModel class. 11 12 Test authors should never need to know that this class is a proxy. 13 """ 14 @staticmethod 15 def _EscapeForQuotedJavascriptExecution(js): 16 # Poor man's string escape. 17 return js.replace('\'', '\\\''); 18 19 def __init__(self, js_executor, shim_id): 20 self._js_executor = js_executor 21 self._shim_id = shim_id 22 23 # Warning: The JSON serialization process removes cyclic references. 24 # TODO(eatnumber): regenerate these cyclic references on deserialization. 25 def _CallModelMethod(self, method_name, *args): 26 result = self._js_executor( 27 """window.timelineModelShims['%s'].invokeMethod('%s', '%s')""" % ( 28 self._shim_id, 29 self._EscapeForQuotedJavascriptExecution(method_name), 30 self._EscapeForQuotedJavascriptExecution(json.dumps(args)) 31 ) 32 ) 33 if result['success']: 34 return result['data'] 35 # TODO(eatnumber): Make these exceptions more reader friendly. 36 raise RuntimeError(result) 37 38 def __del__(self): 39 self._js_executor(""" 40 window.timelineModelShims['%s'] = undefined; 41 window.domAutomationController.send(''); 42 """ % self._shim_id) 43 44 def GetAllThreads(self): 45 return self._CallModelMethod('getAllThreads') 46 47 def GetAllCpus(self): 48 return self._CallModelMethod('getAllCpus') 49 50 def GetAllProcesses(self): 51 return self._CallModelMethod('getAllProcesses') 52 53 def GetAllCounters(self): 54 return self._CallModelMethod('getAllCounters') 55 56 def FindAllThreadsNamed(self, name): 57 return self._CallModelMethod('findAllThreadsNamed', name); 58