| 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 platform |
| 11 import sys | 11 import sys |
| 12 import struct | 12 import struct |
| 13 | 13 |
| 14 def WriteMessage(message): | 14 def WriteMessage(message): |
| 15 try: | 15 try: |
| 16 sys.stdout.write(struct.pack("I", len(message))) | 16 sys.stdout.write(struct.pack("I", len(message))) |
| 17 sys.stdout.write(message) | 17 sys.stdout.write(message) |
| 18 sys.stdout.flush() | 18 sys.stdout.flush() |
| 19 return True | 19 return True |
| 20 except IOError: | 20 except IOError: |
| 21 return False | 21 return False |
| 22 | 22 |
| 23 def Main(): | 23 def Main(): |
| 24 message_number = 0 | 24 message_number = 0 |
| 25 | 25 |
| 26 caller_url = None | |
| 27 parent_window = None | 26 parent_window = None |
| 28 | 27 |
| 29 if len(sys.argv) < 2: | 28 if len(sys.argv) < 2: |
| 30 sys.stderr.write("URL of the calling application is not specified.\n") | 29 sys.stderr.write("URL of the calling application is not specified.\n") |
| 31 return 1 | 30 return 1 |
| 31 caller_url = sys.argv[1] |
| 32 |
| 32 # TODO(sergeyu): Use argparse module to parse the arguments (not available in | 33 # TODO(sergeyu): Use argparse module to parse the arguments (not available in |
| 33 # Python 2.6). | 34 # Python 2.6). |
| 34 for arg in sys.argv[1:]: | 35 for arg in sys.argv[2:]: |
| 35 if arg.startswith('--'): | 36 if arg.startswith('--'): |
| 36 if arg.startswith('--parent-window='): | 37 if arg.startswith('--parent-window='): |
| 37 parent_window = long(arg[len('--parent-window='):]) | 38 parent_window = long(arg[len('--parent-window='):]) |
| 38 elif caller_url == None: | |
| 39 caller_url = arg | |
| 40 | 39 |
| 41 # Verify that the process was started in the correct directory. | 40 # Verify that the process was started in the correct directory. |
| 42 cwd = os.getcwd() | 41 cwd = os.getcwd() |
| 43 script_path = os.path.dirname(os.path.abspath(sys.argv[0])) | 42 script_path = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 44 if cwd.lower() != script_path.lower(): | 43 if cwd.lower() != script_path.lower(): |
| 45 sys.stderr.write('Native messaging host started in a wrong directory.') | 44 sys.stderr.write('Native messaging host started in a wrong directory.') |
| 46 return 1 | 45 return 1 |
| 47 | 46 |
| 48 # Verify that --parent-window parameter is correct. | 47 # Verify that --parent-window parameter is correct. |
| 49 if platform.system() == 'Windows' and parent_window: | 48 if platform.system() == 'Windows' and parent_window: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 | 84 |
| 86 message_number += 1 | 85 message_number += 1 |
| 87 | 86 |
| 88 message = '{{"id": {0}, "echo": {1}, "caller_url": "{2}"}}'.format( | 87 message = '{{"id": {0}, "echo": {1}, "caller_url": "{2}"}}'.format( |
| 89 message_number, text, caller_url).encode('utf-8') | 88 message_number, text, caller_url).encode('utf-8') |
| 90 if not WriteMessage(message): | 89 if not WriteMessage(message): |
| 91 break | 90 break |
| 92 | 91 |
| 93 if __name__ == '__main__': | 92 if __name__ == '__main__': |
| 94 sys.exit(Main()) | 93 sys.exit(Main()) |
| OLD | NEW |