Chromium Code Reviews| 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 client in python. | 6 # A simple native client in python. |
| 7 # All this client does is echo the text it receives back at the extension. | 7 # All this client does is echo the text it receives back at the extension. |
| 8 | 8 |
| 9 import os | 9 import os |
| 10 import platform | |
| 10 import sys | 11 import sys |
| 11 import struct | 12 import struct |
| 12 | 13 |
| 13 def WriteMessage(message): | 14 def WriteMessage(message): |
| 14 try: | 15 try: |
| 15 sys.stdout.write(struct.pack("I", len(message))) | 16 sys.stdout.write(struct.pack("I", len(message))) |
| 16 sys.stdout.write(message) | 17 sys.stdout.write(message) |
| 17 sys.stdout.flush() | 18 sys.stdout.flush() |
| 18 return True | 19 return True |
| 19 except IOError: | 20 except IOError: |
| 20 return False | 21 return False |
| 21 | 22 |
| 22 def Main(): | 23 def Main(): |
| 23 message_number = 0 | 24 message_number = 0 |
| 24 | 25 |
| 26 caller_url = None; | |
|
miket_OOO
2014/01/07 17:15:11
Begone, semicolon!
Sergey Ulanov
2014/01/07 20:56:10
Done.
| |
| 27 parent_window = None | |
| 28 | |
| 25 if len(sys.argv) < 2: | 29 if len(sys.argv) < 2: |
| 26 sys.stderr.write("URL of the calling application is not specified.\n") | 30 sys.stderr.write("URL of the calling application is not specified.\n") |
| 27 return; | 31 return 1 |
| 28 for arg in sys.argv[1:]: | 32 for arg in sys.argv[1:]: |
| 29 if not arg.startswith('--'): | 33 if arg.startswith('--'): |
| 34 if arg.startswith('--parent-window='): | |
|
miket_OOO
2014/01/07 17:15:11
Not a big deal, but it makes me sad when I see man
Sergey Ulanov
2014/01/07 20:56:10
Added TODO. FWIW argparse is available only starti
| |
| 35 parent_window = long(arg[len('--parent-window='):]) | |
| 36 elif caller_url == None: | |
| 30 caller_url = arg | 37 caller_url = arg |
| 31 break | 38 |
| 39 if platform.system() == 'Windows': | |
| 40 import win32gui | |
| 41 if not win32gui.IsWindow(parent_window): | |
| 42 sys.stderr.write('Invalid --parent-window.\n') | |
| 43 return 1 | |
| 32 | 44 |
| 33 while 1: | 45 while 1: |
| 34 # Read the message type (first 4 bytes). | 46 # Read the message type (first 4 bytes). |
| 35 text_length_bytes = sys.stdin.read(4) | 47 text_length_bytes = sys.stdin.read(4) |
| 36 | 48 |
| 37 if len(text_length_bytes) == 0: | 49 if len(text_length_bytes) == 0: |
| 38 break | 50 break |
| 39 | 51 |
| 40 # Read the message length (4 bytes). | 52 # Read the message length (4 bytes). |
| 41 text_length = struct.unpack('i', text_length_bytes)[0] | 53 text_length = struct.unpack('i', text_length_bytes)[0] |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 56 if 'stopHostTest' in text: | 68 if 'stopHostTest' in text: |
| 57 # Using os.close() here because sys.stdin.close() doesn't really close | 69 # Using os.close() here because sys.stdin.close() doesn't really close |
| 58 # the pipe (it just marks it as closed, but doesn't close the file | 70 # the pipe (it just marks it as closed, but doesn't close the file |
| 59 # descriptor). | 71 # descriptor). |
| 60 os.close(sys.stdin.fileno()) | 72 os.close(sys.stdin.fileno()) |
| 61 WriteMessage('{"stopped": true }') | 73 WriteMessage('{"stopped": true }') |
| 62 sys.exit(0) | 74 sys.exit(0) |
| 63 | 75 |
| 64 message_number += 1 | 76 message_number += 1 |
| 65 | 77 |
| 66 if not WriteMessage( | 78 message = '{{"id": {0}, "echo": {1}, "caller_url": "{2}"}}'.format( |
| 67 '{{"id": {0}, "echo": {1}, "caller_url": "{2}"}}'.format( | 79 message_number, text, caller_url).encode('utf-8') |
| 68 message_number, text, caller_url).encode('utf-8')): | 80 if not WriteMessage(message): |
| 69 break | 81 break |
| 70 | 82 |
| 71 if __name__ == '__main__': | 83 if __name__ == '__main__': |
| 72 Main() | 84 sys.exit(Main()) |
| OLD | NEW |