| 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 # A simple native messaging host. Shows a Tkinter dialog with incoming messages | 6 # A simple native messaging host. Shows a Tkinter dialog with incoming messages |
| 7 # that also allows to send message back to the webapp. | 7 # that also allows to send message back to the webapp. |
| 8 | 8 |
| 9 import struct | 9 import struct |
| 10 import sys | 10 import sys |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 # Write message size. | 22 # Write message size. |
| 23 sys.stdout.write(struct.pack('I', len(message))) | 23 sys.stdout.write(struct.pack('I', len(message))) |
| 24 # Write the message itself. | 24 # Write the message itself. |
| 25 sys.stdout.write(message) | 25 sys.stdout.write(message) |
| 26 sys.stdout.flush() | 26 sys.stdout.flush() |
| 27 | 27 |
| 28 # Thread that reads messages from the webapp. | 28 # Thread that reads messages from the webapp. |
| 29 def read_thread_func(queue): | 29 def read_thread_func(queue): |
| 30 message_number = 0 | 30 message_number = 0 |
| 31 while 1: | 31 while 1: |
| 32 # Read the message type (first 4 bytes). | 32 # Read the message length (first 4 bytes). |
| 33 text_length_bytes = sys.stdin.read(4) | 33 text_length_bytes = sys.stdin.read(4) |
| 34 | 34 |
| 35 if len(text_length_bytes) == 0: | 35 if len(text_length_bytes) == 0: |
| 36 if queue: | 36 if queue: |
| 37 queue.put(None) | 37 queue.put(None) |
| 38 sys.exit(0) | 38 sys.exit(0) |
| 39 | 39 |
| 40 # Read the message length (4 bytes). | 40 # Unpack message length as 4 byte integer. |
| 41 text_length = struct.unpack('i', text_length_bytes)[0] | 41 text_length = struct.unpack('i', text_length_bytes)[0] |
| 42 | 42 |
| 43 # Read the text (JSON object) of the message. | 43 # Read the text (JSON object) of the message. |
| 44 text = sys.stdin.read(text_length).decode('utf-8') | 44 text = sys.stdin.read(text_length).decode('utf-8') |
| 45 | 45 |
| 46 if queue: | 46 if queue: |
| 47 queue.put(text) | 47 queue.put(text) |
| 48 else: | 48 else: |
| 49 # In headless mode just send an echo message back. | 49 # In headless mode just send an echo message back. |
| 50 send_message('{"echo": %s}' % text) | 50 send_message('{"echo": %s}' % text) |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 thread.daemon = True | 112 thread.daemon = True |
| 113 thread.start() | 113 thread.start() |
| 114 | 114 |
| 115 main_window.mainloop() | 115 main_window.mainloop() |
| 116 | 116 |
| 117 sys.exit(0) | 117 sys.exit(0) |
| 118 | 118 |
| 119 | 119 |
| 120 if __name__ == '__main__': | 120 if __name__ == '__main__': |
| 121 Main() | 121 Main() |
| OLD | NEW |