OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Chrome remote inspector utility for pyauto tests. | 6 """Chrome remote inspector utility for pyauto tests. |
7 | 7 |
8 This script provides a python interface that acts as a front-end for Chrome's | 8 This script provides a python interface that acts as a front-end for Chrome's |
9 remote inspector module, communicating via sockets to interact with Chrome in | 9 remote inspector module, communicating via sockets to interact with Chrome in |
10 the same way that the Developer Tools does. This -- in theory -- should allow | 10 the same way that the Developer Tools does. This -- in theory -- should allow |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 self._requests.append( | 354 self._requests.append( |
355 _DevToolsSocketRequest(message[0], message[1], message_id)) | 355 _DevToolsSocketRequest(message[0], message[1], message_id)) |
356 | 356 |
357 # Send out each request. Wait until each request is complete before | 357 # Send out each request. Wait until each request is complete before |
358 # sending the next request. | 358 # sending the next request. |
359 for request in self._requests: | 359 for request in self._requests: |
360 self._FillInParams(request) | 360 self._FillInParams(request) |
361 self._client.SendMessage(str(request)) | 361 self._client.SendMessage(str(request)) |
362 | 362 |
363 request.is_fulfilled_condition.acquire() | 363 request.is_fulfilled_condition.acquire() |
364 self._condition_to_wait = request.is_fulfilled | 364 self._condition_to_wait = request.is_fulfilled_condition |
365 request.is_fulfilled_condition.wait() | 365 request.is_fulfilled_condition.wait() |
366 request.is_fulfilled_condition.release() | 366 request.is_fulfilled_condition.release() |
367 | 367 |
368 if self._killed: | 368 if self._killed: |
369 self._client.close() | 369 self._client.close() |
370 return | 370 return |
371 | 371 |
372 # Clean up so things are ready for the next request. | 372 # Clean up so things are ready for the next request. |
373 self._requests = [] | 373 self._requests = [] |
374 | 374 |
(...skipping 600 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
975 """ | 975 """ |
976 if self._timeline_started: | 976 if self._timeline_started: |
977 self._logger.warning('Timeline monitoring already started.') | 977 self._logger.warning('Timeline monitoring already started.') |
978 return | 978 return |
979 TIMELINE_MESSAGES = [ | 979 TIMELINE_MESSAGES = [ |
980 ('Timeline.start', {}) | 980 ('Timeline.start', {}) |
981 ] | 981 ] |
982 | 982 |
983 self._event_callback = event_callback | 983 self._event_callback = event_callback |
984 | 984 |
| 985 done_condition = threading.Condition() |
985 def HandleReply(reply_dict): | 986 def HandleReply(reply_dict): |
986 """Processes a reply message received from the remote Chrome instance. | 987 """Processes a reply message received from the remote Chrome instance. |
987 | 988 |
988 Args: | 989 Args: |
989 reply_dict: A dictionary object representing the reply message received | 990 reply_dict: A dictionary object representing the reply message received |
990 from the remote Chrome instance. | 991 from the remote Chrome instance. |
991 """ | 992 """ |
| 993 if 'result' in reply_dict: |
| 994 done_condition.acquire() |
| 995 done_condition.notify() |
| 996 done_condition.release() |
992 if reply_dict.get('method') == 'Timeline.eventRecorded': | 997 if reply_dict.get('method') == 'Timeline.eventRecorded': |
993 self._event_callback(reply_dict['params']['record']) | 998 self._event_callback(reply_dict['params']['record']) |
994 | 999 |
995 # Tell the remote inspector to start the timeline. We can return | 1000 # Tell the remote inspector to start the timeline. |
996 # immediately, since there is no result for which to wait. | |
997 self._timeline_callback = HandleReply | 1001 self._timeline_callback = HandleReply |
998 self._remote_inspector_thread.AddMessageCallback(self._timeline_callback) | 1002 self._remote_inspector_thread.AddMessageCallback(self._timeline_callback) |
999 self._remote_inspector_thread.PerformAction(TIMELINE_MESSAGES, None) | 1003 self._remote_inspector_thread.PerformAction(TIMELINE_MESSAGES, None) |
| 1004 |
| 1005 done_condition.acquire() |
| 1006 done_condition.wait() |
| 1007 done_condition.release() |
| 1008 |
1000 self._timeline_started = True | 1009 self._timeline_started = True |
1001 | 1010 |
1002 def StopTimelineEventMonitoring(self): | 1011 def StopTimelineEventMonitoring(self): |
1003 """Stops timeline event monitoring.""" | 1012 """Stops timeline event monitoring.""" |
1004 if not self._timeline_started: | 1013 if not self._timeline_started: |
1005 self._logger.warning('Timeline monitoring already stopped.') | 1014 self._logger.warning('Timeline monitoring already stopped.') |
1006 return | 1015 return |
1007 TIMELINE_MESSAGES = [ | 1016 TIMELINE_MESSAGES = [ |
1008 ('Timeline.stop', {}) | 1017 ('Timeline.stop', {}) |
1009 ] | 1018 ] |
1010 | 1019 |
1011 # Tell the remote inspector to stop the timeline. We can return | 1020 done_condition = threading.Condition() |
1012 # immediately, since there is no result for which to wait. | 1021 def HandleReply(reply_dict): |
| 1022 """Processes a reply message received from the remote Chrome instance. |
| 1023 |
| 1024 Args: |
| 1025 reply_dict: A dictionary object representing the reply message received |
| 1026 from the remote Chrome instance. |
| 1027 """ |
| 1028 if 'result' in reply_dict: |
| 1029 done_condition.acquire() |
| 1030 done_condition.notify() |
| 1031 done_condition.release() |
| 1032 |
| 1033 # Tell the remote inspector to stop the timeline. |
1013 self._remote_inspector_thread.RemoveMessageCallback(self._timeline_callback) | 1034 self._remote_inspector_thread.RemoveMessageCallback(self._timeline_callback) |
1014 self._remote_inspector_thread.PerformAction(TIMELINE_MESSAGES, None) | 1035 self._remote_inspector_thread.PerformAction(TIMELINE_MESSAGES, HandleReply) |
| 1036 |
| 1037 done_condition.acquire() |
| 1038 done_condition.wait() |
| 1039 done_condition.release() |
| 1040 |
1015 self._timeline_started = False | 1041 self._timeline_started = False |
1016 | 1042 |
1017 def _ConvertByteCountToHumanReadableString(self, num_bytes): | 1043 def _ConvertByteCountToHumanReadableString(self, num_bytes): |
1018 """Converts an integer number of bytes into a human-readable string. | 1044 """Converts an integer number of bytes into a human-readable string. |
1019 | 1045 |
1020 Args: | 1046 Args: |
1021 num_bytes: An integer number of bytes. | 1047 num_bytes: An integer number of bytes. |
1022 | 1048 |
1023 Returns: | 1049 Returns: |
1024 A human-readable string representation of the given number of bytes. | 1050 A human-readable string representation of the given number of bytes. |
1025 """ | 1051 """ |
1026 if num_bytes < 1024: | 1052 if num_bytes < 1024: |
1027 return '%d B' % num_bytes | 1053 return '%d B' % num_bytes |
1028 elif num_bytes < 1048576: | 1054 elif num_bytes < 1048576: |
1029 return '%.2f KB' % (num_bytes / 1024.0) | 1055 return '%.2f KB' % (num_bytes / 1024.0) |
1030 else: | 1056 else: |
1031 return '%.2f MB' % (num_bytes / 1048576.0) | 1057 return '%.2f MB' % (num_bytes / 1048576.0) |
OLD | NEW |