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 sys | 9 import sys |
10 import struct | 10 import struct |
11 | 11 |
12 def Main(): | 12 def Main(): |
13 message_number = 0 | 13 message_number = 0 |
14 | 14 |
| 15 if len(sys.argv) < 2: |
| 16 sys.stderr.write("URL of the calling application is not specified.\n") |
| 17 return; |
| 18 caller_url = sys.argv[1] |
| 19 |
15 while 1: | 20 while 1: |
16 # Read the message type (first 4 bytes). | 21 # Read the message type (first 4 bytes). |
17 text_length_bytes = sys.stdin.read(4) | 22 text_length_bytes = sys.stdin.read(4) |
18 | 23 |
19 if len(text_length_bytes) == 0: | 24 if len(text_length_bytes) == 0: |
20 break | 25 break |
21 | 26 |
22 # Read the message length (4 bytes). | 27 # Read the message length (4 bytes). |
23 text_length = struct.unpack('i', text_length_bytes)[0] | 28 text_length = struct.unpack('i', text_length_bytes)[0] |
24 | 29 |
25 # Read the text (JSON object) of the message. | 30 # Read the text (JSON object) of the message. |
26 text = sys.stdin.read(text_length).decode('utf-8') | 31 text = sys.stdin.read(text_length).decode('utf-8') |
27 | 32 |
28 message_number += 1 | 33 message_number += 1 |
29 | 34 |
30 response = '{{"id": {0}, "echo": {1}}}'.format(message_number, | 35 response = '{{"id": {0}, "echo": {1}, "caller_url": "{2}"}}'.format( |
31 text).encode('utf-8') | 36 message_number, text, caller_url).encode('utf-8') |
32 | 37 |
33 try: | 38 try: |
34 sys.stdout.write(struct.pack("I", len(response))) | 39 sys.stdout.write(struct.pack("I", len(response))) |
35 sys.stdout.write(response) | 40 sys.stdout.write(response) |
36 sys.stdout.flush() | 41 sys.stdout.flush() |
37 except IOError: | 42 except IOError: |
38 break | 43 break |
39 | 44 |
40 if __name__ == '__main__': | 45 if __name__ == '__main__': |
41 Main() | 46 Main() |
OLD | NEW |