- add sources.
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / backends / chrome / inspector_runtime.py
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.
4 from telemetry.core import exceptions
5
6 class InspectorRuntime(object):
7   def __init__(self, inspector_backend):
8     self._inspector_backend = inspector_backend
9     self._inspector_backend.RegisterDomain(
10         'Runtime',
11         self._OnNotification,
12         self._OnClose)
13
14   def _OnNotification(self, msg):
15     pass
16
17   def _OnClose(self):
18     pass
19
20   def Execute(self, expr, timeout=60):
21     """Executes expr in javascript. Does not return the result.
22
23     If the expression failed to evaluate, EvaluateException will be raised.
24     """
25     self.Evaluate(expr + '; 0;', timeout)
26
27   def Evaluate(self, expr, timeout=60):
28     """Evalutes expr in javascript and returns the JSONized result.
29
30     Consider using Execute for cases where the result of the expression is not
31     needed.
32
33     If evaluation throws in javascript, a python EvaluateException will
34     be raised.
35
36     If the result of the evaluation cannot be JSONized, then an
37     EvaluationException will be raised.
38     """
39     request = {
40       'method': 'Runtime.evaluate',
41       'params': {
42         'expression': expr,
43         'returnByValue': True
44         }
45       }
46     res = self._inspector_backend.SyncRequest(request, timeout)
47     if 'error' in res:
48       raise exceptions.EvaluateException(res['error']['message'])
49
50     if 'wasThrown' in res['result'] and res['result']['wasThrown']:
51       # TODO(nduca): propagate stacks from javascript up to the python
52       # exception.
53       raise exceptions.EvaluateException(res['result']['result']['description'])
54     if res['result']['result']['type'] == 'undefined':
55       return None
56     return res['result']['result']['value']