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 |
| 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 |
| 32 # TODO(sergeyu): Use argparse module to parse the arguments (not available in |
| 33 # Python 2.6). |
28 for arg in sys.argv[1:]: | 34 for arg in sys.argv[1:]: |
29 if not arg.startswith('--'): | 35 if arg.startswith('--'): |
| 36 if arg.startswith('--parent-window='): |
| 37 parent_window = long(arg[len('--parent-window='):]) |
| 38 elif caller_url == None: |
30 caller_url = arg | 39 caller_url = arg |
31 break | 40 |
| 41 if platform.system() == 'Windows': |
| 42 import win32gui |
| 43 if parent_window and not win32gui.IsWindow(parent_window): |
| 44 sys.stderr.write('Invalid --parent-window.\n') |
| 45 return 1 |
32 | 46 |
33 while 1: | 47 while 1: |
34 # Read the message type (first 4 bytes). | 48 # Read the message type (first 4 bytes). |
35 text_length_bytes = sys.stdin.read(4) | 49 text_length_bytes = sys.stdin.read(4) |
36 | 50 |
37 if len(text_length_bytes) == 0: | 51 if len(text_length_bytes) == 0: |
38 break | 52 break |
39 | 53 |
40 # Read the message length (4 bytes). | 54 # Read the message length (4 bytes). |
41 text_length = struct.unpack('i', text_length_bytes)[0] | 55 text_length = struct.unpack('i', text_length_bytes)[0] |
(...skipping 14 matching lines...) Expand all Loading... |
56 if 'stopHostTest' in text: | 70 if 'stopHostTest' in text: |
57 # Using os.close() here because sys.stdin.close() doesn't really close | 71 # 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 | 72 # the pipe (it just marks it as closed, but doesn't close the file |
59 # descriptor). | 73 # descriptor). |
60 os.close(sys.stdin.fileno()) | 74 os.close(sys.stdin.fileno()) |
61 WriteMessage('{"stopped": true }') | 75 WriteMessage('{"stopped": true }') |
62 sys.exit(0) | 76 sys.exit(0) |
63 | 77 |
64 message_number += 1 | 78 message_number += 1 |
65 | 79 |
66 if not WriteMessage( | 80 message = '{{"id": {0}, "echo": {1}, "caller_url": "{2}"}}'.format( |
67 '{{"id": {0}, "echo": {1}, "caller_url": "{2}"}}'.format( | 81 message_number, text, caller_url).encode('utf-8') |
68 message_number, text, caller_url).encode('utf-8')): | 82 if not WriteMessage(message): |
69 break | 83 break |
70 | 84 |
71 if __name__ == '__main__': | 85 if __name__ == '__main__': |
72 Main() | 86 sys.exit(Main()) |
OLD | NEW |