OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 import json | 4 import json |
5 import logging | 5 import logging |
6 import socket | 6 import socket |
7 | 7 |
8 from chrome_remote_control import websocket | 8 from chrome_remote_control import websocket |
9 | 9 |
10 class InspectorException(Exception): | 10 class InspectorException(Exception): |
(...skipping 27 matching lines...) Expand all Loading... |
38 logging.debug('got [%s]', data) | 38 logging.debug('got [%s]', data) |
39 if 'method' not in res: | 39 if 'method' not in res: |
40 return | 40 return |
41 | 41 |
42 mname = res['method'] | 42 mname = res['method'] |
43 dot_pos = mname.find('.') | 43 dot_pos = mname.find('.') |
44 domain_name = mname[:dot_pos] | 44 domain_name = mname[:dot_pos] |
45 if domain_name in self._domain_handlers: | 45 if domain_name in self._domain_handlers: |
46 try: | 46 try: |
47 self._domain_handlers[domain_name][0](res) | 47 self._domain_handlers[domain_name][0](res) |
48 except: | 48 except Exception: |
49 import traceback | 49 import traceback |
50 traceback.print_exc() | 50 traceback.print_exc() |
51 | 51 |
52 def SendAndIgnoreResponse(self, req): | 52 def SendAndIgnoreResponse(self, req): |
53 req['id'] = self._next_request_id | 53 req['id'] = self._next_request_id |
54 self._next_request_id += 1 | 54 self._next_request_id += 1 |
55 self._socket.send(json.dumps(req)) | 55 self._socket.send(json.dumps(req)) |
56 | 56 |
57 def SyncRequest(self, req, timeout=60): | 57 def SyncRequest(self, req, timeout=60): |
58 # TODO(nduca): Listen to the timeout argument | 58 # TODO(nduca): Listen to the timeout argument |
59 # pylint: disable=W0613 | 59 # pylint: disable=W0613 |
60 # self._socket.settimeout(timeout) | 60 # self._socket.settimeout(timeout) |
61 req['id'] = self._next_request_id | 61 req['id'] = self._next_request_id |
62 self._next_request_id += 1 | 62 self._next_request_id += 1 |
63 self._socket.send(json.dumps(req)) | 63 self._socket.send(json.dumps(req)) |
64 | 64 |
65 while True: | 65 while True: |
66 data = self._socket.recv() | 66 data = self._socket.recv() |
67 | 67 |
68 res = json.loads(data) | 68 res = json.loads(data) |
69 logging.debug('got [%s]', data) | 69 logging.debug('got [%s]', data) |
70 if 'method' in res: | 70 if 'method' in res: |
71 mname = res['method'] | 71 mname = res['method'] |
72 dot_pos = mname.find('.') | 72 dot_pos = mname.find('.') |
73 domain_name = mname[:dot_pos] | 73 domain_name = mname[:dot_pos] |
74 if domain_name in self._domain_handlers: | 74 if domain_name in self._domain_handlers: |
75 try: | 75 try: |
76 self._domain_handlers[domain_name][0](res) | 76 self._domain_handlers[domain_name][0](res) |
77 except: | 77 except Exception: |
78 import traceback | 78 import traceback |
79 traceback.print_exc() | 79 traceback.print_exc() |
80 else: | 80 else: |
81 logging.debug('Unhandled inspector mesage: %s', data) | 81 logging.debug('Unhandled inspector mesage: %s', data) |
82 continue | 82 continue |
83 | 83 |
84 if res['id'] != req['id']: | 84 if res['id'] != req['id']: |
85 logging.debug('Dropped reply: %s', json.dumps(res)) | 85 logging.debug('Dropped reply: %s', json.dumps(res)) |
86 continue | 86 continue |
87 return res | 87 return res |
88 | 88 |
89 def RegisterDomain(self, | 89 def RegisterDomain(self, |
90 domain_name, notification_handler, will_close_handler): | 90 domain_name, notification_handler, will_close_handler): |
91 """Registers a given domain for handling notification methods. | 91 """Registers a given domain for handling notification methods. |
92 | 92 |
93 For example, given inspector_backend: | 93 For example, given inspector_backend: |
94 def OnConsoleNotification(msg): | 94 def OnConsoleNotification(msg): |
95 if msg['method'] == 'Console.messageAdded': | 95 if msg['method'] == 'Console.messageAdded': |
96 print msg['params']['message'] | 96 print msg['params']['message'] |
97 return | 97 return |
98 def OnConsoleClose(self): | 98 def OnConsoleClose(self): |
99 pass | 99 pass |
100 inspector_backend.RegisterDomain('Console', | 100 inspector_backend.RegisterDomain('Console', |
101 OnConsoleNotification, OnConsoleClose) | 101 OnConsoleNotification, OnConsoleClose) |
102 """ | 102 """ |
103 assert domain_name not in self._domain_handlers | 103 assert domain_name not in self._domain_handlers |
104 self._domain_handlers[domain_name] = (notification_handler, | 104 self._domain_handlers[domain_name] = (notification_handler, |
105 will_close_handler) | 105 will_close_handler) |
106 | 106 |
OLD | NEW |