OLD | NEW |
---|---|
1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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 """An implementation of the server side of the Chromium sync protocol. | 6 """An implementation of the server side of the Chromium sync protocol. |
7 | 7 |
8 The details of the protocol are described mostly by comments in the protocol | 8 The details of the protocol are described mostly by comments in the protocol |
9 buffer definition at chrome/browser/sync/protocol/sync.proto. | 9 buffer definition at chrome/browser/sync/protocol/sync.proto. |
10 """ | 10 """ |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
400 | 400 |
401 def __init__(self): | 401 def __init__(self): |
402 # Monotonically increasing version number. The next object change will | 402 # Monotonically increasing version number. The next object change will |
403 # take on this value + 1. | 403 # take on this value + 1. |
404 self._version = 0 | 404 self._version = 0 |
405 | 405 |
406 # The definitive copy of this client's items: a map from ID string to a | 406 # The definitive copy of this client's items: a map from ID string to a |
407 # SyncEntity protocol buffer. | 407 # SyncEntity protocol buffer. |
408 self._entries = {} | 408 self._entries = {} |
409 | 409 |
410 # TODO(nick): uuid.uuid1() is better, but python 2.5 only. | 410 self.ResetStoreBirthday() |
411 self.store_birthday = '%0.30f' % random.random() | |
412 | 411 |
413 self.migration_history = MigrationHistory() | 412 self.migration_history = MigrationHistory() |
414 | 413 |
415 def _SaveEntry(self, entry): | 414 def _SaveEntry(self, entry): |
416 """Insert or update an entry in the change log, and give it a new version. | 415 """Insert or update an entry in the change log, and give it a new version. |
417 | 416 |
418 The ID fields of this entry are assumed to be valid server IDs. This | 417 The ID fields of this entry are assumed to be valid server IDs. This |
419 entry will be updated with a new version number and sync_timestamp. | 418 entry will be updated with a new version number and sync_timestamp. |
420 | 419 |
421 Args: | 420 Args: |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
593 """Ensure creation of all permanent items for a given set of sync types. | 592 """Ensure creation of all permanent items for a given set of sync types. |
594 | 593 |
595 Args: | 594 Args: |
596 requested_types: A list of sync data types from ALL_TYPES. | 595 requested_types: A list of sync data types from ALL_TYPES. |
597 Permanent items of only these types will be created. | 596 Permanent items of only these types will be created. |
598 """ | 597 """ |
599 for spec in self._PERMANENT_ITEM_SPECS: | 598 for spec in self._PERMANENT_ITEM_SPECS: |
600 if spec.sync_type in requested_types: | 599 if spec.sync_type in requested_types: |
601 self._CreatePermanentItem(spec) | 600 self._CreatePermanentItem(spec) |
602 | 601 |
602 def ResetStoreBirthday(self): | |
603 """Resets the store birthday to a random value. | |
604 """ | |
ncarter (slow)
2011/07/29 18:40:40
The closing """ should be on the prev line for a s
lipalani1
2011/07/29 19:33:24
Done.
| |
605 # TODO(nick): uuid.uuid1() is better, but python 2.5 only. | |
606 self.store_birthday = '%0.30f' % random.random() | |
607 | |
608 def StoreBirthday(self): | |
609 """Gets the store birthday. | |
610 """ | |
ncarter (slow)
2011/07/29 18:40:40
Here too.
lipalani1
2011/07/29 19:33:24
Done.
| |
611 return self.store_birthday | |
612 | |
603 def GetChanges(self, sieve): | 613 def GetChanges(self, sieve): |
604 """Get entries which have changed, oldest first. | 614 """Get entries which have changed, oldest first. |
605 | 615 |
606 The returned entries are limited to being _BATCH_SIZE many. The entries | 616 The returned entries are limited to being _BATCH_SIZE many. The entries |
607 are returned in strict version order. | 617 are returned in strict version order. |
608 | 618 |
609 Args: | 619 Args: |
610 sieve: An update sieve to use to filter out updates the client | 620 sieve: An update sieve to use to filter out updates the client |
611 has already seen. | 621 has already seen. |
612 Returns: | 622 Returns: |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
923 return '?' | 933 return '?' |
924 client_id = client_id[0] | 934 client_id = client_id[0] |
925 if client_id not in self.clients: | 935 if client_id not in self.clients: |
926 self.clients[client_id] = self.client_name_generator.next() | 936 self.clients[client_id] = self.client_name_generator.next() |
927 return self.clients[client_id] | 937 return self.clients[client_id] |
928 | 938 |
929 def CheckStoreBirthday(self, request): | 939 def CheckStoreBirthday(self, request): |
930 """Raises StoreBirthdayError if the request's birthday is a mismatch.""" | 940 """Raises StoreBirthdayError if the request's birthday is a mismatch.""" |
931 if not request.HasField('store_birthday'): | 941 if not request.HasField('store_birthday'): |
932 return | 942 return |
933 if self.account.store_birthday != request.store_birthday: | 943 if self.account.StoreBirthday() != request.store_birthday: |
934 raise StoreBirthdayError | 944 raise StoreBirthdayError |
935 | 945 |
936 def HandleMigrate(self, path): | 946 def HandleMigrate(self, path): |
937 query = urlparse.urlparse(path)[4] | 947 query = urlparse.urlparse(path)[4] |
938 code = 200 | 948 code = 200 |
939 self.account_lock.acquire() | 949 self.account_lock.acquire() |
940 try: | 950 try: |
941 datatypes = [DataTypeStringToSyncTypeLoose(x) | 951 datatypes = [DataTypeStringToSyncTypeLoose(x) |
942 for x in urlparse.parse_qs(query).get('type',[])] | 952 for x in urlparse.parse_qs(query).get('type',[])] |
943 if datatypes: | 953 if datatypes: |
944 self.account.TriggerMigration(datatypes) | 954 self.account.TriggerMigration(datatypes) |
945 response = 'Migrated datatypes %s' % ( | 955 response = 'Migrated datatypes %s' % ( |
946 ' and '.join(SyncTypeToString(x).upper() for x in datatypes)) | 956 ' and '.join(SyncTypeToString(x).upper() for x in datatypes)) |
947 else: | 957 else: |
948 response = 'Please specify one or more <i>type=name</i> parameters' | 958 response = 'Please specify one or more <i>type=name</i> parameters' |
949 code = 400 | 959 code = 400 |
950 except DataTypeIdNotRecognized, error: | 960 except DataTypeIdNotRecognized, error: |
951 response = 'Could not interpret datatype name' | 961 response = 'Could not interpret datatype name' |
952 code = 400 | 962 code = 400 |
953 finally: | 963 finally: |
954 self.account_lock.release() | 964 self.account_lock.release() |
955 return (code, '<html><title>Migration: %d</title><H1>%d %s</H1></html>' % | 965 return (code, '<html><title>Migration: %d</title><H1>%d %s</H1></html>' % |
956 (code, code, response)) | 966 (code, code, response)) |
957 | 967 |
968 def HandleCreateBirthdayError(self): | |
969 self.account.ResetStoreBirthday() | |
970 return ( | |
971 200, | |
972 '<html><title>Birthday error</title><H1>Birthday error</H1></html>') | |
973 | |
958 def HandleCommand(self, query, raw_request): | 974 def HandleCommand(self, query, raw_request): |
959 """Decode and handle a sync command from a raw input of bytes. | 975 """Decode and handle a sync command from a raw input of bytes. |
960 | 976 |
961 This is the main entry point for this class. It is safe to call this | 977 This is the main entry point for this class. It is safe to call this |
962 method from multiple threads. | 978 method from multiple threads. |
963 | 979 |
964 Args: | 980 Args: |
965 raw_request: An iterable byte sequence to be interpreted as a sync | 981 raw_request: An iterable byte sequence to be interpreted as a sync |
966 protocol command. | 982 protocol command. |
967 Returns: | 983 Returns: |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1087 | 1103 |
1088 update_sieve.CheckMigrationState() | 1104 update_sieve.CheckMigrationState() |
1089 | 1105 |
1090 new_timestamp, entries, remaining = self.account.GetChanges(update_sieve) | 1106 new_timestamp, entries, remaining = self.account.GetChanges(update_sieve) |
1091 | 1107 |
1092 update_response.changes_remaining = remaining | 1108 update_response.changes_remaining = remaining |
1093 for entry in entries: | 1109 for entry in entries: |
1094 reply = update_response.entries.add() | 1110 reply = update_response.entries.add() |
1095 reply.CopyFrom(entry) | 1111 reply.CopyFrom(entry) |
1096 update_sieve.SaveProgress(new_timestamp, update_response) | 1112 update_sieve.SaveProgress(new_timestamp, update_response) |
OLD | NEW |